-
Notifications
You must be signed in to change notification settings - Fork 0
/
atlas_hydro.py
141 lines (114 loc) · 3.28 KB
/
atlas_hydro.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
import time
from pylibftdi.device import Device
from pylibftdi.driver import FtdiError
from sets import Set
class AtlasDevice(Device):
def read_line(self):
"""
Read the response from the Atlas Sensor
:return:
"""
line_buffer = []
try:
start_time = time.time()
while True:
# read bytes until Carriage Return is received.
next_char = self.read(1) # read one byte
if next_char == "\r": # sensor always ends with CR.
break
line_buffer.append(next_char)
if time.time() - start_time > 1.0: # timeout
line_buffer = ''
break
return ''.join(line_buffer)
except FtdiError:
return ''
def send_cmd(self, cmd):
"""
Send command to the Atlas Sensor.
Before sending, add Carriage Return at the end of the command.
:param cmd:
:return:
"""
buf = cmd + "\r" # add carriage return
try:
self.write(buf)
return True
except FtdiError:
print ("Failed to send command to the sensor.")
return False
def read_sensors():
readings = []
ref_temp = 25
for sensor in sensors:
if sensor["sensor_type"] == "atlas_scientific_temp":
# instantiate atlas scientific temp device
dev = AtlasDevice(sensor["serial_number"])
dev.send_cmd("R")
sensor_reading = dev.read_line()
report_temp = round((float(sensor_reading) * 9 / 5 + 32), sensor["accuracy"])
readings.append(
{
"type": sensor["type"],
"serial_number": sensor["serial_number"],
"sensor_type": sensor["sensor_type"],
"sensor_reading": report_temp
})
if sensor["is_ref"] is True:
ref_temp = sensor_reading # calibration temp for pH
else:
# Set reference temperature sensor on the sensor
dev.send_cmd("T," + str(ref_temp))
# Get the readings from any Atlas Scientific Elec Conductivity sensors
if sensor["sensor_type"] == "atlas_scientific_ec":
# instantiate atlas scientific EC device
dev = AtlasDevice(sensor["serial_number"])
dev.send_cmd("R")
sensor_reading = dev.read_line()
ppm = sensor_reading.split(',')[1]
ec = round((float(sensor_reading.split(',')[0]) / 1000), 2)
readings.append({
'type': sensor["type"],
'serial_number': sensor["serial_number"],
'sensor_type': sensor["sensor_type"],
'ec': ec,
'ppm': ppm
})
if sensor["sensor_type"] == "atlas_scientific_ph":
# instantiate atlas scientific EC device
dev = AtlasDevice(sensor["serial_number"])
dev.send_cmd("R")
sensor_reading = round(float(dev.read_line()), 2)
readings.append({
'type': sensor["type"],
'serial_number': sensor["serial_number"],
'sensor_type': sensor["sensor_type"],
'sensor_reading': sensor_reading
})
return readings
sensors = [{
# TEMP Atlas Scientific Sensor (also reference temp)
"sensor_type": "atlas_scientific_temp",
"type": "hydro-temp",
"is_ref": True,
"serial_number": 'DJ00RVZR',
"accuracy": 2
}, {
# pH Atlas Scientific Sensor
"sensor_type": "atlas_scientific_ph",
"type": "hydro-ph",
"is_ref": False,
"serial_number": 'DJ00RUV8',
"accuracy": 2
}, {
# Atlas Scientific EC Sensor
"sensor_type": "atlas_scientific_ec",
"type": "ec",
"is_ref": False,
"serial_number": 'DJ00RU96',
"accuracy": 2,
}]
read_sensors()
def get_sensor_data():
read_sensors()