How to Send & Receive MQTT Sparkplug B Messages Using Raspberry Pis, Node-RED, and HiveMQ Cloud

How to Send & Receive MQTT Sparkplug B Messages Using Raspberry Pis, Node-RED, and HiveMQ Cloud

author portrait

Written by Kudzai Manditereza

Category: MQTT Industry 4.0 MQTT Sparkplug

Published: June 13, 2021


In part 1 of this blog series How to Connect a Smart Factory and Visualize IIoT Data with MQTT Sparkplug, we demonstrated one of the ways to use MQTT Sparkplug B messages to connect a smart factory to the cloud without installing any software. In part 2 of this blog series, we will simulate a simple greenhouse remote monitoring system using Raspberry Pis, Node-Red, and HiveMQ Cloud as the MQTT broker. The purpose of this blog post is to show how to use the MQTT Sparkplug B specification to make greenhouse information discoverable by industry 4.0 applications that may join an MQTT network.

A Real-World Use Case of MQTT Sparkplug B Messages

For the purpose of the demonstration, we will use two Raspberry Pis. One of the Raspberry Pis will act as a greenhouse control unit measuring humidity and temperature. We will use a DHT11 sensor for that. The same Raspberry Pi will also monitor unauthorised access using a proximity sensor. The other Raspberry Pi will act as a remote monitoring station, which receives greenhouse telemetry data and displays the temperature and humidity data on an HMI. The second Raspberry Pi will also monitor the intrusion and switch ON an AC lamp when an intrusion is detected. Refer the below diagram, which represents the simulation of a remote greenhouse monitoring system.

Simulation of a remote greenhouse monitoring system using MQTT Sparkplug B, HiveMQ Cloud, Raspberry Pi, and Node-Red

The Raspberry Pi devices shown above act as MQTT clients that are sending and receiving MQTT messages. To facilitate MQTT communication between the devices, we are going to use HiveMQ Cloud as the centralized MQTT broker. The Greenhouse control unit device is responsible for:

  • Measuring temperature and humidity of the greenhouse
  • Detecting intrusion when it occurs
  • Publishing the data to HiveMQ Cloud Broker under a specific topic.

To receive this data from the Greenhouse control unit, our remote monitoring station will subscribe to the same topic on the HiveMQ Cloud broker.

Note that we do not want our Greenhouse remote monitoring system to be a standalone MQTT system. Instead we want our greenhouse information to be automatically discoverable by other Industry 4.0 applications that may be interested in it. Therefore, we are going to use the MQTT Sparkplug B specification, which is also supported by HiveMQ Cloud MQTT broker.

The hardware setup is as shown below:

Hardware setup to simulate a remote greenhouse monitoring system using Raspberry Pi, Node-Red, and HiveMQ Cloud MQTT broker.

On our publishing Raspberry Pi, we will use Node-Red to read sensor data, convert it into Sparkplug B format and publish the data to the HiveMQ Cloud broker, which will forward that message to the remote monitoring station.

When the remote monitoring unit receives the messages, it will also use Node-Red to read the MQTT message, decode the MQTT Sparkplug B message format to extract the information, display the information on HMI and switch the AC light On or OFF depending on the state of the proximity sensor.

Step-by-Step Guide to Monitor a Remote Greenhouse Using MQTT Sparkplug B Messages

To start building the greenhouse remote monitoring system, follow the below steps to set up everything:

Step 1: Setting up HiveMQ Cloud MQTT Broker

To setup your MQTT broker on HiveMQ Cloud, follow the below steps:

  1. Visit HiveMQ MQTT Cloud broker page and sign up for the free version.
HiveMQ Cloud MQTT Broker
  1. Once you sign up, either use your email ID or your GitHub login to log in to your account as shown below.
HiveMQ Cloud MQTT Broker Sign-up Page
  1. Set up the credentials that your Raspberry Pi Edge of Network Nodes will use to connect to the HiveMQ Cloud MQTT broker as shown below. These credentials can be updated whenever necessary.
