Coffee HOWTO: making coffee with Linux, the smart-plug way
Can Linux make coffee? It always could; the hard part is the switch. The classic Coffee HOWTO drove a relay from the PC's parallel port. Today you buy a smart plug, and one command from a terminal (through Home Assistant or MQTT) turns the coffee maker on.
Pick the right coffee maker
A smart plug can only switch power on and off. That works with a simple drip machine whose mechanical switch stays in the "on" position: power arrives, coffee happens. It does not work with machines that wait for a button press after power-up, which includes most espresso and bean-to-cup machines. Fill the water tank and the filter the night before; Linux does not do that part (yet).
Mains electricity is not a weekend project. Use a certified smart plug rated for the machine's power, rather than building your own relay board. Read the coffee maker's manual: some manufacturers advise against leaving it to run unattended. A machine with an automatic shut-off is a much better candidate.
Option 1: Home Assistant
Home Assistant is an open-source home automation system that runs on a small computer on your network and talks to most brands of smart plug. Once the plug is added, it appears as an entity such as switch.coffee_maker. Create a long-lived access token in your Home Assistant profile (under Security), then call the REST API from any Linux shell:
curl -X POST \
-H "Authorization: Bearer $HA_TOKEN" \
-H "Content-Type: application/json" \
-d '{"entity_id": "switch.coffee_maker"}' \
http://homeassistant.local:8123/api/services/switch/turn_on
Use switch/turn_off or switch/toggle for the other actions. The API listens on port 8123 by default, or on port 80 when Home Assistant runs with the Supervisor (as on Home Assistant OS). Keep the token in a file only you can read, not in your shell history.
Option 2: MQTT
Many plugs speak MQTT, a lightweight publish/subscribe protocol, through a broker such as Mosquitto. A plug running Tasmota firmware listens on cmnd/<topic>/POWER:
mosquitto_pub -h broker.lan -u me -P '<mqtt-password>' -t cmnd/coffee/POWER -m ON
mosquitto_sub -h broker.lan -u me -P '<mqtt-password>' -t stat/coffee/POWER -C 1
The second command waits for the plug's reply (ON) and exits. A Zigbee plug handled by Zigbee2MQTT takes JSON instead: mosquitto_pub -t zigbee2mqtt/coffee/set -m '{"state":"ON"}'. Keep unencrypted MQTT on your own network. A password given with -P is visible to other users in the process list, so on a shared machine put the options in the default-options file ~/.config/mosquitto_pub (and ~/.config/mosquitto_sub) instead, readable only by you.
Schedule breakfast
Put the command in a script, for example ~/bin/coffee-on, make it executable, and let systemd run it on weekday mornings:
systemd-analyze calendar 'Mon..Fri 06:45'
systemd-run --user --on-calendar='Mon..Fri 06:45' ~/bin/coffee-on
The first line checks the expression and shows when it next fires. The second creates a timer that lasts until you log out or reboot; for a permanent one, write a .timer and .service unit pair in ~/.config/systemd/user/. Home Assistant can also schedule it with its own automations, which keep working when your laptop is asleep.
The do-it-yourself route
If you still want the original experience, a single-board computer's GPIO pin can drive a ready-made, isolated relay module (see I/O port programming for libgpiod). Keep the low-voltage side and the mains side physically separate, put the mains part in a closed, earthed box, and let a qualified person check it.
Coffee in the standards
Coffee control has been written down before. RFC 2324, published on 1 April 1998, defines the Hyper Text Coffee Pot Control Protocol (HTCPCP/1.0), with a BREW method and the error 418 I'm a teapot for a teapot that is asked to brew coffee. It is an April Fools' document, not something your smart plug implements, but 418 is still a well-known joke among web developers.
Overdose symptoms
The original HOWTO had a section on caffeine overdose symptoms. We leave caffeine advice to your doctor, and only note that a systemd timer can make coffee faster than you can drink it.
The classic Coffee mini-HOWTO (now titled Coffee Making HOWTO, version 1.0 of 2004) is kept by the Linux Documentation Project at tldp.org.
Sources
- REST API, Home Assistant contributor documentation
- Switch integration and MQTT integration, Home Assistant
- mosquitto_pub(1) and mosquitto_sub(1), Eclipse Mosquitto
- MQTT, Tasmota documentation
- systemd-run(1) and systemd.time(7), systemd
- RFC 2324: Hyper Text Coffee Pot Control Protocol (HTCPCP/1.0), RFC Editor