Workaround for the Raspberry Pi's WiFi Driver Crashing
When keeping the Raspberry Pi Zero or Zero 2 or even other Raspberry Pis plugged in for weeks or months at a time, it can happen that you lose connectivity and get this or a similar error message from a problem with the wifi chip driver:
Nov 10 03:03:54 genmon kernel: [125997.788183] WARNING: CPU: 0 PID: 6804 at net/wireless/sme.c:752 __cfg80211_connect_result+0x3c0/0x410 [cfg80211]
Nov 10 03:03:54 genmon kernel: [125997.788187] Modules linked in: 8021q garp stp llc rtc_ds1307 brcmfmac brcmutil sha256_generic cfg80211 raspberrypi_hwmon hwmon rfkill snd_bcm2835(C) snd_pcm snd_timer snd bcm2835_codec(C) v4l2_mem2mem bcm2835_v4l2(C) bcm2835_mmal_vchiq(C) i2c_bcm2835 videobuf2_dma_contig v4l2_common videobuf2_vmalloc videobuf_mem uio_pdrv_genirq fixed uio i2c_dev2_memops videobuf2_v4l2 videobuf2_common videodev media vc_sm_cma(C) rpivid ip_tables x_tables ipv6
Nov 10 03:03:54 genmon kernel: [125997.788333] CPU: 0 PID: 6804 Comm: kworker/u8:2 Tainted: G WC 4.19.75-v7l+ #1270
Nov 10 03:03:54 genmon kernel: [125997.788336] Hardware name: BCM2835
This issue is also mentioned in several forums, including here.
If you had a screen and keyboard connected to the Rasbperry Pi, you'd notice that you lost your wifi address and with that connectivity to the internet or your local network. All you'd have to do to get wifi fully working again is
sudo ifconfig wlan0 down
sudo ifconfig wlan0 up
I automated this with a cronjob that runs once every 3 minutes. You can copy paste the following section into your Raspberry Pi shell, and it will set it up for you. Note that this is assuming your local ip address starts with 10. As in 10.0.1.22 for instance. If that's not the case, replace the 10. with 192. or whatever number is the beginning of a valid IP address in your wifi network:
sudo tee /usr/local/bin/wifi_fix.sh > /dev/null << 'EOF'
#!/bin/bash
status=`/sbin/ifconfig wlan0 | grep "inet 10."`
if [ ! -z "$status" ]; then exit 0; fi
echo "Detected wifi failure"
date
dmesg | grep __cfg80211_connect_result
/sbin/ifconfig wlan0 down
/sbin/ifconfig wlan0 up
echo "WiFi restored"
EOF
sudo chmod a+x /usr/local/bin/wifi_fix.sh
(sudo crontab -u root -l 2>/dev/null; echo "*/3 * * * * /usr/local/bin/wifi_fix.sh >> /var/log/wifi_fix.log 2>&1") | sudo crontab -u root -