-
Notifications
You must be signed in to change notification settings - Fork 0
/
salinity_calibrate.py
100 lines (79 loc) · 3.12 KB
/
salinity_calibrate.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
# Luke Myers
# 5/28/2024
# Uses information from the DF Robot EC sensor sample code, available here:
# https://github.com/DFRobot/DFRobot_PH
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
import temp
# Create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# Create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)
# Create the mcp object
mcp = MCP.MCP3008(spi, cs)
# Create an analog input channel on pin 1
chan1 = AnalogIn(mcp, MCP.P1)
def calibrate(voltage, temperature):
try:
file = open("salinity_calibration.txt", 'r+')
except:
print("Error opening salinity_calibration.txt")
quit()
ec = 1000 * voltage / 820.0 / 200.0
if ec > 0.9 and ec < 1.9:
compensated_solution_ec = 1.413 * (1.0 + 0.0185 * (temperature - 25.0))
temp_k = 820.0 * 200.0 * compensated_solution_ec / 1000.0 / voltage
temp_k = round(temp_k, 2)
print("1.413 ms/cm buffer solution detected")
lines = file.readlines()
lines[0] = 'kvalueLow=' + str(temp_k) + '\n'
file.seek(0)
file.writelines(lines)
print("1.413 ms/cm calibration complete")
elif ec > 9.0 and ec < 16.8:
compensated_solution_ec = 12.88 * (1.0 + 0.0185 * (temperature - 25.0))
temp_k = 820.0 * 200.0 * compensated_solution_ec / 1000.0 / voltage
temp_k = round(temp_k, 2)
print("12.88 ms/cm buffer solution detected")
lines = file.readlines()
lines[1] = 'kvalueHigh=' + str(temp_k) + '\n'
file.seek(0)
file.writelines(lines)
print("12.88 ms/cm calibration complete")
else:
print("no calibration solution detected")
file.close()
def reset():
try:
file = open("salinity_calibration.txt", 'w+')
except:
print("Error opening salinity_calibration.txt")
quit()
low_k = 1.0
high_k = 1.0
file.write("kvalueLow=" + str(low_k) + '\n')
file.write("kvalueHigh=" + str(high_k))
file.close()
print("Calibration reset to default.")
if __name__ == "__main__":
while(True):
print("Enter 'c' when the probe is placed within 1.413 or 12.88 ms/cm calibration solution.\nPress Ctrl-C to quit calibration.")
print("If this package has a temperature sensor, it will be used to compensate for temperature.\nPlease place the temperature sensor in the calibration solution as well.")
print("Enter 'r' to reset calibration to default.")
command = input("Enter command: ")
if command == 'c':
temp_dir = '/sys/bus/w1/devices/'
temp_folder = temp.find_device(temp_dir)
temp_file = temp_folder + '/w1_slave'
# temperature compensation
temperature = temp.read(temp_file)
if temperature == -997:
temperature = 25.0 # if sensor error, use default temp of 25C
calibrate(chan1.voltage * 1000, temperature)
elif command == 'r':
reset()
else:
print("Please enter a valid command.")