HTML Push Buttons only when pressed

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Polluxi
Posts: 2
Joined: Sat Jun 08, 2019 8:10 am

HTML Push Buttons only when pressed

Post by Polluxi » Sat Jun 08, 2019 8:48 am

Hello I am new in this Forum and new in this programming stuff. Maybe somebody here has an idea or can help me, how to code a push button on a webserver, that only drives a gpio as long as the button on the webpage is pressed. I found many simple code-snippets for button pressed -> LED on , button pressed one more time -> led off. But I need button pressed LED on, button released -> LED off. Is this possible to code this in micropython?

Thanks very much

Olaf

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: HTML Push Buttons only when pressed

Post by jimmo » Sat Jun 08, 2019 9:10 am

Hi Olaf,

The basic idea would be that you have a http request for every user action. So the snippets you've probably seen would send a request for the "onclick" event on the button, and the code running on the board would remember the state of the LED and invert it on each request.

Instead you can use the "onmousedown" and "onmouseup" events in HTML/JavaScript to send a different "on" or "off" request.

Maybe if you share the code you have so far we can help with specific suggestions.

Polluxi
Posts: 2
Joined: Sat Jun 08, 2019 8:10 am

Re: HTML Push Buttons only when pressed

Post by Polluxi » Sat Jun 08, 2019 7:08 pm

Hello Jimmo,

here you can see code to toggle LED with one or two buttons on and off:

viewtopic.php?t=1940#p10926

but i want to programm something that functions like a volume remote-control for a TV or HIFI set.
As long as i push the button the volume increases and on another button the volume decreases, for example.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: HTML Push Buttons only when pressed

Post by jimmo » Sat Jun 08, 2019 11:25 pm

Ok, just a note on that thread - as other people have commented, you'll have a much better time using an http server library rather than using sockets directly. See https://github.com/miguelgrinberg/microdot for an example library. And you can find some good MicroPython tutorials by the same author.

In your case, the "key repeat" behaviour can be implemented in the JavaScript by setting a timer on mouse down, then stopping that timer on mouse up. Look up setInterval() for javascript. Every time the timer is run, you send your "volume up" or "volume down" request.

Post Reply