Using a raspberry pi, we listen to the coffee machine and count the number of coffee consumption

Overview

maintained by dataroots

Fresh-Coffee-Listener

A typical datarootsian consumes high-quality fresh coffee in their office environment. The board of dataroots had a very critical decision by the end of 2021-Q2 regarding coffee consumption. From now on, the total number of coffee consumption stats have to be audited live via listening to the coffee grinder sound in Raspberry Pi, because why not?

Overall flow to collect coffee machine stats

  1. Relocate the Raspberry Pi microphone just next to the coffee machine
  2. Listen and record environment sound at 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 a threshold it means that the coffee machine has been started and a datarootsian is grabbing a coffee
  5. Connect to DB and send timestamp, office name, and serving type to the DB in case an event is detected ( E.g. 2021-08-04 18:03:57, Leuven, coffee )

Raspberry Pi Setup

  1. Hardware: Raspberry Pi 3b
  2. Microphone: External USB microphone (doesn't have to be a high-quality one). We also bought a microphone with an audio jack but apparently, the Raspberry Pi audio jack doesn't have an input. So, don't do the same mistake and just go for the USB one :)
  3. OS: Raspbian OS
  4. Python Version: Python 3.7.3. We used the default Python3 since we don't have any other python projects in the same Raspberry Pi. You may also create a virtual environment.

Detecting the Coffee Machine Sound

  1. In the sounds folder, there is a coffee-sound.m4a file, which is the recording of the coffee machine grinding sound for 1 sec. You need to replace this recording with your coffee machine recording. It is very important to note that record the coffee machine sound with the external microphone that you will use in Raspberry Pi to have a much better performance.
  2. When we run detect_sound.py, it first reads the coffee-sound.m4a file and extracts its MFCC features. By default, it extracts 20 MFCC features. Let's call these features original sound features
  3. The external microphone starts listening to the environment for about 0.7 seconds with a 44100 sample rate. Note that the 44100 sample rate is quite overkilling but Raspberry Pi doesn't support lower sample rates out of the box. To make it simple we prefer to use a 44100 sample rate.
  4. After each record, we also extract 20 MFCC features and compute the Euclidean Distance between the original sound features and recorded sound features.
  5. We append the Euclidean Distance to a python deque object having size 3.
  6. If the maximum distance in this deque is less than self.DIST_THRESHOLD = 85, then it means that there is a coffee machine usage attempt. Feel free to play with this threshold based on your requirements. You can simply comment out line 66 of detect_sound.py to print the deque object and try to select the best threshold. We prefer to check 3 events (i.e having deque size=3) subsequently to make it more resilient to similar sounds.
  7. Go back to step 3, if the elapsed time is < 12 hours. (Assuming that the code will run at 7 AM and ends at 7 PM since no one will be at the office after 7 PM)
  8. Exit

Scheduling the coffee listening job

We use a systemd service and timer to schedule the running of detect_sound.py. Please check coffee_machine_service.service and coffee_machine_service.timer files. This timer is enabled in the makefile. It means that even if you reboot your machine, the app will still work.

coffee_machine_service.service

In this file, you need to set the correct USER and WorkingDirectory. In our case, our settings are;

User=pi
WorkingDirectory= /home/pi/coffee-machine-monitoring

To make the app robust, we set Restart=on-failure. So, the service will restart if something goes wrong in the app. (E.g power outage, someone plugs out the microphone and plug in again, etc.). This service will trigger make run the command that we will cover in the following sections.

coffee_machine_service.timer

The purpose of this file is to schedule the starting time of the app. As you see in;

OnCalendar=Mon..Fri 07:00

It means that the app will work every weekday at 7 AM. Each run will take 7 hours. So, the app will complete listening at 7 PM.

Setup a PostgreSQL Database

You can set up a PostgreSQL database at any remote platform like an on-prem server, cloud, etc. It is not advised to install it to Raspberry Pi.

  1. Install and setup a PostgreSQL server by following the official documentation

  2. Create a database by typing the following command to the PostgreSQL console and replace DB_NAME with your database name;

    createdb DB_NAME
    

    If you got an error, check here

  3. Create a table by running the following query in your PostgreSQL console by replacing DB_NAME and TABLE_NAME with your own preference;

    CREATE TABLE DB_NAME.TABLE_NAME (
        "timestamp" timestamp(0) NOT NULL,
        office varchar NOT NULL,
        serving_type varchar NOT NULL
    );
    
  4. Create a user, password and give read/write access by replacing DB_USER, DB_PASSWORD, DB_NAME and DB_TABLE

    create user DB_USER with password 'DB_PASSWORD';
    grant select, insert, update on DB_NAME.DB_TABLE to DB_USER;
    

Deploying Fresh-Coffee-Listener app

  1. Installing dependencies: If you are using an ARM-based device like Raspberry-Pi run

    make install-arm

    For other devices having X84 architecture, you can simply run

    make install
  2. Set Variables in makefile

    • COFFEE_AUDIO_PATH: The absolute path of the original coffee machine sound (E.g. /home/pi/coffee-machine-monitoring/sounds/coffee-sound.m4a)
    • SD_DEFAULT_DEVICE: It is an integer value represents the sounddevice input device number. To find your external device number, run python3 -m sounddevice and you will see something like below;
         0 bcm2835 HDMI 1: - (hw:0,0), ALSA (0 in, 8 out)
         1 bcm2835 Headphones: - (hw:1,0), ALSA (0 in, 8 out)
         2 USB PnP Sound Device: Audio (hw:2,0), ALSA (1 in, 0 out)
         3 sysdefault, ALSA (0 in, 128 out)
         4 lavrate, ALSA (0 in, 128 out)
         5 samplerate, ALSA (0 in, 128 out)
         6 speexrate, ALSA (0 in, 128 out)
         7 pulse, ALSA (32 in, 32 out)
         8 upmix, ALSA (0 in, 8 out)
         9 vdownmix, ALSA (0 in, 6 out)
        10 dmix, ALSA (0 in, 2 out)
      * 11 default, ALSA (32 in, 32 out)

    It means that our default device is 2 since the name of the external device is USB PnP Sound Device. So, we will set it as SD_DEFAULT_DEVICE=2 in our case.

    • OFFICE_NAME: it's a string value like Leuven office
    • DB_USER: Your PostgreSQL database username
    • DB_PASSWORD: the password of the specified user
    • DB_HOST: The host of the database
    • DB_PORT: Port number of the database
    • DB_NAME: Name of the database
    • DB_TABLE: Name of the table
  3. Sanity check: Run make run to see if the app works as expected. You can also have a coffee to test whether it captures the coffee machine sound.

  4. Enabling systemd commands to schedule jobs: After configuring coffee_machine_service.service and coffee_machine_service.timer based on your preferences, as shown above, run to fully deploy the app;

    make run-systemctl
  5. Check the coffee_machine.logs file under the project root directory, if the app works as expected

  6. Check service and timer status with the following commands

    systemctl status coffee_machine_service.service

    and

    systemctl status coffee_machine_service.timer

Having Questions / Improvements ?

Feel free to create an issue and we will do our best to help your coffee machine as well :)

