# Python Script into .JSON file for node red?

**URL:** <https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535>\
**Category:** Developing Nodes\
**Created:** [12 May 2021 10:49 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535 "2021-05-12T10:49:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 10:49 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/1 "2021-05-12T10:49:48Z")

</div>

Hello there!

I wrote a Scipt in python to read data from a sensor which is connected via usb to my raspberry pi 4B.

Is it possible to convert this skript into a .json file and import it into node red to get the readed data into the debug node or into the dashboard ?

Thanks.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 May 2021 11:03 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/2 "2021-05-12T11:03:21Z")

</div>

What is the sensor?  
Have you looked to see if there is a node that uses that sensor?  
You could use the `exec` node to run the script but how does it report the data?

---

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 11:08 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/3 "2021-05-12T11:08:36Z")

</div>

the sensor is the sds011, I once tried to use the available node but it didn't worked. You told me an another topic I made that the author for this node doesn't update it for almost 3 years and thats why the node doenst work, it seems to be abonded. So i wrote a script to read it and now i want to import it to node red, if possible.

---

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 11:10 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/4 "2021-05-12T11:10:18Z")

</div>

The script i wrote shows the data in an array, but I can change that into the form it must have.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 May 2021 11:22 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/5 "2021-05-12T11:22:19Z")

</div>

If I may suggest, alternative solutions with various degree of work to do

1. Add MQTT to the script and deliver the array as a message via MQTT to NR. Very easy if you have enough experience in Python. Controol the execution of the script from NR; use an exec node to start it, add a heartbeat function as well to the script, monitor it and if needed restart it. Also not too complicated to achieve

2. Re-write the logic in javascript and put into a function node but I do not know if there are libraries available to also read the sensor readings directly from javascript or if you still have to have some python script in between (if so, I would stick to alternative 1) ) EDIT: I see now, it is just serial so it should be ppossible to use the serial node in NR

---

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 11:27 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/6 "2021-05-12T11:27:06Z")

</div>

I forgot to say that the whole system must work without any internet connection.  
I am not really a professional, more an absolute beginner. does mqtt requiere an internet connection?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 May 2021 11:29 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/7 "2021-05-12T11:29:37Z")

</div>

> [@Beginner96](#):
>
> does mqtt requiere an internet connection?

Not at all, you can run it locally

Have you tried the serial node and checked if you can receive the raw data directly from the sensor?  
Bit rate ：9600  
Data bit ：8  
Parity bit：NO  
Stop bit ：1

---

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 11:49 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/8 "2021-05-12T11:49:24Z")

</div>

I tried the "serial in" node and the "serial request" node, both are connected but in the debug window appears nothing, even when i switch to "full report".

 ![Screenshot 2021-05-12 134426](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/859e7553d70398a69e15390bed6e6483c5ae8b59.jpeg)

![Screenshot 2021-05-12 134605](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/49fd7028c955f0261996a80cf7d61f1c0f766b5f.jpeg)

Because I#m a beginner, can you tell me in detail how i add he MQTT & exec Node and how i set them up?

Thank you very much. 🙂

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 May 2021 11:52 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/9 "2021-05-12T11:52:42Z")

</div>

> [@Beginner96](#):
>
> Because I#m a beginner, can you tell me in detail how i add he MQTT & exec Node and how i set them up?

Sure, share your Python script here, I will modify it and also make a simple flow that monitors it via heartbeat function. If you have any personal credentials in the Python script, please remove them first

---

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 11:54 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/10 "2021-05-12T11:54:40Z")

</div>

Thank you !

import array  
import sys  
import usb.core  
import usb.util

VID =0x1a86  
PID = 0x7523  
DATA\_SIZE = 4

device = usb.core.find(idVendor = VID, idProduct = PID)  
if device is None:  
sys.exit("Could not find sds011 Sensor.")

if device.is\_kernel\_driver\_active(0):  
try:  
device.detach\_kernel\_driver(0)  
except usb.core.USBError as e:  
sys.exit("Could not detatch kernel driver: %s" % str(e))

try:  
device.reset()  
device.set\_configuration()  
except usb.core.USBError as e:  
sys.exit("Could not set configuration: %s" % str(e))

