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
HTML Push Buttons only when pressed
Re: HTML Push Buttons only when pressed
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.
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.
Re: HTML Push Buttons only when pressed
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.
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.
Re: HTML Push Buttons only when pressed
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.
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.