def plus(s): print(color.yellow()+'\n Starting CAM+DATA_SYSTEM') while True: e='' url = "http://asmon.com.ar:8080/esp_data/dev_cam01" headers = {'X-AIO-Key': 'xxxxxxxxxxxxxxxxxxx', 'Content-Type': 'application/json'} data = json.dumps(machine_data.get()) while True: try: r = requests.post(url, data=data, headers=headers) results = r.json() print(results) except: e='' break url = "http://asmon.com.ar:8081/snap/dev_cam01" headers = {'X-AIO-Key': 'xxxxxxxxxxxxxxxxxxx', 'Content-Type': 'image/jpeg'} while True: try: data = next(pic) r = requests.post(url, data=data, headers=headers) results = r.json() except: e='' break sleep(.1)
def tcam(sc): print(color.yellow() + '\n Starting CAM_SYSTEM:') while True: s = socket.socket() s.setblocking(False) try: while True: try: print('connecting') s.connect(("asmon.com.ar", 8081)) except OSError as e: if str(e) == "127": print('connected') break sleep(0.2) img = next(pic) s.sendall(b'%s %d\r\n\r\n' % (hdr['POST'], len(img))) s.sendall(img) s.sendall(b'\r\n') result = s.recv(2000) while (len(result) > 0): #print(result) print('\ngot result') break except Exception as e: ee = str(e) if ee != '': print('cam error: ' + ee) s.close() # flash buffer and close socket gc.collect() sleep(.1)
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)
def start(): print(color.yellow() + '\n Starting asmon_system') server_list = get_server_list() host, port = server_list[0].split(':') if host != 'null': print(color.green() + '\nStart server communication\n' + 'IP:', host, '\nPORT:', port + '\n' + color.normal()) try: address = socket.getaddrinfo(host, port)[0][-1] print(address) th.start_new_thread(start_com, (address, )) except: print(color.red() + 'Error getting addr info from', host, port) else: print(color.red() + 'Error starting asmon_system')
def start(): print(color.yellow()+'\n Starting CAM_SYSTEM') #server_list = get_server_list() host, port = ['asmon.com.ar','8081'] #server_list[0].split(':') if host != 'null': print(color.green()+'\nStart server communication\n' + 'IP:', host, '\nPORT:', port+'\n'+color.normal()) try: address = socket.getaddrinfo(host, port)[0][-1] print(address) th(start_cam, (address,)) except: print(color.red()+'Error getting addr info from', host, port, color.normal()) else: print(color.red()+'Error starting CAM_SYSTEM'+color.normal())
def plus(h1, h2): global json_command print(color.yellow() + '\n Starting CAM+DATA_SYSTEM:') pic = cam.frame_gen() new_json_command = {} data_host = h1 cam_host = h2 print('data_host:', data_host) print('cam_host:', cam_host) while True: e = '' url = data_host headers = { 'X-AIO-Key': 'xxxxxxxxxxxxxxxxxxx', 'Content-Type': 'application/json' } data = json.dumps(machine_data.get()) while True: try: r = requests.post(url, data=data, headers=headers) new_json_command = r.json() if json_command != new_json_command: json_command = new_json_command print(json_command) except: e = '' break url = cam_host headers = { 'X-AIO-Key': 'xxxxxxxxxxxxxxxxxxx', 'Content-Type': 'image/jpeg' } while True: try: data = next(pic) r = requests.post(url, data=data, headers=headers) results = r.json() except: e = '' break gc.collect() sleep(.1)
def snap(s): print(color.yellow()+'\n Starting CAM_SYSTEM') while True: url = "http://asmon.com.ar:8081/snap/dev_cam01" headers = {'X-AIO-Key': 'xxxxxxxxxxxxxxxxxxx', 'Content-Type': 'image/jpeg'} data = next(pic) results={} try: r = requests.post(url, data=data, headers=headers) results = r.json() print(green,results,normal) except OSError as error: print('\n'+red) print('cam failed') print(error) print('\n'+normal) gc.collect() sleep(.1)
def start_com(address): global json_command print(color.yellow() + '\n Starting DATA_SYSTEM:') while True: try: client_socket = socket.socket() client_socket.setblocking(False) try: client_socket.connect(address) except OSError as error: if error.args[0] not in [uerrno.EINPROGRESS, uerrno.ETIMEDOUT]: print(color.red() + '**** Error connecting ****', error, color.normal()) try: POST(client_socket, 'esp_data/dev_cam01', 'json', machine_data.get()) while True: res = str(client_socket.recv(2000)) if res.find('200 OK') != -1: command = str(res)[str(res).find('\\r\\n\\r\\n') + len('\\r\\n\\r\\n'):len(str(res)) - 1] new_json_command = ujson.loads(command) if json_command != new_json_command: json_command = new_json_command print(json_command) break except OSError as error: if error.args[0] not in [uerrno.EINPROGRESS, uerrno.ETIMEDOUT]: print( color.red() + '\t\t\t**** ERROR writing ****\n\t\t\t', error, color.normal()) client_socket.close() # flash buffer and close socket del client_socket gc.collect() except OSError as error: print( color.red() + '\t\t\t**** ERROR creating socket ****\n\t\t\t', error, color.normal()) sleep(.1)
def ur(s): print(color.yellow()+'\n Starting ESP_DATA SYSTEM') while True: url = "http://asmon.com.ar:8080/esp_data/dev_cam01" headers = {'X-AIO-Key': 'xxxxxxxxxxxxxxxxxxx', 'Content-Type': 'application/json'} data = json.dumps(machine_data.get()) results={} try: r = requests.post(url, data=data, headers=headers) results = r.json() print(blue,results,normal) except OSError as error: print('\n'+red) print('esp_data failed') print(error) print('\n'+normal) gc.collect() sleep(.1)
def start(): print(color.yellow() + '\n Starting asmon_system') #server_list = get_server_list() #host, port = ['asmon.com.ar','8080'] #server_list[0].split(':') host = 's' h1 = "http://asmon.com.ar:8080/esp_data/dev_cam01" h2 = "http://asmon.com.ar:8081/snap/dev_cam01" if host != 'null': print(color.green() + '\nStart server communication\n' + color.normal()) try: th(plus, ( h1, h2, )) #th.start_new_thread(start_com, (data_address,cam_address,)) except: print(color.red() + 'Error starting com thread' + color.normal()) else: print(color.red() + 'Error starting asmon_system' + color.normal())
async def cli(sc): print(color.yellow() + '\n Starting CAM_SYSTEM:') while True: s = socket.socket() try: s.connect(("marstv.softether.net", 8081)) img = next(pic) s.send(b'%s %d\r\n\r\n' % (hdr['POST'], len(img))) s.send(img) s.send(b'\r\n') result = s.recv(12) while (len(result) > 0): #print(result) #print('\ngot result') break except Exception as e: ee = str(e) if ee != '': print('cam error: ' + ee) s.close() # flash buffer and close socket gc.collect() sleep(.1)
async def start(to): await asyncio.sleep(.24) print(color.yellow() + 'STARTING AP_CAM' + color.normal()) await asyncio.sleep(1) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) a = ('0.0.0.0', 81) s.bind(a) s.listen(2) # queue at most 2 clients s.setblocking(False) while True: try: client, addr = s.accept() ip, port = str(addr[0]), str(addr[1]) print('{') print(color.yellow() + '\tConnection from ' + ip + ':' + port) # Use: poller = uselect.poll() poller.register(client, uselect.POLLIN) res = poller.poll(50) # time in milliseconds if not res: print('\toperation timed out') else: client_data = client.recv(1024) client_data = client_data.decode('utf-8') req = client_data.split(' ') try: print('\t', req[0], '##', req[1], "##", addr) req = req[1].split('/') print('req split', req) except OSError as e: print('\t#failed to split req', e) while True: try: if req[1] == 'live': print('\taccesing', req[1]) client.send(b'%s' % hdr.get('stream')) client.send(b'%s' % hdr.get('frame')) n_try = 0 buf = False cam.light('1') 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(1) n_try = n_try + 1 cam.light('0') #print('\tsending img:', len(buf)) try: while buf: sent = client.send(buf) buf = buf[sent:] #print('\timg sent') except OSError as e: print('send apcam error', e) client.send( b'\r\n') # send and flush the send buffer except OSError as e: print(e) break await asyncio.sleep(.1) client.close() print(color.red() + '\tConnection ' + ip + ':' + port + ' closed' + color.normal() + '\n}') except OSError as e: if str(e) != '[Errno 11] EAGAIN': print(e) await asyncio.sleep(.1)
def start_main_system(arg1,arg2): while True: print(color.yellow()+'Start main_system'+color.normal()) sleep(2)
async def start(to): await asyncio.sleep(.18) print(color.yellow() + 'STARTING AP_SV' + color.normal()) await asyncio.sleep(1) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) a = ('0.0.0.0', 80) s.bind(a) s.listen(2) # queue at most 2 clients s.setblocking(False) while True: try: client, addr = s.accept() ip, port = str(addr[0]), str(addr[1]) print('{') print(color.yellow() + '\tConnection from ' + ip + ':' + port) # Use: poller = uselect.poll() poller.register(client, uselect.POLLIN) res = poller.poll(200) # time in milliseconds if not res: print('\toperation timed out') else: await asyncio.sleep(.1) client_data = client.recv(1024) client_data = client_data.decode('utf-8') req = client_data.split(' ') try: print('\t', req[0], '##', req[1], "##", addr) req = req[1].split('/') print('req split', req) except OSError as e: print('\t#failed to split req', e) if req[1] == 'app': print('\taccesing', req[1]) html = hdr.get_index() index_data = (b'%s' % html) while True: try: while index_data: sent = client.send(index_data) index_data = index_data[sent:] break except OSError as e: print(e) elif req[1] == 'gpio': pin, value = req[2], req[3] command_json = { 'command': 'output_state', 'data': { 'pin': pin, 'value': value } } machine_data.parse_data(command_json) esp_data = machine_data.get() gpio_output = esp_data['gpio']['output_state'] json_response = json.dumps({ 'command': 'output_state', 'output_state': gpio_output }) json_header = hdr.get('json').replace( '$len', str(len(json_response))) client.send(b'%s' % json_header) client.sendall(json_response.encode()) elif req[1] == 'credentials': ssid, psw = req[2], req[3] wifi.set_credentials(ssid + ',' + psw) json_response = json.dumps({ 'command': 'credentials', 'state': 'saved' }) json_header = hdr.get('json').replace( '$len', str(len(json_response))) client.send(b'%s' % json_header) client.sendall(json_response.encode()) elif req[1] == 'user_data': user, id = req[2], req[3] try: id = id.replace('%20', ' ') except: e = '' dev_id = machine_data.get_key('id') machine_data.set( 'command', { 'command': 'popdev', 'dev_id': dev_id, 'user': machine_data.get_key('user') }) machine_data.set('user', user) machine_data.set('id', id) json_response = json.dumps({ 'command': 'user_data', 'state': 'saved' }) json_header = hdr.get('json').replace( '$len', str(len(json_response))) client.send(b'%s' % json_header) client.sendall(json_response.encode()) client.close() print(color.red() + '\tConnection ' + ip + ':' + port + ' closed' + color.normal() + '\n}') except OSError as e: if str(e) != '[Errno 11] EAGAIN': print(e) 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(.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 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)