# Node Red - R Pi sensor

**URL:** https://discourse.nodered.org/t/node-red-r-pi-sensor/53116
**Category:** General
**Created:** [2 November 2021 09:53 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116 "2021-11-02T09:53:26Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [2 November 2021 09:53 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/1 "2021-11-02T09:53:26Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [2 November 2021 10:12 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/2 "2021-11-02T10:12:03Z")

</div>

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

> **[node-red-contrib-dht-sensor](https://flows.nodered.org/node/node-red-contrib-dht-sensor)**
>
> Node-red node for node-dht-sensor

---

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [2 November 2021 10:17 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/3 "2021-11-02T10:17:12Z")

</div>

I tried but the pi crashes and becomes unresponsive.

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [2 November 2021 10:26 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/4 "2021-11-02T10:26:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [2 November 2021 10:38 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/5 "2021-11-02T10:38:15Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [2 November 2021 11:10 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/6 "2021-11-02T11:10:53Z")

</div>

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](https://learn.adafruit.com/adafruit-aht20/python-circuitpython) may help you get it working from the Raspberry Pi command line. Adafruit tutorials are usually very good.

---

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [2 November 2021 11:56 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/7 "2021-11-02T11:56:45Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05e76de8d857f5b4187d2da3c2c3b753c248feb6.jpeg)  
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.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [2 November 2021 12:17 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/8 "2021-11-02T12:17:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [2 November 2021 13:33 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/9 "2021-11-02T13:33:48Z")

</div>

"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?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [2 November 2021 14:00 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/10 "2021-11-02T14:00:32Z")

</div>

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.

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/26d5d955dba367056386a7e2197bd383a45b3898.jpeg)

```auto
[{"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

---

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [2 November 2021 20:35 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/11 "2021-11-02T20:35:10Z")

</div>

unfortunately not working

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [2 November 2021 20:53 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/12 "2021-11-02T20:53:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [2 November 2021 20:59 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/13 "2021-11-02T20:59:47Z")

</div>

I am not getting any errors,

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c70cf1363f2ae7d329e68dc4408657a9bce42ddf.png)

---

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [2 November 2021 21:06 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/14 "2021-11-02T21:06:35Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3ff22b52f896bc2ffe86d083b5164cdbf51da998.png)

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [2 November 2021 22:29 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/15 "2021-11-02T22:29:49Z")

</div>

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](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [2 November 2021 22:58 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/16 "2021-11-02T22:58:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [4 November 2021 19:23 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/17 "2021-11-04T19:23:27Z")

</div>

```auto
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")

```

---

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [4 November 2021 19:44 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/18 "2021-11-04T19:44:06Z")

</div>

This code was working before Node Red update.

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [4 November 2021 20:29 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/19 "2021-11-04T20:29:48Z")

</div>

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"

```auto
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

```

```auto
[
    {
        "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": []
    }
]

```

---

<div class="post-metadata">

### Author: ![sefaguntepe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sefaguntepe/32/94497_2.png) [@sefaguntepe](https://discourse.nodered.org/u/sefaguntepe)
#### Post date: [4 November 2021 20:33 UTC](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116/20 "2021-11-04T20:33:49Z")

</div>

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

> [@jbudd](#):
>
> ```auto
> [
> {
> "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": "",
> 
> ```

[Next page](https://discourse.nodered.org/t/node-red-r-pi-sensor/53116.md?page=2)
