I think you might as well blame this on a physical problem with the signal, or weird behaviour of the router that can't be altered, or something else which cannot be fixed directly.
It sounds like it is not serious in the sense that this system is providing services to people and arbitrarily cutting them off. It's mostly idle in terms of network activity, so the cut-offs aren't interrupting anything, they're just leaving the system unreachable.
In which case you just have to make sure the pi knows when it has become disconnected, and get it to reconnect, which is the idea here:
How to automatically reconnect WiFi?
Although I wrote it, I wouldn't recommend using that exact script -- unless you like it, and note although it has evolved quite a bit in the past three years (and that wasn't the first version, either), I still use basically the same thing with everything. Like Milliways, I actually don't have many problems with Pi's; I just checked a Pi 3 that I left in a building several weeks ago, and the log for my reconnection script shows it hasn't had to reconnect at all since then (though there's a Pi 0 w/ a Ralink wifi adapter in the same building that's had to reconnect twice).
The system I have the most trouble with is a laptop with Realtek wifi (if you use linux, don't buy Realtek; I am not alone in this problem...). It will randomly disconnect while I'm using it unless I use an external adapter.
Here's the basic strategy:
Pick an IP that should always be pingable. This could be the router, or it could be Google's public DNS servers at 22.214.171.124, which respond to pings. Or it could be the external nameserver provided via DHCP. Or a server of yours you trust, etc.
Pick an interval that's appropriate for your use case; it sounds like you could use a few minutes.
Ping the IP at that interval and check if it succeeds. You can do that easily enough in a shell script:
ping -c 1 126.96.36.199
if [ $? -ne 0 ]; then
# Ping failed, reconnect.
$? here is the exit status; you should be able to check that in whatever programming language you want to use (although this is one case where shell is probably the simplest).
Ping will return 0 when it succeeds and some positive integer otherwise. Note the
-c 1 switch; this is important (see
man ping for why). You could also try
-W if you want, just make sure they work the way you think they do.
When the ping fails, you should do two things:
Ensure that whatever networking service you are using is shut down.
Start the networking service again.
There could be an issue depending on the behaviour of the networking service when it fails to connect. As mentioned in the other answer, my preference is to disable the stock stuff and arrange it myself so the behaviour is totally predictable (keep trying and do not stop trying until a connection is successful), but this is perhaps a bit ignorant of me and you will likely be better off investigating how the stock stuff works (if it doesn't do this by default, it should be an option).