Restart WiFi on raspberry pi zero when it loses its connection

Started on the old forum in March 2018 · view the original in the archive · old links redirect here

1 reply · 84 views · started 8 years ago · 1 min read

2 participants
GumSlone Administrator Principal · 1,690 posts #1

The script below restarts wifi on your raspberry pi when wifi crashes or your pi cant connect to your network.

Login via ssh to your raspberry pi,

default username is: pi default password is: raspberry

Go to folder /usr/local/bin

cd /usr/local/bin

create a file restartwifi.sh

sudo nano restartwifi.sh

Copy and paste the contents below into the file, change 192.168.0.1to ip address of your router.

#!/bin/bash

# The IP for the server you wish to ping (8.8.8.8 is a public Google DNS server)
SERVER=192.168.0.1

# Only send two pings, sending output to /dev/null
ping -c2 ${SERVER} > /dev/null

# If the return code from ping ($?) is not 0 (meaning there was an error)
if [ $? != 0 ]
then
    # Restart the wireless interface
    #ifdown --force wlan0
    #ifup wlan0
    ifconfig wlan0 down
    sleep 5
    ifconfig wlan0 up
fi

press ctrl+x to exit nano, save the file with y.

now we have to create a cronjob which will check wifi every 15 minutes, and if there is no connection to router to restart it.

sudo nano crontab -e

select nano as editor.

add at the end of file this line:

*/15 * * * * /usr/bin/sudo -H /usr/local/bin/restartwifi.sh >> /dev/null 2>&1

press ctrl+x to exit nano, save the file with y.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close