My old Raspberry Pi Zero W sometimes had problems restoring the WiFi connection when the AP was rebooted or reprovisioned. I don't know why Rasbian can't do this, but this workaround is my solution: I created a script that continuously tests the connection to the local gateway. If the gateway cannot be reached, the WiFi interface is restarted.
- Add the following line to
/etc/crontab
:
*/1 * * * * root /usr/local/bin/wifi_reconnect.sh
#
- Create bash script
/usr/local/bin/wifi_reconnect.sh
with this content:
!/bin/bash
#echo "Script runned @ $(date)" >>/var/log/wifi_reconnect
# The IP for the server you wish to ping (get default getway)
SERVER=$(/sbin/ip route | awk '/default/ { print $3 }')
#echo "> Server: ${SERVER}" >>/var/log/wifi_reconnect
# Specify wlan interface
WLANINTERFACE=wlan0
#echo "> WLAN Interface: ${WLANINTERFACE}" >>/var/log/wifi_reconnect
# Only send two pings, sending output to /dev/null
ping -I ${WLANINTERFACE} -c2 ${SERVER} >/dev/null
# If the return code from ping ($?) is not 0 (meaning there was an error)
if [ $? != 0 ]
then
echo "> WiFi doenst work. Restart!" >>/var/log/wifi_reconnect
# Restart the wireless interface
ip link set wlan0 down
ip link set wlan0 up
#else
#echo "> WiFi works. No restart" >>/var/log/wifi_reconnect
fi
I added some "echo to file" lines. Remove the # to log what the script does.
One thought on “Workaround for Raspberry Pi automatic WiFi/WLAN reconnect”