-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCameraSettings.py
More file actions
116 lines (100 loc) · 4.22 KB
/
CameraSettings.py
File metadata and controls
116 lines (100 loc) · 4.22 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
import network
import asyncio
import time
from acamera import Camera, FrameSize, PixelFormat # Import the async version of the Camera class, you can also use the sync version (camera.Camera)
cam = Camera(frame_size=FrameSize.VGA, pixel_format=PixelFormat.JPEG, jpeg_quality=85, init=False)
# WLAN config
ssid = '<yourSSID>'
password = '<yourPW>'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while not station.isconnected():
time.sleep(1)
print(f'Connected! IP: http://{station.ifconfig()[0]}. Open it in your browser')
try:
with open("CameraSettings.html", 'r') as file:
html = file.read()
except Exception as e:
print("Error reading CameraSettings.html file. You might forgot to copy it from the examples folder.")
raise e
async def stream_camera(writer):
try:
cam.init()
await asyncio.sleep(1)
writer.write(b'HTTP/1.1 200 OK\r\nContent-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n')
await writer.drain()
while True:
frame = await cam.acapture() # This is the async version of capture, you can also use frame = cam.capture() instead
if frame:
writer.write(b'--frame\r\nContent-Type: image/jpeg\r\n\r\n')
writer.write(frame)
await writer.drain()
finally:
cam.deinit()
writer.close()
await writer.wait_closed()
print("Streaming stopped and camera deinitialized.")
async def handle_client(reader, writer):
try:
request = await reader.read(1024)
request = request.decode()
if 'GET /stream' in request:
print("Start streaming...")
await stream_camera(writer)
elif 'GET /set_' in request:
method_name = request.split('GET /set_')[1].split('?')[0]
value = int(request.split('value=')[1].split(' ')[0])
try:
# Use property assignment instead of method call
setattr(cam, method_name, value)
print(f"setting {method_name} to {value}")
response = 'HTTP/1.1 200 OK\r\n\r\n'
writer.write(response.encode())
await writer.drain()
except (AttributeError, ValueError) as e:
# Fallback to reconfigure if property doesn't exist
try:
cam.reconfigure(**{method_name: value})
print(f"Camera reconfigured with {method_name}={value}")
print("This action restores all previous configuration!")
response = 'HTTP/1.1 200 OK\r\n\r\n'
writer.write(response.encode())
await writer.drain()
except Exception as e2:
print(f"Error setting {method_name}: {e2}")
response = 'HTTP/1.1 404 Not Found\r\n\r\n'
writer.write(response.encode())
await writer.drain()
elif 'GET /get_' in request:
method_name = request.split('GET /get_')[1].split(' ')[0]
try:
# Use property access instead of method call
value = getattr(cam, method_name)
print(f"{method_name} is {value}")
response = f'HTTP/1.1 200 OK\r\n\r\n{value}'
writer.write(response.encode())
await writer.drain()
except AttributeError as e:
print(f"Error getting {method_name}: {e}")
response = 'HTTP/1.1 404 Not Found\r\n\r\n'
writer.write(response.encode())
await writer.drain()
else:
writer.write('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n'.encode() + html.encode())
await writer.drain()
except Exception as e:
print(f"Error: {e}")
finally:
writer.close()
await writer.wait_closed()
async def start_server():
server = await asyncio.start_server(handle_client, "0.0.0.0", 80)
print(f'Server is running on {station.ifconfig()[0]}:80')
while True:
await asyncio.sleep(3600)
try:
asyncio.run(start_server())
except KeyboardInterrupt:
cam.deinit()
print("Server stopped")