Hello,
I would like to use ESP32 (with micropython) and picoweb to made some thermostat. I will get and set temperature via picoweb "web app". To communicate I will use simple json messages... but I need to check temperature every e.g. 2 minutes, compare it to stored one and turn on or off my heater.
So my idea is:
in file "init.py" I will add second scheduled job:
loop.create_task(asyncio.start_server(self._handle, host, port))
loop.create_task(check_temperature())
then define this function (outside WebApp class) and put some code to read temperature and turn on/off heater
async def check_temperature():
while True:
#check temperature
# some IFs
# turn on/off heater
await asyncio.sleep(120)
And the question is... Is this the right way to do this? Any better solution?
Best regards,
MvincM
Picoweb and temperature read in the loop (uasyncio)
- MostlyHarmless
- Posts: 166
- Joined: Thu Nov 21, 2019 6:25 pm
- Location: Pennsylvania, USA
Re: Picoweb and temperature read in the loop (uasyncio)
I am aware that this is an old thread, but I have been wondering the same lately and none of the "web server" packages out there seem to have a clear answer to that question, which is
"how do I combine my uasyncio coros with a web server?"
For now I created https://github.com/pfalcon/picoweb/pull/59
It would be one clean way.
Regards, Jan
"how do I combine my uasyncio coros with a web server?"
For now I created https://github.com/pfalcon/picoweb/pull/59
It would be one clean way.
Regards, Jan
- MostlyHarmless
- Posts: 166
- Joined: Thu Nov 21, 2019 6:25 pm
- Location: Pennsylvania, USA
Re: Picoweb and temperature read in the loop (uasyncio)
Actually that change is entirely obsolete.
Digging further I realized that the uasyncio event loop is a global thing, not an instance created by get_event_loop(). This means that the right way to do this is:
Regards, Jan
Digging further I realized that the uasyncio event loop is a global thing, not an instance created by get_event_loop(). This means that the right way to do this is:
Code: Select all
loop = asyncio.get_event_loop()
loop.create_task(do_something())
app.run(debug = True, host = "0.0.0.0", port = 80, log = log)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
New uasyncio version
Just FYI there is a new version of uasyncio in the pipeline which is based on CPython 3.8 asyncio syntax. In normal programming the event loop is completely ignored. Applications are started with
The new version is at quite an advanced stage of development and hopefully will be merged soon. In asyncio V3.8 docs the use of the event loop is deprecated.
Code: Select all
import uasyncio as asyncio
asyncio.run(coroutine())
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
- MostlyHarmless
- Posts: 166
- Joined: Thu Nov 21, 2019 6:25 pm
- Location: Pennsylvania, USA
Re: New uasyncio version
Thanks for the heads up. I probably should merge that into a local branch and use that.pythoncoder wrote: ↑Thu Dec 05, 2019 3:24 pmJust FYI there is a new version of uasyncio in the pipeline which is based on CPython 3.8 asyncio syntax. In normal programming the event loop is completely ignored. Applications are started with
- MostlyHarmless
- Posts: 166
- Joined: Thu Nov 21, 2019 6:25 pm
- Location: Pennsylvania, USA
Re: New uasyncio version
Looking good. Here is the complete example (slightly modified example_webapp2.py from picoweb):MostlyHarmless wrote: ↑Thu Dec 05, 2019 4:43 pmThanks for the heads up. I probably should merge that into a local branch and use that.
Code: Select all
#
# This is a picoweb example showing a web page route
# specification using view decorators (Flask style)
#
import logging
import picoweb
import uasyncio as asyncio
app = picoweb.WebApp(__name__)
@app.route("/")
def index(req, resp):
yield from picoweb.start_response(resp)
yield from resp.awrite("I can show you a table of <a href='squares'>squares</a>.\r\n")
@app.route("/squares")
def squares(req, resp):
yield from picoweb.start_response(resp)
yield from resp.awrite("Oh well, I lied\r\n")
async def do_something():
count = 0
while True:
await asyncio.sleep(2)
count += 1
print("doing something", count)
logging.basicConfig(level=logging.INFO)
applog = logging.getLogger("picoweb")
asyncio.create_task(do_something())
app.run(host = "0.0.0.0", port = 80, log = applog)
picoweb's run() still uses get_event_loop(), but that's just confirming that the backwards compatibility is working in a mixed usage environment.
Pretty cool stuff!
Regards, Jan
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: New uasyncio version
A good way to try it out; the final implementation is up for grabs. I suggested a Python package so synchronisation primitives may be loaded independently, saving RAM where they are not required. The doc also proposes other changes.MostlyHarmless wrote: ↑Thu Dec 05, 2019 6:26 pm...
I symlinked the uasyncio.py from extmod to ports/esp32/modules. Not sure that is how it was intended, but it was easy and works...
@Damien also mentioned implementing parts of it in C for performance - although even in Python performance is pretty good
We will see what he decides.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.