This tutorial demonstrates collecting sensor data in Node-RED on a Raspberry Pi and storing the data in the FairCom EDGE database utilizing MQTT.
Node-RED is a simple way to create applications by dragging together nodes. FairCom EDGE provides a plug-in so it can be used with Node-RED. Because it supports several IoT operating systems, FairCom EDGE can run on an edge device or on a PC used as a gateway. For more about support for MQTT provided by FairCom, see the FairCom MQTT guide in the on-line documentation.
If your Node-RED project is using MQTT, the easiest way to add edge persistence to your project is to drop in the FairCom EDGE MQTT Broker.
In this section, you will learn how to install Node-RED and FairCom EDGE on a Raspberry Pi. For more about the Node-RED development environment, see Node-RED later in this document.
You will need to install and start the FairCom EDGE database, as described in Download and Installation.
To use FairCom EDGE with Node-RED, follow these steps:
sudo apt-get update
sudo apt-get dist-upgrade
Raspberry Pi install/upgrade page for Node-RED:
The following command works well:
bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)
sudo systemctl enable nodered.service
sudo systemctl start nodered.service
sudo apt-get install firefox-esr
This is done inside Node-RED. Using the Hamburger Menu, select Manage Palette and go to the Install tab. For this example, search for "BME280" to complete the install.
Note the Inject node on the far left is set to repeat a Boolean True signal. Select a reasonable value, such as every 10 seconds. The Bme280 sensor will produce JSON output on any input, so this results in a sensor reading every 10 seconds.
Drag an MQTT output node onto the flow and connect the sensor output to its input.
Now let's check to see if the messages are getting to the FairCom EDGE MQTT Broker:
Turn on persistence and save your sensor data.
MQTT must be configured as described in Configuring the MQTT Plug-in.
Persistence is controlled via JSON data published to the FairCom EDGE broker on a predefined System Topic, ctreeAdministration, that the server watches.
The JSON object describes the database to create and what data to save. By adjusting the JSON code you can control the database name created, the tags persisted to fields, and the names and types of those fields.
If you want to be able to stop or turn off persisting the data, drag another inject node into the flow and connect it to the MQTT output node the same way you did the first inject node. Change the Payload Field to be JSON and open the JSON editor like before and paste the JSON code shown below into the editor window. Click Done and then change the Name field to be "Persistence Off". Click Done to save the changes to the node.
Click the button to the left of the "Persistence On" inject node to create the table.
Select * from Sensor1Table where temperature > 72.0