Auto modem reset when internet down

My modem/router occasionally lockup and I have to manually turn it off and on to resume internet service. This happens only occasionally. The issue is that I do not know when it goes down nor how long its been down, and in that time the phone will also be down, not to mention various IOT stuff connected round the place.
I came up with a brute force approach below using node red and broadlink spmini power connected to the modem.

The logic:
The modem is plug into broadlink spmini power plug.
NR pings Google every 30s, if not false, do nothing.
If false (ie internet down), wait for 3 secs and re ping Google, if not false, do nothing.
If false, turn off power plug to modem, wait 5 s, then on power plug again (power resetting the modem).
By that time, ready for another check cycle timing at beginning of loop.

HW: Raspberry Pi, Broadlink SPmini3 power switch
SW: NodeRed v19.4;
Nodes: Ping, Advance Ping, Broadlink Control


I am not sure if this is the best approach or if it is robust enough, or it may goes to an endless loop turning the modem on and off under certain circumstances.
Testing it manually seems to work (note:I use a short cycle time in secs instead of minutes for testing purposes).
I am seeking the vast knowledge from the Forum to review, critique and help to improve my approach and make it practical. Also if someone can work out how to notify when internet is down, let me know!

First see if there is a firmware update available for the router.

If not then does the router crash completely or is it just internet access that fails? In other words can you still ping the router when you can't ping google? The fact that you can still control the power switch suggests that it is only the internet that has stopped. If so then it may well be that you can reset the router via its web interface rather than switch the power.

On the other hand you might be better to get a better modem/router.

I pretty much have the same problem, and it usually crashes/freezes when I'm away from home!!

Yep, up to date.

Cannot ping the router or even access it's web interface via local network.

Googling this, it seems to be a common issue...

In my case, every time the internet drop out I have to turn the modem/router on off to restore internet, but the LAN works ie I can ping the router and NR works locally, but not internet access.
I have spent months with the ISP and they have replaced the modem/router twice (with latest sw, etc), checked all the lines etc.
As the cannot find anything wrong and because it only occurs less than once a month, the ISP states that it is within acceptable service level.
I have no issue with the occasional internet drop outs, just that when it happens I have to power reset the modem to restore internet, and if I am away my home IOTs could be out till I come back to restore it.
It seems others have similiar issues, to the extend that a commercial product been developed for this:

Anyway I am more seeking comments and advice on the NR flow and logic for this scenario.


1 Like

Fair enough, you have obviously already investigated the alternatives. In your flow I would put in something so that having reset the router it won't do it again for, say, 15 mins. That will stop it endlessly resetting if it takes a little while to connect. Also rather than pinging one address twice it might be better to ping two different addresses, and only reset when both fail, in case there is a temporary issue with one server.

1 Like

I started by using a node-RED flow which controlled a MQTT remote relayboard, which worked very similar to your flow above, and removed power from the router causing a reboot, but eventually decided to build a stand-alone device, which would not be reliant upon node-RED running, or having communication between node-RED and the relayboard, for better resilience.

I purchased a Wemos D1 Mini (ESP8266 device), also a 3V opto-isolated relay. Total cost about ÂŁ4 from AliExpress!

Then wrote a sketch which;

  1. Connects to my wifi
  2. Every 20 minutes, it pings 'google' (and if no reply - then 'cloudflare') and if it gets
    a response, the ESP will go to deep sleep mode for 20 mins, then repeat.
  3. If no reply is received from either, the sketch enters a loop, rechecking google &
    cloudflare, for a further 4 times, 1 minute apart.
  4. If no reply is received, the relay will remove power to the router for 5
    seconds causing a router reboot.
  5. ESP will then go into deep sleep for 5 minutes to allow the router to
    stabilise, then resume checking for connectivity.

I've just uploaded the sketch to Github so you can see the process, and possibly adapt bits of it for your own interpretation.



Didn't you need a power supply and a box to put it in?

