Remote operation - Modem rebooting

  • 2
  • Idea
  • Updated 2 years ago
How I deal with a Router / Modem lock up on my remote base.

For the 11 years I have been running a remote base I have had issues with my modem hanging up from time to time. In fact, it was the first thing that took me offline in the middle of a winter and a 3 hour drive away. I quickly fixed that by adding a timer that rebooted the modem at 3am.

Finally, it was time to get smarter. There are many 'canned' solutions, but some are expensive and even more expensive when you have them shipped outside of the US. I knew I should be able to do with with a RPI (or similar).

I was too busy to build an AC interface box, but I did find: http://www.digital-loggers.com/iot.html for $30 CDN. :) And, you can drive it directly from a PI GPIO port. Awesome.

In the KISS model, here is a script you can cron every 15 minutes to check and power cycle as required. Make sure you modem is plugged into the NC port of the switch (default to always on). As this is Python, tabs are critical, so if you are copying and pasting, you may have to clean this up.

Mike va3mw

#!/usr/bin/python
#
# Python script to check for live internet based on a cron schedule
# If no connection to both servers, then toggle GPIO pin to power cycle
# both router and modem
#
# GPIO pin is held low for active, HI to toggle power off and on
# Pin 6 GND and Pin 12 for GPIO logic
# http://www.digital-loggers.com/iot.html for logic switch
# wired to use the NC contacts
#
# M. Walker va3mw 09/16/2016

import socket
import RPi.GPIO as GPIO
import time
import syslog
PIN = 12

def is_connected(REMOTE_SERVER):
try:
# see if we can resolve the host name -- tells us if there is a DNS listening
host = socket.gethostbyname(REMOTE_SERVER)
# connect to the host -- tells us if the host is actually reachable
s = socket.create_connection((host, 80), 2)
# syslog.syslog('Successful Test')
return True
except:
pass
return False

def ResetRouter():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN,1) # turn on pin
syslog.syslog('Modem Reset - connection failed')
time.sleep( 5 ) # sleep for 5
GPIO.output(PIN,0) # turn off pin
GPIO.cleanup() # cleanup

def main():
if not (is_connected("www.google.ca") and is_connected("www.cnn.com")):
ResetRouter()
else:
syslog.syslog('Internet Up')
if __name__ == "__main__":
main()
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes

Posted 3 years ago

  • 2
Photo of WX7Y

WX7Y

  • 808 Posts
  • 169 Reply Likes
Well relying on a Pi for me is not a real wise choice, for me I use the Digital loggers remote power switch, They have Autoping which you can set up to try to ping some outside and inside IP address and if it can't ping one of the IP you have set for a certain number of pings you can set it toggles the outlets you have asked it to. 
I use these for ALL my Communication sites and my home automation stuff and some of the switches I have 
had in full time service since 2005 and I have 30 to 35 of these power switches. 

I like the turn key and very dependable service these provide

73's and just my 2 cents.
Bret
WX7Y
Photo of Bob G   W1GLV

Bob G W1GLV

  • 821 Posts
  • 145 Reply Likes
The Raspberry Pi is probably the most reliable $35 computer on th planet.
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes
I would have preferred to use the Digital Loggers device, but the price is through the roof to get it into Canada. The way I have it coded if the PI dies, which they do, I will still have power going to the Modem/Router.
Photo of Arnie

Arnie

  • 108 Posts
  • 20 Reply Likes
Mike:
You have come up with a really nice solution that is very cost effective. I will be trying it. The seems to accomplish the same thing effectively as the microloggers autoping does. Tnx!!!!
73 de Arnie W8DU
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes
BTW, if you just copy and paste this into a file on the pi, it likely will not run. You'll have to conform to python format. The tabbing is key.
Photo of Michael Coslo

Michael Coslo

  • 947 Posts
  • 259 Reply Likes
Your reboot is probably caused by RFI into the router. A few weeks back, I had a problem hwile contesting on a particular frequency on 40 meters. Turned out my wireless router was exquisitly sensitive, and would reboot, which made for a real nuisance. A big toroid and heavy duty ethernet cable fixed it.
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes
Michael

My issue happens with just about every cable modem I have used and sadly is not RF related (I have my fair share of chokes in place).  This happens when there is no RF being transmitted or I haven't been on the air for weeks.  I can tell when the security cameras or weather station goes offline.

The latest is due to the Motorola Modem whose LAN port dies.  This is the port that the Router plugs into.  I have proven and reported it to my ISP who claims 'No one else has reported it, so that isn't the problem.  Grrr... I reply 'that is because for everyone that has a problem, the first thing you tell them to do is to reboot the modem and the problem goes away.  The tech responds with 'hmmmm ... I guess you are correct!".

