def train(player): print("Training, quit program to finish training") f = open("training_data.csv", "w+") sock = udp.run_server() while True: player.unpause() data = udp.receive(sock) while (data == None): data = udp.receive(sock) to_write = data.decode("utf-8") to_write = to_write.replace(" ", ",") # enemy_loc = to_write[:7] # enemy_dir = '0' # if(enemy_loc == '1,0,0,0'): # enemy_dir = '1' # if(enemy_loc == '0,1,0,0'): # enemy_dir = '2' # if(enemy_loc == '0,0,1,0'): # enemy_dir = '3' # if(enemy_loc == '0,0,0,1'): # enemy_dir = '4' if (to_write[:7] != '0,0,0,0' and to_write[-3:] != '0,0'): # If there are no enemies, don't record any data # f.write(enemy_dir+to_write[7:]+"\n") f.write(to_write + '\n') f.close()
def main(): while True: udp.send(PI_DATA_IP, PI_DATA_UDP_PORT, 'h') hum, addr = udp.receive(PI_CONTROL_UDP_PORT_DATA) hum = float(hum) udp.send(PI_DATA_IP, PI_DATA_UDP_PORT, 't') tem, addr = udp.receive(PI_CONTROL_UDP_PORT_DATA) tem = float(tem) delay = tem ** 2 - tem * 10 - hum * 2 bash.command('sudo systemctl start motor') time.sleep(delay) bash.command('sudo systemctl stop motor') time.sleep(86400 - delay - 1)
def main(): # test humidity access udp.send('localhost', PI_CONTROL_UDP_PORT_AND, 'h') val = udp.receive(ANDROID_UDP_PORT) assert int(val) >= 0 and int(val) <= 100, 'Invalid humidity value!' + val # test temperature access udp.send('localhost', PI_CONTROL_UDP_PORT_AND, 't') val = udp.receive(ANDROID_UDP_PORT) assert int(val) >= -50 and int(val) <= 50, 'Invalid temperature value!' + val # test image access udp.send('localhost', PI_CONTROL_UDP_PORT_AND, 'i') val = tcp.receive(ANDROID_TCP_PORT) assert val != None, 'Did not receive a picture!' assert os.path.isfile(val), 'File not exist!'
def main(): bash.command('sudo systemctl start readSensor') while True: command, address = udp.receive(PI_DATA_UDP_PORT) if command == 'h': udp.send(PI_CONTROL_IP_DATA, PI_CONTROL_UDP_PORT_DATA, '15.2') elif command == 't': time, hum, tem = database.read_data_from_db() udp.send(PI_CONTROL_IP_DATA, PI_CONTROL_UDP_PORT_DATA, '22.4') elif command == 'i': tcp.send(PI_CONTROL_IP_DATA, PI_CONTROL_TCP_PORT_DATA, '~\proj\image.jpg')
def main(): bash.command('sudo systemctl start readSensor') while True: command, address = udp.receive(PI_DATA_UDP_PORT) if command == 'h': time, hum, tem = database.read_data_from_db() udp.send(PI_CONTROL_IP_DATA, PI_CONTROL_UDP_PORT_DATA, str(hum)) elif command == 't': time, hum, tem = database.read_data_from_db() udp.send(PI_CONTROL_IP_DATA, PI_CONTROL_UDP_PORT_DATA, str(tem)) elif command == 'i': path = takePhoto() tcp.send(PI_CONTROL_IP_DATA, PI_CONTROL_TCP_PORT_DATA, path)
def main(): while True: command, address = udp.receive(PI_CONTROL_UDP_PORT_AND) if command == 'h' or command == 't': # humidity or temperature requested udp.send(PI_DATA_IP, PI_DATA_UDP_PORT, command) val, addr = udp.receive(PI_CONTROL_UDP_PORT_DATA) udp.send(address[0], ANDROID_UDP_PORT, val) elif command == 'i': # image requested, udp.send(PI_DATA_IP, PI_DATA_UDP_PORT, command) #tcp.forward(PI_CONTROL_TCP_PORT_DATA, address[0], ANDROID_TCP_PORT) # Due to implementation difficulties, replaces by cloudnary library filename = tcp.receive(PI_CONTROL_TCP_PORT_DATA) url = basic.upload_files(filename) # upload received photo to cloudnary udp.send(address[0], ANDROID_UDP_PORT, url) # send url to android elif command == 'r': # start motor requested bash.command('sudo systemctl start motor') elif command == 's': # stop motor requested bash.command('sudo systemctl stop motor') elif command == 'a': # turn auto irrigation mode on bash.command('sudo systemctl start autoIrrigation') elif command == 'u': # turn auto irrigation mode off bash.command('sudo systemctl stop autoIrrigation')
def main(): while True: command, address = udp.receive(PI_CONTROL_UDP_PORT_AND) if command == 'h': # humidity or temperature requested udp.send(address[0], ANDROID_UDP_PORT, '15.1') elif command == 't': # humidity or temperature requested udp.send(address[0], ANDROID_UDP_PORT, '22.3') elif command == 'i': # image requestedm, tcp.send(address[0], ANDROID_TCP_PORT, '~\proj\image.jpg') elif command == 'r': # start motor requested print('sudo systemctl start motor') elif command == 's': # stop motor requested print('sudo systemctl stop motor') elif command == 'a': # turn auto irrigation mode on print('sudo systemctl start autoIrrigation') elif command == 'u': # turn auto irrigation mode off print('sudo systemctl stop autoIrrigation')
def test_server_udp(): sock = udp.run_server() while True: user_in = input('press r to receive\n') data = udp.receive(sock) print("Received: {}".format(data))