Temperature sensor data visualisation with Node-RED: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
Line 176: Line 176:


= Putting it together =
= Putting it together =
Adding final nodes and cleaning up


== Without file saving ==
== Without file saving ==

Revision as of 03:25, 22 September 2023

Overview

This lesson assumes that a temperature sensor is sending data to The Things Network. In this lesson we will use the MQTT protocol to subscribe to data transmission events. We will create a program in Node-RED to subscribe to the data transmissions and to graph the data coming from the sensor using the in-built Dashboard function in Node-RED.

Learning Objectives

  • Learn how to subscribe to a MQTT using the Node-RED MQTT-in node
  • Learn how to extract sensor data using the Node-RED function node.
  • Learn how to create a Dashboard in Node-RED to monitor live data.

Subscribe to MQTT

Open Node-RED

  • Click on the Raspberry Pi main drop down menu.
  • Select Programming > Node-RED

  • Node-RED will automatically open the Terminal on the Raspberry Pi.
  • It takes approximately ten seconds for Node-RED to initialise itself.

  • To actually use Node-RED you need to open the web browser on the Raspberry Pi.
  • Click on the default web browser.
  • In the search bar enter localhost:1880
  • Localhost is the address of the local or host machine.
  • In this example the host address is 192.168.1.168, but your computer will have a different address.
  • The number at the end of the URL (1880) is the port number for the Node-RED application.
  • Different ports are assigned to different applications using HTTP protocols.
  • Note - if you are using UFW (uncomplicated firewall) you may need to open this port otherwise Node-RED will not work.

Subscribing to MQTT

  • To subscribe to MQTT we need the 'mqtt in node.
  • Find the mqtt in node and drag it onto the Flow 1
  • You will notice that the node has both a blue circle and a red triangle.
  • The red triangle indicates that the node requires further configuration before it will work

Configuring the mqtt in node

  • Double click on the mqtt in node.
  • In Properties > Server > select Add new mqtt-broker
  • Click on the Edit button to the right - which looks like a pencil

  • Enter the following details:
    • Properties > Name > pycom-temperature-1
    • Connection > Server > au1.cloud.thethings.network
    • Connection > Port > 1883
    • Leave all other details as shown in the example below.

  • Click on the Security tab
  • The Username is whi-seedling-1-pycom@ttn
  • The Password is NNSXS.Z625CQKKEWAPSQK467JRSAFFYINH7LTHSUC5CYY.MHEVWHKKLPWXQWESSCALZFVHA6EKUNISQH4IVHKAOXYZRMOH6D7A
  • If copying these fields from mediawiki or another computer use right mouse button click copy
  • Click on the red Add button to commit the changes.

  • You will be asked to complete some final configuration settings.

  • Update the following fields:
    • Topic # - The hash symbol means subscribe to all topics
    • QoS 0 - quality of service. A value of zero equates to a low quality. Data losses are acceptable.
    • Output a Parsed JSON Object - JSON format output for all data
    • Name pycom-temperature-1
  • Then click on Done to commit all the edits.

  • The finished mqtt in node should now appear in the main flow window.
  • It will still have a blue circle because the flow has not been Deployed.

Add a Debug Node

  • Before we Deploy this first flow we will need to add a Debug node.
  • Drag a Debug node from the pallet and connect it to the output of the mqtt in node (pycom-temperature-1).

Set the Debug Window

  • The Debug node will show us all the JSON data output coming from mqtt in node.
  • To see the Debug output click on the Debug messages tab.

  • Click on the Deploy button to make the flow active.
  • Message should appear indicating that the flow has been successfully Deployed.
  • The mqtt in node will also show a green connected status message underneath the node.

Receiving mqtt data

  • When data arrives from the temperature sensor (approximately every 30 minutes) it will appear in the Debug window on the right of the screen.
  • The data is presented in a compacted form.

  • By clicking on the drop down arrows in the message the different sections of the message can be expanded.
  • Expand the uplink_message and the decoded_payload to see the sensor data.

Extracting sensor data

  • We can extract sensor data using the Node-RED function node.

Obtaining data using the function node

  • In this example we will prepare a function node to extract all the data.
  • Drag a function node into the Edit window.

  • Then drag a Debug node and wire up all nodes as shown.

Editing the Function node

  • Double click on the Function node to bring up the edit window.

  • Enter the following code to extract all the data.
  • Then click on Done to commit the edits.

  • The code has been reproduced below.
var time = msg.mytime.concat(" " + (msg.myymd));
var battery; // battery voltage data
var humidity; // humidity data from within the sensor housing
var onewire_temp; // onewire temperature data
var temperature; // temperature data from within the sensor housing

