-
- Posts: 9
- Joined: Tue Oct 23, 2012 4:01 pm
Re: Wifi Reconnect on drop
Yes, it is likely that the Pi still has the IP address due to DHCP lease. So, it's not going to try and reconnect. I suppose I could use my ping test to reset the lease. Will try that.
Re: Wifi Reconnect on drop
So elyobelyob, you gave me the idea to try a ping test instead of an ifconfig test. I don't know if it will work in every scenario, as you have stated, but since the ifconfig method was not working for me at all, I needed something that would work for most network disconnects anyways. Here are the modifications that I made to Dweeber's script:
1) First I added a new variable named "pingip" to the "Settings" portion of the program. I use the address of my router for the test, but because I have two different routers that I might use, with different addresses, I made it a variable. It also keeps in line with the flexibility of the program:
2) Then I changed the "We can perform check" portion of the program from an ifconfig test to a ping test:
The original code was created by:
# A Project of TNET Services, Inc
#
# Title: WiFi_Check
# Author: Kevin Reed (Dweeber)
# dweeber.dweebs@gmail.com
# Project: Raspberry Pi Stuff
#
# Copyright: Copyright (c) 2012 Kevin Reed <kreed@tnet.com>
# https://github.com/dweeber/WiFi_Check
and can be found here:
http://rpi.tnet.com/project/scripts/wifi_check
1) First I added a new variable named "pingip" to the "Settings" portion of the program. I use the address of my router for the test, but because I have two different routers that I might use, with different addresses, I made it a variable. It also keeps in line with the flexibility of the program:
Code: Select all
##################################################################
# Settings
# Where and what you want to call the Lockfile
lockfile='/var/run/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='eth0'
# Which address do you want to ping to see if the network interface is alive?
pingip='192.168.1.1'
##################################################################
Code: Select all
# We can perform check
echo "Performing Network check for $wlan"
/bin/ping -c 1 -I $wlan $pingip > /dev/null 2> /dev/null
if [ $? -ge 1 ] ; then
echo "Network connection down! Attempting reconnection."
ifdown $wlan
sleep 5
ifup --force $wlan
else
echo "Network is Okay"
fi
The original code was created by:
# A Project of TNET Services, Inc
#
# Title: WiFi_Check
# Author: Kevin Reed (Dweeber)
# dweeber.dweebs@gmail.com
# Project: Raspberry Pi Stuff
#
# Copyright: Copyright (c) 2012 Kevin Reed <kreed@tnet.com>
# https://github.com/dweeber/WiFi_Check
and can be found here:
http://rpi.tnet.com/project/scripts/wifi_check
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/
https://www.flaminghellmet.com/launch/
Re: Wifi Reconnect on drop
I just received my Rev 2 board a couple of days ago, and because the hardware/OS software is so much faster than the Rev 1 board, I found that I had to add an extra ping to get a reliable error code back in the above example:
Changed:
/bin/ping -c 1 -I $wlan $pingip > /dev/null 2> /dev/null
To:
/bin/ping -c 2 -I $wlan $pingip > /dev/null 2> /dev/null
Changed:
/bin/ping -c 1 -I $wlan $pingip > /dev/null 2> /dev/null
To:
/bin/ping -c 2 -I $wlan $pingip > /dev/null 2> /dev/null
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/
https://www.flaminghellmet.com/launch/
-
- Posts: 1
- Joined: Sun May 12, 2013 7:02 pm
Re: Wifi Reconnect on drop
I found a far more simple solution here: http://raspberrypi.stackexchange.com/qu ... nnect-wifi.
Works like a charm for me.
Code: Select all
cd /etc/ifplugd/action.d/
sudo mv ifupdown ifupdown.original
sudo cp /etc/wpa_supplicant/ifupdown.sh ./ifupdown
sudo reboot
Re: Wifi Reconnect on drop
that certainly did work like a charm leomekenkamp!!
The only problem now is that it has stopped my ethernet port from working
The only problem now is that it has stopped my ethernet port from working

