-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32cam.py
289 lines (243 loc) · 7.51 KB
/
esp32cam.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
import time
import machine
import network
import ubinascii
import usocket
board_devSSID = "teacher001"
board_device_id = "teacher001"
def do_connect():
global connected
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
print("connecting to network...")
# sta_if.disconnect()
if not sta_if.isconnected():
sta_if.connect("WIFI_SSID", "WIFI_PASSWORD")
cnt = 0
while not sta_if.isconnected():
cnt = cnt + 1
time.sleep(0.5)
if cnt == 60:
break
connected = sta_if.isconnected()
print("network config:", sta_if.ifconfig())
class Response:
def __init__(self, f):
self.raw = f
self.encoding = "utf-8"
self._cached = None
def close(self):
if self.raw:
self.raw.close()
self.raw = None
self._cached = None
@property
def content(self):
if self._cached is None:
try:
self._cached = self.raw.read()
finally:
self.raw.close()
self.raw = None
return self._cached
@property
def text(self):
return str(self.content, self.encoding)
def json(self):
import ujson
return ujson.loads(self.content)
def request(method, url, data=None, json=None, headers={}, stream=None):
try:
proto, dummy, host, path = url.split("/", 3)
except ValueError:
proto, dummy, host = url.split("/", 2)
path = ""
if proto == "http:":
port = 80
elif proto == "https:":
import ussl
port = 443
else:
raise ValueError("Unsupported protocol: " + proto)
if ":" in host:
host, port = host.split(":", 1)
port = int(port)
# print("host:",host,",port:",port)
ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
ai = ai[0]
s = usocket.socket(ai[0], ai[1], ai[2])
try:
s.connect(ai[-1])
if proto == "https:":
s = ussl.wrap_socket(s, server_hostname=host)
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
if not "Host" in headers:
s.write(b"Host: %s\r\n" % host)
# Iterate over keys to avoid tuple alloc
for k in headers:
s.write(k)
s.write(b": ")
s.write(headers[k])
s.write(b"\r\n")
if json is not None:
assert data is None
import ujson
data = ujson.dumps(json)
s.write(b"Content-Type: application/json\r\n")
if data:
s.write(b"Content-Length: %d\r\n" % len(data))
s.write(b"\r\n")
if data:
s.write(data)
l = s.readline()
# print(l)
l = l.split(None, 2)
status = int(l[1])
reason = ""
if len(l) > 2:
reason = l[2].rstrip()
while True:
l = s.readline()
if not l or l == b"\r\n":
break
# print(l)
if l.startswith(b"Transfer-Encoding:"):
if b"chunked" in l:
raise ValueError("Unsupported " + l)
elif l.startswith(b"Location:") and not 200 <= status <= 299:
raise NotImplementedError("Redirects not yet supported")
except OSError:
s.close()
raise
resp = Response(s)
resp.status_code = status
resp.reason = reason
return resp
def head(url, **kw):
return request("HEAD", url, **kw)
def get(url, **kw):
return request("GET", url, **kw)
def post(url, **kw):
return request("POST", url, **kw)
def put(url, **kw):
return request("PUT", url, **kw)
def patch(url, **kw):
return request("PATCH", url, **kw)
def delete(url, **kw):
return request("DELETE", url, **kw)
class Res:
def save(url, file):
try:
response = get(url)
print(">>", len(response.text))
print("get file:", file, "size:", len(response.text), ",save to:", file)
f = open(file, "w")
f.write(response.text)
f.close()
print("OK.")
except Exception as e:
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print(e)
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
def get(url, file):
try:
response = get("https://webduinoio.github.io/pythonCode/" + url)
print(">>", len(response.text))
print("get file:", file, "size:", len(response.text), ",save to:", file)
f = open(file, "w")
f.write(response.text)
f.close()
print("OK.")
except Exception as e:
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print(e)
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
def exe(dir):
srcDir = dir
try:
while True:
idx = dir.index("/")
try:
name = dir[0:idx]
try:
print("mkdir", name)
os.mkdir(name)
except:
pass
try:
os.chdir(name)
print("cd", name)
except:
pass
except:
pass
dir = dir[idx + 1 :]
except:
pos = -1
try:
pos = dir.index(".mpy")
except:
pass
try:
if pos == -1:
pos = dir.index(".py")
except:
pass
try:
if pos > 0:
pyFile = dir
Res.get(srcDir, pyFile)
else:
try:
print("mkdir", dir)
os.mkdir(dir)
except:
pass
try:
os.chdir(dir)
print("cd ", dir)
except:
pass
except:
pass
os.chdir("/")
def install(deviceId=""):
# WiFi Connect
print("connect...")
do_connect()
print("get files...")
# 開源必備
Res.exe("lib/urequests.py")
Res.exe("lib/umqtt/simple.py")
# Webduino 類別庫
Res.exe("lib/webduino/led.py")
Res.exe("lib/webduino/config.py")
Res.exe("lib/webduino/gdriver.py")
Res.exe("lib/webduino/camera.py")
Res.exe("lib/webduino/board.py")
Res.exe("lib/webduino/mqtt.py")
Res.exe("lib/webduino/wifi.py")
Res.exe("lib/webduino/webserver.py")
Res.exe("lib/webduino/debug.py")
Res.exe("lib/utils.py") # save url to file
Res.get("", "index.html")
from utils import Utils
from webduino.config import Config
Utils.save("https://webduinoio.github.io/pythonCode/app/boot.py", "boot.py")
Utils.save("https://webduinoio.github.io/pythonCode/app/CamApp.py", "main.py")
Config.load()
if not deviceId == "":
Config.data["devId"] = deviceId
else:
deviceId = Config.data["devId"]
if not board_devSSID == "":
Config.data["devSSID"] = board_devSSID
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
print("- Device ID: [" + deviceId + "] -")
Config.save()
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
print("Mac address:", ubinascii.hexlify(network.WLAN().config("mac"), ":").decode())
install(deviceId=board_device_id)
time.sleep(1)
machine.reset()