async def start(loop_delay): await asyncio.sleep(.1) print(color.yellow() + 'wifi state running' + color.normal()) while True: cd_state, cd_ssid, cd_pw = wifi.get_credentials() st_wlan = network.WLAN(network.STA_IF) wifi_st = st_wlan.isconnected() server_list = sys_info.get('server_list') data_server = sys_info.get('data_server') cam_server = sys_info.get('cam_server') git_sys_info = sys_info.get('git_sys_info') sys_info.set('wifi', wifi_st) if wifi_st == False: if cd_state == True: print('Connecting to:', cd_ssid, cd_pw) st_wlan.active(True) st_wlan.connect(cd_ssid, cd_pw) else: print('Waiting for Wifi Credentials') else: if git_sys_info == '': sys_info.set('git_sys_info', sys_info.git_info()) if update.check('sys_info')[0] == True: print('\nSystem OUTDATED') update.system() print('\nSystem UPDATED') print('\nRestarting system\n-----------------------\n\n') machine.reset() else: print('\nSystem updated\nStart system') if server_list == 'null': try: server_request = update.read_remote( 'server_list', sys_info.get('esp_sys_info')['git_url']) server_list = json.loads(server_request.text) data_host, data_port = server_list['data_host'][0].split( ':') cam_host, cam_port = server_list['cam_host'][0].split(':') sys_info.set('server_list', server_list) sys_info.setd('data_server', 'host', data_host) sys_info.setd('data_server', 'port', data_port) sys_info.setd('cam_server', 'host', cam_host) sys_info.setd('cam_server', 'port', cam_port) except: sys_info.reset() # if data_server['timeout'] != 0: # sys_info.set('server_list','null') # sys_info.setd('data_server','timeout',0) await asyncio.sleep(loop_delay)
async def start(to): await asyncio.sleep(.6) print(color.green() + 'STARTING PYCOM DATA' + color.normal()) await asyncio.sleep(1) while True: data_server = sys_info.get('data_server') data_host = data_server['host'] port = data_server['port'] data_address = data_server['address'] wifi_st = sys_info.get('wifi') if wifi_st == False: await asyncio.sleep(3) if wifi_st != False: if data_address == '': if data_host != '': try: data_address = socket.getaddrinfo(data_host, port)[0][-1] sys_info.setd('data_server', 'address', data_address) except: data_address = '' print('Error getting DATA addr info') else: print('data_host not ready') await asyncio.sleep(3) else: conn_try = 0 print(color.green() + '{\n\tCONNECTING TO PYCOM DATA' + color.normal()) try: s = socket.socket() s.setblocking(False) connected = False while connected == False: try: s.connect(data_address) except OSError as e: if str(e) == "127": connected = True else: conn_try = conn_try + 1 if conn_try > to: print(color.red() + '\tDATA CONN F' + color.normal()) conn_try = to break await asyncio.sleep(.1) pass #connected if connected == True: print(color.green() + '\tconnected to data_address' + color.normal()) conn_try = 0 while True: if conn_try > to: print('\n\tcouldnt send data') break data = json.dumps(machine_data.get()) data = data.encode() while True: try: while data: sent = s.send(data) data = data[sent:] await asyncio.sleep(.01) 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()) conn_try = 0 break conn_try = conn_try + 1 await asyncio.sleep(.1) print('\treceiving DATA server data') while True: try: res = s.recv(256) await asyncio.sleep(.01) if str(res).find('command') != -1: print('\tDATA server data received: ') print('\t', color.yellow(), res, color.normal()) try: js_res = json.loads(res) except: print('failed to load json') try: machine_data.parse_data(js_res) conn_try = 0 except: print('failed to parse data', js_res) break if conn_try > to * 10: print(color.red() + '\tDATA RECV F' + color.normal()) break conn_try = conn_try + 1 except OSError as e: if conn_try > to: print(color.red() + '\tERROR DATA RECV F' + color.normal()) break conn_try = conn_try + 1 await asyncio.sleep(.1) await asyncio.sleep(.1) print(color.yellow() + '\tdata conn_try', conn_try) print(color.red() + '\tdata out\n}\n' + color.normal()) sys_info.setd('data_server', 'timeout', conn_try) s.close() del s except OSError as e: print('data socket failed', str(e)) gc.collect() 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 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 start(to): await asyncio.sleep(.6) print(color.green() + 'STARTING PYCOM DATA' + color.normal()) await asyncio.sleep(1) while True: data_server = sys_info.get('data_server') data_host = data_server['host'] port = data_server['port'] data_address = data_server['address'] wifi_st = sys_info.get('wifi') if wifi_st == False: await asyncio.sleep(3) if wifi_st != False: if data_address == '': if data_host != '': try: data_address = socket.getaddrinfo(data_host, port)[0][-1] sys_info.setd('data_server', 'address', data_address) except: data_address = '' print('Error getting DATA addr info') else: print('data_host not ready') await asyncio.sleep(3) else: conn_try = 0 timeout = 0 timeout_time = to trycount = 0 trycount_timeout = to print(color.green() + '{\n\tCONNECTING TO PYCOM DATA' + color.normal()) try: client = socket.socket() client.setblocking(False) connected = False while connected == False: try: client.connect(data_address) except OSError as e: if str(e) == "127": connected = True else: conn_try = conn_try + 1 if conn_try > to: print(color.red() + '\tDATA CONN F' + color.normal()) conn_try = to break await asyncio.sleep(.1) pass #connected if connected == True: print(color.green() + '\tconnected to data_address' + color.normal()) while True: if timeout > timeout_time: print('failed to send data', timeout) break if trycount > trycount_timeout: print('couldnt send data') break await asyncio.sleep(.1) data = json.dumps(machine_data.get()) data = data.encode() while True: try: while data: sent = client.send(data) data = data[sent:] print(color.yellow(), 'data sent', color.normal()) machine_data.set('command', {'command': 'wait'}) timeout = 0 break except: timeout = timeout + 1 if timeout == 0: while True: trycount = trycount + 1 if trycount > trycount_timeout: print(color.yellow(), 'couldnt receive data', color.normal()) break print(color.yellow(), 'tries to read:', trycount, timeout, color.normal()) await asyncio.sleep(.1) try: res = client.recv(256) if str(res).find('command') != -1: print(color.yellow(), res, color.normal()) timeout = 0 break except: timeout = timeout + 1 if timeout > timeout_time: print(color.yellow(), 'failed to read data', timeout, color.normal()) break if timeout == 0: trycount = 0 try: js_res = json.loads(res) except: print('failed to load json') try: machine_data.parse_data(js_res) except: print('failed to parse data', js_res) print(color.yellow() + '\tdata trycount', trycount) print(color.red() + '\tdata out\n}\n' + color.normal()) sys_info.setd('data_server', 'timeout', conn_try) client.close() del client except OSError as e: print('data socket failed', str(e)) gc.collect() 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)