How to put msg.payload in a variable in Python to use it to be displayed on a LCD 16x2 Screen

Hello, I'm sorry to ask such Noob questions here, but I am a IT student and we have a branch about creating a project with Node Red and a Raspberry Pi. First of all we simulate a Glasshouse where we check every minute the Temperature and Humidity inside and these values will be shown seperately in the dashboard. This is all I got, but we have to display this now on a separate LCD Screen we connect to the Raspberry Pi and I simply do not know how I should get the msg.payload and msg.humidity in a variable to show it on the LCD Screen with some text like "Temp" and "Humi" as abreviations. I used some nodes from this user on GitHub(GitHub - danilo-lorenzo/I-C-16x2-Display-with-Node-RED: Learn how to run a Python code inside Node-RED to write stuff in a 1602 Display) to get Text on the Display but I still don't know how to show the Temperature and Humidity on the Display. If someone would be so nice and would help me or explain me how to do this in Python, i would be very happy. I appreciate any help
Here is the Flow if anyone wants to see how it works maybe. :slightly_smiling_face:

[{"id":"1f44ce61.f97b5a","type":"tab","label":"Projekt Gewächshaus","disabled":false,"info":""},{"id":"25a2e504.95a09a","type":"inject","z":"1f44ce61.f97b5a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":120,"wires":[["8abd454.62ecf38"]]},{"id":"56748990.2c2238","type":"comment","z":"1f44ce61.f97b5a","name":"Title","info":"Projekt PROTO2\nGewächshaus\nPleger Mike 2021","x":270,"y":60,"wires":[]},{"id":"300e0e41.4d6d2a","type":"function","z":"1f44ce61.f97b5a","name":"humidity","func":"msg.payload = msg.humidity\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":540,"wires":[["3a3057c8.7be97","c03d74da.f16488","336421f1.01817e"]]},{"id":"8013dd3c.0608c8","type":"function","z":"1f44ce61.f97b5a","name":"temperature","func":"msg.payload = msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":440,"wires":[["8c8a45ea.131908","787be9f5.bb23d8","336421f1.01817e"]]},{"id":"8abd454.62ecf38","type":"rpi-dht22","z":"1f44ce61.f97b5a","name":"","topic":"rpi-dht22","dht":"11","pintype":"0","pin":4,"x":380,"y":120,"wires":[["8013dd3c.0608c8","300e0e41.4d6d2a"]]},{"id":"8c8a45ea.131908","type":"ui_gauge","z":"1f44ce61.f97b5a","name":"","group":"318aef03.c874a","order":1,"width":0,"height":0,"gtype":"gage","title":"Temperatur Innen","label":"°C","format":"{{value}}","min":0,"max":"50","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":810,"y":400,"wires":[]},{"id":"3a3057c8.7be97","type":"ui_gauge","z":"1f44ce61.f97b5a","name":"","group":"91dadf78.6bfb6","order":1,"width":0,"height":0,"gtype":"gage","title":"Humidity Innen","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":800,"y":580,"wires":[]},{"id":"4726e4a8.75135c","type":"ui_slider","z":"1f44ce61.f97b5a","name":"Temp(Aussen)","label":"Temperatur Aussen","tooltip":"","group":"318aef03.c874a","order":2,"width":0,"height":0,"passthru":true,"outs":"all","topic":"tempregler","topicType":"msg","min":0,"max":"50","step":1,"x":800,"y":300,"wires":[["354c5dea.fe6baa"]]},{"id":"354c5dea.fe6baa","type":"ui_gauge","z":"1f44ce61.f97b5a","name":"Temp(Aussen)","group":"318aef03.c874a","order":3,"width":0,"height":0,"gtype":"gage","title":"Temperatur Aussen","label":"°C","format":"{{value}}","min":0,"max":"50","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":1040,"y":340,"wires":[]},{"id":"4dd2e491.208a14","type":"ui_slider","z":"1f44ce61.f97b5a","name":"Humidity(Aussen)","label":"Humidity Aussen","tooltip":"","group":"91dadf78.6bfb6","order":2,"width":0,"height":0,"passthru":true,"outs":"all","topic":"humidityregler","topicType":"msg","min":0,"max":"100","step":1,"x":810,"y":680,"wires":[["1be81589.a39ab2"]]},{"id":"1be81589.a39ab2","type":"ui_gauge","z":"1f44ce61.f97b5a","name":"Humidity(Aussen)","group":"91dadf78.6bfb6","order":3,"width":0,"height":0,"gtype":"gage","title":"Humidity Aussen","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":1050,"y":640,"wires":[]},{"id":"2f814117.1f6fbe","type":"comment","z":"1f44ce61.f97b5a","name":"AussenTemperatursimulierung","info":"Simulierter Temperatursensor für AussenTemperatursimulierung\nPleger Mike 2021","x":950,"y":240,"wires":[]},{"id":"c23d7adb.eeb76","type":"comment","z":"1f44ce61.f97b5a","name":"AussenFeuchtigkeitssimulierung","info":"Simulierter Feuchtigkeitssensor für AussenFeuchtichkeitssimulierung\nPleger Mike 2021","x":950,"y":740,"wires":[]},{"id":"a1cade67.1ab","type":"comment","z":"1f44ce61.f97b5a","name":"Trennung Feuchtigkeit Temperatur","info":"Trennung der 2 Werte Feuchtigkeit und Temperatur über 2 function Nodes\nPleger Mike 2021","x":640,"y":480,"wires":[]},{"id":"2ef32079.71b568","type":"file","z":"1f44ce61.f97b5a","name":"","filename":"temperature.txt","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":1120,"y":440,"wires":[[]]},{"id":"158dc784.639a1","type":"file","z":"1f44ce61.f97b5a","name":"","filename":"humidity.txt","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":1110,"y":540,"wires":[[]]},{"id":"787be9f5.bb23d8","type":"delay","z":"1f44ce61.f97b5a","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":860,"y":440,"wires":[["2ef32079.71b568"]]},{"id":"c03d74da.f16488","type":"delay","z":"1f44ce61.f97b5a","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":860,"y":540,"wires":[["158dc784.639a1"]]},{"id":"336421f1.01817e","type":"template","z":"1f44ce61.f97b5a","name":"","field":"payload","fieldType":"msg","format":"python","syntax":"plain","template":"# _____ _____ _____ __ __ _____ _____ \n#|     |   __|     |  |  |     |     |\n#|  |  |__   |  |  |_   _|  |  |  |  |\n#|_____|_____|_____| |_| |_____|_____|\n#\n# Project Tutorial Url:http://osoyoo.com/?p=1031\n#  \nimport smbus\nimport time\n\n# Define some device parameters\nI2C_ADDR  = 0x38 # I2C device address, if any error, change this address to 0x3f\nLCD_WIDTH = 16   # Maximum characters per line\n\n# Define some device constants\nLCD_CHR = 1 # Mode - Sending data\nLCD_CMD = 0 # Mode - Sending command\n\nLCD_LINE_1 = 0x80 # LCD RAM address for the 1st line\nLCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line\nLCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line\nLCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line\n\nLCD_BACKLIGHT  = 0x08  # On\n#LCD_BACKLIGHT = 0x00  # Off\n\nENABLE = 0b00000100 # Enable bit\n\n# Timing constants\nE_PULSE = 0.0005\nE_DELAY = 0.0005\n\n#Open I2C interface\n#bus = smbus.SMBus(0)  # Rev 1 Pi uses 0\nbus = smbus.SMBus(1) # Rev 2 Pi uses 1\n\ndef lcd_init():\n  # Initialise display\n  lcd_byte(0x33,LCD_CMD) # 110011 Initialise\n  lcd_byte(0x32,LCD_CMD) # 110010 Initialise\n  lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction\n  lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off \n  lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size\n  lcd_byte(0x01,LCD_CMD) # 000001 Clear display\n  time.sleep(E_DELAY)\n\ndef lcd_byte(bits, mode):\n  # Send byte to data pins\n  # bits = the data\n  # mode = 1 for data\n  #        0 for command\n\n  bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT\n  bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT\n\n  # High bits\n  bus.write_byte(I2C_ADDR, bits_high)\n  lcd_toggle_enable(bits_high)\n\n  # Low bits\n  bus.write_byte(I2C_ADDR, bits_low)\n  lcd_toggle_enable(bits_low)\n\ndef lcd_toggle_enable(bits):\n  # Toggle enable\n  time.sleep(E_DELAY)\n  bus.write_byte(I2C_ADDR, (bits | ENABLE))\n  time.sleep(E_PULSE)\n  bus.write_byte(I2C_ADDR,(bits & ~ENABLE))\n  time.sleep(E_DELAY)\n\ndef lcd_string(message,line):\n  # Send string to display\n\n  message = message.ljust(LCD_WIDTH,\" \")\n\n  lcd_byte(line, LCD_CMD)\n\n  for i in range(LCD_WIDTH):\n    lcd_byte(ord(message[i]),LCD_CHR)\n\ndef main():\n  # Main program block\n\n  # Initialise display\n  lcd_init()\n\n  while True:\n\n    # Send some test\n    lcd_string(\"Hallo\",LCD_LINE_1)\n    lcd_string(\"Hallo\",LCD_LINE_2)\n\n    time.sleep(3)\n  \n    # Send some more text\n    lcd_string(\"Innen:\",LCD_LINE_1)\n    lcd_string(\"Aussen:\",LCD_LINE_2)\n\n    time.sleep(3)\n\nif __name__ == '__main__':\n\n  try:\n    main()\n  except KeyboardInterrupt:\n    pass\n  finally:\n    lcd_byte(0x01, LCD_CMD)\n\n\n\n","output":"str","x":580,"y":820,"wires":[["f9742f28.f4556"]]},{"id":"f9742f28.f4556","type":"file","z":"1f44ce61.f97b5a","name":"","filename":"pythontesta.py","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":780,"y":820,"wires":[["179403bb.f13c9c"]]},{"id":"179403bb.f13c9c","type":"exec","z":"1f44ce61.f97b5a","command":"python3 pythontesta.py","addpay":false,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"","x":1010,"y":820,"wires":[["ad2cee87.ec3dd"],[],[]]},{"id":"ad2cee87.ec3dd","type":"debug","z":"1f44ce61.f97b5a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1230,"y":820,"wires":[]},{"id":"318aef03.c874a","type":"ui_group","name":"Temperatur","tab":"9765eae4.b0bf3","order":1,"disp":true,"width":"6","collapse":false},{"id":"91dadf78.6bfb6","type":"ui_group","name":"Humidity","tab":"9765eae4.b0bf3","order":2,"disp":true,"width":"6","collapse":false},{"id":"9765eae4.b0bf3","type":"ui_tab","name":"Gewächshaus","icon":"dashboard","disabled":false,"hidden":false}]

Hi, in order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```

You can edit and correct your post by clicking the pencil icon.
image

See this post for more details - How to share code or flow json

You could send the data to the script with MQTT.

When it receives a new message, change the values of the displayed variables.

I fully agree!

The design/architecture described above is also not what I would choose. In fact, with MQTT everything will be very simple instead of trying to transfer data via file system. I assume the script is controlling the LCD.

Basically you do like this:

  • you install a MQTT broker in your RPi
  • in your Python script you add support for MQTT, you start the script and it connects and subscribes to certain topics where NR will publish sensor data
  • in NR you take the sensor data and publish it to the same topics as the script is subscribing to. Use MQTT out nodes in NR for this

If you want you can also add a function in the python script code that terminates the script. So add a button in NR that sends a dedicated "abort" message to the MQTT broker. When the python script receives this, it should terminate itself. This type of feature is not so important for a simple script that you can kill anyway but more important for scripts using threads. There it is important to "tell the script" to terminate all open threads, clean up and make a correct termination of itself

A second thought, are you sure the LCD could not be controlled buy NR directly? Have you searched around? If it would, it will make the solution even simpler

But now is enough, otherwise your examinator will not be happy! Wish you good luck!

The Script is controlling the LCD and determines the text that will be shown on the LCD Screen. I have the problem that I don't know how I get the Data from the DHT-11 Sensor to be shown on the Screen. Maybe if you would be so nice you could show me an example of how it would work because I didn0t really understand what you wanted to tell me.

Without giving you code directly, this is the principle how I would do it

DHT-11 -> Node-RED -> MQTT Out node -> ----- MQTT Broker (running in the same machine is fine) ----- -> Python script w MQTT support (connect to the same broker, subscribe to topic, then receive sensor value in function on_message)

There are several python scripts already publlished here in the forum with mqtt support. Try the search feature. I do not wan't to give you too much, being critizied helping you cheating, it is anyway you that will have to pass the examination or test. Once you have a "skeleton" I can give you more advice

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.