A Seagulls! button

Tore Lundqvist
4 min readFeb 6, 2018

--

It all started as an internal joke at work, me and my coworkers really liked the Seagulls! (Stop It Now) song and played it every time something stupid or annoying happened to cheer us up. One day someone said that we should have a button to play that song, to make it easy for anyone to play whenever it feels like it’s one of those moments. During the Christmas holidays I had some spare time and felt like tinkering with something so I decided to try to make a device like that.

First I thought about gutting an old mouse, hooking up a big button to it and put it in a box. Then I could just put a script on my coworker’s computer to play the song. But what’s the fun in that when you can make a much more complicated IoT solution including low resource hardware in combination with high level languages and Web APIs? :)

I had already tried Micropython on the ESP8266 and really liked it so I wanted to use that again. In another project I used the Spotify’s Web API so I thought that combining my knowledge of these technologies might save me some time, be fun, and increase the changes to actually finishing the project.

I started developing in Python 3 on the Mac to make it easier and faster to code and test, I knew that I needed to avoid using third party packages as they probably would not run in Micropython on the ESP8266. I also used Micropython on the Mac for testing, I thought that would stop me from using stuff that does not work on the ESP8266.

The biggest challenge was the setup of Oauth 2.0 authentication with Spotify. You have to “create an app”, get client keys, white list an URL, handle HTTP redirects, make API calls, request access tokens and more to get it working. The Spotify documentation is ok and the authentication flow chart helped. After that using the API is a breeze, and there is a lot of fun stuff you can do with the Spotify Connect Web API like controlling play/pause, volume, select track and device to play on. But for this project I just select which device to play on and the specific song.

I got it all working on the Mac, both in CPython 3.6 and Micropython and it was time to upload it to the ESP8266 for testing. I now found out that packages for the Unix version of Micropython can be installed on the device but they are not all made to work on it, so I had to get rid of some dependencies to functions in the urllib.parse library, for example urllib.parse.urlencode. I ended up copy pasting/re-implementing some of urllib.parse in my module. I also used urequests which implements some of Requests API but for Micropython, fortunately that worked on the ESP8266. After that the code run…

Oh no! The program took up too much memory, Micropython on the ESP8266 only has ~28 kilobyte of RAM free for the application and reboots when it runs out of memory. I started to optimize the code to use less memory and got it a little bit further, but then I needed to make an API call at the same time as I held a web server for the setup wizard running and that was too much for the poor little thing. The situation started to feel bad, was I going to fail? I really wanted to finish the project and surprise my coworkers with it and I had a working program, but not working on the target platform :( . I went to bed and had almost given up on it but the next day I remembered reading somewhere that you can “freeze” modules into the firmware when compiling Micropython to save space. It’s always good to sleep on things. I found an article on Adafruit about it and set it up, compiled a custom firmware and it worked, it stopped crashing!

The hardware was very simple to put together, just attach a button to the Adafruit Feather HUZZAH ESP8266. To make it look nice I added a micro USB connector to a box and put the board in it. The button needed to look good so I used an authentic Sanwa arcade button. I’m pretty happy with how it turned out. The project was mostly fun to work on, didn’t take to much time and got finished. I think this project lends itself well to tweaking. The module with the Oauth 2.0 setup wizard and everything needed to make API calls is in the firmware and can be reused, you could just change the main program to have it do other stuff with Spotify. The code is available on GitHub.

A couple of weeks later a PyCharm plugin for Micropython was released, I have tried it on this project and it makes for a pretty good development environment. I have also tested the ESP32 and it looks promising but the Micropython support is not as good for it as it is for the ESP8266 yet.

--

--