-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoistureDetection.py
109 lines (89 loc) · 3.09 KB
/
moistureDetection.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
import sys
sys.path.append('/home/pi/py/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCDPlate')
from time import sleep
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
import mcp3008
from controlmypi import ControlMyPi
import logging
import datetime
import pickle
from genericpath import exists
import smtplib
lcd = Adafruit_CharLCDPlate()
PICKLE_FILE = '/home/pi/py/moisture/moist.pkl'
def on_msg(conn, key, value):
pass
def append_chart_point(chart, point):
if len(chart) >= 48:
del chart[0]
chart.append(point)
return chart
def save(data):
output = open(PICKLE_FILE, 'wb')
pickle.dump(data, output)
output.close()
def load(default):
if not exists(PICKLE_FILE):
return default
pkl_file = open(PICKLE_FILE, 'rb')
data = pickle.load(pkl_file)
pkl_file.close()
return data
def update_lcd(m):
try:
lcd.home()
lcd.message("Moisture level:\n%d%% " % m)
if m < 15:
lcd.backlight(lcd.RED)
elif m < 50:
lcd.backlight(lcd.YELLOW)
else:
lcd.backlight(lcd.GREEN)
except IOError as e:
print e
def send_gmail(from_name, sender, password, recipient, subject, body):
'''Send an email using a GMail account.'''
senddate=datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d')
msg="Date: %s\r\nFrom: %s <%s>\r\nTo: %s\r\nSubject: %s\r\nX-Mailer: My-Mail\r\n\r\n" % (senddate, from_name, sender, recipient, subject)
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(sender, password)
server.sendmail(sender, recipient, msg+body)
server.quit()
logging.basicConfig(level=logging.INFO)
p = [
[ ['G','moist','level',0,0,100], ['LC','chart1','Time','Value',0,100] ],
]
c1 = load([])
readings = []
conn = ControlMyPi('[email protected]', 'password', 'moisture', 'Moisture monitor', p, on_msg)
delta = datetime.timedelta(minutes=30)
next_time = datetime.datetime.now()
delta_email = datetime.timedelta(days=1)
next_email_time = datetime.datetime.now()
if conn.start_control():
try:
while True:
dt = datetime.datetime.now()
m = mcp3008.read_pct(5)
readings.append(m)
update_lcd(m)
to_update = {'moist':m}
# Update the chart?
if dt > next_time:
# Take the average from the readings list to smooth the graph a little
avg = int(round(sum(readings)/len(readings)))
readings = []
c1 = append_chart_point(c1, [dt.strftime('%H:%M'), avg])
save(c1)
next_time = dt + delta
to_update['chart1'] = c1
conn.update_status(to_update)
#Send an email?
if dt > next_email_time:
next_email_time = dt + delta_email
if m < 40:
send_gmail('Your Name', '[email protected]', 'password', '[email protected]', 'Moisture sensor level', 'The level is now: %s' % m)
sleep(30)
finally:
conn.stop_control()