Spotify!

The core features are finally coming together. With the hardware more or less complete, the rest is going to revolve around software and integration with various services - starting with Spotify.

To begin, I needed to make my Raspberry Pi a Spotify Connect target so I can play music to it. Thankfully, that was really easy! I just run a Raspotify daemon on the device and it shows up in the app like magic.

It took a bit of thinking to figure out how I was going to get this to work as an alarm clock, though. After all, there's nothing on the Pi itself that allows it to control when media is played or what's playing. I started looking more at Spotify's developer documentation and quickly came to the conclusion that I'll also need to incorporate the Spotify API into my clock's core server.

Using the API, I can have the clock call out to Spotify and tell it to play a particular playlist on a particular device (this one) in the morning.

Of course, to properly login with Spotify I have to implement a standard OAuth 2.0 flow on my little server running on the Pi. Luckily I've done this flow a few times now, so while it's a bit confusing at first, it didn't take long to get things going. In fact, the biggest thing I got stuck on was trying to figure out how to send a form-encoded request in Node... it's the little things I guess.

For now, I'm just storing the refresh token on the device, which seems like it will work fine. I abstracted the same persistence layer I already had in place for the main clock settings into a reusable class which I'm quite pleased with. It caches the stored values when you set them, so it doesn't hit the disk too much. And it's an event emitter, so you can subscribe to changes.

A few API proxy endpoints later, and I can select a playlist to use on a particular morning:

A screenshot of the alarm settings page with a dropdown showing a playlist name

I'm quite happy with my progress on the software side. The UI is looking a bit more professional, and Spotify integration was one of the bigger hurdles I anticipated when it came to the settings configuration.

Perhaps tomorrow or next week I can wrap up the music part by triggering Spotify to actually play something when the alarm is fired. At which point... this is actually an alarm clock!