endpoint = device[0][(0,0)][0]  
print(endpoint.wMaxPacketSize)

data = array.array('B',(0,)\*4)  
while data[0] != 3:  
try:  
data = device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)  
print(data)

```
except usb.core.USBError as e:
    if e.args == ('Operation timed out',):
        print("timeout)")
        continue

```

print("work!")

---

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 11:56 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/11 "2021-05-12T11:56:55Z")

</div>

I have to say that i write it with the help of a friend, so i don't really know how it works 😃

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 May 2021 11:59 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/12 "2021-05-12T11:59:09Z")

</div>

No problem, I assume you run it in a Raspberry Pi or at least debian/ubuntu system?

You can start to install the mosquitto MQTT broker. See here. It also explains a bit about MQTT

> **[Installing MQTT Broker(Mosquitto) on Raspberry Pi](https://www.instructables.com/Installing-MQTT-BrokerMosquitto-on-Raspberry-Pi/)**
>
> Installing MQTT Broker(Mosquitto) on Raspberry Pi : In this instructables you will find how to install MQTT on Raspberry Pi. There are two methods to install mqtt, one is we directly connect keyboard mouse and monitor to a Pi , turn it on and go...

---

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 12:26 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/13 "2021-05-12T12:26:34Z")

</div>

I installed mosquitto but when i try to test it, like on the website shown, I get the following.

![Screenshot 2021-05-12 142612](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/ef126ab333a86c0e0c205293d7cb0f5157d1257e.jpeg)

---

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 12:32 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/14 "2021-05-12T12:32:12Z")

</div>

ok Now i got it ! MQTT & Mosquitto is installed succesfully. it also passed the test.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 May 2021 12:40 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/15 "2021-05-12T12:40:30Z")

</div>

Perfect! Give me some few minutes more, soon you have something to test 😉

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 May 2021 13:02 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/16 "2021-05-12T13:02:29Z")

</div>

So below is an example to try, hope it does, I could not test the actual usb communication 😉

First you have to install the paho mqtt client for Python3. Like this

`sudo pip3 install paho-mqtt`

Next put the script in your directory (/home/pi) and change extension from .txt to .py

Import the flow to your Node-RED, deploy

Start the script via the "Start script" button. I made it so the exec nodes runs the script visible on your monitor screen or vnc view. If this does not work we will get an error message

Abort the script via the "Abort script" button

Heartbeat checks will start automatically and is sent once per minute

Hopefully it works, I moved the usb communication to a separate thread

You should now see data coming into NR in the debug view

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

