-
Notifications
You must be signed in to change notification settings - Fork 0
/
blink.py
55 lines (46 loc) · 1.13 KB
/
blink.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import network
import socket
from machine import Pin
import time
# Wi-Fi configuration
ssid = 'YourWiFiSSID'
password = 'YourWiFiPassword'
# Connect to Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connection
while not wlan.isconnected():
time.sleep(1)
print('Connected to Wi-Fi')
print('IP:', wlan.ifconfig()[0])
# Set up LED
led = Pin(2, Pin.OUT)
led_state = False
# HTML template
html = """<!DOCTYPE html>
<html>
<body>
<h1>ESP32 LED Control</h1>
<p>LED is {}</p>
<a href="/toggle"><button>Toggle LED</button></a>
</body>
</html>
"""
# Set up web server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
request = conn.recv(1024)
request = str(request)
if request.find('/toggle') != -1:
led_state = not led_state
led.value(led_state)
response = html.format("ON" if led_state else "OFF")
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()