Page 1 of 2

Web server In/out + physical button control via gpio

Posted: Fri Apr 02, 2021 6:09 am
by anakaine
Edit:
After a bit more hunting, I found this link here. It demonstrates, using arduino C code how to have an async webserver that can take input from a url parameter and from a physical button. This is what I'm after - but using python!


Here's a question that's been asked, but whose responses mostly amounted to "wait for new versions".

I'm looking for a way to accept http input (eg http://ip.ip.ip.ip/page?param1=stuff&param2=otherstuff), whilst also keeping an eye on inputs from gpio. The gpio could be a changed state, such as a physical button press, or sensor pulling high/low, ie something that's generally simple (was x, now y).

It seems the preferred way to do this is by somehow mixing a web server (picoweb?), with uasyncio (how?!), and then making a decision on either polling the gpio for change (store state in a global, then check each loop?), or using an interrupt. Peters commentary here suggests that polling would be better on ESP8266 & ESP32.

I guess the question is, has anyone a practical example of running a very small web server like pico web to detect url params, and also checking gpio state / being interrupted? This is just a bit too much for me to bite off. I can set up picoweb to check for incoming url params, but have no idea how to even begin watching for gpio state change, particularly once we start talking about uasyncio.

Basically I'd like to be able to monitor for incoming network instructions, and also physical interaction.

Re: Web server + physical button control

Posted: Sun Apr 04, 2021 2:14 pm
by mrsnail
This is exactly what I want to do, too! Highly appreciate it if someone can explain :)

Re: Web server + physical button control

Posted: Sun Apr 04, 2021 5:17 pm
by russ_h
Here is a quick and dirty example I've been using at my office to monitor temperatures and whether or not I forgot to turn off the lights. I was in a hurry when I wrote it so it's not an example of good python coding but it works well enough that I haven't rewritten it.

https://gist.github.com/russhughes/428e ... a748e6022d it was based on a tutorial from https://randomnerdtutorials.com/

Re: Web server + physical button control

Posted: Mon Apr 05, 2021 3:42 am
by anakaine
Thanks Russ. I may be misinterpreting, but it looks like you're not actually checking for some external trigger here. You are awaiting a webserver parameter input which is great (and tuts available), but I was hoping to also be able to keep an eye on a GPIO changing with something momentary (eg button push, IR input, open close sensor, etc - anything that may be point-in-time and could be missed by polling in while True).

Ie, input could come from:
Web server params
Some transient physical condition on a gpio

Then in some other function, we can decide what to do with the info - eg get/post request, mqtt, etc. There's plenty of tuts for that gear though. It's really the concurrent monitoring of an incoming network request and a physical transient state change that's the head scratcher.

Re: Web server + physical button control

Posted: Tue Apr 06, 2021 12:12 am
by russ_h
You are correct, this example does not do what you are looking for.

Re: Web server + physical button control

Posted: Tue Apr 06, 2021 8:46 am
by anakaine
Thanks anyway

Re: Web server + physical button control

Posted: Sun Apr 18, 2021 7:04 am
by anakaine
It's a bit of a shame, but it looks like this would be easier in C with the link provided.

Re: Web server In/out + physical button control via gpio

Posted: Mon Apr 19, 2021 9:30 am
by pythoncoder
I haven't responded to this query because I lack web programming experience: I was hoping someone better qualified would pop up.

I feel sure this can be done using uasyncio with a uasyncio-based server such as PicoWeb. I can explain how to use uasyncio to respond to hardware changes (see my tutorial).

Unfortunately I haven't a clue how to push changes to the client.

Re: Web server In/out + physical button control via gpio

Posted: Thu Apr 29, 2021 7:51 pm
by anakaine
Thanks @pythoncoder. Yes, doing both the hardware change response and also the web based checking is the crux of the issue.

Re: Web server In/out + physical button control via gpio

Posted: Sat May 01, 2021 11:00 am
by jomas
pythoncoder wrote:
Mon Apr 19, 2021 9:30 am
Unfortunately I haven't a clue how to push changes to the client.
The "push changes to the client" is implemented in the html code that is served. It is some javascript that uses the function 'setInterval'.
The javascript function 'setinterval' will then refresh the page automatically every second. Since the html code for the button (on the webpage) is rendered dynamically it will show the current state of the led.

This should be very easy to implement in Micropython using picoweb because picoweb supports templating (for your dynamically generated button).