WebLights Revisited
As a sort of follow up to one of my first posts I thought I'd describe some of the updates to a Rasberry Pi IOT lighting project I put together last year.
Christmas 2014 |
Built from the WebLamp example in the RPi MakeProjects book and using code posted at Matt Richardson's site I was able to reboot the project this year by simply re-establishing the /etc/init.d wrapper program I wrote to initialize the webserver and partner python program. By connecting the lights to my PowerSwitch Tail the setup worked exactly as last year, with only about 10 minuntes of set up required.
New Connections
One enhancement I made this year was to add a better pair of connectors for the power switch connections. For this I used my favourite method of connector for relatively portable projects; stereo 3.5mm audio jacks. One of these, plus some pre-cut solid core wiring, gives a more stable connection which can be easily removed. Since the plugs are cheap and the breakouts built they can be reused for other projects.Design
Not wanting to stop at hardware I also included some soft enhancements this year. First, I decorated the site a little more with some simple HTML formatting. Since the python Flask webserver includes a main page and CSS I just inserted some additional text and format tags as well as a button object to make a prettier webpage:Data
This year I've also added a data gathering element to the project, making it a two way IOT project. To do this I first created a shell table on a MySQL server running on my network, SENSORS.Tree. This table keeps a log of the webpage status and timestamp captured from the weblamp.py webserver program. To do this I added a MySQL connection using the MySQLdb package to my existing program and added a pair of update statements to the existing If structure based on the toggle of the HTML button for the lights:cur.execute("Insert into sensors.tree values ('on', sysdate());")
cur.execute("Insert into sensors.tree values ('off', sysdate());")
These statements (and the MySQLdb connection string) update the SENSORS.Tree table with the status and timestamp at the time the switch was triggered.
After collecting some data I wrote a quick R program to ggplot the statuses for the following chart:
A 5PM On and 2AM Off timer is set |
The code I used for the above was:
library(ggplot2) library(RMySQL) con <- dbConnect(MySQL(), user="user1", password="pass1", dbname="sensors", host="localhost") rs <- dbSendQuery(con, "select * from sensors.tree;") xmas <- fetch(rs) huh <- dbHasCompleted(rs) dbClearResult(rs) dbDisconnect(con) xmas$updated <- as.POSIXct(xmas$updated) xmas$status <- as.factor(xmas$status) ggplot(data=xmas, aes(x=updated, y=status, group=1, colour=status)) + geom_line()