Build an Automatic Router Restarter for Just Three Dollars with an ESP8266 and One Relay.
![]()
The weakest link in our digital life at home is the pipe into our house and the device connected to it. Our cable modem is what allows our devices to communicate with the outside world.
Like all computers, modems are prone to failure; some of us have to restart them at least once a week.
Recently, my modem started disconnecting once a day, and because I cannot face the hassle of setting up a new one, I decided on a workaround.
If a sensor could detect when the modem had disconnected, it could cycle the power itself.
The project turned out to be much simpler and cheaper than I expected.
Required Components
- ESP8266 01 – costs about USD 1.50 from Aliexpress
- A programming board for the ESP. Some of us already have a programming device, but if you buy both together, you can get them for about USD 2.50.

- A small relay board for the ESP. Search for an ESP8266 5V 1CH ESP-01 relay module. This also costs about one dollar.

How Does It Work?
In layman’s terms, the process is simple. The ESP regularly pings Google through the modem. If it receives a response, it does nothing; the relay stays closed and the modem stays on.
If the ESP receives no response, it “understands” that the modem is not working. When this happens, it switches the relay off, waits 30 seconds and then switches it back on, cycling the modem’s power.
“Endless Loop”
One bug left me puzzled for a while before I worked it out.
We all know that a modem takes several minutes to complete a successful “handshake” with the line. When I first set up my device, I had the ESP ping Google during the setup sequence. Since the modem had not yet connected, the ESP received no response, so it power-cycled again. I was stuck in an endless loop and getting nowhere.
I needed the ESP to give the modem time to complete a successful handshake. Once that happened, the ESP would begin its regular polling of Google.
Connections
The relay is placed in the middle of the router’s power cable. It switches only 12 V, so there are no safety issues. The only complication is that the ESP relay board also needs power, which I supplied through an old 5v, 1A mobile phone charger. I printed a small box to put it in, but decided not to cover it to avoid overheating.

Additional Considerations
Since I installed the device, it has worked flawlessly. However, I can see that the disconnections are becoming more frequent, and I wonder whether the router will need to be replaced sooner than I thought. To get more detail on how often the power is cycled, I am working on a version (with the blog’s co-founder, Allan) that will:
- Notify me when it does its job
- Record the time, dates and frequency of events.
This whole project got me thinking: why is this not built into routers? I am sure it would be very easy to tell a router to cycle its power if it loses its Internet connection.
Allan’s Technical Discussion
The brilliant programmer and colleague Allan Schwartz provided the following discussion of the code we installed on the Arduino:
Our solution can best be described as an event-driven finite-state machine.
In this application, we have exactly 3 states:
- Testing state
- Failure state
- Success state
What the program does during each of these states is best described in the following state diagram, which is a kind of flowchart. This is formally called a UML state diagram.
The state diagram shows the three states highlighted in yellow and the state transitions:

We can translate this state machine and its implied logical flow into Arduino C very precisely, so that the intent of the C code is very clear and we can check that the C code matches the logical flow exactly.
State transitions are performed by assigning a new value to CurrentState and then “breaking out” of the switch statement. Replacing break with return would produce exactly the same behavior, because loop runs continuously.











