-
Notifications
You must be signed in to change notification settings - Fork 3
/
Simple_led.py
64 lines (53 loc) · 1.47 KB
/
Simple_led.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
56
57
58
59
60
61
62
63
64
import ESPWebServer
import network
import machine
GPIO_NUM = 2 # Builtin led (D4)
# Wi-Fi configuration
STA_SSID = "MEE_MI"
STA_PSK = "PinkFloyd1969"
# Disable AP interface
ap_if = network.WLAN(network.AP_IF)
if ap_if.active():
ap_if.active(False)
# Connect to Wi-Fi if not connected
sta_if = network.WLAN(network.STA_IF)
if not ap_if.active():
sta_if.active(True)
if not sta_if.isconnected():
sta_if.connect(STA_SSID, STA_PSK)
# Wait for connecting to Wi-Fi
while not sta_if.isconnected():
pass
# Show IP address
print("Server started @", sta_if.ifconfig()[0])
# Get pin object for controlling builtin LED
pin = machine.Pin(GPIO_NUM, machine.Pin.OUT)
pin.on() # Turn LED off (it use sinking input)
# Handler for path "/cmd?led=[on|off]"
def handleCmd(socket, args):
global ledData
if 'led' in args:
if args['led'] == 'on':
ledData["status"]="ON"
pin.off()
elif args['led'] == 'off':
ledData["status"]="OFF"
pin.on()
ESPWebServer.ok(socket, "200", args["led"])
else:
ESPWebServer.err(socket, "400", "Bad Request")
# Start the server @ port 8899
ESPWebServer.begin(8899)
# Register handler for each path
ESPWebServer.onPath("/cmd", handleCmd)
ESPWebServer.setDocPath("/www2")
ledData = {
"status":"Off",
}
ESPWebServer.setTplData(ledData)
try:
while True:
# Let server process requests
ESPWebServer.handleClient()
except:
ESPWebServer.close()