-
Notifications
You must be signed in to change notification settings - Fork 1
/
MoistureReader.py
50 lines (40 loc) · 1.27 KB
/
MoistureReader.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
import serial
import time
import WriteMoistureToFile as writer
import sys
import re
'''set up the serial connection speed'''
ser = serial.Serial('/dev/ttyACM0', 9600)
print "Don't press enter until the arduino light is orange."
ready = 0
def isReasonable(num):
try:
n = float(num)
if (n < 0) or (n > 1000):
return False
except:
return False
return True
'''main loop'''
while 1:
if ready == 0:
c = 'a'
raw_input('Press Enter to start ')
if len(c) == 1:
'''send write data back to arduino'''
ser.write(c.encode())
'''receive data from the arduino'''
response = ser.readline()
print(response)
ready = 1
else:
response = ser.readline()
if 'bucket' in response:
response = re.sub("[^0-9]", "", response)
if isReasonable(response):
print "bucket water level: " + str(response)
elif 'moisture' in response:
response = re.sub("[^0-9]", "", response)
if isReasonable(response):
writer.sendData(float(response))
print "writing data: " + str(response)