]> vault307.fbx.one Git - esp32Cam.git/blob - streaming_server.py
esp32Camera webserver
[esp32Cam.git] / streaming_server.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) Sharil Tumin
4 #
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:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
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
21 # THE SOFTWARE.
22 #-----------------------------------------------------------------------------
23
24 # run this on ESP32 Camera
25
26 import esp
27 # from bluetooth import BLE
28 from Wifi import Sta
29 import socket as soc
30 import camera
31 from time import sleep
32
33 hdr = {
34 # live stream -
35 # URL: /live
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
41 Pragma: no-cache""",
42 # live stream -
43 # URL:
44 'frame': """--kaki5
45 Content-Type: image/jpeg"""}
46
47 UID = const('espCam') # authentication user. Whatever you want
48 PWD = const('doesThisWork?') # authentication password. Whatever you want
49
50 cam = camera.init() # Camera
51 print("Camera ready?: ", cam)
52
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
59 sta.wait()
60
61 # wait for WiFi
62 con = ()
63 for i in range(5):
64 if sta.wlan.isconnected():con=sta.status();break
65 else: print("WIFI not ready. Wait...");sleep(2)
66 else:
67 print("WIFI not ready")
68
69 if con and cam: # WiFi and camera are ready
70 if cam:
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
75 if con:
76 # TCP server
77 port = 80
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)
81 s.bind(addr)
82 s.listen(1)
83 # s.settimeout(5.0)
84 while True:
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('/')
89 # print(_, uid, pwd)
90 if not (uid==UID and pwd==PWD):
91 print('Not authenticated')
92 cs.close()
93 continue
94 # We are authenticated, so continue serving
95 cs.write(b'%s\r\n\r\n' % hdr['stream'])
96 pic=camera.capture
97 put=cs.write
98 hr=hdr['frame']
99 while True:
100 # once connected and authenticated just send the jpg data
101 # client use HTTP protocol (not RTSP)
102 try:
103 put(b'%s\r\n\r\n' % hr)
104 put(pic())
105 put(b'\r\n') # send and flush the send buffer
106 except Exception as e:
107 print('TCP send error', e)
108 cs.close()
109 break
110 else:
111 if not con:
112 print("WiFi not connected.")
113 if not cam:
114 print("Camera not ready.")
115 else:
116 camera.deinit()
117 print("System not ready. Please restart")
118
119 print('System aborted')