Motivation for this project

Every time I add a new device (IOT, server or other) to my home network, I want to make sure that I can rely on it to do its job and keep being connected.
When a device has a critical part to play in my home or office, I want to be alerted when it fails or if it disconnects from the network/internet.

A critical part may be my home router, a computer connected to the internet and acts as a server (web, ftp) or IOT device like a gas sensor.

The project

The solution for me was a centralized system that each device sends updates to, and if a device fails to send an update (a heartbeat), I’ll  get notified by mail.

What I used for this project

  1. Web hosting/server -when we want to get notified when something in our network fails, we cant rely on anything inside our network, we need an external tool that will be able to monitor and act in the case of a malfunction. So the most important tool is an external server, for this project I’m using a PHP web hosting, which is rather cheap, and there are even some free out there. Preferably – the hosting your choose includes a database – MySql, otherwise, you’ll need to work with files, which is not recommended.
  2. A script/code for each of the devices that I want to keep track on, depending on the system (Windows, Arduino, DD-WRT). This part is in charge of sending the heartbeat and letting our server know that the device is alive and well.
  3. If you want to do or use parts of this article, you’ll need a basic understanding of code and how the Internet works. this article is not for beginners, but beginners can learn from it.

The server/web hosting part


First, I created a table in the database, which I’ll use to update heartbeats for each device and configure whether to send an email and where to if something fails.

The table should look something like this:

deviceID deviceName deviceDescription lastIpAddress lastSeen sendMailOnError sentMail emailonError notes
1 FtpServer Home Ftp Server 2017-12-02 16:15:40 True False
2 GasSensor1 Kitchen Gas Sensor 2017-12-02 16:15:48 True False
3 HomeRouter Main Router 2017-12-02 16:15:48 True False

Description of the table: First of all, we have the field deviceID, which is the key for the table. Then deviceName & deviceDescription let’s see clearly to which device this row belongs to.
lastIpAddress is for us to know which internal network address each device got, which is very useful when you don’t have a fixed address set up for devices.
lastSeen is used to indicate the last heartbeat the server got from each device, we’ll use this field later to determine whether this device has failed.
sendMailOnError is a boolean field in which we indicate if a mail is needed if the device didn’t send a heartbeat in the needed timeframe.
Next, we have the sentMail field, which is used to indicated that an alert has already been sent out, and we don’t need to send another.

The code for the MySql table:

PHP Script for heartbeats

After we have our table built and ready, we need to have a PHP script which can fill and update it.
This script and table are very generic, and of course, you can fit it to your requirements and needs. Let’s name this file heartbeat.php

The device’s part – sending the heartbeats

Basically, what we need is to get each device to send a heartbeat every X minutes (in my case I wanted 5).
On every type of device, it’s done a little differently.
Here I’ll show how to do it for Windows, DD-WRT (or cron capable) running router and Arduino (with network capability, like ESP8266).

Microsoft Windows – Most versions are supported

The idea is to write a batch file which opens the link and then run it in Task Scheduler. This script to reach our PHP code over the Internet, pass it some parameters (for example the internal IP address of the network) and sends the heartbeat. For this to work, you need to upload the PHP script to the hosted server and point this script to its address, for example http://yourserver/heartbeat.php

Save the script below as sendheartbeat.bat:

Please note that we’re requesting http://yourserver/heartbeat.php – you need to point this to where ever you host this file

add a scheduled task for it to execute every 5 minutes:

Windows Task Scheduler

Windows Task Scheduler

Arduino devices and home automation, like ESP8266

Same here, the idea is to create a function that runs every 5 minutes, that sends the heartbeat. We can even implement it in a non-blocking way.

The code below is a part of a sketch in the Arduino IDE

Here we have two sections, first the function that sends the heartbeat, and the loop function that is in charge of executing every 5 minutes, in the non-blocking way of course. In this code I also wanted the built-in led of the ESP8266 to blink, so I added to the beginning and the end of the heartbeat function the digitalWrite method.

DD-WRT (or a cron capable) Router

The router might just be the most important component of your network, if it fails (ISP, blackout etc.), nothing else can be controlled or monitored from outside.

DD-WRT routers usually have the option to run a script at a schedule, also known as Cron-jobs (we’ll talk about cron jobs in the next section as well), on my router the cron section can be found on Administration -> Management -> Cron on the router’s interface. Make sure it’s enabled. There enter this line of code:

Click on apply, and from then on, every 5 minutes, your router will send a heartbeat to your server.

Monitoring the health of the devices and send alerts when necessary

A PHP script to check that all devices have “checked-in”. The script below is checking that all devices have sent a heartbeat and sends a mail message about the ones that have not and are marked with send mail. It also checks that the mail hasn’t already been sent to prevent flooding. Save this script on the server and call it checkHeartBeat.php

The last and final step of this project is to run this script on the server every 5 minutes, that too we can do using the cron job functionality. In the cron job section of your hosting (I’m using a CPANEL hosting on Linux) do something like this:

cron job on CPANEL hosting


Well, here I covered the basics of tracking and monitoring devices on the network, which is mainly useful for devices that have some importance, like sensors, servers and so on. The idea is basically the same for all devices, send a heartbeat every X minutes, and if a device fails to do so, send a mail telling us about it.

I hope you enjoy this project!


Leave a comment