forked from ESW-NU/autoaquaponics-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetData.py
171 lines (161 loc) · 5.62 KB
/
getData.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#initialize GPIO pins for TDS sensor switch + distance sensor
pin_num = 17
pin_num2 = 27
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pin_num,GPIO.OUT)
GPIO.setup(pin_num2,GPIO.OUT)
import time #need this for sleep and distance sensor
from time import sleep
#import necessary modules and initialize I2C bus
import board
import busio
i2c = busio.I2C(board.SCL, board.SDA)
#import board module (ADS1115)
import adafruit_ads1x15.ads1115 as ADS
#import ADS1x15 library's version of AnalogIn
from adafruit_ads1x15.analog_in import AnalogIn
#import Adafruit DHT22 stuff (humidty)
#import Adafruit_DHT as dht
import adafruit_dht
#DHT = 14 #set DHT's GPIO pin number
dhtDevice = adafruit_dht.DHT22(board.D14, use_pulseio=False)
#import the w1 water temp sensor module
#from w1thermsensor import W1ThermSensor
#wt_sensor = W1ThermSensor()
import glob
import time
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
#create ADS object
ads = ADS.ADS1115(i2c)
ads.gain = 2/3
#single ended mode read for pin 0 and 1
chan = AnalogIn(ads, ADS.P0)
chan1 = AnalogIn(ads, ADS.P1)
#import numpy for NaN
import numpy as np
def getData(last_distance, last_wtemp, last_hum, last_atemp): #main function that calls on all other functions to generate data list
#read w1 water temp sensor
wtemp = getWTemp()
GPIO.output(pin_num,GPIO.HIGH) #turn TDS sensor on
#GPIO.output(pin_num2,GPIO.HIGH)
sleep(0.5)
#call TDS function to get a value while pin is HIGH
if wtemp == np.nan: #use last wtemp value if it's NaN
TDS = getTDS(last_wtemp)
else:
TDS = getTDS(wtemp)
GPIO.output(pin_num,GPIO.LOW) #turn TDS sensor off
#GPIO.output(pin_num2,GPIO.LOW)
sleep(0.5)
#define readings from ADC
pH = -5.82*chan.voltage + 22.1 #calibrated equation
#pH = chan.voltage
#read air temp and air humidity
atemp, hum = getDHT()#dht.read_retry(dht.DHT22, DHT)
if hum == np.nan or atemp == np.nan:
hum, atemp = last_hum, last_atemp
distance = getDistance(last_distance)
#make sure distance is the last value on this list
return pH, TDS, 15, 20, wtemp, distance
#DS18B20 functions
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def getWTemp():
lines = read_temp_raw()
#print(lines)
if len(lines) > 0: #only index below if lines is not empty
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
#print("temp_c = " + str(temp_c))
return temp_c
else:
print("READING DS18B20 AGAIN!")
return getWTemp() #rerun function again
#TDS sensor function
def getTDS(wtemp):
Vtds_raw = chan1.voltage #raw reading from sensor right now
TheoEC = 684 #theoretical EC of calibration fluid
Vc = 1.085751885 #v reading of sensor when calibrating
temp_calibrate = 23.25 #measured water temp when calibrating
rawECsol = TheoEC*(1+0.02*(temp_calibrate-25)) #temp compensate the calibrated values
K = (rawECsol)/(133.42*(Vc**3)-255.86*(Vc**2)+857.39*Vc)#defined calibration factor K
EC_raw = K*(133.42*(Vtds_raw**3)-255.86*(Vtds_raw**2)+857.39*Vtds_raw)
EC = EC_raw/(1+0.02*(wtemp-25)) #use current temp for temp compensation
TDS = EC/2 #TDS is just half of electrical conductivity in ppm
return TDS
#DHT function
def getDHT():
temperature_c = np.nan
humidity = np.nan
while is_nan(temperature_c) or is_nan(humidity):#test to see if the value is still nan
#print('Running DHT...') #can comment out later, just here to test reliability
try:
# get temp and humidity
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
temperature_c = float('NaN')
humidity = float('NaN')
#continue
except Exception as error:
dhtDevice.exit()
raise error
return temperature_c, humidity
def is_nan(x): #used in DHT function
return (x is np.nan or x != x)
def getDistance(last_distance): #output distance in cm
#setup distance sensing stuff
new_reading = False
counter = 0
GPIO_TRIGGER = 6 #set GPIO Pins
GPIO_ECHO = 18
GPIO.setup(GPIO_TRIGGER, GPIO.OUT) #set GPIO direction (IN / OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
# set Trigger to HIGH
StopTime = time.time()
GPIO.output(GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00006)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
pass
counter += 1 #stop loop if it gets stuck
if counter == 5000:
new_reading = True
break
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
pass
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
if new_reading:
return last_distance
else:
return (TimeElapsed * 34300)/2
'''
from time import sleep
from datetime import datetime
while True:
print('updating...')
print(datetime.now().strftime("%m/%d/%Y %H:%M:%S"),getData(1, 1, 1, 1))
sleep(1)
'''