```auto
[{"id":"76342fda.61ba7","type":"exec","z":"44db0721.1c8138","command":"export DISPLAY=:0 && lxterminal -e python3 /home/pi/sds011.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":570,"y":180,"wires":[[],[],[]]},{"id":"cb8849f3.036878","type":"inject","z":"44db0721.1c8138","name":"Start script","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":160,"y":180,"wires":[["76342fda.61ba7"]]},{"id":"260e8f38.3f615","type":"mqtt out","z":"44db0721.1c8138","name":"","topic":"sds011/abort","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"397bfe63.7d8242","x":400,"y":510,"wires":[]},{"id":"3fb86da5.3e02e2","type":"inject","z":"44db0721.1c8138","name":"Abort script","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"abort","payloadType":"str","x":170,"y":510,"wires":[["260e8f38.3f615"]]},{"id":"634dd6c6.21d108","type":"inject","z":"44db0721.1c8138","name":"Heartbeat check","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Are you alive?","payloadType":"str","x":190,"y":450,"wires":[["2dbd1fb6.009ef"]]},{"id":"2dbd1fb6.009ef","type":"mqtt out","z":"44db0721.1c8138","name":"","topic":"sds011/heartbeat_check","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"397bfe63.7d8242","x":440,"y":450,"wires":[]},{"id":"7f2b6c64.37d954","type":"mqtt in","z":"44db0721.1c8138","name":"","topic":"sds011/heartbeat_response","qos":"2","datatype":"auto","broker":"397bfe63.7d8242","nl":false,"rap":false,"x":220,"y":390,"wires":[["ad4c3c00.0776b8","38edfbd5.dc9ca4"]]},{"id":"ad4c3c00.0776b8","type":"debug","z":"44db0721.1c8138","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":390,"wires":[]},{"id":"38edfbd5.dc9ca4","type":"trigger","z":"44db0721.1c8138","name":"","op1":"","op2":"Script is restarting","op1type":"nul","op2type":"str","duration":"5","extend":true,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":600,"y":260,"wires":[["76342fda.61ba7","ad4c3c00.0776b8","cf3eecb5.6f8d9"]]},{"id":"cf3eecb5.6f8d9","type":"switch","z":"44db0721.1c8138","name":"","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":330,"wires":[["38edfbd5.dc9ca4"]]},{"id":"37b069c1.c685d6","type":"debug","z":"44db0721.1c8138","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":400,"y":600,"wires":[]},{"id":"84ee7b81.3236d8","type":"mqtt in","z":"44db0721.1c8138","name":"","topic":"sds011/data","qos":"2","datatype":"auto","broker":"397bfe63.7d8242","nl":false,"rap":true,"rh":0,"x":170,"y":600,"wires":[["37b069c1.c685d6"]]},{"id":"397bfe63.7d8242","type":"mqtt-broker","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

[sds011.txt](https://discourse.nodered.org/uploads/short-url/ijOJhIjyXAhmPgZNA9fIEuSTr7L.txt) (2.3 KB)

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 May 2021 13:14 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/17 "2021-05-12T13:14:15Z")

</div>

Just updated the script, it did not abort fully correctly

---

<div class="post-metadata">

**Author:** ![Beginner96](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Beginner96](https://discourse.nodered.org/u/Beginner96)\
**Post date:** [12 May 2021 13:21 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/18 "2021-05-12T13:21:13Z")

</div>

First of all, I don't know how to thank you for all this, this seems to be a lot of work you've done for me 😃

I installed paho mqtt like you said and reboot everything.  
The Script is already in .py, when i start it in the console the following appears.

 ![Screenshot 2021-05-12 151226](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/7/374cd04ae1a0d302d05a457195dc80fe12a4eb38.jpeg)

Than I imported the flow, but when i press start nothing appears in the debug window. Everything shows "connected" and when i press start, under the exec node appears for a second "pid: 1264" with a little blue dot infront of it.  
When i press start through the vnc connection, the console is opening for a sec., I think thats because the script starts.  
so i think everything you've done is correct but something is wrong with my script 😃  
Or did I anything wrong ?

 ![Screenshot 2021-05-12 152057](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2da26e49558315cd3ecb2bef55c21719d030ad87.jpeg)

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [12 May 2021 13:55 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/19 "2021-05-12T13:55:54Z")

</div>

> [@Beginner96](#):
>
> I wrote a Scipt in python to read data from a sensor which is connected via usb to my raspberry pi 4B.
> 
> Is it possible to convert this skript into a .json file and import it into node red to get the readed data into the debug node or into the dashboard ?

You can also consider launching the python script through [node-red-node-daemon](https://flows.nodered.org/node/node-red-node-daemon). In that case you must assure that your python script is outputting the sensor data to standard output (and not to a specific file).

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 May 2021 14:42 UTC](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535/20 "2021-05-12T14:42:54Z")

</div>

No problem, my pleasure!

I see an error message related to python 2.7

When you did try your original script, how did you start it? With python or python3?

You could maybe try to run it also directly from a command prompt like `python3 /home/pi/sds011.py` or `python /home/pi/sds011.py` so we can see what is happening

But to make it work with python 2.7 you have to install paho mqtt client again but this time `sudo pip install paho-mqtt`

EDIT: Maybe we are hitting the usb port too hard, we can eventually put in a delay between the readings. How frequent do we have to read it?

EDIT AGAIN: I see now you started the script incorrectly, you should try it with `sudo python3 /home/pi/sds011.py`  
If that works you have to edit the command in the exec node to add sudo in front

[Next page](https://discourse.nodered.org/t/python-script-into-json-file-for-node-red/45535.md?page=2)
