DEV Community

Cover image for Monitoring my internet connection with Micropython and ESP8266
Jorge Alberto Díaz Orozco (Akiel)
Jorge Alberto Díaz Orozco (Akiel)

Posted on

Monitoring my internet connection with Micropython and ESP8266

A small introduction

I love playing with IoT devices. Coming from Cuba that's not a corresponded love. It is really difficult to get your hands on them as they are usually not sold on any market. Some projects like The Cuban Tech Group have been doing an amazing job bringing tech and knowledge for all the interested parties for free but there is still a huge lack of development in that matter in the island. Thanks to some friends I managed to acquire some ESP8266 modules and I have played with them a lot. Micropython is an awesome tool to work with them and since I already knew Python and did not want to dig into learning how to code with all of the Arduino stack I limited myself to learn how to do things with micopython on these fantastic boards.

And the story actually starts

Some months ago I visited my parents in the countryside Cuba. I was very happy because they have managed to do a contract with the telecommunications company to have an ADSL connection at home. ADSL is now coming to Cuba and the service is pretty new. Users not only have to pay for the connection on a monthly bases but they also need to login on a captive portal that works per time (yes, this telecommunications company is evil and greedy and it's the only telecommunications company in the country). Since you need to pay per time and every hour of internet costs 0.5 USD, my parents can't afford to have it connected all the time, so they only use internet during some small periods of time during the day. And then there was me wanting to be notified when I could use the wifi to save my data internet which is also very expensive in Cuba.

I wanted to have some sort of indicator that will tell me when the ADSL router was actually connected to internet so I could use it and it needed to work even with me connected to another access point as I would probably be using internet from my phone almost all the time, and so, the idea came to my mind: Let's make a notification light with one ESP8266 board!

How should I check?

Well, there are lots of recipes to check if you are connected to internet, but here goes my preferred one: I once found this Google URL http://clients3.google.com/generate_204 that serves a simple purpose. Whenever you access it, the HTTP response code will be 204. So I'll just periodically check this URL expecting a 204 status code. If I get a 200 (OK) it means the device is connected but I'm hitting the captive portal and if I have any other response code or any connection error, well it means the problem is somewhere else. Probably the device is disconnected or there are some communications problems.

Here comes Micropython

First of all, I needed to flash micropython into my ESP8266 device (in my case a WemOS D1 mini) and you can find all the instructions on how to do it and how to start using the device here. Be patient. It's easy once you understand the process ;-)

Let's code

In the end, I ended up with this small script that will manage the on-board led according to the internet status:

import gc
import time
import network
import urequests
from machine import Pin

pin = machine.Pin(2, machine.Pin.OUT)
pin.on()

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

while True:
    try:
        wlan.connect("user", "password")
        response = urequests.get("http://clients3.google.com/generate_204")
        print(response.status_code)
        if response.status_code == 204:
            print("online")
            pin.off()
        elif response.status_code == 200:
            print("portal")
            pin.off()
            time.sleep(1)
            pin.on()
        else:
            print("offline")
            pin.on()
    except:
        print("error")
        pin.on()
    gc.collect()

    time.sleep(10)
Enter fullscreen mode Exit fullscreen mode

It will check every 10 seconds for a connection and let us know. I actually created a Github repository for it https://github.com/jadolg/IoTInternetMonitior so please feel free to comment, improve and use :)

Expected behavior:
When the router is connected to internet the status led will be ON. When the captive portal is detected the status led will blink and when there's an error in the connection and there's no internet access the led will be completely OFF.

Some things you probably noticed:
Yes, there's a lot of printing in there. Feel free to remove it as it's not really needed if you are not "old-style debugging".
Yes, for some reason the pin works completely in reverse so pin.on() is actually turning the led off.

Conclusions

I had a lot of fun building the indicator and in the end, even my parents ended up using it to figure out if they had correctly logged out of the internet service. I hope that someday we will not need this kind of device for anything in Cuba and that being connected all the time to internet will be cheap and easy. Since it is sort of a hacky story about solving a problem with tech I decided to share it with you. I hope you enjoyed reading.


Image credits to https://www.etsy.com/ . https://www.etsy.com/il-en/listing/537115947/offline

Top comments (2)

Collapse
 
cashoefman profile image
Cas Hoefman

I think you need to add import machinefor this to work.

Collapse
 
jadolg profile image
Jorge Alberto Díaz Orozco (Akiel)

I'm only using Pin from machine, so I did from machine import Pin