In my previous post I talked about the Digispark board and the RGB LED shield I had just received.
This afternoon in a spare 30mins I had while some tests ran I built my first little hack to use one of them. It is inspired by this really cool hack that got mentioned on twitter by Dave CJ while we were debating Blink(1) vs Digispark/RGB Shields. The idea of a strip of LEDs that represent the working day with different colours for free time and meetings is cool, but I only have 1 LED to work with, so I thought how about just showing how close to the next meeting I am.
The idea is that if the next entry in my calendar is more than 20mins away then the light is green, as the meeting start time gets closer the LED changes closer to red and finally during the meeting if glows blue.
I started out prototyping this against my google calendar as it’s pretty easy to get at the data, if you poke around in the settings page you can get hold of a URL that points to either a XML or ICAL version of the data.
I’m running this on my Raspberry Pi so python seamed like a good choice to run this up in.
#!/usr/bin/python import sys import pycurl import StringIO from icalendar import Calendar, Event from datetime import datetime, timedelta curl = pycurl.Curl() curl.setopt(pycurl.URL,sys.argv) body = StringIO.StringIO() curl.setopt(pycurl.WRITEFUNCTION, body.write) curl.perform() cal = Calendar.from_ical(body.getvalue()) body.close() now = datetime.now() offset = 3600 for component in cal.walk(): if component.name == "VEVENT": dtstart = component["dtstart"].to_ical() dtend = component["dtend"].to_ical() if "T" in dtstart: dt = datetime.strptime(dtstart, "%Y%m%dT%H%M%SZ") end = datetime.strptime(dtend, "%Y%m%dT%H%M%SZ") if dt > now: delta = dt - now if delta.days == 0: if delta.seconds < offset: offset = delta.seconds elif end > now and dt < now: offset = -1 break if offset > 0 and offset < 300: print "200 0 0" elif offset >= 300 and offset < 600: print "150 50 0" elif offset >= 600 and offset < 900: print "100 100 0" elif offset >= 900 and offset < 1200: print "50 150 0" elif offset == -1: print "0 0 200" else: print "0 200 0"
This script outputs RGB values that can be fed directly to DigiBlink.py script like this in a cron job
*/5 * * * * DigiBlink.py `calColur.py <calendar URL>`
Or since I’ve set up a little script to control my DigiSpark via MQTT the following publishes the output of the script on the ‘digiblink’ topic on the local broker.
*/5 * * * * calColur.py <calendar URL> | mosquitto_pub -h localhost -t digiblink -l
This script should also work with the command line tool for the Blink(1)
I still need to tweak the colours for each step and next work out how to get the same feed out of the company calendaring system, but it’s a good start.