Out of curiosity I ordered a tiny ESP8266 relay switch board from aliexpress.com, for next to nothing. Blissfully unaware, then, of the struggle ahead to make the thing work as advertised. I survived and conquered — here’s the story.
By Somnath Bera
The little gizmo pictured in Figure1 was stated by the vendor(s) to contain an inbuilt switching program, so all I had to do is “use some Android phone to control it”. Sadly, despite several attempts using various Chinese and English-language Android apps, nothing ever happened! Sure, the device gets connected with the Wi-Fi of my PC or laptop as a server and it also allocates the dynamic IP address — but nothing happens after that. No server page etc. opens anywhere! Utterly frustrated, one day I decided to develop and install my own program into its dumb head!
Starting out
I reprogrammed the switch to connect to my Wi-Fi router with a fixed IP address and a fixed port like these: 192.168.1.99 and port 8083. Once connected the board behaved as a webserver at http://192.168.1.99:8083 and the ‘switch’ was visible in the browser. Pressing the On or Off button made it act accordingly. For simplicity, I added a refresh counter and a relay status to have a readout for the state of the relay (that being, on or off).
Strategy
Next, in my DSL router I set up ‘port forwarding’ of the 8083 port to the 8083 port of the “192.168.1.99” computer (i.e. the relay board) which gets connected to it. Once you know the external IP address of your router, you can connect to it from the other side of the universe! So easy! But the question is, how to know the external IP address of your router? Simple, you ask your son, daughter or your spouse to open the router’s admin page, go to the Status tab and locate the IP address of the router — it’s a common feature of most DSL router modems out there. When your laptop, desktop, or mobile phone gets connected to your Wi-Fi network, it gets an IP address as the identity of that device on the Wi-Fi network. Likewise, your DSL router gets an IP address when it gets connected to Internet, and that’s the address you have to know.
But there is no dearth of problems in Bera’s world of electronics! The external IP address of any router changes quite often — up to 10 times per is not uncommon. So really, you’d need a permanent valet near the modem to shout numbers 10 times a day … you’d wish you could train your pet to do this work there and then!
But where there is a problem there is solution! At least in the world of electronics this is so true. When you can formulate and then simplify a problem, you are actually pushing towards the solution. Fortunately, most run of the mill DSL modem haves an inbuilt DDNS (Dynamic DNS) tracking facility. You just supply the DNS host server’s name, the DNS name and the remaining authorization details, and the Modem will track the changes of the external address on its own. You just need to remember the DNS name you have registered with the DNS host server’s site and the port you are forwarding in the modem — in my case it’s berapi.ddns.net:8083 from noip.com. See Figures 2a and 2b for my setups.
Read more: FREE ELEKTOR ARTICLE: A 3-DOLLAR TINY WI-FI SWITCH THAT WORKS