Some way to run python script and inject the data to node red

9,268 views
Skip to first unread message

Christian de Lasson

unread,
Jan 25, 2018, 11:17:00 AM1/25/18
to Node-RED
Hello 

I got a problem i wan't the output from this python script in to node red as a msg.payload:


import RPi.GPIO as GPIO
import time
import sys, json
from hx711 import HX711

def cleanAndExit():
    print "Cleaning..."
    GPIO.cleanup()
    print "Bye!"
    sys.exit()

hx = HX711(23, 24)

hx.set_reading_format("LSB",)

hx.set_reference_unit(-921)

hx.reset()
hx.tare()

while True:
    try:

        val = hx.get_weight(5)
        print val,

        hx.power_down()
        hx.power_up()
        time.sleep(5)
    except (KeyboardInterrupt, SystemExit):
        cleanAndExit()


I have installed python-function but i cant figure out how the output the numbers to a message.
i have also installed python-shell but with no luck

what am i dooing wrong

Christian

Colin Law

unread,
Jan 25, 2018, 12:05:05 PM1/25/18
to node...@googlegroups.com
You can use the Exec node to run it as if you were running it manually from the terminal, and the output will go back to node-red.

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/99b2497c-f6f6-477c-afd2-2f408aeab2a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Walter Kraembring

unread,
Jan 25, 2018, 1:38:52 PM1/25/18
to Node-RED
Well, there so many ways but

1) If you use the python-node, you can do like this (as example with a number)

val = 1.23456
node.warn (msg);
msg['payload'] = val
return msg

2) Let's assume that one day you would like to separate Node-RED and your GPIO pi and let them run on separate pi's in a distributed environment. In that case, use MQTT on both sides, very convenient, robust and secure if you use tls with certificates

Christian de Lasson

unread,
Jan 26, 2018, 1:50:21 AM1/26/18
to Node-RED
Tanks both of you.

I am using mqtt now to send the message but it's on the same Pi. And it says it lost connection and can't reconnect so I will try both you examples and send what's best.

Colin Law

unread,
Jan 26, 2018, 2:00:36 AM1/26/18
to node...@googlegroups.com
In that case fix the mqtt problem. Have you got two clients connected with the same client ID? That will cause connection problems.

Colin

On 26 Jan 2018 6:50 a.m., "Christian de Lasson" <christianr...@gmail.com> wrote:
Tanks both of you.

I am using mqtt now to send the message but it's on the same Pi. And it says it lost connection and can't reconnect so I will try both you examples and send what's best.

--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.

Christian de Lasson

unread,
Jan 26, 2018, 6:09:44 AM1/26/18
to Node-RED
No thats not the problem. it can run for an hour and then it says it can't reconnect.


Den fredag den 26. januar 2018 kl. 08.00.36 UTC+1 skrev Colin Law:
In that case fix the mqtt problem. Have you got two clients connected with the same client ID? That will cause connection problems.

Colin
On 26 Jan 2018 6:50 a.m., "Christian de Lasson" <christianr...@gmail.com> wrote:
Tanks both of you.

I am using mqtt now to send the message but it's on the same Pi. And it says it lost connection and can't reconnect so I will try both you examples and send what's best.

--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Colin Law

unread,
Jan 26, 2018, 6:17:12 AM1/26/18
to node...@googlegroups.com
Are you using mosquitto?
Is the mqtt server in the same computer?

What is there in the node-red log when it fails?

What is in the mosquitto log when it fails?  You can increase the verbosity on the mosquitto log if necessary.

Colin

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.

Max Hadley

unread,
Jan 26, 2018, 6:20:33 AM1/26/18
to Node-RED
If you have a long-running external Python (or other) script that you want to send messages from into Node-RED, since the script is on the same computer you can use the node-red-contrib-ipc node, and either Netcat or the Python socket library to communicate using a UNIX-domain socket (assuming you are running Linux on your Pi)

Dave C-J

unread,
Jan 26, 2018, 7:32:41 AM1/26/18
to node...@googlegroups.com
or the node-red-node-daemon node that will run a long running command and provide both input and output to it... and restart it if it stops.

Christian de Lasson

unread,
Jan 26, 2018, 2:31:31 PM1/26/18
to Node-RED
It's a raspberry pi 3 with raspian. i have installed mosqito as the mqtt broker and using paho to mqtt the data to the broker and then to node red. I just wan't a stable running of the script.

So the easy way is to run the python script in node red or what? I just can't get Walter's idea to work.

Dave C-J

unread,
Jan 26, 2018, 3:30:50 PM1/26/18
to node...@googlegroups.com
So the script needs to run and then it keeps sending readings ? If so you probably need to install node-red-node-daemon
Then set it to run python -u yourscript. With restart if you want. Feed that into a debug node to start with. 
--
Sent from phone.

