/
led.py
58 lines (54 loc) · 1.54 KB
/
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
import socket
import machine
#HTML to send to browsers
html = """<!DOCTYPE html>
<html>
<head> <title>ESP8266 LED ON/OFF</title> </head>
<center><h2>A simple webserver for turning LED's on and off with Micropython</h2></center>
<center><h3>(for noobs to both the ESP8266 and Micropython)</h3></center>
<form>
LED0:
<button name="LED" value="ON0" type="submit">LED ON</button>
<button name="LED" value="OFF0" type="submit">LED OFF</button><br><br>
LED2:
<button name="LED" value="ON2" type="submit">LED ON</button>
<button name="LED" value="OFF2" type="submit">LED OFF</button>
</form>
</html>
"""
#Setup PINS
LED0 = machine.Pin(0, machine.Pin.OUT)
LED2 = machine.Pin(2, machine.Pin.OUT)
LED2.on()
LED0.on()
#Setup Socket WebServer
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print("Got a connection from %s" % str(addr))
request = conn.recv(1024)
print("Content = %s" % str(request))
request = str(request)
LEDON0 = request.find('/?LED=ON0')
LEDOFF0 = request.find('/?LED=OFF0')
LEDON2 = request.find('/?LED=ON2')
LEDOFF2 = request.find('/?LED=OFF2')
#print("Data: " + str(LEDON0))
#print("Data2: " + str(LEDOFF0))
if LEDON0 == 6:
print('TURN LED0 ON')
LED0.off()
if LEDOFF0 == 6:
print('TURN LED0 OFF')
LED0.on()
if LEDON2 == 6:
print('TURN LED2 ON')
LED2.off()
if LEDOFF2 == 6:
print('TURN LED2 OFF')
LED2.on()
response = html
conn.send(response)
conn.close()