We’re giving away 1,500 DJI Tello drones. Enter to win ›
By Ori Pomerantz | Published June 19, 2018 - Updated June 21, 2018
In the first article in this series, “Integrating LPWAN networking into your IoT solutions,” you learned how to monitor hay barns for humidity and temperature to identify dangerous conditions. You learned how to get sensor readings for temperature and humidity from NodeMCU devices to the IBM Watson IoT Platform. In this article, you learn how to preserve those readings in a database, how to display them in an IoT dashboard, and how to generate alerts.
Read more about the issues of preventing hay bale fires in this article ” Preventing fires in baled hay and straw.”
Because you are adding on to the IoT system that you built in the first tutorial in this series, you need to have completed the first tutorial before working on this one. To do that, you need:
Figure 1 shows the addition of the IoT cloud analytics system, including an IoT dashboard, to our overall IoT monitoring system (which also includes the IoT edge analytics system).
We will build our IoT dashboard application using Node-RED, and it will display the data that is sent to Watson IoT Platform and stored in a Cloudant database.
The first article in the series just uses the Watson IoT Platform because for the purposes of that article, there was no need for either Node-RED or a database. Here, it is just easier to recreate the Watson IoT Platform instance, so it will be attached to the Node-RED as a service.
The easiest way to have a configured Node-RED, Cloudant, and IBM Watson IoT Platform is to create them together from a boilerplate.
The next step is to configure the new IBM Watson IoT Platform.
<your application name>-iotf-service
Now, we verify that the Watson IoT Platform is receiving events by using an MQTT client to send messages that mimic our sensor readings.
Enter these parameters using the values from the credentials you set up when registering with Watson IoT Platform:
Note that this MQTT library has a length limit on the client ID field, which is why we can’t call our device test: it is one character too long.
Go to the IBM Watson IoT Platform dashboard on the Recent Events tab to view the messages.
The next step is to receive the events in a Node-RED flow. This flow then formats the event messages and stores the information in a Cloudant database.
Follow these instructions to create the flow on your own. You can see my instance and use it as a template, but remember that it is the entire flow and not just the part we focus on at the moment. However, if you want to copy the flow and customize it as you follow along, copy the file JSON file from here to the clipboard and then in the Node-RED editor, click Import > Clipboard from the hamburger menu and paste the flow.
https://<your application name>.mybluemix.net/red
Create a new flow:
a. From the hamburger menu in the top right corner, select Flow > Add.
b. Double-click the Flow 2 tab and rename it to IoT Flows. Change the status to Enabled. Click Done.
c. Double-click the Flow 1 tab, the default flow, and change the status to Disabled (or delete it). Click Done.
d. Click the IoT Flows tab.
From the palette’s input section, drag the ibmiot into the main editor area.
Double-click the IBM IoT node to configure it:
a. Change the authentication to Bluemix Service to receive events from the IoT Platform that is already connected to our Node-RED application. The authentication is handled for you automatically.
b. Click All for the device ID and for the format to see all the events.
c. Click Done.
From the palette’s output section, drag debug into the main editor area.
In the database, we want to have documents with the temperature and humidity as separate values, not part of a JSON string. Also, we’d want a timestamp. So, we need to reformat the messages we receive.
msg.payload.time = new Date().getTime();
msg.payload.deviceId = msg.deviceId;
Now that we have the message payload, we can write it to the database.
When either the temperature or the humidity exceeds a threshold, we want to issue an alert.
Looking at the article about hay bale fires, these are the alert conditions (assuming your system does not use the edge analytics in the second article in the series to deal with problems automatically):
If your system does use edge analytics, the first two alert conditions do not require an alert. The edge analytics controls a heater and a fan to solve them automatically. You only need alerts when the temperature is above 175 b0F, because you need to call fire services.
The DHT11 that produces our temperature readings is Chinese. It uses Celsius. However, the article about hay bale fires presents its data in Fahrenheit. So, the first step is to add a node that calculates the Fahrenheit temperature.
msg.payload.tempF = msg.payload.temp*9/5+32;
Next, we calculate the status string. This is important, because we only want to generate alerts when the status changes.
Double-click the switch node to configure it.
a. Set the property to msg.payload.tempF.
b. Set the top rule’s operator to >=, the data type to number, and the value 190.
c. Click Add to create two more rules, both with the operator >= and the data type number. The first has the value 175, and the second 150.
d. Click Add to create a fourth rule with the operator otherwise.
e. Change the bottom value to stopping after first match.
From the function section, drag the template node to create a node with these parameters:
Finally, we can create alerts.
If you use edge analytics, there is no need for the Start air movement and Too humid nodes. Instead, you can just link to the input (left) side of the Fine node.
Of course, you can also not put the 150 value in tempAlerts and remove the humidityAlert node completely, going directly from tempAlerts to Fine — it is functionally the same. However, by keeping those values, we make it possible for the dashboard we create in the next step to show us when the fan or the heater should be on.
Send these messages using the HiveMQ client to verify that you get the correct alerts in the debug pane:
If you are using edge analytics, you can use the same messages. However, you will not get alerts telling you to reduce humidity or promote air circulation. You can skip messages 2 through 5 because they do not change the status.
Users of the Hay Barn Monitoring app want to have a dashboard to visualize and analyze the sensor data that the application collects and stores. In this step, we create that dashboard.
To create the initial dashboard, it is necessary to first import a Node-RED dashboard library.
Note: On a production system, you would have more meaningful names for tabs and groups.
It could be useful to know what the current temperature and humidity values are. To do so:
Now, we add to our dashboard a chart of recent temperatures.
The URL for my dashboard is http://haybale-ori.mybluemix.net/ui. The one for yours would be https://<your application name>.mybluemix.net/ui.
https://<your application name>.mybluemix.net/ui
If your system uses the edge analytics from the second article in the series to deal with problems automatically, it is useful to have an indication of whether the fan or the heater should be on. Here is how you create those indicators:
In this example, the alerts go to the dashboard. That’s nice for testing purposes, but obviously it is not good enough in production. In a production application you would probably also use the IBM Push Notifications service for your alerts. With this service, you can send alerts by SMS. It also includes an API for writing alert applications for devices that do not have a cellular SMS number.
Another issue is dealing with multiple devices. The recent history chart, shown in Figure 15, automatically adjusts and shows multiple lines.
Unfortunately, the gauges and alerts just rely on the latest information received. For the gauges, you can solve this by using a switch node and creating gauges for each, as shown in Figure 16.
We could use a similar approach with the alerts, but then we’d have to replicate a fairly complicated workflow. A better solution would be to replace the Report by Exception node with a node that keeps a list of devices and their latest statuses and identifies whenever the status of a specific device changes.
With this tutorial, we added an IoT cloud analytics system, including an IoT dashboard that generates alerts to our IoT monitoring system. We stored the IoT sensor data in a Cloudant database, and we used Node-RED to generate alerts and create a dashboard for the user (a farmer) of our IoT solution. Throughout the series so far, we’ve explored integrating LPWAN networking and edge computing as well as pushing analytics out to the edge devices, and using the IoT data to generate alerts for the user of the IoT solution.
Back to top