Web server In/out + physical button control via gpio

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
anakaine
Posts: 10
Joined: Wed Mar 31, 2021 11:41 am

Web server In/out + physical button control via gpio

Post by anakaine » Fri Apr 02, 2021 6:09 am

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.
Last edited by anakaine on Sun Apr 18, 2021 7:04 am, edited 1 time in total.

mrsnail
Posts: 5
Joined: Thu Apr 01, 2021 9:00 am
Location: Vienna, Austria

Re: Web server + physical button control

Post by mrsnail » Sun Apr 04, 2021 2:14 pm

This is exactly what I want to do, too! Highly appreciate it if someone can explain :)

User avatar
russ_h
Posts: 88
Joined: Thu Oct 03, 2019 2:26 am
Contact:

Re: Web server + physical button control

Post by russ_h » Sun Apr 04, 2021 5:17 pm

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/

anakaine
Posts: 10
Joined: Wed Mar 31, 2021 11:41 am

Re: Web server + physical button control

Post by anakaine » Mon Apr 05, 2021 3:42 am

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.

User avatar
russ_h
Posts: 88
Joined: Thu Oct 03, 2019 2:26 am
Contact:

Re: Web server + physical button control

Post by russ_h » Tue Apr 06, 2021 12:12 am

You are correct, this example does not do what you are looking for.

anakaine
Posts: 10
Joined: Wed Mar 31, 2021 11:41 am

Re: Web server + physical button control

Post by anakaine » Tue Apr 06, 2021 8:46 am

Thanks anyway

anakaine
Posts: 10
Joined: Wed Mar 31, 2021 11:41 am

Re: Web server + physical button control

Post by anakaine » Sun Apr 18, 2021 7:04 am

It's a bit of a shame, but it looks like this would be easier in C with the link provided.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

Post by pythoncoder » Mon Apr 19, 2021 9:30 am

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.
Peter Hinch
Index to my micropython libraries.

anakaine
Posts: 10
Joined: Wed Mar 31, 2021 11:41 am

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

Post by anakaine » Thu Apr 29, 2021 7:51 pm

Thanks @pythoncoder. Yes, doing both the hardware change response and also the web based checking is the crux of the issue.

jomas
Posts: 59
Joined: Mon Dec 25, 2017 1:48 pm
Location: Netherlands

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

Post by jomas » Sat May 01, 2021 11:00 am

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).

Post Reply