battery = msg.payload.uplink_message.decoded_payload.bat;
humidity = msg.payload.uplink_message.decoded_payload.humid;
onewire_temp = msg.payload.uplink_message.decoded_payload.onewire_temp;
temperature = msg.payload.uplink_message.decoded_payload.temp;

msg.payload = "time=" + String(time) + " "
            + "bat=" + String(battery) + " "
            + "humid=" + String(humidity) + " "
            + "onewire_temp=" + String(onewire_temp) + " "
            + "temp=" + String(temperature);

return msg;


  • The complete nodes should appear as in the image below.
  • Click on Deploy to make the flow active.

  • Click on the Clear log button to clear the Debug window.

  • When new messages arrive they can be more clearly seen in the Debug window.
  • In this case the voltage of the battery is 4 (4.00 V).

Putting it together

Adding final nodes and cleaning up

Without file saving

With file saving

Add/remove nodes if necessary. Your final setup should be similar to below

For the file save node ensure you have set you path. Example shown below

Appendix

  • Node-RED code
[
    {
        "id": "4a48c27d77b24951",
        "type": "tab",
        "label": "Remote Tiny-House-12 Monitoring",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "9eb5f086aba506f2",
        "type": "mqtt in",
        "z": "4a48c27d77b24951",
        "name": "pycom-temperature-1",
        "topic": "#",
        "qos": "0",
        "datatype": "json",
        "broker": "770475c1.8593ac",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 160,
        "y": 300,
        "wires": [
            [
                "1fb65b545310430e",
                "8495f4220d172f63"
            ]
        ]
    },
    {
        "id": "1b6fa839582e5f27",
        "type": "debug",
        "z": "4a48c27d77b24951",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 640,
        "y": 300,
        "wires": []
    },
    {
        "id": "42efdbdb620f1c41",
        "type": "function",
        "z": "4a48c27d77b24951",
        "name": "function 1",
        "func": "var time = msg.mytime.concat(\" \" + (msg.myymd));\nvar battery; // battery voltage data\nvar humidity; // humidity data from within the sensor housing\nvar onewire_temp; // onewire temperature data\nvar temperature; // temperature data from within the sensor housing\n\nbattery = msg.payload.uplink_message.decoded_payload.bat;\nhumidity = msg.payload.uplink_message.decoded_payload.humid;\nonewire_temp = msg.payload.uplink_message.decoded_payload.onewire_temp;\ntemperature = msg.payload.uplink_message.decoded_payload.temp;\n\nmsg.payload = \"time=\" + String(time) + \" \"\n            + \"bat=\" + String(battery) + \" \"\n            + \"humid=\" + String(humidity) + \" \"\n            + \"onewire_temp=\" + String(onewire_temp) + \" \"\n            + \"temp=\" + String(temperature);\n\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 500,
        "y": 320,
        "wires": [
            [
                "1b6fa839582e5f27",
                "2b50ee3d9439b414"
            ]
        ]
    },
    {
        "id": "1fb65b545310430e",
        "type": "debug",
        "z": "4a48c27d77b24951",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 340,
        "y": 280,
        "wires": []
    },
    {
        "id": "8495f4220d172f63",
        "type": "simpletime",
        "z": "4a48c27d77b24951",
        "name": "",
        "mydate": true,
        "myymd": true,
        "myyear": true,
        "mymonth": true,
        "mymonthn": true,
        "mydom": true,
        "mydoy": true,
        "myday": true,
        "myhourpm": true,
        "myhour": true,
        "mytime": true,
        "mytimes": true,
        "myminute": true,
        "myminutes": true,
        "mysecond": true,
        "mymillis": true,
        "myepoch": true,
        "myrawdate": true,
        "mypm": true,
        "x": 350,
        "y": 320,
        "wires": [
            [
                "42efdbdb620f1c41"
            ]
        ]
    },
    {
        "id": "2b50ee3d9439b414",
        "type": "file",
        "z": "4a48c27d77b24951",
        "name": "",
        "filename": "/home/birdnet/Documents/tinyhouse.txt",
        "filenameType": "str",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 730,
        "y": 340,
        "wires": [
            [
                "90306f0828267f4c"
            ]
        ]
    },
    {
        "id": "90306f0828267f4c",
        "type": "debug",
        "z": "4a48c27d77b24951",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 960,
        "y": 340,
        "wires": []
    },
    {
        "id": "770475c1.8593ac",
        "type": "mqtt-broker",
        "name": "pycom-temperature-1",
        "broker": "au1.cloud.thethings.network",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]