The Internet of Things Debuts in My Bedroom, Starring Raspberry Pi and Clojure

Posted on May 4, 2014

I built a blue light sleep therapy alarm clock. It’s a strip of blue LEDs over my bed that wakes me up by gradually increasing in brightness in the morning. It’s hooked up to my Raspberry Pi and is connected to the Internet of Things by a Clojure web server. I turn the lights on/off from my phone using a web app that uses Twitter Bootstrap. The code is available on Github.

If there were a Pulitzer for writing SEO copy, I like to think I’d be a finalist.

Hardware

Look up the pin out for the Pi’s GPIO pins. Wire the MOSFET to the LED strip and Raspeberry Pi analogously. Basically, connect:

  • Rpi Gate to GPIO PWM out
  • MOSFET Drain to -ve lead of the LED strip
  • MOSFET Source to Rpi GND
  • Rpi GND and 12VDC GND
  • +12VDC to LED +ve lead

Essentially, use the MOSFET in this configuration.

Then, hang up the 15ft LED strip on your bedroom ceiling. The adhesive backing on the strip is very weak. Mount command strips on your ceiling and hook the LED strip through for mechanical support.

That’s it! Pretty simple

Software

The web server component is similarly simple.

The easiest way to access the Pi’s GPIO pins is probably to use the Python RPi.GPIO library.

That said, I wanted to take a tour through LISP Clojure land, so I ended up using Pi4J + compojure. The meat of it is < 100 lines of code. There was really zero advantage to coding this in Clojure (vs. Python) except for the educational aspect. Project is on github: https://github.com/dvdt/light-alarm.

As usual for me, I spent way more time fighting with frontend code to make it look nice. Unless you want your eyes to bleed, please don’t look at my frontend code.

Parting Thoughts

  1. Buy a logic level MOSFET. Most MOSFETs gates require higher voltages than could be supplied by the Raspberry Pi’s 3.3V output. I learned this the hard way.
  2. RPi really does make simple hardware hacking easer. Often, you just don’t need a full-fledged microcontroller with interrupt service routines that give you microsecond timing resolution.
  3. Having a full-fledged Linux box controlling hardware is awesome. Debug it live by issuing commands via SSH; write hardware interfacing code using high level languages like Python.
  4. I have absolutely zero security on these lights. If you guess the URI, you too can make my lights turn on/off. I should probably put it behind a harder-to-guess address.