Skip to content

Commit

Permalink
Create Raspberry Pi Project: Color My Desk
Browse files Browse the repository at this point in the history
  • Loading branch information
NerdiusMaximus committed Aug 1, 2014
1 parent bf97f88 commit ee556bf
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions Posts/Raspberry Pi Project: Color My Desk
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Cross-posted from <a title="Will Makes Things" href="http://willmakesthings.com/color-my-desk/" target="_blank">Will Makes Things</a>
<h3>Control Something</h3>
Do you need to exercise some control over the physical world, but don't want to leave your computer? Well then, take a minute to set the color of my desk on a day of your choosing at <a href="http://colormydesk.com/" target="_blank">http://colormydesk.com/</a>

Yes, I backlit my desk and connected the lights to a server that anyone can control.

[gallery ids="553,552,551,550,549"]
<h3><strong>THE BASICS</strong></h3>
<ul>
<li>I used a <a href="http://en.wikipedia.org/wiki/Raspberry_Pi" target="_blank">Raspberry Pi</a>, a small Linux server.</li>
<li>I built a circuit that allows me to power RGB lights with 12-volt power without frying the Raspberry Pi.</li>
<li>I developed the website, <a href="http://colormydesk.com/" target="_blank">http://colormydesk.com/</a>, on <a href="http://cakephp.org/" target="_blank">CakePHP</a> that handles requests from the general public. I ran this on my web server.</li>
<li>I wrote some software in Python on the Raspberry Pi that pulls the information daily from the web server using JSON.</li>
</ul>
<strong>If this is already too much for you, consider just going to the site and setting a color: <a href="http://colormydesk.com/" target="_blank">http://colormydesk.com/</a>. </strong>If you're really interested in that how / why, read on.
<h3><strong>THE NOT-SO-BASICS</strong></h3>
<h4>Forcing the Pi to Fake PWM</h4>
The first step was to get the Raspberry Pi to run some lights. Since the Pi doesn't have 3 PWM pins, I used <a href="http://mitchtech.net/raspberry-pi-pwm-rgb-led-strip" target="_blank">this tutorial </a>on using <a href="https://github.com/richardghirst/PiBits/tree/master/ServoBlaster" target="_blank">ServoBlaster</a> for RGB lights.

I ran into some issues and had to use Wheezy instead of Occidentalis because its kernel version, 3.2.7 was more up-to-date. Then I used the older version of ServoBlaster, instead of the one built for the newest 3.6.11 mainly because I didn't want to figure out how to upgrade the kernel. I used this thread to figure out kernel versions: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=15011&amp;p=278566
<h4>Building the Shield</h4>
Once that was working, I soldered the basic circuit onto some proto board. I then realized I should've used my Raspberry Pi proto board instead of the regular Radio Shack one. But no worries, I hooked up some headers and a custom shield was born.
<h4>CakePHP and a Web Server</h4>
I built the CakePHP site on a separate web server because I didn't want the Pi to get overloaded. Besides, I'm a web developer and have plenty of server resources at my disposal. I then wrote some Python code on the Pi to pull the data from the site's JSON feed with some simple authentication to protect email addresses from the public. A crontab runs every day at 6pm ET to pull the data and turn the lights on. Another one runs at 11pm ET to turn them off.
<h4>Translating Hex to ServoBlaster</h4>
ServoBlaster allows you to set a 'servo position' from 0 - 249. Since a color value for each red, green, or blue, can only be between 0 - 255, I decided to simply clip the last 6 values (250, 251, etc...) down to a servo position of 249. What this really means is that anything above the hex value of F9, just looks like F9. #FFFFFF, is just #F9F9F9, a dimmer white.
<h4> The Python Code</h4>
[code]

#!/usr/bin/env python

import os

import requests
import json
import datetime
import time

import smtplib
# FUNCTIONS
def pwm(pin, angle):
angle = checkmax(angle)
print "servo[" + str(pin) + "][" + str(angle) + "]"
cmd = "echo " + str(pin) + "=" + str(angle) + " &gt; /dev/servoblaster"
os.system(cmd)

def checkmax(angle): #PWM can only handle 249 units, so we're simply cutting the hex values 250-255 down to 249
if angle &gt; 249:
angle = 249
return angle

def setcolor(hex):
pwm(5, int(hex[1:3],16))
pwm(2, int(hex[3:5],16))
pwm(0, int(hex[5:7],16))

# Define a Thank You Email
def send_email():
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587

sender = ''
password = ''
recipient = data[0]['email_address']
subject = 'Color My Desk: Thank you!'
body = 'Hey '+data[0]['name']+",&lt;br /&gt;&lt;br /&gt;"+"Thanks for setting my desk color to &lt;span style='color:"+data[0]['color']+"'&gt;" + data[0]['color'] + "&lt;/span&gt; today! &lt;br /&gt;&lt;br /&gt;"+"You wrote: "+data[0]['details']+"&lt;br /&gt;&lt;br /&gt;"+"Will Wnekowicz"

headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)

session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)

session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)

session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()

# Getting the JSON Feed
os.environ['TZ'] = 'America/New_York'
time.tzset()
r = requests.get('http://colormydesk.com/full_calendar/events/feed?start='+str(datetime.date.today())+'&amp;api_secret=thesecret')
data = r.json()

# Setting the Color of the Strip
if data:
setcolor(data[0]['color'])
send_email()
else:
setcolor("#7733F0") # default color if no color is scheduled, aka no one loves me.

[/code]

0 comments on commit ee556bf

Please sign in to comment.