Webserver aan/uitschakelen lamp

# webserver.py

import network
import socket
from machine import Pin

#Configure the pins as outputs
LED0 = Pin(21, Pin.OUT)
LED1 = Pin(22, Pin.OUT)

#Turn OFF the LEDs to start with
LED0.value(0)
LED1.value(0)

# The HTML code
html = “””<!DOCTYPE html>
<html>
<body>
<center><h1>MakePython ESP32 LED ON/OFF</h1></center>
<center><h2>MicroPython Web Server Example with 2 LEDs</h2></center>
<form>

<button name=”LED0″ button style=”color:green” value=”ON” type=”submit”>LED0 ON</button>
<button name=”LED0″ button style=”color=red” value=”OFF” type=”submit”>LED0 OFF</button><br><br>

<button name=”LED1″ button style=”color:green” value=”ON” type=”submit”>LED1 ON</button>
<button name=”LED1″ button style=”color:red” value=”OFF” type=”submit”>LED1 OFF</button>
</form>
</body>
</html>
“””

# This function connects to the Wi-Fi
def Connect_WiFi():
net = network.WLAN(network.STA_IF)
if not net.isconnected():
net.active(True)
net.connect(“BTHomeSpot-XNH”, “49345xyz”)

# Connect to local Wi-Fi
Connect_WiFi()
addr=socket.getaddrinfo(“0.0.0.0”,80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)

while True:
conn, address = s.accept()
request = conn.recv(1024)
request = str(request)
LEDON0 = request.find(“/?LED0=ON”)
LEDOFF0 = request.find(“/?LED0=OFF”)
LEDON1 = request.find(“/?LED1=ON”)
LEDOFF1 = request.find(“/?LED1=OFF”)
if LEDON0 != -1:
LED0.value(1)
if LEDOFF0 != -1:
LED0.value(0)
if LEDON1 != -1:
LED1.value(1)
if LEDOFF1 != -1:
LED1.value(0)
resp = html
conn.send(resp)
conn.close()

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *