-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduino_comm.py
More file actions
66 lines (60 loc) · 2.64 KB
/
arduino_comm.py
File metadata and controls
66 lines (60 loc) · 2.64 KB
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
import json
import serial
import time
import datetime
def connect_to_arduino():
ser = serial.Serial('/dev/ttyACM3', baudrate=9600,timeout=1) # definiamo la connessione seriale con l'arduino sulla porta,con bump 9600 e delay di 1 secondo
ser.flush()
return ser
if __name__ == "__main__":
ser = serial.Serial('/dev/ttyACM2', baudrate=9600,timeout=1) # definiamo la connessione seriale con l'arduino sulla porta,con bump 9600 e delay di 1 secondo
ser.flush() # flushiamo il buffer
print("eseguo")
while (True):
time.sleep(1)
if (ser.in_waiting > 0):
# line1=ser.readline().decode("utf-8").rstrip()
# print(line1)
temperature = ser.readline().decode("utf-8").rstrip()
newtemperature = int(float(temperature))
humidity = ser.readline().decode("utf-8").rstrip()
newhumidity = int(float(humidity))
print(f"TEMPERATURE:{newtemperature} HUMIDITY:{newhumidity}%")
if (newtemperature >= 23):
print(f"MAGGIORE >20 {temperature}")
ser.write(b"light\n")
else:
print(f"MINORE <20 {temperature}")
ser.write(b"off\n")
def measure_temp_and_hum():
ser=connect_to_arduino()
if (ser.in_waiting > 0):
temperature = int(float(ser.readline().decode("utf-8").rstrip()))
humidity =int(float(ser.readline().decode("utf-8").rstrip()))
print(f"TEMPERATURE:{temperature} HUMIDITY:{humidity}%")
return None
def monitor_th_measurement(th_list:list):
print("dentro funzione")
ser = serial.Serial('/dev/ttyACM3', baudrate=9600,timeout=1) # definiamo la connessione seriale con l'arduino sulla porta,con bump 9600 e delay di 1 secondo
ser.flush()
print("dopo connessione ad arduino")
while(True):
time.sleep(1)
if (ser.in_waiting > 0):
print("connesso ad arduino")
temperature = int(float(ser.readline().decode("utf-8").rstrip()))
humidity =int(float(ser.readline().decode("utf-8").rstrip()))
now = datetime.datetime.now()
print(f"TEMPERATURE:{temperature} HUMIDITY:{humidity}% TIME:{now.strftime('%Y-%m-%d %H:%M:%S')}")
th_list.append(temperature,humidity,now)
print(str(th_list))
with open('data.json', 'w') as file:
json.dump(th_list, file)
if (temperature >= 23):
print(f"MAGGIORE >20 {temperature}")
ser.write(b"light\n")
else:
print(f"MINORE <20 {temperature}")
ser.write(b"off\n")
else:
print("non connesso")