Handle temporary loss of WiFi
-
Hi,
I am using LoPy4 as a HTTP server to send data over LoRa. The setup is as follows:- LoPy is connected to a WiFi
- a device that wishes to send data over LoRa will send a request to the server using a pre configured static IP.
is there a good way for handling the loss of wifi and how does LoPy react to that?
I tried to lookup the docs and found nothing there mentioning this.Thanks!
-
You should be able to use
wlan.isconnected()to find out if the WiFi connection is still alive before using the socket. Another solution could be to use a try-except block around the section where you send data. There can be many exceptions associated with sockets, depending on the state of the connection and server, which might give you more information about what is going on in the event of a connection loss -
@Gijs I believe @Assem-Hasna has an interesting case where it's the LoPy which is acting as a server and receiving the data (before forwarding it over LoRa), so that probably changes a bit the logic involved.
Though of course something needs to happen on the device which sends to the LoPy, I suppose the main question here is whether the LoPy will automatically reconnect to the WiFi network.
-
The problem with the standard wifi connect example
wlan.connect(ssid='ssid', auth=(WLAN.WPA2, 'password')) while not wlan.isconnected(): machine.idle() print("WiFi connected succesfully")is that it is too polite, asks once for a connection then waits. Such code can hang, stuck in a wait loop that never exits. If I loose a wifi connection & need to reconnect I hammer it a bit harder
for i in range(10): if wlan.isconnected(): print("WiFi connected succesfully"); break wlan.connect(ssid='ssid', auth=(WLAN.WPA2, 'password')); print(i, end=' '); time.sleep(3) -
@kjm
there is atimeoutparameter in theconncet
e.g.wlan.connect(ssid='ssid', auth=(WLAN.WPA2, 'password', timeout=5000) -
-
@Gijs said in Handle temporary loss of WiFi:
Though, as @kjm noted in a different forum thread, the timeout is not recognized explicitely as an error in a try-except block. I just (still) need to convince someone here to merge my fix :)
But it should not generate an error.
If you looking for some "fix" then you must add new parameter to connect to run it sync/async.
If sync then throw an error if assync (as it is now) do not throw an error.
Becaouse i do now e.g. run connect with some timeout and do different things in the middle and then check state of connection.
If i need to wait for connection and be sure it is on i do it in the loop (and the loop with the timeout too ;-) ).while not wlan.isconnected(): machine.idle() # save power while waiting if chrono.read()>30: #30 sedcond timeout break -
@Gijs said in Handle temporary loss of WiFi:
I just (still) need to convince someone here to merge my fix :)Ah, the GateKeepers Gijs, what would we do without them!
-
@Assem-Hasna said in Handle temporary loss of WiFi:
a device that wishes to send data over LoRa will send a request to the server using a pre configured static IP.
It seems you have found your answer. But if connection robustness is what you need, the pre-configured static IP concerns me. If you convert it to a floating IP or DNS name with a low TTL, you will better handle a failure in that server. Alternately, configure multiple IPs in your device for multiple servers, and have it try each one until success. Only one of them needs to be alive at any one time.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login