Christian de Lasson

unread,
Jan 27, 2018, 3:08:18 AM1/27/18
to Node-RED
i have tryed all sort of thing. i have uninstalled the node and installed it again byt nothing helps from the log:

27 Jan 09:01:17 - [warn] [daemon:64cf645b.69cb7c] Restarting : python test.py
27 Jan 09:01:17 - [warn] [daemon:64cf645b.69cb7c] Command not found

It's just a python script that prints hallo

i have update npm and node red.

Dave C-J

unread,
Jan 27, 2018, 5:52:25 AM1/27/18
to node...@googlegroups.com
did you try fully pathing both the executable and the script ?
eg something like
/usr/bin/python /home/pi/test.sh

Christian de Lasson

unread,
Jan 27, 2018, 10:01:26 AM1/27/18
to Node-RED
yes i have still saying command not found.... i got it to work with exec but it aint restarting if the pi looses power. but i will test it for a cuppel of days now

Walter Kraembring

unread,
Jan 27, 2018, 1:29:40 PM1/27/18
to Node-RED
If you want better help, share your flow so we can have a look into it

For sure MQTT works very stable if it is correctly configured. I have been running such connections for years. Also python scripts work fine. I have used several times the node-red-contrib-python3-function and it works fine but with limited funcytionality, one is you cannot run python threads inside the node. Also external python scripts works excellent with MQTT, you just have to setup correctly

Since I do not have a HX711 I just want to know if you run the python script outside of node-red or inside a node-red-contrib-python3-function node?

But anyway, share your flow, too many people spend too much time guessing while trying to help you


Message has been deleted

Christian de Lasson

unread,
Jan 28, 2018, 2:35:09 AM1/28/18
to Node-RED
Here is the node flow i cant get to work but i would like to use this on that it will restart.

[{"id":"64cf645b.69cb7c","type":"daemon","z":"69504213.e7e4ec","command":"python/home/pi/test.py","args":"","cr":false,"redo":false,"op":"string","name":"","x":966,"y":741.5,"wires":[["58ca4340.17513c"],[],[]]},{"id":"58ca4340.17513c","type":"debug","z":"69504213.e7e4ec","name":"","active":true,"console":"false","complete":"false","x":1244,"y":732,"wires":[]}]

Her is the string i use it turns on the motor i 5 seconds hvis the value is under 100.

