Skip to content

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

by Kudzai Manditereza
24 min read

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 unauthorized 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.

Raspberry Pi devices as MQTT clients that are sending and receiving MQTT messagesThe 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 specification, which is also supported by HiveMQ Cloud MQTT broker.

The hardware setup is as shown below:

Raspberry Pi MQTT Hardware SetupOn 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, or directly sign up for free here.

  2. Once you sign up, either use your email ID or your GitHub login to log in to your account as shown below.Use your email ID or your GitHub login to log in to your account

  3. 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.Set up the credentials that your Raspberry Pi Edge of Network Nodes will use to connect to the HiveMQ Cloud MQTT

  4. 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.

  5. Click on Manage Cluster as shown in the below.HiveMQ Your Clusters Page

  6. You will have access to your Cluster Details as shown below.HiveMQ Cloud Cluster Details Page

  7. 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.

  8. 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.

  9. 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.IP address of the Raspberry Pi in the_ PuTTY Configuration_

  2. 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-start” command on the terminal to start Node-Red

  3. 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.IP address of the Raspberry Pi followed by 1880, which is the port number used by Node-Red

  4. 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.install the DHT11 Node-Red package using the Deploy menu

  5. 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.Search for the DHT sensor package

  6. 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.Create a Node-Red flow for reading, converting, and publishing MQTT sparkplug messages

  7. Double-click on the timestamp and set it to repeat at intervals of five seconds.Double-click on the timestamp and set it to repeat at intervals of five seconds

  8. 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.DHT 11 sensor settings

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

  2. 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.Create two function blocks called Prepare Sparkplug B Message

  2. 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.paste the code for creating an MQTT Sparkplug object along with corresponding Properties values

  3. Follow the same steps for humidity data.

  4. Connect the function blocks.

  5. Drag two encoder nodes named sparkplug_b encoder onto the canvas. This will encode the messages before transmission.

  6. 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.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

  7. Connect the output of the Prepare Sparkplug B Message functions to the input of the respective sparkplug_b encoder function.Connect the output of the Prepare Sparkplug B Message functions

  8. 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.Copy the file to a directory of your choice on the Raspberry Pi device

  3. Double click on the Sparkplug in the called node and notice that we have put the file path to the sparkplug_b.proto file.Double click on the Sparkplug in the called node

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.Add the access credentials created on my HiveMQ cloud portal

  4. 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.Add the Sparkplug B topic namespace

  5. Connect all the functional blocks in the Node-Red canvas to start transmitting temperature and humidity values as Sparkplug B payloads.

  6. 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.Humidity and temperature flow

  7. Use the Raspberry Pi specific nodes to read the GPIO pin where the proximity sensor signal is connected.Raspberry Pi specific nodes to read the GPIO pin

  8. 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.Use the output of the sensor to create a Sparkplug B object

  9. 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.Use Node-Red’s_ debug_ nodes to see the information

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.current humidity is at 83 and our temperature is at 20 degrees celsius

  3. 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.Using the hand to create an intrusion scenario

  4. Upon intrusion detection, you get a boolean value of 1.

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.Connect to HiveMQ Cloud and subscribe to the same topic

  2. Go back to the browser and log into the monitoring station, Raspberry Pi.

  3. 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.Start the Node-Red to build the flow

  4. Double click onto the MQTT subscribe node and use the same HiveMQ Cloud settings as those of the publisher.Double click onto the MQTT subscribe node and use the same HiveMQ Cloud settings

  5. Subscribe to the same topic namespace.

  6. Download the Sparkplug B to the proto file, and transfer it to the same directory as that of the previous Raspberry Pi.

  7. Go to the function block to filter through the messages.Go to the function block to filter through the messages

  8. 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.Output number 1, 2 and 3Note: 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.

  9. 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.Open the Display Temperature node and get access to the Edit gauge node

  10. Repeat Step 9 and use a similar setting for Humidity. Refer to the image below to check all the setting details.Repeat Step 9 and use a similar setting for Humidity

  11. After you complete the settings, deploy this flow.

  12. Open a new tab to access the Node-Red user interface which is at Port 8080/UI.

  13. Get the current readings of the greenhouse temperature and humidity as shown below.Get the current readings of the greenhouse temperature and humidity

  14. Test the setting by plugging the DHT 11 sensor and check if the needle moves as the temperature and humidity readings change.

  15. Simulate an intrusion as shown in the image below and see if the AC lamp goes On and Off to test the intrusion reading.Simulate an intrusion

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.

Watch this demonstration in action

Ready to elevate your MQTT experience? Try HiveMQ Cloud Starter for 15 days absolutely free and get dedicated resources, 99.95% uptime, and round-the-clock support.

Get Started FREE

Kudzai Manditereza

Kudzai is a tech influencer and electronic engineer based in Germany. As a Sr. Industry Solutions Advocate at HiveMQ, he helps developers and architects adopt MQTT and HiveMQ for their IIoT projects. Kudzai runs a popular YouTube channel focused on IIoT and Smart Manufacturing technologies and he has been recognized as one of the Top 100 global influencers talking about Industry 4.0 online.

  • Kudzai Manditereza on LinkedIn
  • Contact Kudzai Manditereza via e-mail
HiveMQ logo
Review HiveMQ on G2