-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcode.py
70 lines (55 loc) · 1.58 KB
/
code.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
# SPDX-FileCopyrightText: 2023 Jeff Epler for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
import asyncio
import binascii
import os
import espcamera
import socketpool
import wifi
from adafruit_httpserver.response import ChunkedResponse
from adafruit_httpserver.server import Server
import adafruit_pycamera
pycam = adafruit_pycamera.PyCamera()
pycam.camera.reconfigure(
pixel_format=espcamera.PixelFormat.JPEG,
frame_size=espcamera.FrameSize.SVGA,
)
pycam.camera.quality = 6
server = Server(socketpool.SocketPool(wifi.radio))
PORT = 81
BOUNDARY = b"FRAME" + binascii.hexlify(os.urandom(8))
@server.route("/")
def base(request):
def body():
while True:
jpeg = pycam.camera.take()
yield b"--"
yield BOUNDARY
yield b"\r\n"
yield b"Content-Type: image/jpeg\r\nContent-Length: "
yield str(len(jpeg))
yield "\r\n\r\n"
yield jpeg
yield "\r\n"
return ChunkedResponse(
request,
body,
headers={
"Content-Type": "multipart/x-mixed-replace; boundary=%s"
% BOUNDARY.decode("ascii")
},
)
async def poll(interval):
server.start(str(wifi.radio.ipv4_address), port=PORT)
while True:
try:
server.poll()
except BrokenPipeError as e:
print(e)
await asyncio.sleep(interval)
async def main():
poll_task = asyncio.create_task(poll(0))
await asyncio.gather(poll_task)
pycam.display_message(f"{wifi.radio.ipv4_address}:{PORT}/", scale=2)
asyncio.run(main())