Crazy Arduino Yun solution to a router problem

My father-in-law helps out with computer-related stuff at a coffee shop. They’ve been having WiFi troubles; the WiFi signal is strong, but they lose the internet connection. The solution has been to turn the router off and then back on; if they do that, things then work fine. He even installed a wall switch for this purpose, to make it easier.

He asked me whether I could build a device that would do this automatically. It sounded like an interesting project, and there’d be some useful things to learn in the process. Probably they should just buy a new router, but what follows here is a description of my crazy auto-router-resetter: my goal was to build an Arduino-based device that would check for an internet connection, and if the internet was down, use a PowerSwitch Tail (from Sparkfun) to shut off the router, wait 30 seconds, and turn it back on again.

Here’s a picture of the final product:

Arduino Yun project

Arduino Wifi Shield

The project seemed perfect for the new Arduino Yún, but I didn’t have one, so I started out using an Arduino Leonardo and an Arduino WiFi Shield, which I had in a drawer.

This wasn’t too difficult. The code is here. I attached red, green, and yellow LEDs to digital pins 3, 5, and 6, respectively (each with a 220 Ohm resistor).

  • Yellow LED on while “pinging” google
  • Green LED on when getting a good response from google
  • Red LED on when getting a bad response from google

Then, of course, there’s the PowerSwitch Tail, attached to pin 8, and a blue LED attached to pin 9 (with a 470 Ohm resistor). If the internet is down, we shut off both pins 8 and 9, wait 30 second, turn them on again, and then wait another 30 seconds. (Later I increased the second wait time to 60 seconds; we want to be sure the router is back up before we starting testing for an internet connection again.)

The bit of code that attempts to connect to google was based on the WiFi Web Client example.

To add a further bit of fanciness: I logged events to the SD card on the WiFi shield. I wanted to log times, too. That got a bit complicated. But I followed the Network Time Protocol (NTP) Client example to set the Arduino clock, and then the SD Datalogger example to write to the SD card.

The whole sketch came in with just 8 bytes to spare on the Arduino, though, and my father-in-law wanted the device to play a tone when the internet was down.

Arduino Wifi Shield with a speaker

I didn’t remember exactly how to play a tone from the Arduino, so I figured I should give that a try, at least to refresh my memory. I had no space on the Arduino for this, so I needed to scrap the time clock aspect. (I figured I could put all of it together again later, if I could find a way to make it the code more lean.)

Here’s the code that uses the speaker to play a repeated tone while the internet is down. I just use the tone function. The speaker was attached to digital pin 2 without any resistor.

The speaker I was using was a tiny little one, less than 1″ diameter, and so it wasn’t loud enough to be useful in a coffee shop. And I was against the tone-based alert, anyway. So when my Arduino Yún arrived from SparkFun, I scrapped that feature.

Arduino Yún

The Arduino Yún is brilliant.

I’ve played with the Arduino Ethernet Shield, and then the Arduino WiFi Shield above, but you don’t really have enough memory on an Arduino to deal with web-based stuff, it seems to me. The WiFi aspects would be easier with a Raspberry Pi or BeagleBone Black, but for this sort of device, the time to load Linux is annoyingly long. Arduinos seem more nimble.

So I was really excited to get the Yún. It has a regular Arduino chip plus also a linux computer (running a very lean Linux OS, Linino). And then there’s built-in WiFi and ethernet.

The Linux side handles the internet access and has Python 2.7 installed. (Why not Python 3?!) So that means you can move all of the heavy lifting to the Linux side, as Python scripts. The Arduino side just handles the LEDs and the PowerSwitch Tail and then calls Python scripts on the Linux side to check for internet access and to write to a log file on the SD card. And you can get the time from the clock on the Linux side. (It sets its clock from the web at startup.)

You can install the mg editor, which is a light-weight emacs-like editor, to edit directly on the Yún, if you want.

And the Yún has a built-in web server, so you can read the log files on the SD card with a web browser.

You can even send your Arduino sketches (or ssh to the Yún) by WiFi. A direct connection is easier for debugging purposes, though (with the Serial console).

Also, the Yún fit into the enclosure I’d purchased from SparkFun. The Leonardo + WiFi Shield were a tiny bit too tall for that enclosure. (I like the clear case, so you can peer in at my sloppy soldering work.)

Finally, the Arduino side of the Yún is running almostly instantaneously, and the Linux side just takes 58 seconds or so for startup.

Arduino code

The Arduino sketch is pretty simple, and is based on the one I wrote for the Leonardo + WiFi Shield.

To check for an internet connection, it uses runShellCommand to call a Python script that is sitting on the Linux side, and then reads the result (1 = up, 0 = down).

To log an event to the SD card, it uses Process.begin, Process.addparameter, and Process.run to call another Python script on the Linux side.

Python code

There are three Python scripts sitting on the Linux side.

pingGoogle.py uses urllib2 to check for a connection to Google. I got this solution from StackOverflow.

logEvent.py logs events to the SD card; it uses the usual sort of datetime and os stuff.

That second script also calls (when necessary) a third Python script, updateWeb.py. I thought I’d keep month-specific log files, with an index.html file that contains links to the different log files.

I forgot about the switch

I forgot to mention the slide switch I’d installed. If you slide this up, it overrides the turning-off of the router. I was thinking that, if the device was causing problems, it’d be good to disable the whole “reset the router” aspect and have the device simply be an indicator of internet access or not.

Things to work on

I think it’s good to go, but there are a few remaining issues or enhancements that I should work on.

  • Sometimes the time on the Linux side hasn’t been set when the first event is logged. I should force the time update before proceeding. (Update: I figured this out.)
  • The delay following re-starting the router is really important. If I don’t give enough time for the router to be functioning fully by the time the Arduino sketch starts pinging google again, it could enter an endless loop of repeatedly turning the router off and on again.
  • I only turn the router off if I get two bad responses in my effort to connect to Google. I might increase this, and I might try some other sites, too.
  • It would be nice to have a somewhat cleaner web interface to the log files. Right now, it just makes a page with links to plain-text log files. It’d also be cool to include some D3-based plots.

One more picture

I love the Arduino Yún. I can write a simple Arduino sketch to handle inputs and outputs, and I can write Python code in Linux to handle web and file IO.

Here’s one more picture, with some speaker wire attached to the port that goes to the PowerSwitch Tail.

Arduino Yun project, 2

Tags: , , ,

3 Responses to “Crazy Arduino Yun solution to a router problem”

  1. Donncha Says:

    That is a great idea for a project! Yes, it would have been easier to buy a new router but where’s the fun in that?

    This reminds me of a cron job I had running on a web server over a decade ago. Apache would stop responding after a while for some reason and the only way to fix it would be a restart. I guess I used telnet to port 80 probably and if Apache didn’t respond it restarted the service! Brutal, but it worked.

  2. Yavuz Says:

    Great solution, i would definitely like to build this. will the code work if i work with Yun or do i need to modify it? Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: