| | 1 | = Node-RED |
| | 2 | |
| | 3 | Node-RED can be used with the Gateworks SBCs. |
| | 4 | |
| | 5 | Typically, Node-RED may actually run on a server machine and the Gateworks SBC acts as a client and talks back to the server. Or, Node-RED can be install on the Gateworks SBC itself. |
| | 6 | |
| | 7 | = Install Node-RED on Gateworks SBC |
| | 8 | |
| | 9 | Doing the below commands will make the Gateworks SBC act as the 'server' |
| | 10 | {{{ |
| | 11 | |
| | 12 | sudo apt-get update |
| | 13 | sudo apt-get install mosquitto #if wanting to use the MQTT protocol to talk to client |
| | 14 | sudo apt-get install nodejs #beware Ubuntu default may be 10.19, and it would be better to use a newer version |
| | 15 | sudo apt-get install npm #get the node package manager |
| | 16 | sudo npm install -g --unsafe-perm node-red #install node-red using npm |
| | 17 | node-red |
| | 18 | }}} |
| | 19 | |
| | 20 | Once node-red is running, use a browser to open the node-red gui on port 1880. |
| | 21 | * eg http://122.24.11.93:1880/ |
| | 22 | |
| | 23 | = Create a Client on Gateworks SBC |
| | 24 | {{{ |
| | 25 | |
| | 26 | sudo apt-get update |
| | 27 | sudo apt-get install python #if wanting to use the MQTT protocol to talk to client |
| | 28 | sudo apt-get install nodejs #beware Ubuntu default may be 10.19, and it would be better to use a newer version |
| | 29 | |
| | 30 | }}} |
| | 31 | |
| | 32 | Then, create a python script (example below) to send MQTT messages from the client SBC to the server SBC. |
| | 33 | |
| | 34 | == Gateworks Python Script to send MQTT Messages |
| | 35 | {{{ |
| | 36 | root@focal-venice:~# cat gateworks.py |
| | 37 | # Compatible with python 2.* version |
| | 38 | import sys |
| | 39 | import os.path |
| | 40 | import json |
| | 41 | import time |
| | 42 | import random |
| | 43 | import paho.mqtt.client as mqtt |
| | 44 | from datetime import datetime |
| | 45 | |
| | 46 | def callbackMessage(msg): |
| | 47 | if msg: |
| | 48 | print("\n--- Command Message Received ---") |
| | 49 | print(str(msg['ack'])) |
| | 50 | print(str(msg['ackId'])) |
| | 51 | print(str(msg['command'])) |
| | 52 | print(str(msg['uniqueId'])) |
| | 53 | |
| | 54 | def callbackTwinMessage(msg): |
| | 55 | if msg: |
| | 56 | print("\n--- Twin Message Received ---") |
| | 57 | print(json.dumps(msg)) |
| | 58 | |
| | 59 | def main(argv): |
| | 60 | while True: |
| | 61 | try: |
| | 62 | env = "enter_evn_name_here" |
| | 63 | |
| | 64 | uniqueId = "enter_device_id_here_example_device1" |
| | 65 | cpId = "enter_company_id_here_from_avnet_portal" |
| | 66 | print("in loop") |
| | 67 | client = mqtt.Client("clientone") |
| | 68 | broker = "122.24.11.93" |
| | 69 | client.connect(broker) |
| | 70 | client.subscribe("house/bulbs") |
| | 71 | client.publish("house/bulbs","OFF") |
| | 72 | print("done publishing") |
| | 73 | |
| | 74 | except Exception as ex: |
| | 75 | print(ex.message) |
| | 76 | sys.exit(0) |
| | 77 | time.sleep(10) |
| | 78 | |
| | 79 | |
| | 80 | if __name__ == "__main__": |
| | 81 | main(sys.argv) |
| | 82 | |
| | 83 | }}} |
| | 84 | |
| | 85 | |
| | 86 | |