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

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: 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

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

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

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

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

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.

Epilog Contest VII

Participated in the
Epilog Contest VII



    • Indoor Lighting Contest

      Indoor Lighting Contest
    • Make It Fly Challenge

      Make It Fly Challenge
    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest

    11 Discussions


    1 year ago

    Going to try and build this one. I have already a single relay module, I guess I don't need a 2-relay board, right? So I will just connect D0 with E0 single relay board? Sorry for the dumb question!


    Daniel AugustoG

    1 year ago

    Hi, Im using your code, all works good but when I try simulate a internet fail (removing cable of modem), I recive this message below, and relay dont change state.

    I can not reset the relay, I dont know why (I tested the relay in other arduino, and its works fine).

    PS: I change local onValue = 1 or local onValue = 0, and in both situation allways the green led is HIGH on the rellay (I think it should be changed the start mode relay)

    PS2: Sorry my english

    Sending request to

    DNS retry 1!

    No callback ack on loop 5 of 6

    Sending request to

    DNS retry 1!

    DNS retry 2!

    No callback ack on loop 6 of 6

    Resetting Access Point

    Sending request to

    DNS retry 1!

    DNS retry 2!

    DNS retry 3!

    DNS retry 4!

    DNS Fail!

    DNS Fail!

    DNS Fail!


    3 years ago on Step 5

    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?

    3 replies

    Reply 3 years ago on Introduction

    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
    -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!



    Reply 2 years ago

    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


    Reply 3 years ago on Introduction

    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 :-)


    2 years ago

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

    Thanks for sharing!


    3 years ago

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

    2 replies

    Reply 3 years ago on Introduction

    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:


    Reply 3 years ago on Introduction

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