-
Notifications
You must be signed in to change notification settings - Fork 0
/
AirControl.py
46 lines (37 loc) · 1.38 KB
/
AirControl.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
import serial
import time
class AirControl:
def __init__(self):
self.nano = serial.Serial('/dev/ttyUSB1', 115200, timeout=2) #Change port if needed, default /dev/ttyUSB1
def __del__(self):
self.nano.close()
def set_air_on(self):
"""Turns the air relay on
"""
self.nano.write(b'air on\n')
self.nano.write(b'air on\n')
print(' >> AirControl: air on')
def set_air_off(self):
"""Turns the air relay off
"""
self.nano.write(b'air off\n')
self.nano.write(b'air off\n')
print(' >> AirControl: air off')
def get_humidity(self):
"""Returns the current humidity as an integer percentage
"""
environment_string = self.nano.readline().decode('ASCII').rstrip()
humidity_string = environment_string.split(',')[0]
return int(humidity_string)
def get_temperature(self):
"""Returns the current temperature in degrees Celcius as an integer
"""
environment_string = self.nano.readline().decode('ASCII').rstrip()
temperature_string = environment_string.split(',')[1]
return int(temperature_string)
if __name__ == "__main__":
controller = AirControl()
while True:
print(f"Temperature = {controller.get_temperature()}ºC")
print(f"Humidity = {controller.get_humidity()}%")
time.sleep(1)