-
Notifications
You must be signed in to change notification settings - Fork 0
/
plantMonitoring.py
147 lines (114 loc) · 4.2 KB
/
plantMonitoring.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
import os
import glob
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
#importing the library that is used to be able to communicate with Raspberry Pi (and sensors attached to it)
import RPi.GPIO as GPIO
import time
count = 0
#GPIO SETUP
channel = 21 #using GPIO 21 pin to connect soil moisture sensor
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
sendNotification=0
def callback(channel):
global count, sendNotification
if GPIO.input(channel):
print("No Moisture Detected!")
count = count+1
print(count)
if count==3 and sendNotification==0:
sendNotification=1
#Takepicture()
emailSent(sendNotification)
print("Mail about needing to water plant sent.")
else:
print("Moisture Detected!")
count = 0
GPIO.add_event_detect(channel, GPIO.BOTH) #lets us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback) #assign function to GPIO PIN, Run function on change
print(count)
#code to detect temperature using the on-board temperature sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')
device_file = device_folder[0]+ '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos + 2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
#sends email from one specified Gmail account (with attachments) to another
def emailSent(sendNotification):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
#for privacy purposes, a made up email address has been entered below
fromaddr = "[email protected]"
toaddr = "[email protected]"
#instance of MIMEMultipart
msg = MIMEMultipart()
#storing the senders email address
msg['From'] = fromaddr
#storing the receivers email address
msg['To'] = toaddr
#storing the subject
msg['Subject'] = "Plant details with moisture update"
#string to store the body of the mail
body = "This is your plant" + ". The current temperature is : "+str(read_temp())+"."
if sendNotification==1:
body=body+"Low moisture...Please water your plant."
#attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))
#open the file to be sent
filename = "Plant.jpg"
attachment = open("/home/pi/Plant.jpg", "rb")
#instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')
#To change the payload into encoded form
p.set_payload((attachment).read())
#encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
#attach the instance 'p' to instance 'msg'
msg.attach(p)
#creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
#start TLS for security
s.starttls()
#Authentication
s.login(fromaddr, "password")
#Converts the Multipart msg into a string
text = msg.as_string()
#sending the mail
s.sendmail(fromaddr, toaddr, text)
#terminating the session
s.quit()
#code that makes use of a camera attached to Raspberry Pi to take a picture of the plant
def Takepicture():
import picamera
print("Capturing")
camera = picamera.PiCamera()
camera.capture("Plant.jpg")
print("Done")
while True:
print(str(read_temp()))
time.sleep(1)