Of course Colin, but I didn't pay for them :wink:
We all have old phone/tablet chargers knocking about, and a old HTC charger was ideal, as it even used a micro USB lead, which plugged straight into the Wemos.
Most phone chargers are great for such usage, as they have inbuilt safety features such as short circuit, over-voltage protection etc, and also very efficient (EU regs) which is especially important as the Wemos only draws around 100uA whilst in deep sleep.

As my router is situated in an internal garage, I wasn't too fussy about the physical appearance of the device, so just did a bit of recycling from my man cave.

I did however buy a 0.5m extension lead (single socket) from Poundland (you guess the price!), which I cut in half to provide the mains input/output, to/from the relay.


Nice - be even nicer with a bit of a parts list/diagram explaining the hardware side for those folk not so familiar with that. :wink:

Not necessarily, my phones seem to last longer than the chargers, and phones don't necessarily come with chargers now.

If one had to buy a charger I think it might be cheaper to use something like a Sonoff Basic flashed with the Tasmotta software, which I think would do the job.

Similar problems for me but my stupid router behaves a bit differently. After a while it seems getting choked somehow, disconnected clients cannot connect (wireless) anymore. Those already connected seems to work fine and all have internet access. So my brute force ugly house-keeping method, scheduling a short power off/on every second night. I hate this, latest firmware but no change, unfortunately I have no contact at Huawei management board

I have a slightly different issue in that my main home automation server (Node-RED of course) is still running mainly on my older Pi2. If we have a power outage, the Pi2 loses network connectivity and needs a reboot to get it back. So I have a flow that pings my router and when it can't get a response, it reboots the Pi.

One thing I'd say though, if you are worried about reboot loops, you might want to put at least a limit on the number of reboots. Or better still, an expanding time loop so that the reboots get further apart over time.

If I get some time free at the weekend.

Yes, I wanted to do that, but never managed to persist context between deep sleep sessions.
My C++ is limited!

1 Like

Haha, mine too. I usually manage to work it out in the end - but you are ahead of me already as you've actually done a deep-sleep. I think I've seen how to do persistance with an ESP32 but I can't remember seeing it for an ESP8266. Possibly writing to the flash memory or ESP filing system would do it. I suppose, if desperate, you could use MQTT as an external store.

1 Like

Maybe I am off-topic here, but this is my fix:

Script to reboot the Pi if the net is down

I have a Pi ZeroW with a camera on it that is both remote and headless. It would occasionally go offline
for undetermined reasons and restarting it with a power cycle is not an acceptable solution.

Here is how I solved this with a shell script that cron runs every five minutes.
The script will ping the router (or if you need to test internet connectivity), and if the ping returns a 0 (OK), then nothing happens.
If the ping returns anything other than 0, then a 'sudo /sbin/reboot' is called.

Here is my script file (/home/pi/my/


#This script looks first to see if the enabling file is in the /boot folder.
#If the file doesn't exist, then nothing happens.
#This is a fail-safe if I need to disable the reboot on ping fail.

if [ -e /boot/pingtest.txt ]; then
  ping -c1 > /dev/null 2>&1

  if [ $? -ne 0 ]; then
    echo "Reboot at " `date`
    sudo /sbin/reboot
  echo "ping blocked, " `date`

(Don't forget to make the script executable): chmod +x /home/pi/my/

Here's my crontab line:

*/5 * * * * /home/pi/my/>> /home/pi/logs/reboot.log 2>&1

Hope this helps someone.

1 Like

Well, actually you are. The topic is about the router that provides your home network with internet access, not the Pi's themselves. For the Pi's, your fix is fine. And for such purpose, you can also create a simple Node-RED flow that does the same with a nice view instead of writing scripts and configure cron jobs

I planned to do almost the same thing for my IP camera when the camera bug.
I developped a remote 230V plug and will try something like you mentionate but in C with arduino ide and a esp8285 module

OK, I read too fast. But if the router is down, how will the Broadlink WiFi switch get the off/on commands from Node-Red?

The internet ie wan is down, the lan still works.

As mentioned, the LAN might still be working, but in my case, I use a switch with different technology not depending on LAN/WiFi, I use a 433 MHz switch controlled by a RFXtrx