Changes between Version 5 and Version 6 of nodered


Ignore:
Timestamp:
12/17/2021 11:14:33 PM (3 years ago)
Author:
Ryan Erbstoesser
Comment:

add resources, also update script

Legend:

Unmodified
Added
Removed
Modified
  • nodered

    v5 v6  
    4545
    4646== Gateworks Python Script to send MQTT Messages
     47
     48The 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
    4750{{{
    48 root@focal-venice:~# cat gateworks.py
    4951# Compatible with python 2.* version
    5052import sys
     
    5658from datetime import datetime
    5759
    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']))
     60def 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)
    6565
    66 def callbackTwinMessage(msg):
    67     if msg:
    68         print("\n--- Twin Message Received ---")
    69         print(json.dumps(msg))
     66def 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/#")
    7071
    7172def 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)
    7280 while True:
    7381    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"
    7882        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)
    8485        print("done publishing")
    8586
     
    8788        print(ex.message)
    8889        sys.exit(0)
    89     time.sleep(10)
     90    time.sleep(5)
    9091
    9192
     
    9394    main(sys.argv)
    9495
     96
    9597}}}
    9698
    9799
    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/]