The Raspberry Pi sometimes disconnects from the WiFi.
The main reason for this could be low power. Make sure there's enough by running this command:
/bin/dmesg | grep 'Under-voltage detected'
If you have a good power supply, there are two ways around this problem.
Turn off the WiFi power management by running this command:
sudo /sbin/iwconfig wlan0 power off
Insert this below command in crontab by running crontab -e to disable it automatically on startup:
@reboot /bin/sleep 10 && sudo /sbin/iwconfig wlan0 power off
I wrote a simple script to work around this problem. It runs every 5 minutes and reconnects the Raspberry Pi to the WiFi network if it is disconnected.
# WiFi connectivity test (and reconnect if needed)
# Source: https://feriman.com/automatically-reconnect-to-wifi-on-raspberry-pi/
# Check the connectivity
if ! ping -c2 220.127.116.11 > /dev/null; then
# Shut down the wlan0 adapter if the network is not reachable
ifconfig wlan0 down
# Sleep 2 seconds to be sure
# Start the wlan0 adapter again
# It will reconnect to the WiFi automatically
ifconfig wlan0 up
# Sleep 10 seconds to be sure
# Put here all services with network dependencies
systemctl restart smbd ssh rpimonitor rtorrent apache2
Paste this script into a file and give execute permission:
chmod +x /home/pi/reconnect-wifi.sh
And schedule it in the root crontab:
*/5 * * * * /home/pi/reconnect-wifi.sh
That's all. If you have any other solution, leave a comment below.
If you're looking for Raspberry Pi monitoring, click here