Node-RED Dashboard: Some simple questions

This is a little xample with NR communicating with a python script using mqtt

With "Go" you start the python script, then visible on the display

From the dashboard you pick a number that is sent to the script, returned back and displayed as the result

With "abort" you terminate the python script

[{"id":"ea37930f.f3f81","type":"exec","z":"8627eb61.169ee8","command":"export DISPLAY=:0 && lxterminal -e python3 /home/pi/test.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":550,"y":1280,"wires":[[],[],[]]},{"id":"c8a452e7.c8ba7","type":"mqtt in","z":"8627eb61.169ee8","name":"","topic":"from_python","qos":"2","datatype":"auto","broker":"75eba16c.094f9","x":150,"y":1500,"wires":[["e677846b.f968e8","fcc16d87.c21e1"]]},{"id":"1c4bbd7b.ee5dc3","type":"inject","z":"8627eb61.169ee8","name":"Go","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":1280,"wires":[["ea37930f.f3f81"]]},{"id":"e677846b.f968e8","type":"debug","z":"8627eb61.169ee8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":370,"y":1560,"wires":[]},{"id":"9bbac985.bba978","type":"mqtt out","z":"8627eb61.169ee8","name":"","topic":"to_python","qos":"","retain":"","broker":"75eba16c.094f9","x":380,"y":1360,"wires":[]},{"id":"b90a472b.2d32f8","type":"ui_numeric","z":"8627eb61.169ee8","name":"","label":"pick a number","tooltip":"","group":"b367811d.6667b","order":1,"width":0,"height":0,"passthru":true,"topic":"","format":"{{value}}","min":0,"max":10,"step":1,"x":160,"y":1360,"wires":[["9bbac985.bba978"]]},{"id":"fcc16d87.c21e1","type":"ui_text","z":"8627eb61.169ee8","group":"b367811d.6667b","order":2,"width":0,"height":0,"name":"","label":"result","format":"{{msg.payload}}","layout":"row-spread","x":370,"y":1500,"wires":[]},{"id":"ad31a88a.314728","type":"inject","z":"8627eb61.169ee8","name":"","topic":"","payload":"abort","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":1430,"wires":[["9bbac985.bba978"]]},{"id":"75eba16c.094f9","type":"mqtt-broker","z":"","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"b367811d.6667b","type":"ui_group","z":"","name":"User inputs & results","tab":"f1725e1b.29ba","disp":true,"width":"8","collapse":false},{"id":"f1725e1b.29ba","type":"ui_tab","z":"","name":"NR to Python","icon":"dashboard","disabled":false,"hidden":false}]

And the script....

import time
import paho.mqtt.client as mqtt
abort = False

def on_connect(client, obj, flags, rc):
    client.subscribe("to_python", 0)


def on_subscribe(client, userdata, mid, granted_qos):
    print ('Subscribed:', userdata, mid, granted_qos)
    print ('We are here, waiting for commands...')


def on_message(client, obj, msg):
    global abort
    print (msg.payload)
    if msg.payload.decode("utf-8") == "abort":
        abort = True
        return
    client.publish("from_python", 'Your input was '+str(msg.payload.decode("utf-8")), 1, False)


def main():
    client = mqtt.Mosquitto()
    client.on_connect = on_connect
    client.on_message = on_message
    client.on_subscribe = on_subscribe
    client.connect("127.0.0.1", 1883, 60)
    client.loop_start()
    client.publish("from_python", 'Hello user, please pick a number: ', 1, False)
        
    while abort == False:
        time.sleep(1)

    client.loop_stop()
    client.disconnect()
    del client
    exit(0)


if __name__ == '__main__':
    main()

1 Like