Introduction: Raspberry Pi Reboot Router

Have you ever noticed that you are no longer connected to the internet?

Is it frustrating to get up in the morning and have to 'reboot' the router to get the internet back?

Well, this has happened to me more often than I care for it to occur.

Earlier this year, I went out of town for 3 weeks and on the first day my router disconnected from the internet! This meant I had no connection to the things in my house I rely upon to 'check in' on periodically - e.g., Nest thermostat, Arlo cameras...Since I wasn't home, I could not restart my router. I really didn't want to bother a neighbor to get past my security system and then find my router and restart it. I also thought of calling the power company and having them cut the power to my house for about 5 minutes - my S.O. did not approve.. :(

When I got back, I started to search for a good solution but couldn't find anything comprehensive enough for what I needed. At first, I just bought a digital timed power strip and set it to turn the router off at 2:00 am for 5 minutes. Unfortunately, a couple of mornings I didn't have the internet - a review of the router logs for those days indicated that the internet 'disconnected' around 4:00 am and my router wouldn't connect unless I restarted it.

Having an extra Raspberry Pi laying around and some python skill, I decided to look into how it could help with my dilemma and came up with this solution.

I tested and installed it and so far so good!

Basically, the Raspberry Pi checks for an internet connection every 2 minutes via a 'ping' to 2 different websites. As long as at least one of them responds, we are all good. If there are no returned 'pings' the Pi shuts off power to the internal USB hub which in turn causes the Iot Power Relay to switch off the router. After 2 minutes, the Pi powers up the internal USB hub causing the Iot Power Relay to switch on the router. It then waits for 4 minutes before resuming the 2 minute internet connectivity check.

I also had to consider that maybe there is an outage in my area and the internet is going to be down for awhile - rare, but it does occur (at least in my area...) and I didn't want the router restarting every 6 to 8 minutes so I put in a flag that would postpone the next restart for 1 hour in this case.


Equipment List:

  1. Iot Power Relay for Raspberry Pi (pictured above)
  2. USB 2.0 A Screw Terminal Block Connector (if you are going to make your own USB -> Power strip cable, also pictured above)
  3. Cable with 2 or more conducting wires or an old / extra USB cable you can use. I used an old stereo phono plug cable and cut off the plugs.
  4. Raspberry Pi Model 3+ (you could use a Model 4 and control individual ports - I am using a Model 3)

I may earn a small commission for my endorsement, recommendation, testimonial, and/or link to any products listed above.

Step 1: Build and Connect the Hardware

I built my cable to connect the Pi to the power relay.

I used the USB screw terminal block with 2 wires connected to the + and gnd slots. You should be able to see them on the connector. See the picture with the red(+) and white(-) arrows to the slots.

I connected the other end into the power relay. The green part on the side of the power relay just pulls out and then you can insert the wires into the slots and screw then down tight. See the picture with the red(+) and white(-) arrows to the wires.

Note: if you use an existing USB cable, you will usually have 4 wires at the other end - the red one is the (+5v) and the black one is the (-) ground. You will only use those 2 for the Power Relay.

For testing, I connected a table lamp to one of the outlets marked 'normally off'. This is controlled by the USB power state. I plugged the Raspberry Pi into the 'always on' outlet and plugged it in and turned it on for the next step.

Step 2: Install Software

Set up the Pi:

There are a couple of websites that will guide you in setting up your RPi for the first time if needed.

Life Hacker

Raspberry Pi Org

I set mine up 'headless' and use VNC to connect to it. The links above show how to do this.

Software set up:

  1. Install uhubctl (see that will be used to turn the usb hub off and on.
  2. Download and install the software I wrote from my GitHub page: RPi Router Reboot software.
  3. Set up and register internet-monitor.service if you want to run this as service when the Raspberry Pi starts up.

Step 3: Testing and Operation Details

The main python program, can be run with 1 or 2 parameters for debugging and testing purposes.

Open up a terminal session in the folder you put the code and execute the following command:

:~/Documents/RebootRouter $ python3 -debug -test

Note - this will print out debugging information and turn the lamp off and on as it will test out not connected to the internet. You can just use -debug and see the messages. You can not just use -test, it must be the second parameter after -debug.

You can change the CONSTANTS in the top part of the program to whatever values you like. The first set run with the -test parameter set, the second set are what will be used in all other cases.

I set up a service to run this at boot time so it is always running on my Raspberry Pi.

Open up internet-monitor.service and edit the ExecStart and WorkingDirectory lines with your full paths to the python code. Save the file.

Copy the file to the systemd/system folder:

:~/Documents/RebootRouter $ sudo cp internet-monitor.service /etc/systemd/system/internet-monitor.service

Test that the service starts without any errors:

:~/Documents/RebootRouter $ sudo systemctl start internet-monitor.service

Enable the service to start at boot time:

:~/Documents/RebootRouter $ sudo systemctl enable internet-monitor.service

If all goes well, you can now put it all together and turn the Iot Power Relay off, disconnect the lamp, connect the router and turn the Power Relay back on. Your router should reboot and now is monitoring the internet.

I did one more test after I had everything hooked up - I disconnected my internet cable at the wall and waited; sure enough the router went off and then on. It stayed on and after 10 minutes or so, I reconnected the cable at the wall and had the internet - it has been running fine so far.... :)

Raspberry Pi Contest 2020

Participated in the
Raspberry Pi Contest 2020