-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (105 loc) · 2.87 KB
/
main.py
File metadata and controls
133 lines (105 loc) · 2.87 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
try:
import usocket as socket
except:
import socket
response_404 = """HTTP/1.0 404 NOT FOUND
<h1>404 Not Found</h1>
"""
response_500 = """HTTP/1.0 500 INTERNAL SERVER ERROR
<h1>500 Internal Server Error</h1>
"""
response_template = """HTTP/1.0 200 OK
%s
"""
import machine
import ntptime, utime
from machine import RTC
from time import sleep
try:
seconds = ntptime.time()
except:
seconds = 0
rtc = RTC()
rtc.datetime(utime.localtime(seconds))
def time():
body = """<html>
<body>
<h1>Time</h1>
<p>%s</p>
</body>
</html>
""" % str(rtc.datetime())
return response_template % body
def dummy():
body = "This is a dummy endpoint"
return response_template % body
# ------------------------------------
on_off_pin = machine.Pin(16, machine.Pin.OUT)
def light_on():
on_off_pin.value(0)
body = "You turned a light on!"
return response_template % body
def light_off():
on_off_pin.value(1)
body = "You turned a light off!"
return response_template % body
# ------------------------------------
switch_pin = machine.Pin(5, machine.Pin.IN)
def switch():
body = "{state: " + str(switch_pin.value()) + "}"
return response_template % body
# Port 10 is always in high impedance mode on my microcontroller,
# which prevents wifi connection to router. Instead, I used port 5 (GPI05).
# ------------------------------------
adc_pin = machine.ADC(0)
def light():
body = "{value: " + str(adc_pin.read()) + "}"
return response_template % body
# ------------------------------------
pwm_pin = machine.PWM(machine.Pin(13))
dty=500
def pwm():
pwm_pin.duty(dty)
body = "{pwm duty: " + str(dty) + "}"
return response_template % body
# ------------------------------------
handlers = {
'time': time,
'dummy': dummy,
'light_on': light_on,
'light_off': light_off,
'switch': switch,
'light': light,
'pwm': pwm,
}
# -------------------------------------
def main():
s = socket.socket()
ai = socket.getaddrinfo("0.0.0.0", 8080)
addr = ai[0][-1]
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
print("Listening, connect your browser to http://<this_host>:8080")
while True:
sleep(0.5)
res = s.accept()
client_s = res[0]
client_addr = res[1]
req = client_s.recv(4096)
print("Request:")
print(req)
try:
path = req.decode().split("\r\n")[0].split(" ")[1]
handler = handlers[path.strip('/').split('/')[0]]
response = handler()
except KeyError:
response = response_404
except Exception as e:
response = response_500
print(str(e))
client_s.send(b"\r\n".join([line.encode() for line in response.split("\n")]))
client_s.close()
print()
# -------------------------------------
main()