async def camv2(asd): print(color.blue() + 'SENDING CAM' + color.normal()) await asyncio.sleep(1) while True: connected = False #print('try to connect') s = socket.socket() s.setblocking(False) while connected == False: try: #print('connecting') s.connect(cam_address) except OSError as e: #print(e) if str(e) == "127": connected = True pass img = next(pic) try: POST(s, 'snap/dev_cam01', 'img', img) except: print('failed') s.close() print(color.red() + 'cam out' + color.normal()) del s gc.collect() await asyncio.sleep(.2)
def check(module_name): global git_sys_info, esp_sys_info, git_url esp_sys_info = sys_info.esp_info() git_sys_info = sys_info.git_info() git_url = sys_info.git_url() print( '\n' + color.blue() + 'git_sys_info:', color.normal(), str(git_sys_info).replace(',', ',\n').replace('{', '{\n ').replace('}', '\n}')) print( '\n' + color.red() + 'esp_sys_info:', color.normal(), str(esp_sys_info).replace(',', ',\n').replace('{', '{\n ').replace('}', '\n}')) sys_state = (False, 'system', 'updated') try: if module_name == '': for module in git_sys_info: try: if esp_sys_info[module] != git_sys_info[module]: sys_state = (False, module, "outdated") except: print("error: module '" + module + "' not founded") return sys_state else: if esp_sys_info[module_name] != git_sys_info[module_name]: return True, module_name, "outdated" else: return False, module_name, "updated" except: return "error: module '" + module_name + "' not founded"
def git_info(): try: git_sys_info = json.loads(urequests.get(get('esp_sys_info')['git_url']+'sys_info').text) print('\n'+color.blue()+'git_sys_info:',color.normal(), str(git_sys_info).replace(',',',\n').replace('{','{\n ').replace('}','\n}')) return git_sys_info except: print('\terror getting git system info\n}\n') return False
def read_remote(file_name, dir_url): print(color.blue() + '\nReading', file_name, 'from', dir_url, '\n') try: remote_file = urequests.get(dir_url + file_name) print('got file\n', remote_file.text + color.normal()) return remote_file except: print(color.red, 'error reading remote file' + color.normal()) return 'error reading remote file'
def system(): update_list = [] for module in git_sys_info: try: if esp_sys_info[module] != git_sys_info[module]: update_list.append(module) except: update_list.append(module) print("error: module '"+ module +"' not founded on local esp system") print('OUTDATED Modules:', update_list) for outdated_module in update_list: print(color.blue(),'\nUpdating:', outdated_module+color.normal()) git_file(outdated_module)
def start_com(address): json_data = machine_data.create() while True: print(color.green() + 'Check cam_socket') cam_state = sget('cam') while cam_state != 'socket_closed': cam_state = sget('cam') sleep(.05) pass print(color.green() + 'cam_socket disconnected') try: sset('com', 'creating_socket') client_socket = socket.socket() print(color.normal() + '{') client_socket.setblocking(0) try: print(color.green() + '{\n\tConnect to', "asmon.com.ar:", 8080) sset('com', 'connecting') client_socket.connect(address) print('\t\t{\n\t\t\tSending data') POST(client_socket, 'esp_data/dev_cam01', 'json', machine_data.get()) print('\t\t\tData Sent\n\t\t\t{') print('\t\t\t\tWait for response') sleep(0.2) buffer_data = client_socket.recv(1024) while (len(buffer_data) > 0): print(buffer_data) print(color.blue() + '\t\t\t\tGot response') break connection = True except Exception as error: client_socket.close() connection = False print(color.red() + '\t\t\t**** ERROR Connecting ****\n\t\t\t', error) client_socket.close() # flash buffer and close socket del client_socket sset('com', 'socket_closed') except: print('failed to create socket') gc.collect() print(color.red() + '\t\t\t}') print(color.normal() + '\t\t}') print(color.normal() + '\t}\n') print('}') sleep(.5)
async def camv2(to): print(color.blue() + 'SENDING CAM' + color.normal()) await asyncio.sleep(1) conn_try = 0 while True: connected = False #print('try to connect') s = socket.socket() s.setblocking(False) while connected == False: try: s.connect(cam_address) except OSError as e: #print(conn_try) if str(e) == "127": connected = True conn_try = 0 else: conn_try = conn_try + 1 if conn_try > to: print(color.red() + 'CAM CONN F' + color.normal()) break await asyncio.sleep(.05) pass if conn_try != to: print(color.green() + '{\n\tconnected to cam_address' + color.normal()) try: n_try = 0 buf = False while (n_try < 10 and buf == False): #{ # wait for sensor to start and focus before capturing image print('\tgetting img') buf = camera.capture() if (buf == False): await asyncio.sleep(2) n_try = n_try + 1 print('\tsending img') while True: try: type = 'image/jpeg' size = len(buf) req = 'snap/dev_cam01' header = h['POST'].replace('$req', req).replace('$type', type) s.send(b'%s %d\r\n\r\n' % (header, size)) while buf: sent = s.send(buf) buf = buf[sent:] await asyncio.sleep(.5) break except OSError as e: print(e) if conn_try > to: print(color.red() + 'CAM SEND F' + color.normal()) break conn_try = conn_try + 1 #POST(s, 'snap/dev_cam01', 'img',buf) print('\timg sent') except OSError as e: print('cam failed ' + str(e)) s.close() print(color.red() + '\tcam out\n}\n' + color.normal()) del s gc.collect() conn_try = 0 await asyncio.sleep(.2)
async def start(to): await asyncio.sleep(.12) print(color.blue() + 'STARTING PYCOM CAM' + color.normal()) await asyncio.sleep(1) while True: cam_server = sys_info.get('cam_server') cam_host = cam_server['host'] port = cam_server['port'] cam_address = cam_server['address'] wifi_st = sys_info.get('wifi') if wifi_st == False: await asyncio.sleep(3) if wifi_st != False: if cam_address == '': if cam_host != '': try: cam_address = socket.getaddrinfo(cam_host, port)[0][-1] sys_info.setd('cam_server', 'address', cam_address) except: cam_address = '' print('Error getting CAM addr info') else: print('cam_host not ready') await asyncio.sleep(3) else: conn_try = 0 timeout = 0 timeout_time = to trycount = 0 trycount_timeout = to print(color.blue() + '{\n\tCONNECTING TO PYCOM CAM' + color.normal()) try: client = socket.socket() client.setblocking(False) connected = False while connected == False: try: client.connect(cam_address) except OSError as e: if str(e) == "127": connected = True else: conn_try = conn_try + 1 if conn_try > to: print(color.red() + '\tCAM CONN F' + color.normal()) conn_try = to break await asyncio.sleep(.1) pass #connected if connected == True: print(color.blue() + '\tconnected to cam_address' + color.normal()) while True: if timeout > timeout_time: print(color.blue(), 'failed to send image', trycount, timeout, color.normal()) break if trycount > trycount_timeout: print('couldnt send image') break await asyncio.sleep(.1) img_data = { 'user': machine_data.get_key('user'), 'id': machine_data.get_key('id') } id_data = json.dumps(img_data) id_data = id_data.encode() data = json.dumps({'command': 'imgsent'}) data = data.encode() frame = False cam.light('1') frame = camera.capture() cam.light('0') frame = id_data + frame + data while True: await asyncio.sleep(.1) try: while frame: sent = client.send(frame) frame = frame[sent:] print(color.blue(), 'image sent', color.normal()) timeout = 0 break except: timeout = timeout + 1 if timeout > timeout_time * 2: print(color.blue(), 'failed to send image', trycount, timeout, color.normal()) break if timeout == 0: while True: trycount = trycount + 1 if trycount > trycount_timeout: print(color.blue(), 'couldnt receive image data', color.normal()) break print(color.blue(), 'tries to read:', trycount, timeout, color.normal()) await asyncio.sleep(.1) try: res = client.recv(256) if str(res).find('command') != -1: print(color.blue(), res, color.normal()) timeout = 0 break except: timeout = timeout + 1 if timeout > timeout_time: print(color.blue(), 'failed to read image data', timeout, color.normal()) break if timeout == 0: trycount = 0 print('') #send again print(color.blue() + '\tcam trycount', trycount) print(color.red() + '\tcam out\n}\n' + color.normal()) sys_info.setd('cam_server', 'timeout', conn_try) client.close() del client except OSError as e: print('cam socket failed', str(e)) gc.collect() await asyncio.sleep(.1)
async def cam(to, to2): print(color.blue() + 'SENDING CAM' + color.normal()) await asyncio.sleep(1) conn_try = 0 while True: connected = False #print('try to connect') try: s = socket.socket() s.setblocking(False) while connected == False: try: s.connect(cam_address) except OSError as e: #print(conn_try) if str(e) == "127": connected = True conn_try = 0 else: conn_try = conn_try + 1 if conn_try > to: print(color.red() + 'CAM CONN F' + color.normal()) break await asyncio.sleep(.05) pass if conn_try != to: print(color.green() + '{\n\tconnected to cam_address' + color.normal()) try: buf = camera.capture() while True: try: type = 'image/jpeg' size = len(buf) req = 'snap/dev_cam01' header = h['POST'].replace('$req', req).replace( '$type', type) s.send(b'%s %d\r\n\r\n' % (header, size)) print('img size', size) while buf: sent = s.send(buf) buf = buf[sent:] await asyncio.sleep(to2) break except OSError as e: print(e) if conn_try > to: print(color.red() + 'CAM SEND F' + color.normal()) break conn_try = conn_try + 1 #POST(s, 'snap/dev_cam01', 'img',buf) print('\timg sent') except OSError as e: print('\tsending cam failed ' + str(e)) s.close() print(color.red() + '\tcam out\n}\n' + color.normal()) del s except OSError as e: print('cam socket failed', str(e)) gc.collect() conn_try = 0 await asyncio.sleep(.1)
async def start(to): await asyncio.sleep(.12) print(color.blue()+'STARTING PYCOM CAM'+color.normal()) await asyncio.sleep(1) while True: cam_server = sys_info.get('cam_server') cam_host = cam_server['host'] port = cam_server['port'] cam_address = cam_server['address'] wifi_st = sys_info.get('wifi') if wifi_st == False: await asyncio.sleep(3) if wifi_st != False: if cam_address == '': if cam_host != '': try: cam_address = socket.getaddrinfo(cam_host, port)[0][-1] sys_info.setd('cam_server','address',cam_address) except: cam_address = '' print('Error getting CAM addr info') else: print('cam_host not ready') await asyncio.sleep(3) else: conn_try=0 print(color.blue()+'{\n\tCONNECTING TO PYCOM CAM'+color.normal()) try: s = socket.socket() s.setblocking(False) connected = False while connected == False: try: s.connect(cam_address) except OSError as e: if str(e) == "127": connected = True else: conn_try = conn_try+1 if conn_try > to: print(color.red()+'\tCAM CONN F'+color.normal()) conn_try = to break await asyncio.sleep(.1) pass #connected if conn_try < to: print(color.blue()+'\tconnected to cam_address'+color.normal()) print('\tsending img') img_data = {'user':machine_data.get_key('user'),'id':machine_data.get_key('id')} id_data = json.dumps(img_data) s.send(id_data.encode()) conn_try = 0 #s | | | | #0#1#2 #s | | | | while True: if conn_try > to: print('\n\tcouldnt send picture') break frame = False cam.light('1') print('\tgetting img') frame = camera.capture() cam.light('0') #s | | | | #0#1#2 #s | | | | print('\n\tsending img') while True: try: while frame: sent = s.send(frame) frame = frame[sent:] await asyncio.sleep(.01) print('\timg sent') break except OSError as e: if conn_try > to: print(color.red()+'CAM SEND F'+color.normal()) conn_try = 0 break conn_try = conn_try+1 await asyncio.sleep(.1) #s | | | | #0#1#2 #s | | | | print('\tsending end line') data = json.dumps({'command':'imgsent'}) data = data.encode() while True: try: while data: sent = s.send(data) data = data[sent:] conn_try = 0 print('\tdata sent') machine_data.set('command',{'command':'wait'}) break except OSError as e: if conn_try > to: print(color.red()+'DATA SEND F'+color.normal()) break conn_try = conn_try+1 await asyncio.sleep(.1) #s | | | | #0#1#2 #s | | | | print('\treceiving CAM server data') while True: try: res = s.recv(256) await asyncio.sleep(.01) if str(res).find('command') != -1: print('\tCAM server data received: ') print(res) conn_try = 0 break if conn_try > to*10: print(color.red()+'\tCAM RECV F'+color.normal()) break conn_try = conn_try + 1 except OSError as e: if conn_try > to: print(color.red()+'\tERROR CAM RECV F'+color.normal()) break conn_try = conn_try + 1 await asyncio.sleep(.1) await asyncio.sleep(.1) #s | | | | #0#1#2 #s | | | | print(color.yellow()+'\tcam conn_try', conn_try) print(color.red()+'\tcam out\n}\n'+color.normal()) sys_info.setd('cam_server','timeout',conn_try) s.close() del s except OSError as e: print('cam socket failed',str(e)) gc.collect() await asyncio.sleep(.1)