[{"id":"b5f5b236.3bdd2","type":"mqtt in","z":"69504213.e7e4ec","name":"mad","topic":"V/G","qos":"0","broker":"4486669.9d52898","x":303,"y":218,"wires":[["f994ca89.d2da18"]]},{"id":"fe8b2d89.29a2d","type":"switch","z":"69504213.e7e4ec","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"100","vt":"num"}],"checkall":"true","outputs":1,"x":640,"y":145,"wires":[["13a0f7ef.6e2548","32e3e44b.f2805c","a6079f1d.d6874"]]},{"id":"12ea8a1.43c4d76","type":"pushover","z":"69504213.e7e4ec","name":"mad","device":"","title":"","priority":0,"sound":"siren","url":"","url_title":"","x":1222.857177734375,"y":280.23809814453125,"wires":[]},{"id":"c2e2014a.ed46","type":"ui_text","z":"69504213.e7e4ec","group":"fce0de08.dea92","order":0,"width":0,"height":0,"name":"","label":"Vægt i skål","format":"{{msg.payload}}","layout":"row-spread","x":880.7143184116908,"y":253.33334786551336,"wires":[]},{"id":"497ce2f2.2c169c","type":"rpi-gpio out","z":"69504213.e7e4ec","name":"Korn","pin":"40","set":true,"level":"0","freq":"","out":"out","x":1280,"y":165,"wires":[]},{"id":"b71ffff9.af07a","type":"rpi-gpio out","z":"69504213.e7e4ec","name":"Kraftfoder","pin":"36","set":true,"level":"0","freq":"100","out":"out","x":1290,"y":216,"wires":[]},{"id":"13a0f7ef.6e2548","type":"trigger","z":"69504213.e7e4ec","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"5","extend":false,"units":"s","reset":"","name":"","x":886,"y":151,"wires":[["497ce2f2.2c169c","b71ffff9.af07a"]]},{"id":"32e3e44b.f2805c","type":"delay","z":"69504213.e7e4ec","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":942,"y":200,"wires":[["12ea8a1.43c4d76"]]},{"id":"f994ca89.d2da18","type":"smooth","z":"69504213.e7e4ec","name":"","action":"mean","count":"2","round":"","mult":"single","x":486.45835876464844,"y":182.5,"wires":[["fe8b2d89.29a2d","c2e2014a.ed46"]]},{"id":"80e61985.658d18","type":"exec","z":"69504213.e7e4ec","command":"python vagtudenmqtt.py","addpay":true,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"","x":805,"y":364.5,"wires":[["f994ca89.d2da18"],[],[]]},{"id":"c9c4b142.dde4c","type":"inject","z":"69504213.e7e4ec","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":424,"y":362,"wires":[["80e61985.658d18"]]},{"id":"a6079f1d.d6874","type":"debug","z":"69504213.e7e4ec","name":"","active":true,"console":"false","complete":"false","x":873,"y":73,"wires":[]},{"id":"1fc4b20b.4ee73e","type":"ui_button","z":"69504213.e7e4ec","name":"","group":"fce0de08.dea92","order":0,"width":0,"height":0,"passthru":false,"label":"Start vægt","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":424,"y":312,"wires":[["80e61985.658d18"]]},{"id":"4486669.9d52898","type":"mqtt-broker","z":"","broker":"192.168.1.3","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"fce0de08.dea92","type":"ui_group","z":"","name":"Hønsehus","tab":"560b9413.72345c","order":5,"disp":true,"width":"6"},{"id":"560b9413.72345c","type":"ui_tab","z":"","name":"Main","icon":"dashboard"}]



Here is the flow i got to work it can use mqtt and it works but the python script i got will crash and output this message below. Remember it runs on the same raspberry pi.

Traceback (most recent call last):
  File "vagt.py", line 50, in <module>
    client.connect("192.168.1.3",1883)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 768, in connect
    return self.reconnect()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 895, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "/usr/lib/python2.7/socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 101] Network is unreachable

Her is the script with the mqtt

import RPi.GPIO as GPIO
import time
import sys
import paho.mqtt.client as mqtt
from hx711 import HX711

def cleanAndExit():
    client.disconnect();
    print "Cleaning..."
    GPIO.cleanup()
    print "Bye!"
    sys.exit()

hx = HX711(23, 24)
hx.set_reading_format("LSB",)

hx.set_reference_unit(-921)

hx.reset()
hx.tare()

while True:
    try:
        val = hx.get_weight(5)
        print val

        client = mqtt.Client()
        client.connect("192.168.1.3",1883)
        client.publish("V/G", str(val), qos=1);

        hx.power_down()
        hx.power_up()
        time.sleep(5)
    except (KeyboardInterrupt, SystemExit):
        cleanAndExit()


I also got a script without mqtt and it also work in terminal it is her. this is the script i got to work with the exec node

import RPi.GPIO as GPIO
import time
import sys
from hx711 import HX711

def cleanAndExit():
    print "Cleaning..."
    GPIO.cleanup()
    print "Bye!"
    sys.exit()

hx = HX711(23, 24)

hx.set_reading_format("LSB",)

hx.set_reference_unit(-921)

hx.reset()
hx.tare()

while True:
    try:
        # These three lines are usefull to debug wether to use MSB or LSB in the reading formats
        # for the first parameter of "hx.set_reading_format("LSB", "MSB")".
        # Comment the two lines "val = hx.get_weight(5)" and "print val" and uncomment the three lines to see what it prints.
        #np_arr8_string = hx.get_np_arr8_string()
        #binary_string = hx.get_binary_string()
        #print binary_string + " " + np_arr8_string

        # Prints the weight. Comment if you're debbuging the MSB and LSB issue.
        val = hx.get_weight(5)
        print val

        hx.power_down()

Walter Kraembring

unread,
Jan 28, 2018, 4:13:48 AM1/28/18
to Node-RED
So you have a chicken farm !!! ;)

What I could see in your flows and python samples, I have recognized two things I would change:

First flow: you cant write the command like python/home/pi/test.py, you are missing a space, should be python /home/pi/test.py (you see the difference?) Also, use an exec node instead of the daemon node and do not append the payload from the triggering node unless you really want that





Second flow: Your python script with mqtt. I think you cannot do like that, you are initiating the mqtt client once every 5 second, most likely this is why you get the error. I would change the script like this (provided the rest is working as expected). And not to forget, you have to enter the full path to the script in the exec node. Like python /home/pi/vagtmedmqtt.py

import time
import sys
import paho.mqtt.client as mqtt
from hx711 import HX711

def cleanAndExit():
    client.disconnect();
    print "Cleaning..."
    GPIO.cleanup()
    print "Bye!"
    sys.exit()

hx = HX711(23, 24)
hx.set_reading_format("LSB",)

hx.set_reference_unit(-921)

hx.reset()
hx.tare()

client = mqtt.Client()
client.connect("192.168.1.3",1883)

while True:
    try:
        val = hx.get_weight(5)
        print val

        client.publish("V/G", str(val), qos=1);

Walter Kraembring

unread,
Jan 28, 2018, 4:42:16 AM1/28/18
to Node-RED
Besides, be aware that every time you click on the button "Start vægt" in the UI, a new instance of the python script will start. I guess this is not what you want?

I do not really understand the logic in your flow but that is another story, first step you need to fix the bugs

Dave C-J

unread,
Jan 28, 2018, 4:55:26 AM1/28/18
to node...@googlegroups.com
The way I read the python it looked like it looped and continued to send until stopped, in which case the daemon node would be more appropriate. But yes plenty of wrinkles to iron out first. 
--
Sent from phone.

Christian de Lasson

unread,
Jan 28, 2018, 6:50:34 AM1/28/18
to Node-RED
thanks to both of you.

Walter:
Yes i have tryed that to. that was only the last thing i tryed. and even with the space i still says "command not found" and if inject somthing to the deamon node it says command not running. I got it to work with exec. but maybe the node is broken.
I will try the mqtt part and se how it goes. and the button is only so i can be in the chicken coop and start the script.

Dave: Yes but it says command not found..... can it be broken?

Dave C-J

unread,
Jan 28, 2018, 1:16:17 PM1/28/18
to node...@googlegroups.com
Hi

can you  copy the contents of the debug log (command line where you have started Node-RED) so we can see the full error message ?

Christian de Lasson

unread,
Jan 29, 2018, 3:55:09 AM1/29/18
to Node-RED
29 Jan 09:53:43 - [info] Dashboard version 2.7.0 started at /ui
29 Jan 09:53:48 - [warn] ------------------------------------------------------
29 Jan 09:53:48 - [warn] [pythonshell] Error: Cannot find module './PythonshellNode' (line:19)
29 Jan 09:53:48 - [warn] ------------------------------------------------------
29 Jan 09:53:48 - [info] Settings file  : /home/pi/.node-red/settings.js
29 Jan 09:53:48 - [info] User directory : /home/pi/.node-red
29 Jan 09:53:48 - [info] Flows file     : /home/pi/.node-red/flows_raspberrypi.json
29 Jan 09:53:49 - [info] Server now running at http://127.0.0.1:1880/
29 Jan 09:53:49 - [debug] loaded flow revision: 11b583c2b9b83257301aa430489154fc
29 Jan 09:53:49 - [debug] red/runtime/nodes/credentials.load : no user key present
29 Jan 09:53:49 - [debug] red/runtime/nodes/credentials.load : using default key
29 Jan 09:53:49 - [info] Starting flows
29 Jan 09:53:49 - [debug] red/nodes/flows.start : starting flow : global
29 Jan 09:53:49 - [debug] red/nodes/flows.start : starting flow : 256e2a08.ef78b6
29 Jan 09:53:49 - [debug] red/nodes/flows.start : starting flow : 69504213.e7e4ec
29 Jan 09:53:49 - [info] Started flows
29 Jan 09:53:49 - [warn] [daemon:1334446.7b934bc] Command not found
29 Jan 09:53:49 - [info] [mqtt-broker:4486669.9d52898] Connected to broker: mqtt://192.168.1.3:1883
29 Jan 09:53:50 - [info] [mqtt-broker:393fe762.938e68] Connected to broker: mqtt://localhost:1883
29 Jan 09:54:07 - [warn] [daemon:1334446.7b934bc] Restarting : python /home/pi/test
29 Jan 09:54:07 - [warn] [daemon:1334446.7b934bc] Command not found
29 Jan 09:54:17 - [warn] [daemon:1334446.7b934bc] Restarting : python /home/pi/test
29 Jan 09:54:17 - [warn] [daemon:1334446.7b934bc] Command not found
29 Jan 09:54:27 - [warn] [daemon:1334446.7b934bc] Restarting : python /home/pi/test
29 Jan 09:54:27 - [warn] [daemon:1334446.7b934bc] Command not found
29 Jan 09:54:37 - [warn] [daemon:1334446.7b934bc] Restarting : python /home/pi/test
29 Jan 09:54:37 - [warn] [daemon:1334446.7b934bc] Command not found

Dave C-J

unread,
Jan 29, 2018, 4:15:19 AM1/29/18
to node...@googlegroups.com
Hi,

so there is a broken node in there (pythonshellnode) - which ideally should either be removed or fixed.
and then the command is not fully pathed as I suggested.
outside of Node-RED at a command line type  (where your command actually does work)
    which python
then use the response as the command for daemon 
and also add the full path to your script.

Then paste in the log for that.

Reply all
Reply to author
Forward
0 new messages