-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoInputTest.py
More file actions
201 lines (182 loc) · 6.03 KB
/
ArduinoInputTest.py
File metadata and controls
201 lines (182 loc) · 6.03 KB
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
#!/usr/bin/python
#reading data from Arduino
#Every 86400 seconds is 1 day.
#Version 4 incorporates MQTT messaging, database function moved to server
#Version 5 incorporates Process restart
#Version 6 changes over to UART connection through pins 8 and 10
#Version 6+ controlled via git
import serial
import string
import time
import math
import paho.mqtt.client as mqtt
import traceback
import sys
import atexit
from gpiozero import LED
pulsesPerLiter = 330 #pulses per liter for flow meter
conversion = 5.5 #convert pulses per second to L/min
pauseWF = False
pauseWV = False
waterVolume=0.0
waterVolumeCum = 0.0
prevWaterVolumeCum = 0.0
index = 0
serialData=""
broker_address="192.168.50.201"
client = mqtt.Client("WaterFlowPi")
client.connect(broker_address)
client.loop_start()
def on_exit():
client.loop_stop()
print("client loop stopped")
atexit.register(on_exit)
def initiateSerial():
try:
global ser
print("Connecting")
ser = serial.Serial('/dev/ttyS0', 2400, 8, 'N',1,timeout=1)
ser.flushInput()
except:
#To try to reconnect if the first connection fails
traceback.print_exc()
time.sleep(5)
ser.flushInput()
initiateSerial()
def getArduinoData():
try:
serialData = ser.readline()
string = str(serialData)
array = string.split(",") #split the converted serial data into usable values as string
if len(array)<7:
Print("Array length ",len(array))
getArduinoData()
except:
print("Failed to getArduinoData",len(array))
getArduinoData()
print(array)
return array
initiateSerial()
led = LED(12)
led.on() #High to keep Arduino from resetting
while True:
getArduinoData()
index += 1
if index==10:
# on_exit()
# time.sleep(10)
# initiateSerial()
print("Index at {}".format(index))
led.off()
time.sleep(1)
led.on()
index = 0
def getCurrentTime():
#timeNow = time.localtime()
#year = time.localtime().tm_year
month = time.localtime().tm_mon
day = time.localtime().tm_mday
hour = time.localtime().tm_hour
minute = time.localtime().tm_min
second = time.localtime().tm_sec
return month,day,hour,minute,second
#message (month,day,hr,minute,sec,flow_rate,pump_i,house_v)
def msgWaterFlow(flowRate,pumpCurrent,arduinoData):
timeRunning = math.trunc(float(arduinoData[1])) #eliminate decimals. This values increments seconds
#convert the string serial data to numerical values
blockTime = int(arduinoData[2])
houseVoltage = float(arduinoData[7])
mn,dy,hr,mi,sec = getCurrentTime()
messageWF = ('"'+str(mn)+","+str(dy)+","+str(hr)+","+str(mi)+","+str(sec)+","+str(flowRate)+","
+str(round(pumpCurrent,2))+","+str(houseVoltage)+'"')
#print(messageWF)
client.publish("FlowSensorPi/WaterFlow",messageWF)
def msgWaterVolume(pulseCount2,waterVolumeCum):
global prevWaterVolumeCum
waterVolume = pulseCount2/pulsesPerLiter
waterVolumeCumUpdate = waterVolumeCum + waterVolume
mn,dy,hr,mi,sec = getCurrentTime()
#print(type(hr))
messageWV = ('"'+str(mn)+","+str(dy)+","+str(hr)+","+str(mi)+","+str(round(waterVolume,2))+","
+str(round(waterVolumeCumUpdate,2))+","+str(round(prevWaterVolumeCum,2))+'"')
#print(messageWV)
client.publish("FlowSensorPi/WaterVolume",messageWV)
if hr==23:
#print("in 1st if statement")
if mi==59:
#print("in reset if statement")
prevWaterVolumeCum = waterVolumeCumUpdate
#print(prevWaterVolumeCum)
ser.flushInput()
waterVolumeCumUpdate = 0
#print(waterVolumeCumUpdate)
return waterVolumeCumUpdate
def runFlowSensorPi():
global pauseWF,pauseWV,waterVolumeCum
while True:
if ser.in_waiting>0:
try:
#print("serial data >0")
arduinoData = getArduinoData()
flowRate = round(float(arduinoData[4])/conversion,2) #Divide by Conversion factor to get L/min.
pumpCurrent = float(arduinoData[6]) #Pump Current
pulseCount2 = int(arduinoData[5])
except:
runFlowSensorPi()
traceback.print_exc()
if flowRate == 0:
if pumpCurrent == 0:#to stop saving a lot of null data with no flow
# allows 1 0 flow data to go through to verify that the data stopped when it is supposed to
if pauseWF:
pass
else:
#print("1",flowRate,pumpCurrent,arduinoData)
msgWaterFlow(flowRate,pumpCurrent,arduinoData)
pauseWF = True
else:
pauseWF = False
#adds 1 non zero data to the list
msgWaterFlow(flowRate,pumpCurrent,arduinoData)
else:
pauseWF = False
#adds 1 non zero data to the list
msgWaterFlow(flowRate,pumpCurrent,arduinoData)
cycle = int(arduinoData[3])
#print ("cycle = ",cycle)
if cycle == 1:
#print("in if statement")
waterVolumeCum = msgWaterVolume(pulseCount2,waterVolumeCum)
ser.flushInput()
time.sleep(1)
else:
print("Serial = 0")
time.sleep(1)
def restartProgram():
#Restarts program with file objects and cleanup
try:
os.execl(sys.python3, os.path.abspath(ArduinoInput4.py))
except:
print("Failed to Restart")
time.sleep(10)
restartProgram()
"""
try:
initiateSerial()
runFlowSensorPi()
except ValueError:
time.sleep(2)
traceback.print_exc()
runFlowSensorPi()
except UnboundLocalError:
time.sleep(1)
traceback.print_exc()
runFlowSensorPi()
except IndexError:
time.sleep(1)
traceback.print_exc()
runFlowSensorPi()
except OSError:
traceback.print_exc()
time.sleep(5)
#restartProgram()
"""