Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Friday, January 2, 2015

2014: a Christmas Tree

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()
Created by Pretty R at inside-R.org

Tuesday, January 14, 2014

2013: a Christmas Tree

After a several years of fumbling with guitar electronics, playing with Arduinos, and now cookng with Raspberry Pis, my interest in the application of DIY electronics has infected the holiday rituals of my girlfriend and I.  Christmas 2013 was smart, in the trendy sense of the term.


Smart and shiny!

Raspberry Pi Powered Web Switch


To make use of my second Raspberry Pi (first Pi's application to be detailed later!) I chose to try out some simple smart relay techniques.  Using an example and inspiration from a great Make published RPi book (Great resource, I found every example useful and fun to try) I decided to use my Pi and Wifi to build a hands free Christmas light set up.  Following the timeless ideal of a creative solution to sometimes disproportionate problem I used my Pi to build a web server based remote for our tree's lights.

This project is a modified form of that found in Matt Richardson's RPi book mentioned above, also found at his website (specifically the WebLamp examples).  The script used for tree lights modified was gratefully modified from the example found at these sources


Materials (In order of coolness)
Raspberry Pi Model B     My first, totally worth the frantic refreshing and wait after pre-order
Power Switch Tail II      Such a great tool, makes me feel like an electrician, without trips to the ER
Adafruit T-Cobbler GPIO Breakout     I have both the standard and "T", T shape looks cool




 Hardware Connection

Pin 25 of RPi/Cobbler to +in of PowerSwitch (controls PSwitch relay)
Ground of RPi/Cobbler to -in of PowerSwitch



Software

I won't repeat all of the great work featured at Matt Richardson's site, except for the alterations I made.  The projecct is based around some simple python work using the python Flask extension, which can be used to support a simple webserver and more.  The python-based code is separated into multiple scripts; the main python code and a templates directory (the main HTML to be used in creating the Christmas Lights webpage).  The modular design makes it easy to modify the webpage for different applications.

By following all of the instructions at these resources you should arrive at a workable Flask-based web server accessible through your Pi's internal IP address or with http://raspberrypi.local for Apple products or Bonjour enabled devices. 

I've updated the HTML in the python files to be a little more festive, but this is purely cosmetic.

Additionally, I added a wrapper shell script to my /etc/init.d directory with sudo execution  on the Pi and updated the default boot list to include this shell program.
The wrapper shell script includes the following commands:
sudo nohup python /home/pi/WebLamp/weblamp.py &
Note that the home directory may be different based on your Linux distro and configuration.

With these steps and modifications I was able to create a cell phone (or any browser-capable device on the WiFi network, Flask is very forgiving) switch for our lights.  My girlfriend loved the functionality, and it added another personal touch to our decorations.


eOrnaments


In addition to the WiFi switch, I added a couple more electronics decorations to our tree.


Is there ever a bad time for a Ping)))?

Incorporating an earlier electronics project, this year we added an electronic advent countdown to the tree.


3 alligator clips clipping...

I made this device from Wicked Device's Day Counter kit.  I originally used the kit for a scheduling aid at the office, but liked the idea of an active decoration. The day counter is powered by a  micro-usb breakout (huge fan) and an old cell phone charger.


Almost sad to break down the project, it's definitely made us keep the tree up longer this year