HiveMQ Cloud cluster details
  1. After you login to the console, provision an MQTT cluster. Here, we have already got a cluster that was automatically provisioned by HiveMQ Cloud during signup.
  2. Click on Manage Cluster as shown in the below.
Creating and managing cluster inside HiveMQ Cloud MQTT broker
  1. You will have access to your Cluster Details as shown below.
HiveMQ Cloud cluster details in the portal
  1. Under the Overview tab, you can see your broker url address or hostname and the port number. Make a note that you are using TLS for encrypted MQTT communication.
  2. Under the Access Management tab, make a note of the credentials used by MQTT clients to connect to this broker. This completes the setting up of your MQTT broker. The broker is ready to receive messages from clients.
  3. Copy the broker details and use them to configure Node-Red running on the Raspberry Pi devices to start publishing and subscribing to the MQTT broker.

Step 2: Setting-up Node-Red on Raspberry Pi to Read and Publish Sensor Data to HiveMQ Cloud

To read and publish sensor data to the HiveMQ Cloud Broker as MQTT Sparkplug messages, we need to make sure that Node-Red is running on the Raspberry Pi. To launch Node-Red, open the Raspberry Pi terminal. Here, we are using PuTTY to access the terminal from the desktop.

  1. Type the IP address of the Raspberry Pi in the_ PuTTY Configuration_ and open the connection as shown below.
PuTTY Configuration to set up Node-Red on Raspberry Pi
  1. Log into the device and type in the “node-red-start” command on the terminal to start Node-Red as shown below. If Node-Red is not installed on your Raspberry Pi, you can go ahead and install it first.
Node-Red Console
  1. Access the web interface using the browser. Pull up the browser and type in the IP address of the Raspberry Pi followed by 1880, which is the port number used by Node-Red. You will get access to your Node-Red dashboard as shown below.
Node-Red dashboard
  1. To read the data from the DHT11 temperature and humidity sensor, install the DHT11 Node-Red package using the Deploy menu and select Manage pallette from the drop down menu as shown below.
Node-Red Deploy Raspberry Pi
  1. Search for the DHT sensor package. For example, we have a node-red-contrib-dht-sensor, which we had already installed. If you have not installed yet, go ahead and install it.
Searching for DHT sensor package on Node-Red dashboard
  1. Create a Node-Red flow for reading, converting, and publishing MQTT sparkplug messages. You can do this by dragging an inject node and function blocks onto the canvas and then wiring them up as shown in the below diagram.
Creating a flow in Node-Red for reading, converting and publishing MQTT Sparkplug B messages
  1. Double-click on the timestamp and set it to repeat at intervals of five seconds.
Setting up timestamp node on Node-Red to read and publish MQTT Sparkplug B messages
  1. Open the DHT 11 sensor settings and add the Properties values as shown in the below diagram. The GPIO pin number should be the pin number the DHT sensor is connected to. Click on Done.
DHT11 sensor settings on Node-Red to read MQTT Sparkplug B messages

Step 3: Extracting Greenhouse Humidity and Temperature Values

To extract the greenhouse humidity and temperature values, you need to set the Node-Red function blocks. Follow the below steps:

  1. Open Edit function node.
Edit Function node on Node-Red
  1. Pass the string coming out of the DHT node into a flow to a value.

Step 4: Prepare an MQTT Sparkplug B Message Object on Node-Red

Follow the below steps to prepare MQTT Sparkplug B message blocks on Node-Red:

  1. Create two function blocks called Prepare Sparkplug B Message.
Prepare an MQTT Sparkplug B Message Object on Node-Red
  1. Inside each of these function nodes, paste the code for creating an MQTT Sparkplug object along with corresponding Properties values as shown below. Observe that we are taking the output of the DHT11 sensor and using it to create an Sparkplug message object. We are using data type number as 9, because it corresponds to a float data type and the actual float value.
code for creating an MQTT Sparkplug object on Node-Red
  1. Follow the same steps for humidity data.
  2. Connect the function blocks.
  3. Drag two encoder nodes named sparkplug_b encoder onto the canvas. This will encode the messages before transmission.
  4. Use a protobuf node read package, which you can install by going to Deploy, then Manage palette, and then searching for node-red-contrib-protobuf. Again, we have already got that installed, so you can go ahead and install it.
