How to Send & Receive MQTT Sparkplug B Messages Using Raspberry Pis, Node-RED, and HiveMQ Cloud
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.
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 specification, which is also supported by HiveMQ Cloud MQTT broker.
The hardware setup is as shown below:
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:
- Visit HiveMQ MQTT Cloud broker page and sign up for the free version, or directly sign up for free here.
- Once you sign up, either use your email ID or your GitHub login to log in to your account as shown below.
- 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.
- 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.
- Click on Manage Cluster as shown in the below.
- You will have access to your Cluster Details as shown below.
- 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.
- 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.
- 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.
- Type the IP address of the Raspberry Pi in the_ PuTTY Configuration_ and open the connection as shown below.
- 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.
- 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.
- 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.
- 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.
- 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.
- Double-click on the timestamp and set it to repeat at intervals of five seconds.
- 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.
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:
- Open Edit function node.
- 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:
- Create two function blocks called Prepare Sparkplug B Message.
- 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.
- Follow the same steps for humidity data.
- Connect the function blocks.
- Drag two encoder nodes named sparkplug_b encoder onto the canvas. This will encode the messages before transmission.
- 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.
- Connect the output of the _Prepare Sparkplug B Message _functions to the input of the respective sparkplug_b encoder function.
- 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:
- Go ahead and download the file and make sure to save it with a dot proto extension.
- 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.
- Double click on the Sparkplug in the called node and notice that we have put the file path to the sparkplug_b.proto file.
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:
- 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.
- 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.
- Add the access credentials created on my HiveMQ cloud portal.
- 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.
- Connect all the functional blocks in the Node-Red canvas to start transmitting temperature and humidity values as Sparkplug B payloads.
- 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.
- Use the Raspberry Pi specific nodes to read the GPIO pin where the proximity sensor signal is connected.
- 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 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.
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:
- Open the debug window and check the data that you are currently reading from the DHT sensor.
- 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.
- 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.
- 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:
- Connect to HiveMQ Cloud and subscribe to the same topic. As soon as you do that, you can see the information coming through.
- Go back to the browser and log into the monitoring station, Raspberry Pi.
- 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.
- Double click onto the MQTT subscribe node and use the same HiveMQ Cloud settings as those of the publisher.
- Subscribe to the same topic namespace.
- Download the Sparkplug B to the proto file, and transfer it to the same directory as that of the previous Raspberry Pi.
- Go to the function block to filter through the messages.
- 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.
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.
- 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.
- Repeat Step 9 and use a similar setting for Humidity. Refer to the image below to check all the setting details.
- After you complete the settings, deploy this flow.
- Open a new tab to access the Node-Red user interface which is at Port 8080/UI.
- Get the current readings of the greenhouse temperature and humidity as shown below.
- Test the setting by plugging the DHT 11 sensor and check if the needle moves as the temperature and humidity readings change.
- Simulate an intrusion as shown in the image below and see if the AC lamp goes On and Off to test the intrusion reading.
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.
About Kudzai Manditereza
Kudzai is an experienced Technology Communicator and Electronic Engineer based in Germany. As a Developer Advocate at HiveMQ, his goals include creating compelling content to help developers and architects adopt MQTT and HiveMQ for their IIoT projects. In addition to his primary job functions, Kudzai runs a popular YouTube channel and Podcast where he teaches and talks about IIoT and Smart Manufacturing technologies. He has since been recognized as one of the Top 100 global influential personas talking about Industry 4.0 online.
Follow Kudzai on LinkedInContact Kudzai