Thanks for you suggestions.

Mike va3mw
Photo of Arnie

Arnie

  • 108 Posts
  • 20 Reply Likes
What version of Python did you use? I want to try this on an Arduino.
Tnx de Arnie W8DU
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes
I don't think it is critical.

root@raspberrypi:~# python --version
Python 2.7.9

Make sure if you copy my code that you tab in the routines accordingly. I had some help, so I'm not the one to ask on the formatting.

Mike va3mw
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes
Wow, I can't seem to edit my initial comment.

As for the code (for you non-coders) I have added comments that may help with the Python script

#!/usr/bin/python
#
# Python script to check for live internet based on a cron schedule
# If no connection to both servers, then toggle GPIO pin to power cycle
# both router and modem
#
# GPIO pin is held low for active, HI to toggle power off and on
# http://www.digital-loggers.com/iot.html for logic switch
# wired to use the NC contacts
#
# M. Walker va3mw 09/16/2016

import socket
import RPi.GPIO as GPIO
import time
import syslog
PIN = 12

def is_connected(REMOTE_SERVER):
# 1 tab to the 'try:'
try:
# see if we can resolve the host name -- tells us if there is a DNS listening
# this group has 2 tabs until the 'return True'

host = socket.gethostbyname(REMOTE_SERVER)
# connect to the host -- tells us if the host is actually reachable
s = socket.create_connection((host, 80), 2)
# syslog.syslog('Successful Test')
return True
# 'except' has 1 tab and 'pass' has 2 tabs
except:
pass
# next line - 1 tab
return False

# no tab

def ResetRouter():
# 1 tab - feel free to add other code here to send messages once the box has been turned back on
# before the GPIO.cleanup() - we do the cleanup up to make sure all pins are low
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN,1) # turn on pin
syslog.syslog('Modem Reset - connection failed')
time.sleep( 5 ) # sleep for 5
GPIO.output(PIN,0) # turn off pin
GPIO.cleanup() # cleanup

# no tab
def main():
# 1 tab
if not (is_connected("www.google.ca") and is_connected("www.cnn.com")):
# 2 tabs
ResetRouter()
else:
# 2 tabs
syslog.syslog('Internet Up')
# no tabs
if __name__ == "__main__":
# 1 tab
main()
Photo of Timo - OH5KW

Timo - OH5KW

  • 23 Posts
  • 0 Reply Likes
If there is a modem problem you can not remote control your station any more. So there has to be another systems to reboot or switch on or off the station. 
I have used for many years Ontech GSM relays. Main unit can turn on or off the main power to the station. A slave unit is rebooting the modem if needed. Another slave unit can reboot the PC. Slave units are commanded by main unit by WiFi connection.
It is better to have two separate ways to control your remote station power. If the Internet or modem fails, GSM is still in use. Just send a simple SMS message and you are back in business.

73, Timo OH5KW
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes
Hi Timo

I do have one of those, but I have to notice that the modem is offline. :) I did buy one on eBay but have yet to buy a SIM card to set it up. I went this route in order to have it happen automatically.

My DXcluster customers get pissy when I'm offline. LOL

73 Mike va3mw
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes
Ta da!  It worked.  This was as case of the LAN port on the Modem hanging up.  This is a Motorola modem and I can tell from the Router logs that it was running fine.


Sep 23 08:30:01 raspberrypi internet_check.py: Internet Up
Sep 23 09:00:02 raspberrypi internet_check.py: Internet Up
Sep 23 09:30:02 raspberrypi internet_check.py: Internet Up
Sep 23 10:00:26 raspberrypi internet_check.py: Modem Reset - connection test failed
Sep 23 10:30:02 raspberrypi internet_check.py: Internet Up
Sep 23 11:00:02 raspberrypi internet_check.py: Internet Up
Sep 23 11:30:01 raspberrypi internet_check.py: Internet Up
Photo of Arnie

Arnie

  • 108 Posts
  • 20 Reply Likes
Mike:
I did get this working with some enhancements on my RPi 3 and it is really nice. I have enhanced the code to enable it to email me a log file when the internet returns to service after going down. I am just using simple text files for the log and the email function built into raspian. I also added an indicator LED to show when the reboot has been triggered. I also added on-screen feedback of the script.
Here is the code. As Mike cautioned, tab formatting is really critical in Python.
73 de Arnie W8DU

