fadushin / esp8266

This repository contains source code for the ESP8266.
BSD 2-Clause "Simplified" License
73 stars 22 forks source link

Timer events with uhttpd possible? #4

Closed MortenGuldager closed 6 years ago

MortenGuldager commented 7 years ago

Probably a feature request, but here goes anyway:

Any way with uhttpd to get some code executed say 15 seconds after a post request has been processed ?

I tried to read my way through the uasyncio code, but got lost. Apparently my python-fu isn't strong enough :(

fadushin commented 7 years ago

Potentially, yes, if you install an API handler, something like:

# my_handler.py
class Handler:
    def __init__(self):
        pass

    def get(self, api_request):
        # add a timer here
        return {}

and then

>>> import my_handler
>>> import http_api_handler
>>> api_handler = http_api_handler.Handler([(['test'], my_handler.Handler())])
>>> import uhttpd
>>> server = uhttpd.Server([('/api', api_handler)])
>>> server.run()

and then from a shell on a computer:

prompt% curl http://ip-address-of-the-server/api/test
fadushin commented 7 years ago

@MortenGuldager Let me know if this works.

MortenGuldager commented 7 years ago

@fadushin it sure could be made working, but I had hoped for a way to handle it from within the device/micropython itself. I would prefer the solution to be self contained.

Imagine I was making an automated driveway gate. I send a request and gate opens. a few moments later I would like it to close again, without me to send a close request. I cant simply let the code sleep while waiting, because I would like the alarm center to be able to send status requests too, these needs to be answered instant

costastf commented 6 years ago

Hi @MortenGuldager, not sure if this is relevant any more for you but the code that @fadushin mentions for you is run on the device through micropython itself. If you need a more details example of how you could have things working have a look at a project I have built using @fadushin 's amazing library that manages a relay through an api https://github.com/costastf/wemos_relay_webserver.git . You could very easily tweak that to work for what you need. If you need any help reach out if you want.