-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpServer_recebe1dado.py
97 lines (66 loc) · 2.64 KB
/
udpServer_recebe1dado.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
# Importa pacotes para criação do servidor e manipulação dos dados
import socket
import time
import pandas as pd
# Importa pacotes responsáveis por cuidar da interrupção quando o programa for fechado com 'Ctrl + C'
import signal
import sys
import threading
# Nome do arquivo que armazenará a coleta do SAC-DM
nomeArquivo = 'm5Stick_TR_drone_grande_02_12_2021_com_vento_1'
localIP = "192.168.1.101"
localPort = 20001
bufferSize = 1024
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
x=[]
y=[]
z=[]
timestamp = []
print("UDP server up and listening")
# Cria thread que monitora o fechamento do programa com 'Ctrl + C'
# Quando programa for fechado, executa essa função:
def signal_handler(signal, frame):
print('\n\nSalvando coleta de dados em {}.csv'.format(nomeArquivo))
""" for j in range(len(timestamp)):
df.loc[len(df)] = [str(timestamp[j]), x[j], y[j], z[j]] # Adiciona os novos valores em uma nova linha do DataFrame """
df.to_csv('{}.csv'.format(nomeArquivo))
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
forever = threading.Event()
# Cria DataFrame com as colunas referentes aos dados da coleta
df = pd.DataFrame(columns=['Timestamp','Eixo X', 'Eixo Y', 'Eixo Z'])
# Cria rota que a ESP8266 realizará o http.POST com os resultados do SAC-DM
temp_ini = time.time()
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
print("true")
message = bytesAddressPair[0]
tempo_fim = time.time() - temp_ini
#timestamp.append(time.time() - temp_ini)
address = bytesAddressPair[1]
eixos = format(message)
eixos = eixos.split("'")
print("MSG: {}".format(message))
print("EIXOS: {}".format(eixos[1]))
#LOW M5 BATTERY
if(eixos[1] == "LOW_BATTERY"):
print("|M5| > LOW_BATTERY {}".format(tempo_fim))
print('\n\nSalvando coleta de dados em {}.csv'.format(nomeArquivo))
df.to_csv('{}.csv'.format(nomeArquivo))
sys.exit(0)
else:
eixos = eixos[1].split(',')
#print(eixos)
#print(eixos)
if (len(df)==0):
#print("entrei IF")
df.loc[len(df)] = [str(tempo_fim), eixos[0], eixos[1], eixos[2]]
else:
#print("entrei no else")
if(eixos[0] != df.loc[len(df)-1, 'Eixo X'] or eixos[1] != df.loc[len(df)-1, 'Eixo Y'] or eixos[2] != df.loc[len(df)-1, 'Eixo Z']):
#print("entrei no else if")
df.loc[len(df)] = [str(tempo_fim), eixos[0], eixos[1], eixos[2]]