node-red-contrib-protobuf
  1. Connect the output of the _Prepare Sparkplug B Message _functions to the input of the respective sparkplug_b encoder function.
Preparing Sparkplug B Messages on Node-Red
  1. Repeat the same for humidity data.

Step 5: Encoding the MQTT Sparkplug B Messages

To encode the MQTT Sparkplug B messages, we need a sparkplug_b.proto file, which you can get from the eclipse tower project. Follow the below steps:

  1. Go ahead and download the file and make sure to save it with a dot proto extension.
  2. Now once you’ve got the file, you can use a File Transfer Tool like Win SCP to copy the file to a directory of your choice on the Raspberry Pi device. Here, we have copied the .proto file onto home/pi/sparkplugb directory.
Win SCP to copy the file to a directory of your choice on the Raspberry Pi device
  1. Double click on the Sparkplug in the called node and notice that we have put the file path to the sparkplug_b.proto file.
Encoding message in Sparkplug B specification format using Node-Red

We’ve now encoded our message in Sparkplug B specification format.

Step 6: Publishing Sparkplug B messages to HiveMQ Cloud MQTT Broker

Follow the below steps:

  1. Drag the MQTT transmission node onto the canvas and double click on the MQTT transmission node to see if you have configured the HiveMQ Cloud.
  2. Under the Connection tab as shown in the diagram below, add your HiveMQ Cloud broker URL address or hostname that you copied from the portal. Note that you must set the port 8883, select TLS and assign the device with MQTT Client ID.
  3. Add the access credentials created on my HiveMQ cloud portal.
access credentials created on my HiveMQ cloud portal
  1. Under Topic of mqtt out node, add the Sparkplug B topic namespace as shown in the image below. Here mygreenhouse is the Group ID, DDATA is the message type, and MyRaspberryPi is the Edge Node ID.
Adding the Sparkplug B topic namespace
  1. Connect all the functional blocks in the Node-Red canvas to start transmitting temperature and humidity values as Sparkplug B payloads.
  2. To set up the Node-Red flow for reading the proximity sensor data and publishing it to the same HiveMQ Cloud MQTT broker, you can copy and paste the humidity and temperature flow here.
Node-red flow for reading temperature, humidity and intrusion data
  1. Use the Raspberry Pi specific nodes to read the GPIO pin where the proximity sensor signal is connected.
Using the Raspberry Pi specific nodes to read the GPIO pin where the proximity sensor signal is connected
  1. Use the output of the sensor to create a Sparkplug B object. Here, we are using a data type number 11, which corresponds to a boolean data type in Sparkplug. For the Sparkplug B encoder, we’re using the same settings as those for our DHT 11 sensor.
create a Sparkplug B object
  1. Use Node-Red’s_ debug_ nodes to see the information that we’re sending to the broker on our debug window. And then, change this to a complete object.
configuring our greenhouse control unit Raspberry Pi, MQTT Sparkplug B transmission of temperature, humidity and intrusion detection

And with that, we’re finished configuring our greenhouse control unit Raspberry Pi, MQTT Sparkplug B transmission of temperature, humidity and intrusion detection. Go ahead and deploy this.

Step 7: Testing the Transmission of Greenhouse Monitoring Data

Follow the below steps:

  1. Open the debug window and check the data that you are currently reading from the DHT sensor.
  2. So if you open up one object, you can see that the current humidity is at 83 and our temperature is at 20 degrees celsius.
Reading Greenhouse Humidity Data on Node-Red
  1. At this point, you do not have any information coming out of the proximity sensor because we haven’t detected any intrusion. So we go ahead and use your hand to create an intrusion scenario.
use your hand to create an intrusion scenario
  1. Upon intrusion detection, you get a boolean value of 1 as shown in the right hand side of the image below.

<img src=”/img/blog/real_world_mqtt_for_industry_40_part_2/real_world_mqtt_for_industry_40_31_flow_2_publish_data_to_hivemq_cloud.png” alt="intrusion detection on node red” title="intrusion detection on node red”” width="800” height="393”/>

