forked from alankrantas/esp8266-micropython-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimple_WebServer.py
99 lines (78 loc) · 2.67 KB
/
Simple_WebServer.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
ssid = '' # WiFi name
pw = '' # WiFi password
port = 80 # server port
conns = 1 # number of channels
from machine import Pin
import network, usocket, sys
led = Pin(2, Pin.OUT, value=1)
# webpage template
html = r'<!DOCTYPE html>'
html += r'<html>'
html += r'<head>'
html += r'<title>ESP8266 Web Server</title>'
html += r'<meta name="viewport" content="width=device-width, initial-scale=1">'
html += r'<link rel="icon" href="data:,">'
html += r'<style>body {background-color: Moccasin;} h1 {color: SaddleBrown;} h2 {color: Olive;} </style>'
html += r'</head>'
html += r'<body><center>'
html += r'<h1>ESP8266 Web Server</h1>'
html += r'<h2>LED status: {led_status}</h2>'
html += r'<form methon="GET" action="">'
html += r'<p><input id="led_on" type="submit" name="led" value="On"></p>'
html += r'<p><input id="led_off" type="submit" name="led" value="Off"></p>'
html += r'</form></center></body>'
html += r'</html>'
# generated webpage to be sent to user
def web_page():
led_status = 'ON' if led.value() == 0 else 'OFF'
return html.replace('{led_status}', led_status)
# extract any number of parameter names and values from HTTP response
def get_paras(get_str):
para_dict = {}
q_pos = get_str.find('/?')
if q_pos > 0:
http_pos = get_str.find('HTTP/')
para_list = get_str[q_pos + 2 : http_pos - 1].split('&')
for para in para_list:
para_tmp = para.split('=')
para_dict.update({para_tmp[0] : para_tmp[1]})
return para_dict
# wifi error descriptions
wifi_error = {
network.STAT_WRONG_PASSWORD: 'wrong password',
network.STAT_NO_AP_FOUND: 'wifi AP not found',
-1: 'due to other problems',
}
# connect to wifi
print('Connecting to WiFi...')
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, pw)
while wifi.status() == network.STAT_CONNECTING:
pass
if wifi.status() != network.STAT_GOT_IP:
print('Failed to connect:', wifi_error.get(wifi.status(), wifi_error[-1]))
sys.exit()
print('Connected.')
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
s.bind(('', port))
s.listen(conns)
print('Web server started on', 'http://{}:{}'.format(wifi.ifconfig()[0], port))
while True:
# wait for client
client, addr = s.accept()
print('Client connected from', addr[0])
request = client.recv(1024)
# extract url parameters
request_text = request.decode('utf-8')
paras = get_paras(request_text)
# control the led
led_status = paras.get('led', None)
if led_status == 'On':
led.value(0)
else:
led.value(1)
# send response (web page) to user
response = web_page()
client.send(response)
client.close()