Run python script in Node red and export with MQTT

I have connected a CCS811 sensor with Raspberry PI and I have written a python script to output/print the sensor values. It works fine in the linux terminal.

What are the methods to read the sensor data in Node-red and export it to a MQTT broker?

PS: I am a bit new to Node-red programming and Raspberry Pi

You can use an exec node to run your python script. Alternatively you may be able to do it directly in node-red but I am not an I2C expert so I don't know about that.
If you do it in an exec node use the full path to the script. You may need the full path to python itself too.

Thank you for your reply.

Yes I have already tried the EXEC node. But it is showing an error. I think the error is because I have not configured the input properly.

I could not understand from the documentation what exactly needs to be passed as input to the exec node. Could you please tell me a bit about it.

The pins connect in Raspberry Pi are SCL(GPIO3) and SDA(GPIO2)

Thank you

Show us the error and show us how you have configured the exec node (you can paste a screenshot here). If you are sending parameters to it in the message feed it into a debug node and paste that too.

The flow is fairly simple

The configured exec node is as

The timestamp is injected but it shows the below error and gives no output in the debug node

Does it reconnect after a while?
How did you install node-red?

In a terminal run

node-red-stop
node-red-start

which will start node red showing the log in the terminal. Leave that open and click your inject node and see what it shows in the terminal. If you don't understand it then copy/paste the complete output from the start here. Use the </> button when pasting it in. I suspect node-red is crashing for some reason, but that may be wrong.

I think you need to type also python

Like

python3 /home/pi/test_files/co2test1.py

or

python /home/pi/test_files/co2test1.py

2 Likes

And also, do not check the box append msg.payload unless you know what you are appending and that it is required

1 Like

Yes.. it does start immediately
The node red which I am using is pre-installed in the raspbian OS

Yes.. It worked
Thank you very much

It may not be a factor here but ideally you should install using the recommended script.

2 Likes

I agree totally with @Colin, it is an important recommendation, you should really upgrade using that script

I am surprised that not having the python command (or perhaps it was the payload append) crashed node-red, if that is what was happening. I would have expected an error from the exec node, but not a crash. Perhaps there was a bug in the version of node-red or nodejs that you currently have.

Yes..exactly

Node red was crashing and restarting as there was no python command...
A rookie mistake from my side but @Colin @krambriw thank you very much for your support and immediate replies.

That shouldn't happen, it should not crash node red, just generate an error in the debug output. Have you still got the log? On the other hand if you plan to do the upgrade by running the script then do that first and then take the python command out again and see if it still crashes.

Just tested a similar setup, NR v1.1.2
Gives error codes, behaving correctly in both cases (with and without payload appended)