Step 8: Setting-up Second Node-Red to Receive Greenhouse Monitoring Data

Before you set up your monitoring station to subscribe to the cleaners information, use mqtt.fx to subscribe to the Sparkplug B topic on your HiveMQ Cloud broker to check if you are successfully transmitting the greenhouse MQTT Sparkplug messages. Follow the below steps:

  1. Connect to HiveMQ Cloud and subscribe to the same topic. As soon as you do that, you can see the information coming through.
mqtt.fx to subscribe to the Sparkplug B topic on your HiveMQ Cloud broker
  1. Go back to the browser and log into the monitoring station, Raspberry Pi.
  2. Start the Node-Red to build the flow. Access the Node-Red dashboard of the monitoring station Raspberry Pi, which has an IP address of 192.168.0.169 Port 8080.
Access the Node-Red dashboard of the monitoring station Raspberry Pi
  1. Double click onto the MQTT subscribe node and use the same HiveMQ Cloud settings as those of the publisher.
Setting up HiveMQ Cloud to Receive the Monitoring Data
  1. Subscribe to the same topic namespace.
  2. Download the Sparkplug B to the proto file, and transfer it to the same directory as that of the previous Raspberry Pi.
  3. Go to the function block to filter through the messages.
Filtering Data on HiveMQ
  1. If the name of the metric is Temperature, send that to output one. If the name of the metric is Humidity, send it to output two. And if the name of the metric is ProximitySensor, send that to output three. If you refer to the image below, you can see that:
  • Output number 1 is connected to the gauge for displaying temperature.
  • Output number 2 is connected to the gauge for displaying humidity.
  • Output number 3 is connected to a Raspberry Pi specific node, which controls a GPIO pin where the lamp is connected.
Setting up second Node-Red flow to receive monitoring data

Note: To display this information on an HDMI dashboard, install the Node-Red dashboard package by going back to the Menu, selecting manage palette from the drop down menu, and installing the Node-Red dashboard package.

  1. Open the Display Temperature node and get access to the Edit gauge node. Select the gauge type, add Temperature as the Label, and assign a range from 0 to 100. Refer to the below image for a closer look at the details.
Read Temperature data on Node-Red
  1. Repeat Step 9 and use a similar setting for Humidity. Refer to the image below to check all the setting details.
Read Humidity Data on Node Red
  1. After you complete the settings, deploy this flow.
  2. Open a new tab to access the Node-Red user interface which is at Port 8080/UI.
  3. Get the current readings of the greenhouse temperature and humidity as shown below.
Current readings of the greenhouse temperature and humidity
  1. Test the setting by plugging the DHT 11 sensor and check if the needle moves as the temperature and humidity readings change.
  2. Simulate an intrusion as shown in the image below and see if the AC lamp goes On and Off to test the intrusion reading.
Intrusion detection on second Node-Red

Conclusion: Easily Monitor Greenhouse Remotely with MQTT Sparkplug B Messages

With this demonstration, you can see how MQTT Sparkplug B messages makes it easy to remotely monitor a greenhouse with the help of Node-Red on Raspberry Pi and HiveMQ Cloud. By using MQTT Sparkplug B specification, the greenhouse information is discoverable by any industry 4.0 application that may join your MQTT network.

If you want to watch this demonstration in action, watch this video Send and Receive MQTT Sparkplug Messages Using Raspberry Pis, Node-RED, and HiveMQ Cloud.

Stay tuned with us as we cover a series of blog posts around real-world MQTT for Industry 4.0.



Portrait of Kudzai Manditereza

About Kudzai Manditereza

Kudzai is Technology Communicator and Founder at Industry40.tv. He is currently involved in four efforts: Industry4.0 Research, Educational Videos, Podcast Host, and IIoT Systems Integration. He has a background in Embedded Systems Design, Software Engineering, and Industrial Automation.

newer posts How 5G and MQTT Accelerate Vehicle-2-Everything (V2X) Adoption
Launch HiveMQ on Azure with an ARM Quickstart Template older posts