You might also like...
A Macropad using the Raspberry Pi Pico, programmed with CircuitPython.

A Macropad using the Raspberry Pi Pico, programmed with CircuitPython.

raspberry pi servo control using pca9685

RPi_servo-control_pca9685 raspberry pi 180° servo control using pca9685 Requirements Requires you to have the adafruit servokit library installed You

The example shows using local self-hosted runners on-premises by making use of a runner on a Raspberry Pi with LED's attached to it

The example shows using local self-hosted runners on-premises by making use of a runner on a Raspberry Pi with LED's attached to it

A simple non-official manager interface I'm using for my Raspberry Pis.
A simple non-official manager interface I'm using for my Raspberry Pis.

My Raspberry Pi Manager Overview I have two Raspberry Pi 4 Model B devices that I hooked up to my two TVs (one in my bedroom and the other in my new g

This repository hosts the code for Stanford Pupper and Stanford Woofer, Raspberry Pi-based quadruped robots that can trot, walk, and jump.
This repository hosts the code for Stanford Pupper and Stanford Woofer, Raspberry Pi-based quadruped robots that can trot, walk, and jump.

This repository hosts the code for Stanford Pupper and Stanford Woofer, Raspberry Pi-based quadruped robots that can trot, walk, and jump.

Mycodo is open source software for the Raspberry Pi that couples inputs and outputs in interesting ways to sense and manipulate the environment.
Mycodo is open source software for the Raspberry Pi that couples inputs and outputs in interesting ways to sense and manipulate the environment.

Mycodo Environmental Regulation System Latest version: 8.12.9 Mycodo is open source software for the Raspberry Pi that couples inputs and outputs in i

Minimal and clean dashboard to visualize some stats of Pi-Hole with an E-Ink display attached to your Raspberry Pi
Minimal and clean dashboard to visualize some stats of Pi-Hole with an E-Ink display attached to your Raspberry Pi

Clean Dashboard for Pi-Hole Minimal and clean dashboard to visualize some stats of Pi-Hole with an E-Ink display attached to your Raspberry Pi.

A global contest to grow and monitor your own food with Raspberry Pi
A global contest to grow and monitor your own food with Raspberry Pi

growlab A global contest to grow and monitor your own food with Raspberry Pi A capture from phototimer of my seed tray with a wide-angle camera positi

A Raspberry Pi Pico powered Macro board, like a Streamdeck but cheaper and simpler.
A Raspberry Pi Pico powered Macro board, like a Streamdeck but cheaper and simpler.

Env-MCRO A Raspberry Pi Pico powered Macro board, like a Streamdeck but cheaper and simpler. (btw this image is a bit outdated, some of the silkscreen

Comments
  • Fix makefile and dependencies for raspberry pi

    Fix makefile and dependencies for raspberry pi

    Fix the makefile and requirement files for a fresh install on a Raspberry Pi

    llvmlite 0.33 is the latest version working with LLVM 9 shipped with the RPi, and libarosa + numba needs to be fixed to match this version

    I also added a way to specify the ssl mode for the PG connection

    opened by samueldumont 0
  • Why not use the serial port?

    Why not use the serial port?

    Hello!

    Saw your fun post and looked around for more info on the Jura W6 coffee machine (the one in your picture.)

    I know it's fun to play with the audio but it could be even more fun to connect to the serial port and read what the machine says!

    Here is a cool article about it:

    • https://blog.q42.nl/hacking-the-coffee-machine-5802172b17c1/amp/

    And here is another person on another repo:

    • https://github.com/arendst/Tasmota/issues/7570

    Just dropping it here! I'd love to contribute but I don't have anything even close to that machine at home, so I can only armchair brainstorm on it!

    Good luck and great work!

    opened by Nesh108 0
  • make install-arm failing on pip install llvmlite==0.33

    make install-arm failing on pip install llvmlite==0.33

    When running on fresh raspbian install (buster) the make install-arm command fails as per below

    (log below taken from 2nd attempt at running this command)

    make install-arm

    installing dependencies for ARM architectures like Raspberry Pi sudo apt install -y libatlas-base-dev libportaudio2 llvm-9 libpq5 Reading package lists... Done Building dependency tree
    Reading state information... Done libatlas-base-dev is already the newest version (3.10.3-8+rpi1). libportaudio2 is already the newest version (19.6.0-1+deb10u1). libpq5 is already the newest version (11.13-0+deb10u1). llvm-9 is already the newest version (1:9.0.1-6+rpi1~bpo10+1). 0 upgraded, 0 newly installed, 0 to remove and 119 not upgraded.

    LLVM_CONFIG=llvm-config-9 pip install llvmlite==0.33 Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting llvmlite==0.33 Could not find a version that satisfies the requirement llvmlite==0.33 (from versions: 0.2.0, 0.2.1, 0.2.2, 0.4.0, 0.5.0, 0.6.0, 0.7.0, 0.8.0, 0.9.0, 0.10.0, 0.11.0, 0.12.0.1, 0.12.1, 0.13.0, 0.14.0, 0.15.0, 0.16.0, 0.17.0, 0.17.1, 0.18.0, 0.19.0, 0.20.0, 0.21.0, 0.22.0, 0.23.0, 0.23.2, 0.24.0, 0.25.0, 0.26.0, 0.27.0, 0.27.1, 0.28.0, 0.29.0, 0.30.0, 0.31.0, 0.32.0, 0.32.1) No matching distribution found for llvmlite==0.33 make: *** [makefile:25: install-arm] Error 1

    Additional Details

    Linux raspberrypi 5.10.17-v7+ #1414 SMP Fri Apr 30 13:18:35 BST 2021 armv7l GNU/Linux Distributor ID: Raspbian Description: Raspbian GNU/Linux 10 (buster) Release: 10 Codename: buster

    opened by crestanij 0
Owner
dataroots
Supporting your data driven strategy.
dataroots
Philippe 1 Jan 9, 2022
PyLog - Simple keylogger that uses pynput to listen to keyboard input.

Simple keylogger that uses pynput to listen to keyboard input. Outputs to a text file and the terminal. Press the escape key to stop.

null 1 Dec 29, 2021
Code and build instructions for Snap, a simple Raspberry Pi and LED machine to show you how expensive the electricyty is at the moment

Code and build instructions for Snap, a simple Raspberry Pi and LED machine to show you how expensive the electricyty is at the moment. On row of LEDs shows the cost of the hour, the other row the cost of the day.

Johan Jonk Stenström 3 Sep 8, 2022
Transform a Raspberry Pi into a network diagnostic machine.

EtherView Last updated jan 30, 2022. Welcome to the EtherView project! This is a project to transform a RaspberryPi into a portable network diagnostic

null 1 Jan 30, 2022
A rubiks cube timer using a distance sensor and a raspberry pi 4, and possibly the pi pico to reduce size and cost.

distance sensor cube timer A rubiks cube timer using a distance sensor and a raspberry pi 4, and possibly the pi pico to reduce size and cost. How to

null 3 Feb 21, 2022
Designed a system that can efficiently sort recyclables and transfer them to corresponding bins using Python, a Raspberry Pi, and Quanser Labs.

System for Sorting and Recycling Containers - Project 3 Table of contents Overview The challenge Screenshot My process Built with Code snippets What I

Mit Patel 2 Dec 2, 2022
KIRI - Keyboard Interception, Remapping, and Injection using Raspberry Pi as an HID Proxy.

KIRI - Keyboard Interception, Remapping and Injection using Raspberry Pi as a HID Proxy. Near limitless abilities for a keyboard warrior. Features Sim

Viggo Falster 10 Dec 23, 2022
Home solar infrastructure (with Peimar Inverter) monitoring based on Raspberry Pi 3 B+ using Grafana, InfluxDB, Custom Python Collector and Shelly EM.

raspberry-solar-mon Home solar infrastructure (with Peimar Inverter) monitoring based on Raspberry Pi 3 B+ using Grafana, InfluxDB, Custom Python Coll

cislow 10 Dec 23, 2022
A PYTHON Library for Controlling Motors using SOLO Motor Controllers with RASPBERRY PI, Linux, windows, and more!

A PYTHON Library for Controlling Motors using SOLO Motor Controllers with RASPBERRY PI, Linux, windows, and more!

SOLO Motor Controllers 3 Apr 29, 2022
a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico

pico_ws2812b a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico You'll first need to save the ws2812b.py file to your device (for

null 76 Nov 25, 2022