Re: Wifi Reconnect on drop
Yep doesnt matter what i do i cant get my eth0 to work once doing the above solution
-
- Posts: 3
- Joined: Tue Jul 09, 2013 9:32 am
Re: Wifi Reconnect on drop
got it working the first time (the one from https://github.com/dweeber/WiFi_Check) just make sure you include the cronjob in the sudo account and add "sudo" after the time settings.
Re: Wifi Reconnect on drop
Hi for autostarting scripts or other programs, you can use supervisor. You can easily install supervisor by
For a quick start you can create a conf file for example
Sadly supervisor distribution has not installed with a template configuration on pi so here is a template which was taken from one my ubuntus. ; is comment character.
This redirect standart output to a file which is handy if your script or program writes to console.
You can watch it realtime by from shell for debugging purposes.
Important Warning
It daemonize your script, so DO NOT USE this if If you use python:
A tip. You can see supervisored scripts and their statuses from It helps for troubleshooting. Also there is a wait time for supervisor determine your script is started correctly. Default is 1 second. So if your script exit too quickly supervisor thinks it has a problem. I wish it helps.
Gokcer Gokdal
Code: Select all
sudo apt-get install supervisor
Code: Select all
sudo pico /etc/supervisor/conf.d/reconnect_wifi.conf
Code: Select all
[program:connect]
command=python /home/gokcer/script/connect.py
directory=/home/gokcer/script/
;umask=022 ; umask for process (default None)
;priority=1 ; the relative start priority (default 999)
autostart=true ; start at supervisord start (default: true)
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
user=root ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/home/gokcer/script/connect.log ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/home/gokcer/script/connect.err.log ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="1",B="2" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
Code: Select all
stdout_logfile=/home/gokcer/script/connect.log
Code: Select all
tail -f /home/gokcer/script/connect.log
Important Warning
It daemonize your script, so DO NOT USE this if If you use python:
Code: Select all
with daemon.DaemonContext(): // DONT USE THIS!!!
do_main_program()
Code: Select all
supervisorctl
Gokcer Gokdal
Re: Wifi Reconnect on drop
May seem a silly question, but how do you find out if the Pi has drop/ed a wifi connection?
I can only connect to my pi over SSH, and for some reason, it occasionally won't connect, so i have no option to pull the power and hope it doesn't corrupt!
This script looks good, but don't actually know what is happening thus am nervous to give this a try without understanding that this is the problem first.
I'm using a static IP on my Pis wifi, would this make any difference to this script?
I can only connect to my pi over SSH, and for some reason, it occasionally won't connect, so i have no option to pull the power and hope it doesn't corrupt!
This script looks good, but don't actually know what is happening thus am nervous to give this a try without understanding that this is the problem first.
I'm using a static IP on my Pis wifi, would this make any difference to this script?
Re: Wifi Reconnect on drop
I have tried to use a cronjob to run the following script but it seems to fail and I can't figure out why the file is chmod 755
This is the output of the script (i'm redirecting cronjobs output to a log file)
It seem others here have got similar scripts working so i'm probably missing something simple
Code: Select all
#!/bin/bash
#This script checks if there is an active connection to wifi and if not turns off and on the wifi to force a reconnect
if ifconfig wlan0 | grep -q "inet addr:" ; then
sleep 1
else
echo "Network connection down! Attempting reconnection."
ifdown wlan0
ifup wlan0
sleep 1
fi
Code: Select all
/home/pi/.automatic/wifi-reconnect.sh: line 4: ifconfig: command not found
Network connection down! Attempting reconnection.
/home/pi/.automatic/wifi-reconnect.sh: line 8: ifdown: command not found
/home/pi/.automatic/wifi-reconnect.sh: line 9: ifup: command not found
Re: Wifi Reconnect on drop
You could take a look here for a pretty reliable solution. https://github.com/dweeber/WiFi_Checkreggie4 wrote:May seem a silly question, but how do you find out if the Pi has drop/ed a wifi connection?
I can only connect to my pi over SSH, and for some reason, it occasionally won't connect, so i have no option to pull the power and hope it doesn't corrupt!
This script looks good, but don't actually know what is happening thus am nervous to give this a try without understanding that this is the problem first.
I'm using a static IP on my Pis wifi, would this make any difference to this script?
A favourite way to get your wifi to fail intermitently is to use an inadequate power supply, so don't skimp on buying a decent supply. Depending on your wifi adaptor there are ways to stop it going down in the first place. Disabling power management for a start.
If you use a Realtek rtl8188cus or rtl8188eu based wifi adaptor it is possible to stop the wifi going down unless there is some sort of catastrophic failure. I use SSH to communicate with my Pi's and occasionally the SSH connection goes down due to an intermittent network problem but I can start another SSH connection without having to reboot or power down the Pi. The wifi on the Pi's stays active even when the network goes down.
MrEngman
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra
Please post ALL technical questions on the forum. Please Do Not send private messages.
Please post ALL technical questions on the forum. Please Do Not send private messages.
-
- Posts: 6
- Joined: Sat May 24, 2014 11:38 pm
Re: Wifi Reconnect on drop
EXCELLENT!!!!!!
This is very important for a headless pi, and especially for unreliable wifi - and isn't unreliable wifi the exact kind that encourages a headless pi? (i.e. stealth downloader; connected apps for installation; unattended installation.
It works PERFECTLY and solves EXACTLY the problem it attempts to address.
I think the person who suggested a cron job
would prefer not to leave an app running in the background if it's not necessary - since his connection is usually up - and would thus rather ONLY open the app IF it's needed. I understand that.
In response to this - and defending Mr. Engman's wonderful script - the constant starting is more expensive on the CPU, I think, than this tiny app just sitting there and checking every minute.
To minimize CPU pull, you can check every 2 minutes or 5 minutes or only once an hour.
As for me, I can't find any good reason to check less than once a minute, and I'm adding a Date stamp to the ECHO'd info so I'll know HOW long it's up before it has to reconnect!
Thank you Dr. Engman!
pp
PS and forgot to mention that it's also excellent for when you are changing networks. i.e. I walk into a public library (or other available public WiFi location) and using a hotspot on an iPad, I add the network information to the Pi. now when I attempt to connect, if it fails, I want to be able to reconnect to it without having to constantly restart it!

This is very important for a headless pi, and especially for unreliable wifi - and isn't unreliable wifi the exact kind that encourages a headless pi? (i.e. stealth downloader; connected apps for installation; unattended installation.
It works PERFECTLY and solves EXACTLY the problem it attempts to address.
I think the person who suggested a cron job



To minimize CPU pull, you can check every 2 minutes or 5 minutes or only once an hour.

As for me, I can't find any good reason to check less than once a minute, and I'm adding a Date stamp to the ECHO'd info so I'll know HOW long it's up before it has to reconnect!
Thank you Dr. Engman!
pp

Re: Wifi Reconnect on drop
This problem may just be the difference between wpa_roam and wpa_conf in /etc/network/interfaces ?
I've seen some guides using wpa_conf:
will connect once, but won't attempt a reconnect on drop.
will reconnect on drop
I've seen some guides using wpa_conf:
Code: Select all
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Code: Select all
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
Android app - Raspi Card Imager - download and image SD cards - No PC required !
-
- Posts: 1
- Joined: Thu Sep 25, 2014 9:54 pm
Re: Wifi Reconnect on drop
Some users are having difficulty with the script, which I am running as a Crontab job
I noticed when testing the script that a command to down the wlan0 conection would not work without a sudo
the following script only works If I add a sudo to ifup.
A failed wlan0 connection always re establishes itself.
Anyone wanting to know if their cron is working could try the utility
Thanks, the post was very helpful
I noticed when testing the script that a command to down the wlan0 conection would not work without a sudo
Code: Select all
> ifdown --force wlan0
Code: Select all
#!/bin/bash
if ifconfig wlan0 | grep -q "inet addr:" ; then
sleep 60
else
echo "Network connection down! Attempting reconnection."
sudo ifup --force wlan0
sleep 10
fi
Anyone wanting to know if their cron is working could try the utility
Code: Select all
> htop
- Demontager
- Posts: 9
- Joined: Thu Apr 24, 2014 11:27 am
- Location: Ukraine
- Contact: ICQ Website
Re: Wifi Reconnect on drop
I bit modified original script and added few variables.
So no need to touch core script just define your settings at top. Also i found that dhclient spawning its clones trying to get IP address on running wifi interface and if no WI-FI AP available it spawns new copy of dhclient after timeout to get rid of this i added scanning to check if AP is available before making actual connecting.
Posted to blog as well http://nixtalk.net/wifi_autoconnect
So no need to touch core script just define your settings at top. Also i found that dhclient spawning its clones trying to get IP address on running wifi interface and if no WI-FI AP available it spawns new copy of dhclient after timeout to get rid of this i added scanning to check if AP is available before making actual connecting.
Posted to blog as well http://nixtalk.net/wifi_autoconnect
Code: Select all
#!/bin/bash
# auto reconnect to wifi
#
IFACE="wlan0"
NET="PUT_YOUR_WIFI_AP_HERE"
TIMEOUT="40"
while true ; do
if ifconfig $IFACE &>/dev/null && ifconfig $IFACE | grep -q "inet addr:"; then
echo "Network is up!"
sleep $TIMEOUT
elif ifconfig $IFACE &>/dev/null && iwlist $IFACE scan|grep $NET &>/dev/null; then
echo "Network connection down! WIFI AP available, connecting..."
ifup --force $IFACE
sleep $TIMEOUT
else
sleep $TIMEOUT
fi
done
Re: Wifi Reconnect on drop
Code in the first reply is good. Slightly re-worked to set up as an automated cron job:
1. Make a directory in /etc/ called wlan-monitor
2. Create a script file wlan-monitor.sh that monitors the wlan0 status:
Insert the following code in the file (press "i" and paste the following):
Save and quit. (ESC : wq ENTER)
3. Make the file executable:
4. Add the above file as a cron job run as root. You can do this manually, or use Webmin.
1. Make a directory in /etc/ called wlan-monitor
Code: Select all
sudo mkdir /etc/wlan-monitor
Code: Select all
sudo vi /etc/wlan-monitor/wlan-monitor.sh
Code: Select all
#!/bin/bash
if ifconfig wlan0 | grep -q "inet addr:" ; then
echo "Network connection up! Nothing to do."
else
echo "Network connection down! Attempting reconnection."
ifup --force wlan0
fi
3. Make the file executable:
Code: Select all
sudo chmod +x /etc/wlan-monitor/wlan-monitor.sh
Re: Wifi Reconnect on drop
This script works from consoleDweeber wrote:Script uses/checks for a lockfile, allows you to set what interface. Run from cron, I would do it every 5 minutes, but every 2 or whatever will work (example cron entry in code)
https://raw.github.com/dweeber/WiFi_Che ... WiFi_Check
and not work if runing from crontab -e (root user)
Code: Select all
*/1 * * * * /opt/scripts/wifi_check.sh > /tmp/wifi.log
Code: Select all
Jul 19 08:22:01 raspberrypi /USR/SBIN/CRON[5606]: (root) CMD (/opt/scripts/wifi_check.sh > /tmp/wifi.log)
The WiFi router on the page client list is empty
wifi.log
Code: Select all
# cat /tmp/wifi.log
Starting WiFi check for wlan0
Sun Jul 19 08:36:01 IRKT 2015
Performing Network check for wlan0
Network connection down! Attempting reconnection.
Current Setting:
Code: Select all
/opt/scripts/wifi_check.sh > /tmp/wificonsole.log
Code: Select all
# cat /tmp/wificonsole.log
Starting WiFi check for wlan0
Sun Jul 19 08:35:15 IRKT 2015
Performing Network check for wlan0
Network connection down! Attempting reconnection.
Current Setting:
Re: Wifi Reconnect on drop
if WiFi conected
script not visible IP address
run this command from console link and ip address visible:
Where problem?
Code: Select all
# cat /tmp/wifi.log
Starting WiFi check for wlan0
Sun Jul 19 08:53:01 IRKT 2015
Performing Network check for wlan0
Network connection down! Attempting reconnection.
Current Setting:
run this command from console link and ip address visible:
Code: Select all
# ifconfig wlan0 | grep "inet addr:"
inet addr:192.168.88.55 Bcast:192.168.88.255 Mask:255.255.255.0
Re: Wifi Reconnect on drop
I changed script WiFi_Check
add "sudo" for all commandsNow the script is working properly
add "sudo" for all commands
Code: Select all
#!/bin/bash
##################################################################
# A Project of TNET Services, Inc
#
# Title: WiFi_Check
# Author: Kevin Reed (Dweeber)
# dweeber.dweebs@gmail.com
# Project: Raspberry Pi Stuff
#
# Copyright: Copyright (c) 2012 Kevin Reed <kreed@tnet.com>
# https://github.com/dweeber/WiFi_Check
#
# Purpose:
#
# Script checks to see if WiFi has a network IP and if not
# restart WiFi
#
# Uses a lock file which prevents the script from running more
# than one at a time. If lockfile is old, it removes it
#
# Instructions:
#
# o Install where you want to run it from like /usr/local/bin
# o chmod 0755 /usr/local/bin/WiFi_Check
# o Add to crontab
#
# Run Every 5 mins - Seems like ever min is over kill unless
# this is a very common problem. If once a min change */5 to *
# once every 2 mins */5 to */2 ...
#
# */5 * * * * /usr/local/bin/WiFi_Check
#
##################################################################
# Settings
# Where and what you want to call the Lockfile
lockfile='/var/run/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='wlan0'
##################################################################
echo
echo "Starting WiFi check for $wlan"
date
echo
# Check to see if there is a lock file
if [ -e $lockfile ]; then
# A lockfile exists... Lets check to see if it is still valid
pid=`cat $lockfile`
if kill -0 &>1 > /dev/null $pid; then
# Still Valid... lets let it be...
#echo "Process still running, Lockfile valid"
exit 1
else
# Old Lockfile, Remove it
#echo "Old lockfile, Removing Lockfile"
rm $lockfile
fi
fi
# If we get here, set a lock file using our current PID#
#echo "Setting Lockfile"
echo $$ > $lockfile
# We can perform check
echo "Performing Network check for $wlan"
if sudo ifconfig $wlan | grep -q "inet addr:" ; then
echo "Network is Okay"
else
echo "Network connection down! Attempting reconnection."
sudo ifdown $wlan
sleep 5
sudo ifup --force $wlan
sudo ifconfig $wlan | grep "inet addr"
fi
echo
echo "Current Setting:"
sudo ifconfig $wlan | grep "inet addr:"
echo
# Check is complete, Remove Lock file and exit
#echo "process is complete, removing lockfile"
rm $lockfile
exit 0
##################################################################
# End of Script
##################################################################
Re: Wifi Reconnect on drop
or not change script and run script from cron with sudo
or with test
/tmp is tmpfs
Code: Select all
*/1 * * * * sudo /opt/scripts/wifi_check.sh >> /tmp/wifi_check.log
Code: Select all
*/1 * * * * test -e /opt/scripts/wifi_check.sh && sudo /opt/scripts/wifi_check.sh >> /tmp/wifi_check.log
Re: Wifi Reconnect on drop
Thanks MrEngman for your script. Works perfectly for me and has solved a big problem.
Re: Wifi Reconnect on drop
Checking the output of ifconfig did not work for me. However switching that with a test ping to google.com did the trick.
Code: Select all
#!/bin/bash
while true ; do
ping -c1 google.com
if [ $? -eq 0 ]; then
echo "All good."
sleep 60
else
echo "Network connection down! Attempting reconnection."
ifup --force wlan0
sleep 10
fi
done
-
- Posts: 2
- Joined: Wed Nov 25, 2015 6:39 am
Re: Wifi Reconnect on drop
Here's the script I put together after reading through the examples here.
It checks ifconfig, and if there is an IP address, then it pings Google's DNS twice. If either fail, it attempts to reset with ifup.
If everything is okay, output from the command is silent.
It checks ifconfig, and if there is an IP address, then it pings Google's DNS twice. If either fail, it attempts to reset with ifup.
If everything is okay, output from the command is silent.
Code: Select all
#!/bin/bash
if ifconfig wlan0 | grep -q "inet addr:" ; then
ping -q -c2 8.8.8.8 > /dev/null
if [ $? -eq 0 ]; then
exit 0
else
echo "Network connection down! Attempting reconnection."
ifup --force wlan0
fi
else
echo "Network connection down! Attempting reconnection."
ifup --force wlan0
fi
-
- Posts: 906
- Joined: Tue Dec 15, 2015 4:55 pm
- Location: Detroit, MI USA
- Contact: Website
Re: Wifi Reconnect on drop
Is there a thing where the pi (zero, in this case) starts disconnecting if you aren't using the wifi? Although I'm running one of these scripts (two scripts up, I think) with cron. 5 minutes sometimes left me waiting for the reconnect before I could open/close my garage door. Changed it to one minute and had almost perfect luck for about a month. Then it seems my connection is pretty much down 100% of the time I first attempt to log in (I have an app that ssh handshakes and sends the command to run my c program), and I have to wait about the full minute. The connection ALWAYS does come back after a minute so I believe cron and the script must be working, but I don't understand what happened that I went from dropping connection every few minutes to pretty much within a few seconds of the reconnect script. I have made no changes since it was first working almost perfectly. Should I just set cron to run it every 15 seconds so I don't have to wait as long? Seems ridiculous but if I have to I have to...any negatives to doing so?
And for us simple folk - what is the difference between the last couple of scripts? What makes is one preferable to the other?
And for us simple folk - what is the difference between the last couple of scripts? What makes is one preferable to the other?
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics
Re: Wifi Reconnect on drop
What chip is your wifi adapter using? I had a realtek model that used flaky drivers that would drop and require a power cycle to get going again. It was kinda random on uptime, so it could go hours to days sometimes.ElEscalador wrote:Is there a thing where the pi (zero, in this case) starts disconnecting if you aren't using the wifi? Although I'm running one of these scripts (two scripts up, I think) with cron. 5 minutes sometimes left me waiting for the reconnect before I could open/close my garage door. Changed it to one minute and had almost perfect luck for about a month. Then it seems my connection is pretty much down 100% of the time I first attempt to log in (I have an app that ssh handshakes and sends the command to run my c program), and I have to wait about the full minute. The connection ALWAYS does come back after a minute so I believe cron and the script must be working, but I don't understand what happened that I went from dropping connection every few minutes to pretty much within a few seconds of the reconnect script. I have made no changes since it was first working almost perfectly. Should I just set cron to run it every 15 seconds so I don't have to wait as long? Seems ridiculous but if I have to I have to...any negatives to doing so?
And for us simple folk - what is the difference between the last couple of scripts? What makes is one preferable to the other?
The connection behavior might have changed on yours because of a kernel/driver update, though those usually should make it better not worse.