forked from OnionIoT/smart-plant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurementHelper.py
64 lines (49 loc) · 1.61 KB
/
measurementHelper.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
MAX_MEASUREMENT_VALUE = 1023
# read analog value (0-1023) from the microcontroller
# returns None if value is not read successfully
def readMoistureLevel(serialPort):
# need to write an 'r' character to trigger a measurement response
serialPort.write('r')
# read the response
try:
value = serialPort.readline()
if value == "":
print("Got blank value!")
value = None
else:
value = value.rstrip() #chomp the newline at the end of the response
except:
value = None
return value
# record the latest measurement, ensure the list of measurements is
# also performs error checking
def recordMeasurement(currentMeasurement, measurementList, maxMeasurements):
# check input
if currentMeasurement is None:
return measurementList
# convert the measurement string into an integer
try:
value = int(currentMeasurement)
except:
return measurementList
# convert the raw measurement into a percent
percent = getMeasurementAsPercent(value)
# add it to the list
measurementList.append(percent)
# remove the first element from the list
if len(measurementList) > maxMeasurements:
measurementList = measurementList[1:]
return measurementList
# return the average of all of the measurements
def getAverageMeasurement(measurementList):
# find the sum of all list elements
sum = 0
for val in measurementList:
sum = sum + val
# find the average
average = int( sum / float(len(measurementList)) )
return average
# convert an analog microcntroller reading into a percentage
def getMeasurementAsPercent(measurement):
percent = int( (float(measurement) / float(MAX_MEASUREMENT_VALUE)) * 100.0)
return percent