There was a time before WiFi (1997) and before broadband internet (early 2000's) where a user wishing to access the internet would have to sit down at their computer and manually connect to an internet access provider, which usually included sitting through the screeching and squawking of a modem. Time progressed and WiFi was developed, broadband internet access became available, paving the way for the Internet of Things (IoT), smart home technologies, WiFi speaker systems, and video streaming devices.
Naturally, I have all of the above devices. While I think we are only beginning to see what "connected devices" can do, the ones I have still great fun to use. The problem, and the reason that this became a project, is that my internet connection is not always reliable, requiring me to occasionally reboot my router and modem. Not such a big deal, except that without internet access it's much harder to turn on some of the lights in my condo.
This project really started when I found a controllable four outlet power relay module on Adafruit's website. It seemed like a reasonable way to control outlets from a microcontroller or microcomputer, providing one outlet with constant power, one that is normally open and two outlets that are normally closed. Since I want the project to control both my router and my modem, I will be using the two normally closed outlets unless I rewire the module sometime in the future. While I originally intended to attach the module's control input to a Raspberry Pi 3, I have been so impressed with the Raspberry Pi Zero W's capabilities that I switched to the smaller board. Equipped with an Adafruit Pi OLED screen I can display connection status messages directly on the screen. Although the Pi Zero usually comes without the GPIO headers (rows of pins at the top), the one I purchased from Adafruit already had the pins soldered on. Since the header is made up of 40 pins, this is definitely helpful if you know you have a project that is going to use the pins.
For now, the Raspberry Pi is mounted on a small stand I printed, with the Pi OLED connected to the first 6 pins on the header. Power is provided by a spare iPhone USB charging brick plugged into the "always on" outlet on the relay module. This ensures that the project always has power, no matter the status of the relay. By connecting one of the I/O pins to the positive terminal in the relay and then connecting the negative terminal to a ground pin I am able to provide the reference single for the relay module.
The code, like much of the project, is fairly straight-forward once the pieces are found. When the program starts it initializes the relay's reference signal to high. Receiving a high signal "opens" (or turns on) the module's normally closed (off) outlets, powering the modem and router. After waiting a few minutes to make sure the modem and router have a chance to boot up, the code enters a loop. Since everything has already been initialized, this loop will run as long as the program is running. First, it tries to open a connection to the internet. If it is successful, an image is shown on screen, providing the connection status, how many times the program has attempted to restart the router and modem, the date and time of last connection check as well as the Raspberry Pi's IP address and the WiFi network name. If the connection to the internet fails, the screen displays "No connection" and a status bar. Attempts to reconnect are made for 60 seconds. If the status bar fills before an internet connection is established a power cycle is initiated. The reference signal to the relay module is set to low, closing (turning off) the relay. After waiting 15 seconds, power is restored and the modem and router begin their power-up sequence. Since I don't have a reliable way to know when they have restarted, the code is told to wait for three minutes. During this time a status bar on the display shows the counter's progress. Having done some testing, the timing seems like a pretty good fit. With the connection (hopefully) restored, the program will once again, display the connection information, though now with the resets incremented by one.
Although this project is functional the way it is, it would be nice to eventually design an enclosure that would allow the Pi Zero to be better protected and connected to the power module. A brief look under the power module's enclosure reveals that there is little room to spare so either a new enclosure for both items would need to be designed or the Pi Zero would have to be enclosed separately. I have thought of making my own power module, but the group at Digital Loggers did a great job designing a well-rounded module.
I like to finish my project write-ups with some lessons learned and this one has plenty. However, they mostly involve not being scared to try things. I have been tripping over this power module for months while I put off starting this project. Now, I'm so glad that it's done that I wish I had started working on it sooner.