Introduction: ESP8266 Automatic Router Restart

Picture of ESP8266 Automatic Router Restart

I'm a lazy person, and as a lazy person I'd rather sit on the sofa typing code for a while to create this, than have to occasionally go and fix the internet. If you're a lazy person like me, or you just want to get playing with your ESP module, you're in the right place!

Don't undertake this instructable unless you're comfortable working with potentially lethal voltages. I will not be responsible for your death!

For this instructable, I'll be using the following:

  • NodeMCU development board (can be found here on Amazon or much cheaper in bulk on AliExpress)
  • Relay board (here)
  • Power supply (here)
  • Dupont cable or some other wires
  • Small breadboard
  • Extension lead with cable (this and below were bought from my local hardware store)
  • Screw terminal
  • Plastic food tub
  • Electrical tape

Step 1: Cut Your Cable

Picture of Cut Your Cable

You'll want to cut your extension cable at a suitable point, I'd recommend leaving a couple of feet at each end at a minimum so you can locate things properly.

Strip about 1 1/2" from the cable and reveal the 3 smaller cables. We're going to switch the live cable and connect the earth and neutral back together via the terminal block. As the live cable will have to be a bit longer than the others, cut about 1/2" from the neutral and earth cables.

Finally, strip all of the connections about 1/4" (maybe slightly less) so that you have enough of the conductor to work with without bare copper being left all over the place.

If you've not done this for both sides of the extension cord, repeat now for the other side.

Step 2: Connect Earth and Neutral

Picture of Connect Earth and Neutral

Take your terminal block and cut off two sections. Unscrew and insert the earth conductor into one channel and the neutral into the other. Screw tight and visually inspect the connection to make sure you've not caught the plastic or left a lot of the conductor out of the clamp.

Repeat for the other half of the cable, remembering you want to connect earth to earth and neutral to neutral.

Step 3: Connect Live Connections to the Relay Board

Picture of Connect Live Connections to the Relay Board

We're just mocking things up now to make sure everything fits correctly into the food tub. Connect the live wires to one side of the relay board. I've connected the incoming mains wire to the common pole of relay #2, and the outgoing live wire to the normally open connection (that means by default the circuits aren't connected).

Step 4: Connect the Relay Board to Your ESP Module

Picture of Connect the Relay Board to Your ESP Module

The relay board has some nice and easy connections: VCC, In1, In2, GND. Connect GND to one of the GND terminals on the ESP board and connect VCC to the VIN connection (this is 5V compared to 3.3V of the ESP, but the relay won't trigger with this low voltage). Connect In1 and In2 to D0 and D1 on the ESP board.

The relay board is triggered by pulling the inputs to GND, so no need to worry about level protection.

Now on to the software!

Step 5: Test the Program

Picture of Test the Program

Download the attached program into your ESP module. I use ESPlorer for this job. Make sure to change the settings to match your wireless network and pin configuration.

DON'T PLUG IT IN. Just test the relay board works, and the relay board triggers when you pull your ADSL line. For safety's sake don't connect the board to your PC, or have anything accessible by little fingers when there's power going to the modules.

Step 6: Finish It Up

Picture of Finish It Up

Unfortunately I don't have a picture for this step, because I am useless.

Once you're happy everything is working, drill two large holes on either side of your plastic case, for the mains cable. Drill a smaller hole on one of the other edges to allow the USB power to come through. (If you're so inclined and have a larger box, wire an internal socket into the box and use this to power the module).

Disconnect the cables and feed them through. Reconnect them and check to make sure everything is as it should be with no copper wires splayed out or touching anything they shouldn't, and nothing pulling apart. Use some electrical tape to cover anything base or exposed.

Feed the power cable through for USB power and then seal the box. Don't open it or connect to a PC unless the incoming mains power is disconnected. If you have little ones who like to poke around I'd recommend instead a locking/screw close case.

Comments

diy_bloke (author)2015-08-31

it is still a bit unclear to me what yr program does: "the relay board triggers when you pull yr adsl line"
What exactly does that mean?
you have to unplug your adsl line in order to restart your router/modem?
Isn't that just the same touble as unplugging and reinserting your router? or do i miss something here?

Pjtheman (author)diy_bloke2015-09-01

Hi diy_bloke,

Pulling the ADSL line is just to test that the program is working as expected. In operation you have your ADSL modem and router plugged in at all times and have power coming through the relay switch. A rough explanation of the program is below:

-Turn on the relay
-Allow time for the router to come on and then try to connect to the WiFi network
-Check if we have an IP address on the network, otherwise wait and repeat
-If we haven't got an IP address after a certain amount of time, reset the node.
If we have an IP address, begin waiting to check for an internet connection.
-Once the time is up, try to establish a connection to google.com.
-If the connection succeeds, close the connection and wait until the next interval.
-If the connection is not made after a few seconds, enter a new loop.
-In the new loop, we wait for a period of time, then try to connect again. If it fails we retry until the maximum number of iterations (specified at the top of the code). If we get a successful connection we return to the main loop and continue to check every minute or so. If there are no successful connections we reset the router and the node, and begin again from the top.

Hope that helps!

Pete

MatteoS45 (author)Pjtheman2017-08-09

Hi Pjthema,

thank you! i have 2 questions:

1) if can't get an ip (wifi router is turned off by choice or ther is a main power issue), what does it do after 6 attempts?

-- How many times should we try to connect to the local access point, how long do we wait per attempt
local maxWakeLoop = 6
local wakeDelay = 5 * 1000

if it stop, how could i make it start again (power issue resolved)?

2) if there is a provider issue, so restarting the router is useless, what does it do after 6 attempts?

-- When we can't connect to the website, how many times should we retry, how long do we wait per attempt
local maxRetryLoop = 6
local retryDelay = 5 * 1000

like i said above, if it stop this step, how could i make it start again (provider resolve the issue)?

thank you

diy_bloke (author)Pjtheman2015-09-01

yes thanks. It is all clear to me now. Thanks for yr explanation :-)

as I have a router that needs to restart maybe every 3-4 mo, this might be just what I need as I am lazy as well :-)

gta18 (author)2016-12-09

That is smart! I like it! This setup is perfect for my Charter modem that constantly needs rebooting every other day.

Thanks for sharing!

VETERYNARY POLYCLINICN (author)2016-03-07

how can i make this kind of device with raspberry pi ?

mrsharkit (author)2015-08-28

The Olimex Esp8266-Evb fits better your project... Already has a relay, in a much smaller space, for about 10$

Pjtheman (author)mrsharkit2015-08-29

That looks like a fantastic bit of kit! I was thinking about making my own PCB for the task as I've got a bunch of spare relays and so on. I'd love to make it as small as possible and maybe use this for power rather than relying on a large wall-wart: http://www.aliexpress.com/item/5-pcs-HLK-PM01-AC-DC-220V-to-5V-Step-Down-Power-Supply-Module-Intelligent-Household/32319202093.html

mrsharkit (author)Pjtheman2015-08-29

this modified version includes a box to enclose everything, and has pins exposed on a side:

https://www.olimex.com/Products/IoT/ESP8266-EVB-BAT-BOX/open-source-hardware