Changes between Version 5 and Version 6 of nodered
- Timestamp:
- 12/17/2021 11:14:33 PM (3 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
nodered
v5 v6 45 45 46 46 == Gateworks Python Script to send MQTT Messages 47 48 The below is a sample python script to pass MQTT messages from the Gateworks SBC to the Node-Red server (broker). The script will pass the Gateworks SBC temperature as the payload and send it specific to the devices serial number. 49 47 50 {{{ 48 root@focal-venice:~# cat gateworks.py49 51 # Compatible with python 2.* version 50 52 import sys … … 56 58 from datetime import datetime 57 59 58 def callbackMessage(msg): 59 if msg: 60 print("\n--- Command Message Received ---") 61 print(str(msg['ack'])) 62 print(str(msg['ackId'])) 63 print(str(msg['command'])) 64 print(str(msg['uniqueId'])) 60 def on_message(client, userdata, message): 61 print("message received " ,str(message.payload.decode("utf-8"))) 62 print("message topic=",message.topic) 63 print("message qos=",message.qos) 64 print("message retain flag=",message.retain) 65 65 66 def callbackTwinMessage(msg): 67 if msg: 68 print("\n--- Twin Message Received ---") 69 print(json.dumps(msg)) 66 def on_connect(client, userdata, rc,a): 67 print("Connected with result code "+str(rc)) 68 # Subscribing in on_connect() means that if we lose the connection and 69 # reconnect then subscriptions will be renewed. 70 client.subscribe("gateways/id/#") 70 71 71 72 def main(argv): 73 client = mqtt.Client("clientone") 74 client.on_connect = on_connect 75 client.on_message = on_message 76 broker = "122.24.55.93" 77 client.connect(broker) 78 client.loop_start() 79 time.sleep(5) 72 80 while True: 73 81 try: 74 env = "enter_evn_name_here"75 76 uniqueId = "enter_device_id_here_example_device1"77 cpId = "enter_company_id_here_from_avnet_portal"78 82 print("in loop") 79 client = mqtt.Client("clientone") 80 broker = "122.24.11.93" 81 client.connect(broker) 82 client.subscribe("house/bulbs") 83 client.publish("house/bulbs","OFF") 83 temp = float(open('/sys/class/hwmon/hwmon0/temp1_input', 'r').read().strip())/1000 84 client.publish("gateways/id/45",temp,1) 84 85 print("done publishing") 85 86 … … 87 88 print(ex.message) 88 89 sys.exit(0) 89 time.sleep( 10)90 time.sleep(5) 90 91 91 92 … … 93 94 main(sys.argv) 94 95 96 95 97 }}} 96 98 97 99 98 100 == Links & Resources 101 * [https://nodered.org/] 102 * [http://www.steves-internet-guide.com/into-mqtt-python-client/] 103 * [https://nodejs.org/en/about/releases/]