In this blog I’m going to explain how to monitor your IoT or other devices without having to open up ports, install agents or require access to your network. We will rely on Node-RED, Azure LogicApps and Azure Monitor Alerts.
Opening up ports you say?
If you like to tinker, like me, and have a Raspberri Pi somewhere in your network, you probably want to now if it is running properly, and what the temperature is. You could open up a port on your modem/router and check from the outside (with something like uptimerobot) if the device is up and running.
There are some problems with this approach. First of all, you will need a fixed IP, or some crappy DNS service that will fail once in a while (rendering your monitoring useless). The second problem is, a simple ping is not really helpful so you want to setup a probe of some sort, or allow SSH access.
Opening up ports also open up the possibility of people other than you logging onto your network with all kinds of mayhem as a result.
Example: Temperature monitoring on a Raspberry Pi
So how can we solve the problem? Let’s take a simple example. My Raspberry Pi is located near my fuse box. Yes, I’ve fitted it with heatsinks but still temperature is important to monitor, as a higher temperature will decrease the lifespan. How to monitor this without opening up ports?
In come Node-RED and Azure LogicApps!
Prepare your Pi
For sake of shortening this article, I’m not diving into the full setup. But if you run a factory default Raspberry Pi, Node-RED is by default installed. Otherwise find the installation instructions over here.
Setup a LogicApp
We will start with a really simple LogicApp, that receives a POST request from Node-RED, and parse the information. It will then determine if it needs to notify you, or not. We also will save the temperature into a table storage (bonus) to see the history over time.
Explanation or the LogicApp:
- The first step is a HTTP trigger. We want to receive a POST from the Node-RED flow running on our Raspberri Py
- As we are going to receive a temperature, we need to initialize a float variable to hold that value. While initializing we also decode the incoming message. Node-RED will send a base64 encoded string, so a simple function in LogicApps helps us to create a float: ‘@float(base64ToString(triggerBody()?[‘$content’]))‘
- After this, we want to save the result in a storage table. Just so we can see the history of the values, and perhaps plot some graph in the future, food for another blog.
- Finally we want to check if the temperature is reaching a treshold. In this example the treshold is 70 degrees Celsius. So if the temperature goes above that value, I’ve setup an email to myself in the ‘True’ block of the LogicApp.
To download the LogicApp code, see the gist over here.
Create your Node-RED flow
Now that we’ve setup the receiving end of our solution, we need to setup the Raspberry Pi flow. The following Node-RED flow will get the temperature, and call the Azure LogicApp every 15 minutes!
Explanation of the above flow:
- The timestamp node runs every 15 minutes, simply for trigger usage
- The second node gets the actual temperature from the Raspberry Pi OS
- As this temperature has text in it, we need to extract the value only without any text
- We call the LogicApp and it will post the value
This example is really simple, and you can download the code of the gist here.
And we are done! You now have a fully working example of checking your temperature, and getting notified when the temperature goes beyond your set treshold.
No, not Don Johnsons song; You know how a doctor checks if you are alive right? Listening to your heartbeat is one of those ways.
The problem with the temperature monitoring is that when my Pi dies or hangs, I’m unaware of any issues. A heartbeat check will fix this problem.
If you setup the temperature monitoring, we basically already have a periodical call from the Pi towards Azure. And boom, that is basically a heartbeat!
In come Azure Monitor Alerts!
Alerts in Azure can trigger on fails, but also on successes. Simply put, if we do not receive a call, the device is offline. In my example scenario I’m calling the LogicApp every 15 minutes. So, if I have less than 3 calls in an hour, something is wrong!
Setting up Azure Alerts
Creating alerts is pretty simple. Just go to the alerting section of your LogicApp, in the portal:
Just click on ‘Alerts’ and create the rule!
To simplify things, I copy/pasted/cropped the alert screen below:
As you can see above, we trigger based on number of Runs Completed. Like previously mentioned, we run the trigger every 15 minutes. So, in the above case I trigger the alert when I have less than 2 completed LogicApp runs in one hour. You can also go up to 3 completed runs, but I don’t care if my Pi is down for a short while.
And there you have it; a working example of a heartbeat!
In this blog I’ve explained how you can setup monitoring for your device (in this case a Raspberry Pi temperature) and how you can use those calls as a simple heartbeat.
Basically I now have a warning system for my temperature and as a bonus, when my device goes down, I get a warning and everything without needing remote access!