File tree Expand file tree Collapse file tree 1 file changed +67
-0
lines changed Expand file tree Collapse file tree 1 file changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ import asyncio
2
+ import binascii
3
+ import os
4
+ import struct
5
+ import sys
6
+ import time
7
+
8
+ import adafruit_pycamera
9
+ import espcamera
10
+ import socketpool
11
+ import wifi
12
+ from adafruit_httpserver .response import ChunkedResponse
13
+ from adafruit_httpserver .server import Server
14
+
15
+ pycam = adafruit_pycamera .PyCamera ()
16
+ pycam .camera .reconfigure (
17
+ pixel_format = espcamera .PixelFormat .JPEG ,
18
+ frame_size = espcamera .FrameSize .SVGA ,
19
+ )
20
+ pycam .camera .quality = 6
21
+
22
+ server = Server (socketpool .SocketPool (wifi .radio ))
23
+ PORT = 81
24
+ BOUNDARY = b"FRAME" + binascii .hexlify (os .urandom (8 ))
25
+
26
+
27
+ @server .route ("/" )
28
+ def base (request ):
29
+ def body ():
30
+ while True :
31
+ jpeg = pycam .camera .take ()
32
+ yield b"--"
33
+ yield BOUNDARY
34
+ yield b"\r \n "
35
+ yield b"Content-Type: image/jpeg\r \n Content-Length: "
36
+ yield str (len (jpeg ))
37
+ yield "\r \n \r \n "
38
+ yield jpeg
39
+ yield "\r \n "
40
+
41
+ return ChunkedResponse (
42
+ request ,
43
+ body ,
44
+ headers = {
45
+ "Content-Type" : "multipart/x-mixed-replace; boundary=%s"
46
+ % BOUNDARY .decode ("ascii" )
47
+ },
48
+ )
49
+
50
+
51
+ async def poll (interval ):
52
+ server .start (str (wifi .radio .ipv4_address ), port = PORT )
53
+ while True :
54
+ try :
55
+ server .poll ()
56
+ except BrokenPipeError as e :
57
+ print (e )
58
+ await asyncio .sleep (interval )
59
+
60
+
61
+ async def main ():
62
+ poll_task = asyncio .create_task (poll (0 ))
63
+ await asyncio .gather (poll_task )
64
+
65
+ pycam .display_message (f"{ wifi .radio .ipv4_address } :{ PORT } /" , scale = 2 )
66
+
67
+ asyncio .run (main ())
You can’t perform that action at this time.
0 commit comments