Wifi connected pi, occasionally loses connection, being unable to upload data

HI, I’m trying to figure out what needs restarting when my pi suddenly disappears from the network and then I get the emails later about the unit not reporting

It’s running on debian stretch, is there a script I can run to look for some sort of disconnections and have it automattically reboot the pi (I don’t care about pi uptime) or restart the wifi or something???

Here you go :wink:

checkwifi.sh

Store this script in /usr/local/bin/checkwifi.sh.

ping -c4 192.168.1.1 > /dev/null

if [ $? != 0 ]

then

sudo /sbin/shutdown -r now

fi

Change the IP on the first line to the IP of your router, or some other device on your network that you can assume will be always online.

First step is to ping your IP.

On line three, the $? represents the exit code of the previous command, in this case the ping.

If it failed (not 0), the script assumes something’s wrong with the wireless connection and just reboots.

Make sure the script has the correct permissions to run (thanks for the tip Jason Reibelt)

sudo chmod 775 /usr/local/bin/checkwifi.sh

crontab

SSH into the Raspberry Pi and open up the crontab editor by typing crontab -e.

Add the following line:

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

This runs the script we wrote every 5 minutes as sudo (so you have permission to do the shutdown command), writing its output to /dev/null so it won’t clog your syslog.

It’s a little crude but it will reboot your Pi when there is no network connection.

If the issue is dhcpcd … this is a fix (you can at the same time use the bruteforce thing posted above).

https://github.com/wiedehopf/adsb-wiki/wiki/Raspbian-dropping-off-the-network-potential-workaround

Not that I don’t necessarily like using a hammer, but I did this one… so… this post will shut after a year with no replies… let’s see if it can run for a year :slight_smile:

Thanks very much, I’ll try the dhcp one first and see how I get on

@J604, I was having similar sounding problems with one of my setups, and the issue boiled down to the location of the Pi wasn’t ideal for the WiFi connection to hold steady. At first I thought my Pi was rebooting since it would lose connection at seemingly random times, but eventually connection was restored. However, I discovered that wasn’t the case (running top in an SSH shell will tell you how long since it last rebooted). Poking a bit I discovered that my Pi was too close to a large metal pipe that was apparently impacting its WiFi connectivity; moving the Pi a few inches completely resolved the problem. (Perhaps adding an external WiFi antenna would have helped too, but that would have been more work). So perhaps see if relocating the Pi yields a more stable WiFi connection.

@wiedehopf - is there a way to “reboot” the wifi connection with restarting the whole machine? A full reboot seems like a bit of overkill, but perhaps that’s the easiest path towards achieving a new WiFi attempt.

restarting dhcpcd will likely do that.

might want to have a 2ndary logic to reboot if it doesn’t take.

I have this but it reboots as trying to restart dhcpcd is not that reliable i think:
https://github.com/wiedehopf/adsb-scripts/wiki/pingfail

It wouldn’t reboot as often as it will check every 5 min and only reboot if it fails twice in succession: https://github.com/wiedehopf/adsb-scripts/blob/master/pingfail.sh

1 Like

I’m not sure if it’s a location issue, it’s a floor above (wood frame) where my wifi router is… I just did a crude test using wget and a big file from archive.org and it seemed to pull it down pretty quickly, but I’ll bear it in mind!

I’m not an expert here, but I do believe it’s actually possible that being too close to the router can lead to issues at times (signal saturation?). That said, it might be worth just moving the Pi down closer to the router (same floor or same room), and see if connectivity changes. That might degrade the ADSB signal (if you’ve got it up a floor for better reception), but it’s probably worth it for the debugging.

How often are these dropouts? Is this every few minutes, once an hour, every couple days? I’d think you’d want to leave it in a better (?) location (relative to the router), for probably 5-6x the expected cycle of disconnects. if it doesn’t drop at all, then I’d expect you found the problem (location) - and then need to either figure how to better connect (ethernet, or Wifi antenna), or move it. if that doesn’t fix it, well then back to a software solution, perhaps.

It’s every couple of days… you know, those extremely infuriating to debug kind of issues, especially when it’s just a hobby and I don’t have the time to look at it in depth :slight_smile:

Well, I had my first real failure and reboot with your script this morning. Thank you!

The single most common cause of intermittent Pi’s is the PSU getting old.
Try swapping to another and see if the behaviour changes.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.