-
Notifications
You must be signed in to change notification settings - Fork 0
/
basinMaster.py
352 lines (259 loc) · 11.2 KB
/
basinMaster.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python
"""
Copyright(C) 2021, BrucesHobbies
All Rights Reserved
AUTHOR: BrucesHobbies
DATE: 02/04/2021
REVISION HISTORY
DATE AUTHOR CHANGES
yyyy/mm/dd --------------- -------------------------------------
2021/02/10 BrucesHobbies added pubScribe.py
OVERVIEW:
This program supports multiple different types of range and pressure
sensors to estimate water column depth. Logging of the pressure
measurements,
Current list of sensors:
- HC_SR04 Ultrasonic range finder measures range to surface of water
Pros: low cost, simple
Cons: potential reflection off other objects - solve using PVC pipe with holes
- Honeywell ABP pressure sensors
Pros: good accuracy
Cons: pressure leaks from tubes, add bubbler pump to presurize before taking measurement
(low-cost aquarium type bubbler (<2-watts) with a "T" in air supply hose to sensor)
Future sensor options:
- US-100 (3.3V, echo time serial)
- GP2YOA21
- VL53L3CX
LICENSE:
This program code and documentation are for personal private use only.
No commercial use of this code is allowed without prior written consent.
This program is free for you to inspect, study, and modify for your
personal private use.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import sys
import os
import time
import datetime
import math
import pubScribe
#
# === User configuration === RPi pins are defined in individual files
#
ENABLE_HC_SR04 = 1 # Ultra-sonic range finder option
ENABLE_HNY_ABP = 1 # Honeywell ABP pressure sensor option
# ABP for Amplified Basic Pressure sensor
# ABP_SENSOR = "060MG2" # ABP sensor model, see sensorHnyAbp for details
ABP_SENSOR = "001PDS" # ABP sensor model, see sensorHnyAbp for details
# US for UltraSonic ranging sensor
US_MEAS_AVERAGING = 11 # Number of ultra-sonic measurements to average
US_WELL_DEPTH = 18 # Depth in inches from bottom of sump well to ultra-sonic sensor
# csv log file
depthGaugeLogEnable = 1
# inches of water column delta required before logging
US_GAUGE_DELTA_LOG = 0.5
ABP_GAUGE_DELTA_LOG = 0.5
depthGaugeLogAll = 1 # ignore delta log params and log all measurements
measTime = 60 # Seconds between water depth measurements
pumpOnTime = 5 # Seconds to run pump for ABP
pumpOffTime = 5 # Seconds to wait after running pump before reading pressure
PUMP_ON = True # True = logic level high for on, False = low for on
#
# --- User Email Alerts Configuration ---
#
# First time program starts, it will ask you for the sender's email
# this should be an email that you have established for sending alerts from this program
# gmail is suggested with "Less Secure App Access" turned on. This is required for Python on the RPI.
# If you change passwords, please delete cfgData.json so that this program will again ask for the password.
#
alertMsgEnabled = 0 # non zero enables sending of email / SMS text messages
statusMsgEnabled = 1 # non zero enables sending of email / SMS text messages
statusMsgHHMM = [12, 30] # Status message time to send [hh, mm]
WATER_DEPTH_ALERT = 9 # Send alert when water depth greater than this many inches
WATER_DEPTH_ALERT_ENABLE = 1 # Enable sending alerts
minIntervalBtwWaterEmails = 24*3600 # seconds
#
# === END USER CONFIGURATION ===
#
if ENABLE_HC_SR04 or ENABLE_HNY_ABP:
# Needed in both cases for gpio functions
import hc_sr04_range
if ENABLE_HNY_ABP :
import sensorHnyAbp
abp = []
measCnt = 0 # updated in timer loop
pumpOnCnt = 0 # Calculated during initialization
pumpOffCnt = 0 # Calculated during initialization
# Last readings
last_us_result = -99.0
last_abp_result = -99.0
last_us_t = time.localtime()
last_abp_t = last_us_t
# Last log values
last_us_log = -99.0
last_abp_log = -99.0
us_meas = [] # Ultrasonic measurements for averaging
#
# Initial range and depth sensors
#
def gaugeInit(tInterval) :
global abp, measCnt, pumpOnCnt, pumpOffCnt
measCnt = int(measTime/tInterval) - 1
pumpOffCnt = int(pumpOffTime/tInterval)
if pumpOffTime % tInterval :
pumpOffCnt += 1
pumpOnCnt = int(pumpOnTime/tInterval)
if pumpOffCnt < 1 :
pumpOffCnt = 1
if pumpOnCnt < 1 :
pumpOnCnt = 1
pumpOnCnt += pumpOffCnt
if pumpOnTime % tInterval :
pumpOnCnt += 1
if measCnt < pumpOnCnt :
measCnt = pumpOnCnt
if measCnt <= US_MEAS_AVERAGING :
measCnt = US_MEAS_AVERAGING + 1
if ENABLE_HC_SR04 or ENABLE_HNY_ABP:
hc_sr04_range.sensorInit() # Needed in both cases for gpio functions
if ENABLE_HNY_ABP :
print("Water depth pressure sensor:")
abp = sensorHnyAbp.SensorHnyAbp(ABP_SENSOR)
return
#
# Called before program exits
#
def gaugeClose() :
if ENABLE_HC_SR04 or ENABLE_HNY_ABP :
hc_sr04_range.sensorClose() # Close gpio functions
#
# Read water depth sensors
#
def gaugeRead(tInterval) :
global last_us_result, last_us_t, last_abp_result, last_abp_t
global measCnt, us_meas
global last_us_log, last_abp_log
us_result = -99
abp_result = -99
csv_str = ""
deltaLogResult = 0
if measCnt :
# Pump cycle on-off logic prior to measurement
if ENABLE_HNY_ABP and (measCnt <= pumpOnCnt) :
if (measCnt > pumpOffCnt) :
hc_sr04_range.pump(PUMP_ON) # Low-cost aquarium bubbler pump to pressurize depth tube
else :
hc_sr04_range.pump(not PUMP_ON)
if ENABLE_HC_SR04 and (measCnt <= US_MEAS_AVERAGING) :
# Series of range measures to average
dist = hc_sr04_range.sensorRead()
if dist :
us_meas.append(dist)
measCnt -= 1
else :
measCnt = int(measTime/tInterval) - 1
t = time.localtime()
if ENABLE_HC_SR04 :
# Average ultrasonic measurements
cnt = len(us_meas)
if cnt > (US_MEAS_AVERAGING // 2) :
mean = sum(us_meas) / cnt
# variance = sum([((x - mean) ** 2) for x in us_meas]) / cnt
# res = variance ** 0.5
us_result = round(US_WELL_DEPTH - mean, 2)
if us_result != -99 :
last_us_result = us_result
last_us_t = t
if (abs(us_result - last_us_log) > US_GAUGE_DELTA_LOG) :
deltaLogResult = 1
last_us_log = us_result
us_meas = []
if ENABLE_HNY_ABP :
# Get pressure reading
status, result, tempC = abp.readAbpStatusTemp()
if status == 0 :
abp_result = round(abp.pres2inwc(result),2)
if abp_result != -99 :
last_abp_result = abp_result
last_abp_t = t
if (abs(abp_result - last_abp_log) > ABP_GAUGE_DELTA_LOG) :
deltaLogResult = 1
last_abp_log = abp_result
else :
print("Water depth pressure sensor fault...")
if (depthGaugeLogEnable and (deltaLogResult or depthGaugeLogAll)) :
topic = "basinMaster/WaterDepth"
data = {"Ultrasonic (in)": round(us_result,2), "ABP (in)": round(abp_result,2)}
pubScribe.pubRecord(pubScribe.CSV_FILE, topic, data)
return us_result, abp_result
#
# Send alert via email to another email or as SMS text
#
def sendAlert(subj, alertMsg) :
topic = "basinMaster/Alert"
pubScribe.pubRecord(pubScribe.EMAIL_SMS, topic, alertMsg)
#
# Send status via email to another email or as SMS text
#
def sendStatus() :
s = time.strftime("\n%a, %d %b %Y %H:%M:%S ", last_us_t)
s = s + "US: {: 6.2f}\n".format(last_us_result)
s = s + time.strftime("%a, %d %b %Y %H:%M:%S ", last_abp_t)
s = s + "ABP: {: 6.2f}\n".format(last_abp_result)
topic = "basinMaster/Status"
pubScribe.pubRecord(pubScribe.EMAIL_SMS, topic, s)
#
# Test / debug main
#
if __name__ == '__main__':
# tInterval = 0.5
# tInterval = 1
tInterval = 2
waterDepthLastEmailTime = 0
lastStatusMsg = 0
pubScribe.connectPubScribe()
gaugeInit(tInterval)
if statusMsgEnabled :
topic = "basinMaster/Status"
pubScribe.pubRecord(pubScribe.EMAIL_SMS, topic, "Program start")
print("\nPress CTRL+C to exit...\n")
print("First set of measurement will display in a few minutes...\n")
try :
while (True) :
usMeas, abpMeas = gaugeRead(tInterval)
if usMeas!=-99 or abpMeas!=-99 :
s = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime())
print("{}Ultrasonic depth= {: 6.2f}, ABP depth= {: 6.2f}".format(s, usMeas, abpMeas))
if WATER_DEPTH_ALERT_ENABLE :
if (usMeas != -99) and (usMeas > WATER_DEPTH_ALERT) :
tsec = time.time()
if (tsec > (minIntervalBtwWaterEmails + waterDepthLastEmailTime)) :
# Allowed to send email text message
waterDepthLastEmailTime = tsec
sendAlert("basinMaster Alert", "Water Depth: " + str(usMeas) + "\n")
if (abpMeas != -99) and (abpMeas > WATER_DEPTH_ALERT) :
tsec = time.time()
if (tsec > (minIntervalBtwWaterEmails + waterDepthLastEmailTime)) :
# Allowed to send email text message
waterDepthLastEmailTime = tsec
sendAlert("basinMaster Alert", "ABP Water Depth: " + str(abpMeas) + "\n")
# send daily status email to email or to SMS text
t = datetime.datetime.now()
tsec = time.time()
if (statusMsgEnabled and t.hour==statusMsgHHMM[0] and t.minute==statusMsgHHMM[1] and (tsec >= (lastStatusMsg+12*3600)) ) :
lastStatusMsg = tsec
sendStatus()
time.sleep(tInterval)
except KeyboardInterrupt :
print(" Keyboard interrupt caught.")
gaugeClose()
print("GPIO cleaned up.")
pubScribe.disconnectPubScribe()