-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (46 loc) · 1.44 KB
/
main.py
File metadata and controls
57 lines (46 loc) · 1.44 KB
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
import machine
import sys
pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 2, 4, 5, 12, 13, 14, 15)]
body = """
<!DOCTYPE html>
<html>
<head> <title>ESP8266 Pins</title> </head>
<body> <h1>ESP8266 Pins</h1>
<table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s </table>
</body>
</html>
"""
body_bytes=bytes(body,'utf8')
length=len(body_bytes)
#length=sys.getsizeof(body)
#header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-length: "+str(length)+"\r\nConnection: closed\r\n\r\n"
header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-length: "+str(length)+"\r\n"
#header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n"
html = header+body
print(html)
import network
ap_if = network.WLAN(network.AP_IF)
ap_if.active(True)
print(ap_if.ifconfig())
import socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
try:
s.bind(addr)
s.listen(1)
print('listening on', addr)
while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
rows = ['<tr><td>%s</td><td>%d</td></tr>' % (str(p), p.value()) for p in pins]
response = html % '\n'.join(rows)
cl.send(bytes(response,'utf8'))
cl_file.close() #NOTE this step may be needed
cl.close()
finally:
s.close()