
Darren Darkings
Python Projects
Lamp Controller
Python
I have a Raspberry Pi computer in the living room and it has one sole purpose which is to switch the lamp on and off. It comes on based on the sunset times for each day stored in a MySQL database on the Pi. It switches off at a fixed time each night. It emails me when it turns the lamp on or off. The lamp can be turned on or off at any time using Lamp Override code listed in the next segment. The code for controlling the lamp on the schedule is as follows developed in Python
#!/usr/bin/python
import energenie
import MySQLdb
import sys
import os
import mimetypes
from energenie import switch_on, switch_off
from time import sleep
from smtplib import SMTP_SSL as SMTP
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
import datetime
def SendMail(header, subject):
try:
SMTPserver = '######'
sender = '######'
destination = '######'
username = '######'
password = '######'
text_subtype = 'plain'
msg = MIMEText(subject, text_subtype)
msg['Subject'] = header
msg['From'] = sender
conn = SMTP(SMTPserver)
conn.set_debuglevel(False)
conn.login(username, password)
conn.sendmail(sender, destination, msg.as_string())
except:
pass
finally:
conn.close()
def GetSunSetTimeFromDB(LastTime):
# Open database connection
try:
db = MySQLdb.connect('192.168.0.14','','','' )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute('select SunSet from forecast order by id desc limit 1')
numrows = cursor.rowcount
for x in xrange(0,numrows):
row = cursor.fetchone()
SunSet = row[0]
cursor.close()
db.close()
return SunSet
except KeyboardInterrupt:
db.close()
switch_off(1)
except:
return LastTime
def UpdateLampDB(Status):
# Open database connection
db = MySQLdb.connect('192.168.0.16','','','' )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
if Status == 'Off':
cursor.execute("update LivingRoom set LampOn = 'N'")
else:
cursor.execute("update LivingRoom set LampOn = 'Y'")
numrows = cursor.rowcount
db.commit()
cursor.close()
db.close()
def main():
LastTime = '3:41 pm'
try:
LampOn = 0
while True:
SunSet = GetSunSetTimeFromDB(LastTime)
LastTime = SunSet
if TestTime(LastTime) == 1:
switch_on(1)
if LampOn == 0:
subject = 'Turned Lamp On. Sunset Time = %s' %(SunSet)
header = 'Turned Lamp On'
SendMail(header,subject)
UpdateLampDB('On')
LampOn = 1
else:
switch_off(1)
if LampOn == 1:
subject = 'Turned Lamp Off as Time = %s' %('21:50')
header = 'Turned Lamp Off'
SendMail(header,subject)
UpdateLampDB('Off')
LampOn = 0
sleep(120)
except KeyboardInterrupt:
UpdateLampDB('Off')
switch_off(1)
def TestTime(LastTime):
dt = datetime.datetime.now()
TestHour = dt.hour
TestMinutes = dt.minute
OffTimeHours = 21
OffTimeMinutes = 50
OffTimeTotalMins = (OffTimeHours * 60) + OffTimeMinutes
SunSet = LastTime
AddHours = 12
SunSetHours = SunSet[0:1]
SunSetMins = SunSet[2:5]
SunSetHoursInt = int(SunSetHours) + AddHours
SunSetMinsInt = int(SunSetMins)
SunSetTotalMins = (60 * SunSetHoursInt) + SunSetMinsInt - 30
CurrentTimeTotalMins = (TestHour * 60) + TestMinutes
if CurrentTimeTotalMins > SunSetTotalMins and CurrentTimeTotalMins < OffTimeTotalMins:
LampOn = 1
else:
LampOn = 0
return LampOn
if __name__ == '__main__':
main()
Daily Weather Forecasts
Python
{Describe Here}