Node Red - R Pi sensor

Hello, I am using Node-red over "Pi". I have the "dht21-AM2301" sensor. How can I communicate with node-red by connecting the sensor to the Pi.

Have a look at this node, I have never used these sensors, so I cannot offer further help.

I tried but the pi crashes and becomes unresponsive.

I see that the node hasn't been updated for a while and also requires superuser permissions. I would remove the node.

I would recommend that you get hold of another sensor, that has contrib nodes available.
The SHT3x or BMExxx type sensors would be a good choice. Also remember that having sensors directly connected to a Pi means that you are limited as to where you can place them.
Most people connect these sensors to an ESP32 / ESP8266 / Arduino board and send the data using MQTT to node-red for further processing (Graphing or Database storage).

1 Like

The DHT21 = AM2301 = AHT20 (?) does seem to be much less common than the DHT11 or 22.

If it is the same device as the AHT20 then this page may help you get it working from the Raspberry Pi command line. Adafruit tutorials are usually very good.

image
I don't want to use different devices because they are close. It is easier for me to read the temperature on a single device. My codes are working on the Pi, I just need to get the data.

One way is to call your python script from an exec node. You might want to change the script so it prints one set of values and exits. Then you can call it from exec at whatever interval you like. The output data will go into msg.payload.

Alternatively have the script publish the readings by mqtt and use an mqtt-in node to access them.
You need an mqtt broker for this, the node-red aedes broker would be perfectly adequate.

1 Like

"python dht_simpletest.py"

in console " python dht_simpletest.py
I run it with the " command. I don't understand how to do it in "exec". Can you help?

Whatever you type at the command line you can put in the "Command" field of the Exec node, though it is wise to use full pathnames, eg "/usr/bin/python3 /home/pi/dht_simpletest.py"
So a Node-Red flow for testing might consist of an inject node to start the flow, exec and debug.

[{"id":"18b09d258f2f609f","type":"exec","z":"4a0160e8140fd38e","command":"/usr/bin/python3 /home/pi/dht_simpletest.py","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":390,"y":320,"wires":[["773dfdb818bd7d25"],[],[]]},{"id":"fc2da3f274bfb803","type":"inject","z":"4a0160e8140fd38e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":320,"wires":[["18b09d258f2f609f"]]},{"id":"773dfdb818bd7d25","type":"debug","z":"4a0160e8140fd38e","name":"Do something with the data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":720,"y":320,"wires":[]}]

Note that if your script runs for ever, churning out readings, the exec node has to be set to output "While the command is running" (I have never used it that way, I prefer a script to output one set of data and exit).

And it is neater if your script itself is executable (chmod +x dht_simpletest.py), has the hashbang line #! /usr/bin/python3 and resides in a directory intended for executables such as /home/pi/bin
Then your Exec node command is just /home/pi/bin/dht_simpletest.py

unfortunately not working

If you want help to find out why it would be a good idea to post your flow, your script and whatever error messages you get.

I am not getting any errors,

The error "Command failed: python3 ~ dht_simpletest.py":
~/dht_simpletest.py is a Bash shortcut for <my home directory>/dht_simpletest.py.
You left out the /
It would not have fixed whatever problems you are experiencing though.

Please share your Python code and Node-Red flow.
https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506

Well it would explain the errors seen. As. python3 /home/pi is not a file it can execute

1 Like
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if humidity is not None and temperature is not None:
        print("Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(temperature, humidity))
    else:
        print("Failed to retrieve data from humidity sensor")

This code was working before Node Red update.

[{"id":"6c263e12e5ea67ae","type":"exec","z":"9548c52a3ad67da1","command":"python3","addpay":"payload","append":"~/nem.py","useSpawn":"false","timer":"15","winHide":true,"oldrc":false,"name":"","x":360,"y":1560,"wires":[["1e3e5c3d289a746e"],[],[]]},{"id":"c3dafa4becf24f5b","type":"inject","z":"9548c52a3ad67da1","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"~/nemm.py","payloadType":"str","x":210,"y":1560,"wires":[["6c263e12e5ea67ae"]]},{"id":"1e3e5c3d289a746e","type":"debug","z":"9548c52a3ad67da1","name":"Do something with the data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":1540,"wires":[]}]
  1. Your inject mode is passing "~/nemm.py". Your Exec node has "python3",, Append Payload is ticked and the field for additional parameters has "~/nem.py".
    So is it nem.py or nemm.py?
    You can pass the name of the script in msg.payload OR you can specify it in the Exec node but not both.
    It is wrong to use "~/". Use the full pathname of the script "/home/pi/nem.py" (or "/home/pi/nemm.py")

  2. If you really must have an infinite loop in your Python script, you should call Python in "unbuffered mode" with python -u.
    You should change the Exec node to output "while the command is running - spawn mode".
    It is probably a good idea to include a delay in the script eg time.sleep(2)

  3. You have shown that dht_simpletest.py works from the command line but nem.py is a different script. Does it work from the command line? python3 nem.py

As I don't have a DHT21 sensor, I can't debug the script for you. However please try this version and flow, and tell us your results. You may need to change "nem.py" to "nemm.py"

import Adafruit_DHT
import time

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
#    print("Reading data")    # Uncomment this line for debugging
#    humidity = 84.7          # Uncomment this line to pass dummy data to node-red
#    temperature = 20.1       # Uncomment this line to pass dummy data to node-red

# Comment the following line out to pass dummy data to node-red
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if humidity is not None and temperature is not None:
        print("Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(temperature, humidity))
    else:
        print("Failed to retrieve data from humidity sensor")

    time.sleep(2)             # 2 seconds delay before looping

[
    {
        "id": "a764df5138254362",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "6c263e12e5ea67ae",
        "type": "exec",
        "z": "a764df5138254362",
        "command": "python3 -u /home/pi/nem.py",
        "addpay": "",
        "append": "",
        "useSpawn": "true",
        "timer": "15",
        "winHide": true,
        "oldrc": false,
        "name": "",
        "x": 400,
        "y": 180,
        "wires": [
            [
                "1e3e5c3d289a746e"
            ],
            [],
            []
        ]
    },
    {
        "id": "c3dafa4becf24f5b",
        "type": "inject",
        "z": "a764df5138254362",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 170,
        "y": 180,
        "wires": [
            [
                "6c263e12e5ea67ae"
            ]
        ]
    },
    {
        "id": "1e3e5c3d289a746e",
        "type": "debug",
        "z": "a764df5138254362",
        "name": "Do something with the data",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 720,
        "y": 180,
        "wires": []
    }
]

The "-u" worked. Thank you so much.