]> vault307.fbx.one Git - esp32Cam.git/blob - webcam.py
esp32Camera webserver
[esp32Cam.git] / webcam.py
1 #
2 # The MIT License (MIT)
3 #
4 # Copyright (c) Sharil Tumin
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #-----------------------------------------------------------------------------
24
25 # webcam.py MVC - This is the controller C of MVC
26
27 from machine import reset
28 from time import sleep
29 import usocket as soc
30 import gc
31 #
32 import camera
33 import config as K
34 from wifi import Sta
35 from help import Setting as cam_setting
36 import site
37
38 gc.enable() # Enable automatic garbage collection
39
40 auth=site.auth
41 pwd=site.pwd
42
43 def clean_up(cs):
44 cs.close() # flash buffer and close socket
45 del cs
46 # gc.collect()
47
48 def route(pm):
49 cs,rq=pm
50 pth='*NOP*'
51 rqp = rq.split('/')
52 rl=len(rqp)
53 if rl==1: # ''
54 pth='/';v=0
55 elif rl==2: # '/', '/a'
56 pth=f'/{rqp[1]}';v=0
57 else: # '/a/v' '/a/v/w/....'
58 pth=f'/{rqp[1]}'
59 if rqp[1]=='login':
60 v=rqp[2]
61 else:
62 try:
63 v=int(rqp[2])
64 except:
65 v=0
66 pth='*ERR*'
67 print('Not an integer value', rqp[2])
68 if pth in site.app:
69 #print(pth, site.app[pth])
70 site.app[pth](cs,v)
71 elif pth=='*NOP*':
72 site.NOP(cs)
73 else:
74 site.ERR(cs)
75 clean_up(cs)
76
77 def server(pm):
78 p=pm[0]
79 ss=soc.socket(soc.AF_INET, soc.SOCK_STREAM)
80 ss.setsockopt(soc.SOL_SOCKET, soc.SO_REUSEADDR, 1)
81 sa = ('0.0.0.0', p)
82 ss.bind(sa)
83 ss.listen(1) # serve 1 client at a time
84 print("Start server", p)
85 if auth.on:
86 print(f"Try - http://{site.server}/login/{auth.pwd}")
87 else:
88 print(f"Try - http://{site.server}")
89 while True:
90 ms='';rq=[]
91 try:
92 cs, ca = ss.accept()
93 except:
94 pass
95 else:
96 r=b'';e=''
97 try:
98 r = cs.recv(1024)
99 except Exception as e:
100 print(f"EX:{e}")
101 clean_up(cs)
102 try:
103 ms = r.decode()
104 rq = ms.split(' ')
105 except Exception as e:
106 print(f"RQ:{ms} EX:{e}")
107 clean_up(cs)
108 else:
109 if len(rq)>=2:
110 print(ca, rq[:2])
111 rv,ph=rq[:2] # GET /path
112 if not auth.on:
113 route((cs, ph))
114 continue
115 elif auth.ip==ca[0]: # authenticated client
116 route((cs, ph))
117 continue
118 elif ph.find('login/')>=0: # do login
119 site.client=ca[0]
120 route((cs, ph))
121 continue
122 else:
123 # Unauthorized otherwise
124 site.NO(cs)
125 clean_up(cs)
126
127 # set camera configuration
128 K.configure(camera, K.ai_thinker) # AI-Thinker PINs map - no need (default)
129 #camera.conf(K.XCLK_MHZ, 16) # 16Mhz xclk rate
130 camera.conf(K.XCLK_MHZ, 14) # 14Mhz xclk rate
131 #camera.conf(K.XCLK_MHZ, 13) # 14Mhz xclk rate
132 #camera.conf(K.XCLK_MHZ, 12) # 12Mhz xclk rate - to reduce "cam_hal: EV-EOF-OVF"
133
134 # wait for camera ready
135 for i in range(5):
136 cam = camera.init()
137 print("Camera ready?: ", cam)
138 if cam: break
139 else: sleep(2)
140 else:
141 print('Timeout')
142 reset()
143
144 if cam:
145 print("Camera ready")
146 # wait for wifi ready
147 w = Sta()
148 w.connect()
149 w.wait()
150 for i in range(5):
151 if w.wlan.isconnected(): break
152 else: print("WIFI not ready. Wait...");sleep(2)
153 else:
154 print("WIFI not ready. Can't continue!")
155 reset()
156
157 # set auth
158 auth.on=True
159 #auth.on=False # False: no authentication needed
160
161 if auth.on:
162 auth.pwd=pwd()
163 auth.ip=''
164 print(f'PWD: {auth.pwd}')
165
166 # set preffered camera setting
167 camera.framesize(10) # frame size 800X600 (1.33 espect ratio)
168 #camera.framesize(11)
169 #camera.framesize(12)
170 camera.quality(5)
171 #camera.quality(10)
172 camera.brightness(3)
173 camera.contrast(2) # increase contrast
174 #camera.contrast(0)
175 camera.speffect(2) # jpeg grayscale
176
177 cam_setting['framesize']=10
178 cam_setting['quality']=5
179 cam_setting['contrast']=0
180 cam_setting['speffect']=2
181 cam_setting['brightness']=3
182
183 site.ip=w.wlan.ifconfig()[0]
184 site.camera=camera
185
186 server((80,)) # port 80
187 reset()