]>
vault307.fbx.one Git - esp32Cam.git/blob - streaming_server.py
1 # The MIT License (MIT)
3 # Copyright (c) Sharil Tumin
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 #-----------------------------------------------------------------------------
24 # run this on ESP32 Camera
27 # from bluetooth import BLE
31 from time
import sleep
36 'stream': """HTTP/1.1 200 OK
37 Content-Type: multipart/x-mixed-replace; boundary=kaki5
38 Connection: keep-alive
39 Cache-Control: no-cache, no-store, max-age=0, must-revalidate
40 Expires: Thu, Jan 01 1970 00:00:00 GMT
45 Content-Type: image/jpeg"""}
47 UID
= const('espCam') # authentication user. Whatever you want
48 PWD
= const('doesThisWork?') # authentication password. Whatever you want
50 cam
= camera
.init() # Camera
51 print("Camera ready?: ", cam
)
53 # connect to access point
54 sta
= Sta() # Station mode (i.e. need WiFi router)
55 # sta.wlan.disconnect() # disconnect from previous connection
56 AP
= const('DEA Surveillance Van 3') # Your SSID
57 PW
= const('B72E8Hitron') # Your password
58 sta
.connect(AP
, PW
) # connet to dlink
64 if sta
.wlan
.isconnected():con
=sta
.status();break
65 else: print("WIFI not ready. Wait...");sleep(2)
67 print("WIFI not ready")
69 if con
and cam
: # WiFi and camera are ready
71 # set preffered camera setting
72 camera
.framesize(10) # frame size 800X600 (1.33 espect ratio)
73 camera
.contrast(2) # increase contrast
74 camera
.speffect(2) # jpeg grayscale
78 addr
= soc
.getaddrinfo('0.0.0.0', port
)[0][-1]
79 s
= soc
.socket(soc
.AF_INET
, soc
.SOCK_STREAM
)
80 s
.setsockopt(soc
.SOL_SOCKET
, soc
.SO_REUSEADDR
, 1)
85 cs
, ca
= s
.accept() # wait for client connect
86 print('Request from:', ca
)
87 w
= cs
.recv(200) # blocking
88 (_
, uid
, pwd
) = w
.decode().split('\r\n')[0].split()[1].split('/')
90 if not (uid
==UID
and pwd
==PWD
):
91 print('Not authenticated')
94 # We are authenticated, so continue serving
95 cs
.write(b
'%s\r\n\r\n' % hdr
['stream'])
100 # once connected and authenticated just send the jpg data
101 # client use HTTP protocol (not RTSP)
103 put(b
'%s\r\n\r\n' % hr
)
105 put(b
'\r\n') # send and flush the send buffer
106 except Exception as e
:
107 print('TCP send error', e
)
112 print("WiFi not connected.")
114 print("Camera not ready.")
117 print("System not ready. Please restart")
119 print('System aborted')