-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reconnect after loss signal. #36
Comments
Probably the simplest way to reattempt a connection until success is by using a try/catch block like so: def connect_blynk():
'''Connects to Blynk Server'''
try:
blynk = BlynkLib.Blynk(auth)
except Exception as e:
print("Failed to connect to Blynk, trying again...")
connect_blynk()
print("Connected to Blynk Server")
return blynk As for reconnecting to wireless, that is not "hidden code" so to speak, but is actually the esp8266 firmware that automatically reconnects to the previously connected network upon boot. An easy way to handle this is to make use of the For example: wifi = network.WLAN(network.STA_IF)
if wifi.isconnected():
print("Connected to WiFi")
return wifi
print("Connecting to Wifi...")
wifi.active(True)
wifi.connect(ssid, password)
while not wifi.isconnected():
pass
print("Wifi Connected")
return wifi It has been a minute since I worked with an esp8266 with the BlynkLibrary, but I do remember there being a possible issue when trying to connect to Blynk if the esp automatically reconnected to wifi. I believe I just forced the esp to disconnect and reconnect before attempting the Blynk connection as a quick workaround, something like so: def wifi_connect():
wifi = network.WLAN(network.STA_IF)
if wifi.isconnected():
wifi.disconnect()
return wifi_connect()
...the rest of the connect code, followed by attempting to connect to Blynk |
Please provide details for above code, I am still not able to handle disconnection of wifi. |
@vshymanskyy Still no response, please help as I have tried all the options to handle disconnect of wifi , but seems difficult. I expect prompt support from Blynk team for potential customers. |
For esp8266, we recommend using Arduino for potential customers. ESP8266 lacks resources to run a complex, micropython-based IoT project |
@vshymanskyy , Hi Thanks for response, but is that the case for ESP32 as well. |
@Tarun75
Which part of the code I provided would you like details for? |
Hi. Using Blynk, Micropython and asyncio on a RP2040. My app works fine without Blynk - but Blynk would be a nice addition - but it is important the app keeps working without it. My app normally does not require a WiFi connection. I am trying to implement code to handle:
Below is a simplified version of my app - but if I can get the above working here, it will also work in my full app. @BradenM I am afraid I am at a loss to implement your suggestions at the start of this post - are you able to look at my code and offer some guidance? Thank you!
|
Try this:
As for the wifi issue, try to add this:
I have not reviewed the rest of your code. |
Thank you! I tried incorporating your suggestions (as below), and I tested these two situations. Any help how to proceed really appreciated - I am sure this will also help others using Blynk with micropython :-)
What can I do here to occasionally re-try the connection but continue with the rest of the code in the meantime?
This keeps going for a minute or two, then eventually errors out:
Once it errors out, how can I occasionally keep trying to connect, but in the meantime continue with the rest of the code? My code with your suggestions added:
Thank you! |
I cannot test your code as I’m away from my PC throughout this weekend but, I’m surprised the code compiles as you added the ‘R1_timeout’ function which is not needed (I edited my post).
Where
Try to organize better the code 😉 |
Hi... Thanks again.... would be really appreciated if you could have a look sometime when you are in front of a PC.... I have edited this post - I had an indent issue - this code runs. But if I turn off the wifi I get this error after a minute or so..... and other disconnect or no wifi scenarios fail as well.... The Blynk write_handler() fails with:
Current code:
|
Hi,
output:
|
Thank you! I think the simplest solution to what I am trying to achieve is to run the async connect() loop anytime there is no wifi network, no wifi connection or no internet / Blynk connection. So long as I have a flag blynk_connection = True / False, I think this is workable. So if I connect to a wifi with internet all is good.
This check seems to never be triggered:
This is the current code, including starting to think about restarting the connect() loop if there is no Blynk connection:
Additionally, sometimes during testing I get this error - happens with no changes to the code - and seems to require Thonny restarted, RP2040 power cycled, and a few goes to eliminate. Notice it does not start with 'connect loop running':
|
As I said, I didn't check your whole code, I just wanted to show you how to connect to Blynk with mpython. I don't use uasyncio with Blynk but, I do know the lib works as I'm using it 14/7 with a mqtt server connection. As for "ERROR -6", it means that your devise has no connections to the router/internet thus connect() returns False --> "name 'blynk' isn't defined". I remind you, Blynk doesn't like delays within a loop. Probably this' what's causing your problem. My suggestion: Make Blynk work without uasyncio.
output:
|
Thanks again. I have had it generally working without asyncio.... but asyncio is a key to this project..... And I should have been clear that it was working with asyncio if I had a stable WiFi connection - I am trying to solve lack of wifi / internet / Blynk connection and still have the rest of the app continue running normally and automatically try reconnecting - sorry about that! The below code almost(?) works. If I remove the Blynk write_handlers and don't use blynk.virtual_write either - it restarts the wifi + Blynk etc. First problem is blynk.run() is not available - or rather I think it runs, but is not usable..... so blynk.virtual_write does not work. Second problem is getting the write_handlers working - once 'blynk_final_ready == True'. Obviously what I currently have does not work as it is only runs once (and the condition is false) - but I cant see a method of running these once 'blynk_final_ready == True'. I tried putting them inside a function, and that did not seem to work..... any idea how I can run (or activate) these conditionally? But I guess they need blynk.run() working first..... But I think the bigger issue is blynk.run() not correctly running (or is not usable after it runs?)
The result is close - blynk.virtual_write(4, voltage) does not work and of course the write_handler is not available....
|
I have made some progress... this code now works - I have tested with no wifi, wifi on / off / on, and internet connection on / off / on with wifi remaining connected. Peter on the micropython forum suggested a maintainer loop, which allowed me to get things going..... But the code is terrible - and I have ended up with stuff that should be in the maintainer loop in a new loop called test_loop. Please post back if you tidy it up to use it!
|
Hello
I cannot understand how to use this library working all the time.. When I loss signal its show me the error: BlynkLib.py line 240, in _write. and similar problems.. I do not know how to reconnect blynk correctly after loosing signal.. There is some hidden code which connect into the wifi automaticly, however Im not able to reconnect blynk because I got similar error.
Im using ESP8266 with micropython build.
The text was updated successfully, but these errors were encountered: