Building a Sunrise Lamp - Part 1: Software

The Background

Winter in England is a dark time. On December 21st, the shortest day of the year, sunrise is at 08:23, and sunset is at 15:51. A total amount of 7 hours, 28 minutes, and 27 seconds of daylight. Which, generally speaking, is less time than I spend in work each day. It's dark.

This perpetual darkness can make it rather "fun" getting up in the morning. For the past 6 years I have been using a Lumie Sunrise Lamp to make things a little brighter when my audible alarm goes off, and oh boy does it make a difference. That is, until recently where it felt like the bulbs were blowing on a monthly basis. Couple this with the fact I had about 2 meters of LED strip lighting left over from when I kitted out my office, and its a perfect recipe for tinkering around with an old Raspberry Pi I had laying around.

The Lighter Background

For those of you who might be unsure what a Lumie lamp does, let me shine a light on it (I'll stop soon with the terrible puns).

  1. You set the time you want to get up, say 7:00
  2. 30 minutes prior to the alarm time, so 6:30, the lamp gradually gets brighter, until at 7:00 the lamp is at full brightness, simulating sunrise.

The same works in reverse in the evening. When you want to turn the lights out, the sunlamp gradually gets darker over a span of 30 minutes, simulating sunset.

OK so now we know the background, lets get to it.

The Plan

Take a seat, this is going to get complex. The plan goes as follows;

  1. Make a sunrise lamp, controlled from a Raspberry Pi, using the left over LED strip lighting

This blog post is the first in a multipart part series around the overall project, the second post will be looking at the hardware itself, including the wiring, GPIO connections, and the lamp itself. This post will be looking at the software which controls the lamp itself.

The Software

All of the code for this project can be found on GitHub,

The project is composed of 2 core software parts, the API and the Client. Both of these components are hosted on the RaspberryPi, visible on the local network, that serves up the client to control the lamp via a mobile device (we'll look at the Raspberry Pi hosting in a future post).

Client

sunrise lamp client

The client, in its current form, is fairly trivial.

The top section has two buttons, the first starts the 30 minute sleep time, gradually lowering the brightness of the lamp until it is off. The second button stops this functionality.

The middle section has 2 sliders, which can control each of the channels on the LED strip (Warm White, and Cool White) from 0 - 100%.

The bottom section has input boxes to set what time the sunrise lamp will start to turn on, which it will do over a 30 minute period.

API

The API acts as the controller, the point which the client communicates directly with, and also the connection with the Raspberry Pi GPIO pins, to control the hardware itself. Initially I had a pair of Python scripts controlling this, but it abstracted away the control too much and became inflexible with things such as pausing or stopping the scripts.

Controller

The Controller handles the logic. The main areas in terms of functionality are sleep() and wakeUp(), which do what they say on the tin. These call into an object for each of the channels, which itself calls into a library called pigpio, that is the main conduit to the LED lights by setting the variable output of the GPIO pins to control the light value. This library is the only hard dependency we have for controlling the LED lights, and can be run directly on the command line.

Take a deeper look at the code in GitHub for a more detailed understanding, shortly we'll take a look at the hardware side of things which will elaborate on pigpio once we have an understanding of how it all connects together.