Documentation

c-treeEDGE IoT Database

Previous Topic

Next Topic

Node-RED Using the c-treeEDGE MQTT Broker

This tutorial will demonstrate collecting sensor data in Node-RED on a Raspberry Pi and storing the data in a c-treeEDGE database utilizing MQTT.

Node-RED is a simple way to create applications by dragging together nodes. c-treeEDGE provides a connector so it can be used with Node-RED. Because it supports several IoT operating systems, c-treeEDGE can run on an edge device or on a PC used as a gateway. For more about support for MQTT provided by FairCom, see FairCom MQTT Support 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 c-treeEDGE MQTT Broker.

  • If you are not using MQTT in your project, you can use the c-treeEDGE Node.js node to add edge persistence to your project. See Node-RED Node for c-treeEDGE.

In this section, you will learn how to install Node-RED and c-treeEDGE 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 c-treeEDGE MicroServer, as described in Installing the c-treeEDGE MicroServer.

To use c-treeEDGE with Node-RED, follow these steps:

  1. It is good practice to update the operating system on your Raspberry Pi before installing or updating other software:

    sudo apt-get update

    sudo apt-get dist-upgrade

  2. Upgrade Node-RED on the Pi. Note that an earlier version of Node-RED comes pre-installed on the full Raspbian image, however we will use the newer version:

    https://nodered.org

    Raspberry Pi install/upgrade page for Node-RED:

    https://nodered.org/docs/hardware/raspberrypi

    The following command works well:

    bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)

  3. Set Node-RED to start on launch:

    sudo systemctl enable nodered.service

  4. Reboot or start Node-RED manually:

    sudo systemctl start nodered.service

  5. Optional: Install Firefox on the Pi. In early releases, Node-RED performed better on Firefox than on the built-in browser, but it now works on both.

    sudo apt-get install firefox-esr

  6. Launch Firefox (or the built-in browser) and go to the following URL to open Node-RED:

    http://localhost:1880

Using Node-RED:

  1. Install nodes needed for your sensors on the Pi.

    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.

  2. Make a Flow that reads your sensors on the Pi.

    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.

  3. Install c-treeEDGE. It comes packaged as a standard zip file. Send it to the Pi and unzip it.
  4. Launch c-treeEDGE. Using the terminal, navigate into the folder to c-treeEDGE.<platform>/server. and launch the server with the following command:

    ./startedge

  5. Change the flow to send data to the c-treeEDGE broker via MQTT.

    Drag an MQTT output node onto the flow and connect the sensor output to its input.

  6. Double-click the MQTT node to configure it.

  7. Enter a string in the Topic field such as Sensor1. This will identify messages being sent from this sensor. Set QoS to 0 and Retain to false.
  8. Click the pencil icon to the right of the Server field to configure a new server setup.

  9. Give it a name such as ctreeEDGE.
  10. Enter localhost in the Server field.
  11. Click Add then Done.
  12. Your Raspberry Pi should now be broadcasting MQTT messages to c-treeEDGE.

Check Messages

Now let's check to see if the messages are getting to the c-treeEDGE MQTT Broker:

  1. In your Node-RED flow, drag an MQTT input node in and configure it to connect to the c-treeEDGE Broker. For the Server field, pick the same MQTT setup you added for the MQTT output node above. Set the Topic to Sensor1. Set the QoS to 0.
  2. Drag in a debug node and place it next to your MQTT output node and connect those two nodes. Deploy and switch to the debug tab and you should see your sensor data coming through every 10 seconds.

Turn on persistence and save your sensor data.

MQTT must be configured as described in Setting up MQTT Persistence on the Edge.

Persistence is controlled via JSON data published to the c-treeEDGE broker on a predefined System Topic, ctreeAdministration, that the server watches.

  1. Drag the following nodes in to your flow:
    inject input node
    MQTT output node
    Connect the right side of the inject node to the left side of the MQTT node.
  2. Configure the MQTT output node to use the same MQTT server setup created above. Set the topic to ctreeAdministration.
  3. Configure the inject node to use a payload of JSON. Leave repeat turned off so it will only fire when clicked on. Click the JSON editor button (the three dots at the right edge of the payload field) and paste the JSON code shown below into the JSON editor window. Click Done. Change the Name field to "Persistence On" and then click Done again to save the changes to the inject node.

    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.

    {

    "operation": "CreatePersistenceTopic",

    "persistenceTopic": "Sensor1",

    "databaseConnectionString": "FAIRCOMS@localhost",

    "tableName": "s1db",

    "tableAutoTimeStamp": true,

    "tableReplicationReady": true,

    "mapOfPropertiesToFields": [

    {

    "jsonPropertyPath": "temperature_F",

    "fieldName": "temperature",

    "fieldType": "DOUBLE"

    },

    {

    "jsonPropertyPath": "humidity",

    "fieldName": "humidity",

    "fieldType": "DOUBLE"

    },

    {

    "jsonPropertyPath": "pressure_Hg",

    "fieldName": "pressure",

    "fieldType": "DOUBLE"

    }

    ]

    }

    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.

    {

    "operation": "DeletePersistenceTopic",

    "databaseConnectionString": "FAIRCOMS@localhost",

    "persistenceTopic": "Sensor1"

    }

    Click the button to the left of the "Persistence On" inject node to create the table.

  4. As soon as an MQTT message with a matching topic comes into c-treeEDGE, your table will be created in c-treeEDGE.<platform>/server/data/ctree*.dbs/
  5. Open c-treeACE SQL Explorer (or any ODBC SQL query tool) on a Windows PC and perform a query on the data:

    Select * from Sensor1Table where temperature > 72.0

  6. When you are done, click the button to the left of the "Persistence Off" inject node to stop creating a new record for each matching MQTT message.

TOCIndex