MuriTG25
MuriTG25

Reputation: 11

Change the button value according to the state of the LED

I have a project from my college that is in Micro Python, but my question is about something related to HTML that will have to use JavaScript.
My project simulates a house that has lights controlled by the Raspberry Pi Pico W, through a website created by it.
My question is that I wanted to update the status of the light in the html, if it was turned off, I wanted the button to have the value of: On <name led>, and if it had turned on Off <name led>.
Code in python that check the state of the LED:

def luzEstaLigada(led):
    if led.value==1:
        return True
    else:
        return False

html page:

<!DOCTYPE html>
<html lang="pt-BR">
<head>
</head>
<body>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <br/>
    <form action="./liga-desliga1">
        <input type="submit" style="width: 400px;max-width: 95%;margin-left: 10px;margin-right: 10px;" value="Luz 1" />
    </form>
    <br/>
    <form action="./liga-desliga2">
        <input type="submit" style="width: 400px;max-width: 95%;margin-left: 10px;margin-right: 10px;" value="Luz 2" />
    </form>
    <br/>
    <form action="./liga-desliga3">
        <input type="submit" style="width: 400px;max-width: 95%;margin-left: 10px;margin-right: 10px;" value="Luz 3" />
    </form>
</body>
</html>

Complete project is here and the code is in main.py link here

Code in Javascript that change the value in the button, according to the state send by the code in Python

Upvotes: 0

Views: 48

Answers (1)

Chuckie Zhu
Chuckie Zhu

Reputation: 19

You may set a global variance to record the led's status.

When you toggle led, you should toggle the variance.

Let say there is a func called "LightLed" to light a led.

you can def a DIY func:

# some global variance
LedStatus = {
   "led1": False,
    "led2": False,
}

def LightLedDIY(whichled):
    LedStatus[whichled] = True
    LightLed(whichled)

Upvotes: 0

Related Questions