Version 6 (modified by 3 years ago) ( diff ) | ,
---|
Node-RED
Node-RED is software ran on Node.js that allows for flow-based visual programming in a browser, to wire together hardware, inputs, and services for Internet of Things (IoT) applications.
Node-RED can be used with the Gateworks SBCs.
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.
This wiki page assumes the usage of a Gateworks Venice SBC with the latest kernel (5.10 or newer)
Install Node-RED on Gateworks SBC
Installing Node-Red on a Gateworks SBC isn't any different than many other Linux platforms, which instructions are detailed on the internet.
Doing the below commands will make the Gateworks SBC act as the 'server'
sudo apt-get update sudo apt-get install mosquitto #if wanting to use the MQTT protocol to talk to client sudo apt-get install nodejs #beware Ubuntu default may be 10.19, and it would be better to use a newer version sudo apt-get install npm #get the node package manager sudo npm install -g --unsafe-perm node-red #install node-red using npm node-red #start the node-red application
Once node-red is running, use a browser to open the node-red gui on port 1880.
- Demo Setup:
- This demo takes an incoming MQTT message from a client and just prints it out in the debug screen.
- Use the MQTT input module connected to a debug module ti replicate what is shown in the picture below.
Create a Client on Gateworks SBC
sudo apt-get update sudo apt-get install python #if wanting to use the MQTT protocol to talk to client sudo apt-get install nodejs #beware Ubuntu default may be 10.19, and it would be better to use a newer version
Then, create a python script (example below) to send MQTT messages from the client SBC to the server SBC.
Gateworks Python Script to send MQTT Messages
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.
# Compatible with python 2.* version import sys import os.path import json import time import random import paho.mqtt.client as mqtt from datetime import datetime def on_message(client, userdata, message): print("message received " ,str(message.payload.decode("utf-8"))) print("message topic=",message.topic) print("message qos=",message.qos) print("message retain flag=",message.retain) def on_connect(client, userdata, rc,a): print("Connected with result code "+str(rc)) # Subscribing in on_connect() means that if we lose the connection and # reconnect then subscriptions will be renewed. client.subscribe("gateways/id/#") def main(argv): client = mqtt.Client("clientone") client.on_connect = on_connect client.on_message = on_message broker = "122.24.55.93" client.connect(broker) client.loop_start() time.sleep(5) while True: try: print("in loop") temp = float(open('/sys/class/hwmon/hwmon0/temp1_input', 'r').read().strip())/1000 client.publish("gateways/id/45",temp,1) print("done publishing") except Exception as ex: print(ex.message) sys.exit(0) time.sleep(5) if __name__ == "__main__": main(sys.argv)
Links & Resources
Attachments (2)
- nodered.png (73.7 KB ) - added by 3 years ago.
- nodered2.png (49.2 KB ) - added by 3 years ago.
Download all attachments as: .zip