#!/usr/bin/python#
# Python script to check for live internet based on a cron schedule
# If no connection to both servers, then toggle GPIO pin to power cycle
# both router and modem
#
# GPIO pin is held low for active, HI to toggle power off and on
#  http://www.digital-loggers.com/iot.html for logic switch
#  wired to use the NC contacts
#
# M. Walker va3mw 09/16/2016
# modified by A. Podolsky W8DU 11/02/2016
# calls emailtest.py when internet returns to service


import socket 
import RPi.GPIO as GPIO 
import time 
import datetime
global flag 
PIN = 12  #trigger for dataloggers power socket
LED = 23 #trigger for indicator LED - active when internet goes down
flag = 0
data = open("flagdata.txt","a") #make sure flagdata.txt exists
data.close

def is_connected(REMOTE_SERVER):
     try:

          # see if we can resolve the host name -- tells us if there is a DNS listening
          host = socket.gethostbyname(REMOTE_SERVER)
          # connect to the host -- tells us if the host is actually reachable
          s = socket.create_connection((host, 80), 2)
          print ("Successful Test")
          return True
     except:
          pass
     return False 

def ResetRouter():
     GPIO.setwarnings(False)
     GPIO.setmode(GPIO.BCM) #this is changed from 'GPIO.BOARD' on previous version
     GPIO.setup(PIN, GPIO.OUT)
     GPIO.setup(LED, GPIO.OUT)
     GPIO.output(PIN,GPIO.HIGH) # turn on pin - turns off power on AC control
     GPIO.output(LED,GPIO.HIGH)
     print 'Connection down - resetting now ', datetime.datetime.now()
     fout = open ("checkfile.txt","a")
     s=str(datetime.datetime.now())
     fout.writelines ("Internet DOWN at ")
     fout.write(s)
     fout.write (' \n')
     fout.close()   
     time.sleep( 20 ) # sleep for 20 seconds - this powers off modem and router
     GPIO.output(PIN,GPIO.LOW) # turn off pin - this turns modem/router on
     GPIO.output(LED,GPIO.LOW)
     GPIO.cleanup() # cleanup - all pins low - ensures power is on

     
def main():
     global flag
     if not (is_connected("www.google.com") and is_connected("www.cnn.com")):
          ResetRouter()
          print 'Resetting router at ',datetime.datetime.now()
          fout = open ("checkfile.txt","a")
          s=str(datetime.datetime.now())
          fout.writelines ("Resetting router at ")
          fout.write(s)
          fout.write (' \n')
          fout.close()
          print (flag)
          flag = 1
          data = open("flagdata.txt","w") #set flag for internet down
          data.write("1")
          data.close()

     else:
          print 'Internet UP as of ',datetime.datetime.now()
          global flag
          data = open("flagdata.txt","r") #get flag status from file
          flag = data.read()
          flag = int(flag)
          if flag: #if flag was set, write to checkfile and email file
               fout= open("checkfile.txt","a")
               s=str(datetime.datetime.now())
               fout.writelines ("Internet RETURNED TO SERVICE at ")
               fout.write(s)
               fout.write (' \n')
               fout.close()
               import emailtest #send email
               print 'Internet returned to ONLINE at '
               print (s)
               print (' \n')
          print (flag)     
          flag = 0 #internet back up - reset flag and write to file
          data = open("flagdata.txt","w")
          data.write("0")
          data.close()

# The following 6 lines are for testing purposes. Make sure to comment out the last two lines
#  at the botton of this script when doing any testing with the following 6 lines
#try:
# while True:
# main()
# time.sleep(90) 
#except KeyboardInterrupt:
# print ('program stopped')

if __name__ == "__main__":
     main()
     
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 198 Reply Likes
I thought you said you couldn't program! :) Nicely done.

I just started to play with Node-Red which run on a rPI. https://nodered.org/ This is holding great promise for my new remote antenna switch.

Mike va3mw
Photo of Arnie

Arnie

  • 108 Posts
  • 20 Reply Likes
Thanks...Like they say: "Even a blind squirrel can find a nut every now and then."
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes
Further update.  I had this loaded up on Github to make it easier to find/edit.  There is one for controlling an GPIO port and another one for rebooting a pfSense router.

Mike 

https://github.com/r0n22/Internet_Monitor/
Photo of Mike va3mw

Mike va3mw

  • 824 Posts
  • 199 Reply Likes
BTW, this is the easiest thing to interface to a Arduino or a RPI.

It can be driven right from the GPIO logic on the RPI.  Now, if they just had a 220 one.  

https://www.adafruit.com/product/2935

Mike