Using Blynk with uasyncio #11765
-
Not really sure if this is the right place to seek help..... I am (trying) to use Blynk with uasyncio to stop lack of wifi / internet / Blynk connection roadblocking the rest of my app. Basically I would like my app to run normally while Blynk tries re-connecting (inc wifi connection if necessary) in the background. The below code almost(?) works. If I remove the Blynk write_handlers and don't use blynk.virtual_write either - it starts and restarts the wifi +/- Blynk cloud connection as needed, without blocking the rest of the code. First problem is blynk.run() (which comes from a class or library) is not available - or rather I think it runs, but is not usable..... therefore blynk.virtual_write does not work. I think this is an issue of me not understanding uasyncio. 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 (first) 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....
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Unfortunately I have no experience of Blynk but I assume it is useless without a WiFi connection. Looking at your code I think there is a hazard whereby you might create more than one concurrent instance of the same task; this will probably cause chaos. The way I would design something like this is to have a However this (and any solution) comes with a huge caveat. It will only work if Blynk periodically yields control to the scheduler, or if its interface consists of rapidly returning routines. If anything in Blynk blocks, |
Beta Was this translation helpful? Give feedback.
-
Hi Peter 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. It looks like Blynk is not blocking anything with uasyncio :-) 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. I wonder if there is a way I can do everything inside the maintainer loop?
|
Beta Was this translation helpful? Give feedback.
-
There are two issues here, the detail of the code and the overall logic. Regarding the detail, I would get rid of the globals replacing them with bound variables of classes. If you do have globals, the global blynk
global f
global h
global i
wlan = network.WLAN(network.STA_IF) The Another point of detail is that the Regarding the logic, I would regard your code as a proof of concept: you have demonstrated that there are no fundamental problems such as blocking. I would rewrite the maintainer function as a class method and ensure that its only purpose is maintaining a WiFi connection. When a connection is established it launches tasks that use the connection. When WiFi goes down, it cancels those tasks. |
Beta Was this translation helpful? Give feedback.
-
Hi Peter Thank you - I will look at this again with those items on board! Andrew |
Beta Was this translation helpful? Give feedback.
Unfortunately I have no experience of Blynk but I assume it is useless without a WiFi connection. Looking at your code I think there is a hazard whereby you might create more than one concurrent instance of the same task; this will probably cause chaos.
The way I would design something like this is to have a
maintainer
task which runs continuously whose job it is to create and maintain a WiFi connection (connections can drop out). When a connection is established, it starts Blynk and once Blynk is running, tasks that use it are started. If the WiFi connection is lost,maintainer
cancels the Blynk-dependent tasks then cancels the Blynk task.However this (and any solution) comes with a hug…