Measuring caffeine intake at Spatie

For a while now, we've had some empty space on our office dashboard. A couple of interesting ideas came up over drinks last week, for example:

  • a Weasley clock tile for based on our colleagues' connected WiFi devices
  • a Shazam tile that recognizes the music playing at the office
  • a tile that counts the number of coffees brewed based on the coffee machine's grinding sound

The coffee grinder idea seemed easy enough: put a Raspberry Pi next to it and measure the sound levels. If the levels go above a certain treshold we can assume the machine is brewing coffee. Our coffee machine is located inside of a cabinet which make things even easier by further amplifying the sound and blocking out other random office noise. We could probably even count the espressos, small and large coffees separately based on the different audio patterns: short grind followed by a short pour is espresso, short grind followed by a medium pour is a small cup of coffee, etc...

Listening for coffee

As usual, we weren't the first people with a good idea. Only a month earlier, the people at Dataroots.io built the exact same thing! They even went the extra mile and instead of just comparing audio levels, they actually compare some of the recorded sound's distinct features (MFCC's) to a pre-recorded sample of the coffee machine. Their Python code and a more in-depth explanation can be found here. Here's a quick summary from their readme:

  1. Relocate the Raspberry Pi microphone just next to the coffee machine
  2. Listen and record environment sound every 0.7 seconds
  3. Compare the recorded environment sound with the original coffee grinder sound and measure the Euclidean distance
  4. If the distance is less than the configured threshold, it means that the coffee machine started brewing coffee

In Dataroot's original code, the event is then written to a PostgreSQL database. However, for our use-case we decided to send the event directly to our dashboard app. The coffee tile installed there will process the incoming request and store it in the dashboard's Sqlite database. Additionally, we've also added the python-dotenv package to keep our secrets safe in a .env file. Our fork of Dataroot's Python code can be found here.

The coffee tile

A sidenote on noisy USB microphones

I noticed the microphone we are using was generating a lot of noise when I tried it at the office. This wasn't the case when I tried it at home the day before. The only real difference I can think of is that I had a USB keyboard plugged in at home that might've smoothed out the Pi's 5V rail. Feel free to PM me on Twitter if you have any idea how to solve this. Luckily, our coffee grinder is (a lot) louder than the microphone's background noise so this isn't much of an issue.

Future plans

The duration of each coffee machine sound is currently being logged. We're hoping to be able to recognize different patterns for each type and size of coffee and recognize the sound of the machine cleaning itself at the end of the day. This way we can get more accurate and granular statistics.

We're also thinking of listening for a sound sample of the machine running out of beans. This sound is distinctly different from the regular grinding sound. If that works alright, this would allow us to count the average number of coffees per refill and even predict when the machine is about to run out of beans. There's nothing worse than taking that first sip on a Monday morning and realising you're drinking watery coffee.