0

I am trying to use my raspberry pi for hotspot system but some time dongle internet is not working so that time i need to reboot the device, is there any possibilities to reboot the device if the internet is not available.

3
  • 1
    Write a script that pings Google or makes an internet page request if you get an error then reboot. This question and answer should get you most of the way there. Jul 10, 2017 at 6:40
  • @SteveRobillard good idea, just remember about retrying before reboot or you might face reboot loop
    – Mark
    Jul 10, 2017 at 6:58
  • 1
    @Mark it should definitely have an incremental backoff and circuit breaker included. Jul 10, 2017 at 7:01

1 Answer 1

1

Here's the script I use to do this. I have three LEDs connected to GPIO pins which I use to monitor the status of my (headless) raspberry pi in my garden shed. (I need to do this because if the wifi connection drops out, it never reconnects by itself, whereas if I reboot the pi it is back again in a few minutes).

# script to check connectivity and illuminate LED as appropriate

# using "board" counting scheme on RasPi where outboard pins are all even with 2 in the corner.

import RPi.GPIO as GPIO
import time
import atexit
import sys
import os

pin_green = 16
pin_yellow = 18
pin_red = 22

failcount = 0

def turn_off_LEDs():
    print "Exit: Turning LEDs off"
    GPIO.output(pin_green,False)
    GPIO.output(pin_yellow,False)
    GPIO.output(pin_red,False)
    sys.exit

atexit.register(turn_off_LEDs)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(pin_green,GPIO.OUT) 
GPIO.setup(pin_yellow,GPIO.OUT) 
GPIO.setup(pin_red,GPIO.OUT)

while True:

    # Set "thinking" code
    print 'Yellow'
    GPIO.output(pin_green,False)
    GPIO.output(pin_yellow,True)
    GPIO.output(pin_red,False)

    # Sleep so the human has a chance to see the orange light
    time.sleep(1)

    # Do ping
    hostname_list = ["192.168.1.55", "192.168.1.5"]
    response = 1
    for hostname in hostname_list:
        response = response * os.system("ping -c 1 -t 60 " + hostname)

    if response == 0:
        print 'Green'
        GPIO.output(pin_green,True)
        GPIO.output(pin_yellow,False)
        GPIO.output(pin_red,False)
        failcount = 0
    else:
        print 'Red'
        GPIO.output(pin_green,False)
        GPIO.output(pin_yellow,False)
        GPIO.output(pin_red,True)
        failcount = failcount + 1
        # There is a 5 second cumulative wait per iteration (1 for the orange
        # and 4 at the end) so 100 = 500 seconds which is nearly 10 minutes - long enough
        if failcount > 100:
            os.system("reboot")

    time.sleep(4)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.