Hi Karan
I receive sending errors, so here is the listing (still under development):
Weer_display.py
Note the dht_sensor_type below may need to be changed depending on which DHT sensor you have:
0 - DHT11 - blue one
1 - DHT22 - white one, aka DHT Pro or AM2302 - comes with the GrovePi+ Starter Kit
2 - DHT21 - black one, aka AM2301
from grovepi import *
from grove_rgb_lcd import *
import datetime
import grovepi
relay = 2
switch = 3
led = 4
water = 5
dht_sensor_port = 7 # DHt sensor - port 7
dht_sensor_type = 0
grovepi.pinMode(relay,“OUTPUT”)
grovepi.pinMode(switch,“INPUT”)
grovepi.pinMode(led,“OUTPUT”)
grovepi.pinMode(water,“INPUT”)
Definier de switch toestanden
import itertools
mySW = itertools.cycle([0,1]).next
LED aanzetten
digitalWrite(led,1)
#Startminuut bepalen
beg_min = datetime.datetime.now().strftime(’%M’) #Minuten van de klok
nu_min = beg_min
Om erzeker van te zijn dat er bij opstart onmiddellijk een melding is
Datum en tijd
nu = datetime.datetime.now().strftime(’%d-%m-%y %H:%M’)
Declaratie van menu_val
menu_val = 0
beg_mail_val = 0
#NuVal = beg_mail_val
#Weer
[temp,hum ] = dht(dht_sensor_port,dht_sensor_type) #Get the temperature and Humidity from the DHT sensor
t = str(temp)
h = str(hum)
setRGB(40,200,200)
Mess = “T:” + t + “C H:” + h + "%"
Mess = Mess + “\n” + nu
Meetfunctie bepalen
def Meet(waarde):
#print "Meting minuut: " + datetime.datetime.now().strftime(’%M’) + " minuten"
global Mess
#Datum en tijd
nu = datetime.datetime.now().strftime(’%d-%m-%y %H:%M’)
#Weer
[temp,hum ] = dht(dht_sensor_port,dht_sensor_type) #Get the temperature and Humidity from the DHT sensor
t = str(temp)
h = str(hum)
if waarde == 1:
nu = datetime.datetime.now().strftime('%d-%m-%y * %H:%M')
else:
nu = datetime.datetime.now().strftime('%d-%m-%y %H:%M')
Mess = "T:" + t + "C H:" + h + "%"
Mess = Mess + "\n" + nu
print "temp =", temp, "C\thumidity =", hum,"%" + " - " + nu
setText(Mess)
Gmail zendfunctie
Zendmail = 1
def ZendMail(body):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "freddy.cluckers@gmail.com"
toaddr = "freddy.cluckers@gmail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['SUbject'] = "Waarschuwing van Freddy's Raspberry"
msg.attach(MIMEText(body,'plain'))
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(fromaddr,"prutsatir")
text=msg.as_string()
server.sendmail(fromaddr,toaddr,text)
server.quit()
Eerste maal bij opstarten
Meet(1)
while True:
try:
# Menu
if grovepi.digitalRead(switch) == 1:
menu_val = mySW()
print "Start menu: " + str(menu_val)
# Tijd op dit ogenblik bepalen
nu_min = datetime.datetime.now().strftime('%M') #Minuten van de klok
# Water en relay bediening
pomp = grovepi.digitalRead(water)
print 'Pomp: ' + str(pomp)
grovepi.digitalWrite(relay,pomp)
Meet(pomp)
if pomp == 1:
Zendmail = 1
else:
if Zendmail == 1:
boodschap = 'Geen water meer in de regenton.\nGelieve zo snel mogelijk bij te vullen.\nVerzonden op: '
ZendMail(boodschap + nu)
print 'E-mail: ' + boodschap + nu
Zendmail = 0
#stuur e-mail en stop de pomp
# Uitlezen van de display om de minuut
if str(nu_min) <> str(beg_min):
print 'Uitlezen naar display - ' + Mess
Meet(pomp)
setText(Mess)
beg_min = nu_min
time.sleep(0.5)
except KeyboardInterrupt:
setText('')
setRGB(0,0,0)
digitalWrite(led,0)
time.sleep(0.5)
grovepi.digitalWrite(relay,0)
break
except (IOError,TypeError) as e:
print "Error"