-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.py
75 lines (66 loc) · 2.16 KB
/
arduino.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
import serial
import time
import threading
import config
"""
Example data from Arduino:
$ = Begin of line
\r\n = EOL
$34|23|12|34|30.0|50.0|12.1\r\n
$Ultrasonic1|Ultrasonic2|Ultrasonic3|Ultrasonic4|Temp|Hunid|Voltage\r\n
"""
class Arduino:
def __init__(self):
self.ser = serial.Serial('/dev/serial/by-id/usb-Arduino__www.arduino.cc__0042_851393033313512102C2-if00',
9600,
timeout=1
)
self.ser.reset_input_buffer()
self.ultrasonic_last_check = 0.00
def read_data(self, val):
# don't try more than 4 times
for i in range(0, 3, 1):
try:
# is this needed?
self.ser.reset_input_buffer()
data = self.ser.readline()
d = data.decode()
if d[0] == "$":
d = d[1:]
dd = d.split("|")
if val == 0:
return [dd[0], dd[1], dd[2], dd[3]]
else:
return dd[int(val)]
else:
# not a new line from arduino, try again
continue
except:
continue
return "error"
def get_temperature(self):
"""
Reads the temperature from the Arduino. Returns the temperature in degrees Celsius.
:return: float degrees
"""
return float(self.read_data(4))
def get_humidity(self):
"""
Reads the humidity from the Arduino. Returns the humidity as float.
:return: float humidity
"""
return float(self.read_data(5))
def get_voltage(self):
"""
Reads the voltage from Battery through the Arduino. Returns the voltage as float.
:return: float voltage
"""
return float(self.read_data(6))
def get_ultrasonic(self):
"""
Reads the ultrasonic sensors from the Arduino. Returns the ultrasonic as
:return: list
"""
self.ultrasonic_last_check = time.time()
ints = [int(x) for x in self.read_data(0)]
return ints