A nice feature of the Philips Hue lamps is called Geofencing. This just means that you can instruct the Hue bridge to automatically switch your lights on when you are approiaching your house. So for a time I wondered how they do that. And in fact it is very easy.
It is a technique that can be used for many different purposes. Not only to test if you are around but this also makes it possible to test wether your wireless apparatus are actually functioning.
It's called ping.
Ping is a network function that sends a command to an IP adress. If the device with that IP adress is connected to the router you will get a response. If the device is off (not connected) then you will get an error back.
It is even possible to ping web-adresses. That way you can see if a certain server on the web is on-line.
If you for example want to test wether this web-log is still on-line you could use something like the next command:
Ping Ping Anybody Home ?
So how is that going to tell me wether anybody is home or nearby. Well simple. Most people I know always carry their mobile phone around. So do I. And most of the time wifi is turned on. So if I give my phone, and for that my girlfriends phone etc, a fixed IP adress in my router I can Ping that IP adress et voila.
Give your home mates a fixed IP adress.
Start with opening the IP adress of your router in your browser. In my case my router has IP-adress 192.168.1.1 Your's may vary so look it up in the manual.
Enter the ip-adress in your browser and if you have a Zyxell router like I do you will be presented the above screen. Different routers give different results, but it should be easy to find the page that shows all connected devices.
As you can see I have given all my networked devices meaningfull names. So let's look at the details of my phone.
First you can see my phones IP-adress and as you can see I checked Static DHCP. This makes sure that everytime my phone connects to the router it will get the same IP adress.
Do this for all the phones and wireless apparatus in your home.
Ping is a wifi function. In some languages it is build in (yes in Basic) and in some languages it is not. What I wanted is to have my ESP Ping my devices and depending on the outcome switchs led's (relay's) on or off.
Standard C++ (Arduino) for the ESP has all kinds of Wifi functions build in but no Ping.
Fortunately Daniele Calanardi has build a library that we can use. You can download that library from the following link: https://github.com/dancol90/ESP8266Ping
Unpack the library and place it in your \Arduino\libraries folder and start the Arduino IDE.
The hardware is simple just an ESP8266 (I used a NodeMCU) and 4 leds connected through 220 Ohm delimiting resistors. The led's are attached to D0, D1, D2 and D4.
The Arduino source
Let's look closer to the program.
to activate the wifi functions of the ESP we need to include the Wifi library and for Ping we need to include the ESP8266Ping.h library.
const char* ssid = "XXXXXXXXX";
const char* password = "YYYYYYYYY";
Fill in your router's credentials so you will connect to your network automatically.
const IPAddress thermometer(192, 168, 1, 65);
const IPAddress domoticz(192, 168, 1, 66);
const IPAddress android(192, 168, 1, 70);
const IPAddress elsphone(192, 168, 1, 75);
Look in your router for the IP adresses of your Wifi Devices and replace the ones I have used.
The rest of the program is straighforward.
In the loop all devices are pinged. In this part the IP number of my phone is pinged. If it is present Ping will respond with a 1. If I am not at home Ping will respond with a 0 and the led will be set on or off accordingly.
This can be optimized :
However I wanted the code to be easy to follow for less experienced C++ programmers.
Naturally (I know some loathe this) I wrote the same program in ESP-Basic which is my favorite rapid software devellopment platform. So I hereby give you the simple code that does the same as the C++ program:
Let us look at some details.
These lines set all the leds in the initial state which is off.
timer 1000, [testping]
This line initiates a timer that every second (1000 miliseconds) jumps to the routine testping.
io(po,D2,ping("192.168.1.70")) 'luc's phone
This line actually tests whether my phone is detected by the router so if I am in. Depending on the
feedback it sets the led on or off. Replace the IP numbers with your own.
That's all folks.
Just for usability I modified the program so it has a webpage on which the information is displayed as wellas leds giving the information. So if you are around the ESP the leds will show what's happening and if you are not in its neighbourhood you can check the webpage.
Use your imagination.
You can send the information to cloud-based IOT servers like thingspeak (look for a tutorial here:
Replace the leds with relays so the lamps will go on if you approach your house.
Send the information to your home-automation system (like Domoticz which I am using) so
it can alter your thermostat settings or switch on your lights etc.
Till next time.
The next few weeks I am off on a long-earned holliday. There is however enough material on this web-log to get you going and start experimenting. So till July and remember:
Have fun !!