A coffee maker monitoring office notification system

Coffee pot and sensorsLike most tech offices, we go through a lot of coffee during the day. We have about 15 people spread between 2 rooms upstairs, and a single coffee pot between us. Generally this means someone makes a pot early in the morning and it’s done by lunch time, when someone else makes another. The problem is that it’s difficult to time your coffee intake with a fresh pot. Wouldn’t it be great if we could get some kind of alert when the pot was ready?

How to get that notification was pretty straightforward: we use HipChat, a very well done message platform from the folks at Atlassian. Even better is that HipChat offers an extensive API, with the simplest method being something called Integrations. These provide a self-contained URL request to make to a particular room and alert all of its members. All I’d need to do is track the coffee pot cycle and send an alert at the appropriate time.

Choosing the hardware

The first step is determining what method to use for tracking the cycle. Looking around online, people have done similar things by looking at the weight of the pot, the level of coffee in the pot and the current consumed by the maker itself. I don’t want to deal with weight (yet) and I find level monitoring overly complicated so I decided on current. Ideally I would be able to determine the cycle based on either the on/off times or the actual current consumption.

I have amassed a pretty large collection of Arduinos at this point, so choosing that platform was pretty easy. I also wanted to have everything self contained as much as possible, which meant looking for something with the following capabilities:

  • Analog inputs for connecting the current sensor
  • Digital output for local monitoring (LED, LCD)
  • Network connectivity for sending a notification
  • Logging to collect enough current data to notice a pattern

The only Arduino that satisfies these requirements is the Arduino YUN. The YUN is different from other Arduinos in that it is actually two chips in one, with a standard ARM chip on one side running the Arduino bootloader and a dedicated Linux board on the other side to run more complex operations. The advantage of this setup is that you can collect sensor input on one side and transfer that to Linux for processing.

For actual sensor monitoring I decided on the SCT-013-010 non-invasive current sensor. This is a version of the fairly popular SCT series, has capacity up to 10A and provides scaled 1V output. That last point is important, because I find it simpler to take analog voltage input than creating a circuit to make the scaled current output provide meaningful data.

Finally I added in a 128×32 OLED LCD I had from a previous project, for local monitoring of the current consumption. With that in place, I might even notice a pattern by glancing at the screen.

Collecting the data

While the current sensor is technically “non-invasive”, it does require some wire hacking to work properly. It works on the principle that a voltage can be induced in a neighbouring wire through an electric field. However since it works with AC lines, that power fluctuates through both wires at opposite time, effectively cancelling each other out. The solution is to split your appliance wire apart and wrap the sensor around only one of them.

After assembling a circuit according to numerous other current tutorials (like the Open Energy Monitor project), I was receiving current data to the screen, but it did not appear very useful. In fact, this was what I call Road block #1. I was reading the current data as if it was DC, but since AC power is a wave, I was effectively reading a single point in the wave and not the proper current. Fortunately the solution was to better follow that tutorial, and use the Emonlib Arduino library. This library monitors the entire power wave and does the correct math to give you a current value in amps.

With the current readings accurate, I needed to log them somewhere for later processing. To do that I used a MicroSD card in the YUN along with the example sketch called DataLogger. I modified that script to only save current when the reading is 0.1A or higher and to create a new file every day to keep things organized. After a day in the office, I had useful data, but I immediately ran into Road Block #2.

Setting a minimum threshold of 0.1A seemed to make sense, but after seeing the power consumption through the cycle, our particular coffee marker consumes 0.17A at idle. With a logging interval of 1 second, I ended up collecting a row of data every second for 2 days! It was not very useful so I cleared the SD card, reset the threshold to 0.2A and tried again.
coffee-pot-consumption-list
This is the data that resulted from a day of coffee pot monitoring. The Arduino provided a column containing the current time and the current usage, plus a filtered current value I didn’t end up using. I imported the data into Excel and added the column to the right, which is the difference between any one row and the time above it. Since there is no data logged when the machine is off, it became very easy to see the gaps. I also added the conditional formatting to highlight rows that are more than 2 seconds.

Looking through the rows, I was able to find this pattern for a typical coffee cycle:

  • It idles at 0.17A (idle meaning plugged in but not actively brewing)
  • At the start of the brewing cycle, the current changes to approx. 7.5A for 8-10 minutes
  • The end of the brewing cycle is indicated by 25 seconds on and 25 seconds off, twice
  • Once brewed, it repeats a cycle of 15 seconds on, 2 minutes off until someone turns the power off

With such clearly defined points along the cycle, it turned out to be pretty straightforward to detect. The notable complication is that the times are not precise, and it might be on for 8 minutes instead of 10 or heat for 25 seconds instead of 15. I was somewhat disappointed that it did not cycle differently depending on the volume of coffee to heat. I was hoping that it would heat less as the coffee level dropped and I could determine how many cups remained.

Doing the math

To detect the brewing cycle, I set the Arduino timer to compare current usage every second. I stored the previous value and used that to save a time reference for when the current level went above the threshold (set to 6A). By comparing those values, I could get an event when the coffee maker turns on and off, while checking the time between the two. If the appliance is on for more than 8 minutes, that’s considered the actively brewing stage. After that, if there is an on period of less than 30 seconds, that would be the start of the heating stage. The message alert would go out at the end of the brewing stage. I started with a 10 minute brewing cycle but ended up changing it to 8 when I found that the heater wasn’t always consistent. It turned out to be pretty accurate, though!

Triggered by the 1 second timer is this method

void logCurrent() {
  long timestamp = getTimestamp();
  boolean turningOff = false;
  boolean turningOn = false;
  
  // track the change in current
  if (prevCurrentReading < onThreshold && currentReading > onThreshold) {
	lastOnTime = timestamp;   
	logEvent("On");
	turningOn = true;
	turningOff = false;
  } else if (prevCurrentReading > onThreshold && currentReading < onThreshold) {
	lastOffTime = timestamp;
	logEvent("Off");
	turningOn = false;
	turningOff = true;
  } else {
	turningOn = false;
	turningOff = false;
  }

  bool currentlyOn = (currentReading >= onThreshold);
  bool currentlyOff = (currentReading < onThreshold);

  // determine the brewing cycle
  if (mode != Brewed && turningOff && (lastOffTime - lastOnTime) >= brewDuration) {
	mode = Brewed; 
	logEvent("Brewed");
	sendEventMessage("Brewed");
	sendMessage();
  } else if (mode != Brewing && currentlyOn && (timestamp - lastOnTime) >= heatingDuration) {
	mode = Brewing;
	logEvent("Brewing");
	sendEventMessage("Brewing");
	setGreenLed(true);
  } else if (mode == Brewed && lastOnTime != 0 && turningOff && (lastOffTime - lastOnTime) <= 45) {
	mode = Heating; 
	logEvent("Heating");
	sendEventMessage("Heating");
  } else if (mode != Off && lastOffTime > lastOnTime && (timestamp - lastOffTime) >= offDuration) {
	mode = Off; 
	logEvent("Power off");
	sendEventMessage("Off");
	setGreenLed(false);
  }
  
  prevCurrentReading = currentReading;
}

First I receive a UNIX timestamp from the Linux processor. This makes comparing times easy. Next I set a few variables based on whether the current usage is above or below the threshold set at the start. This is important because events are generally only triggered when things change. The logEvent() method logs the event to the SD card for future comparison and sendEventMessage() sends a notification to a HipChat room I set up specifically for monitoring my projects.

The brewing cycle checking works by using the current mode and checking the time between when the coffee maker when on or off. If it’s determined to be on for more than a minute nonstop, it’s considered “brewing”. Once it turns off and the gap between events is more than 8 minutes, that’s the end of the brewing cycle and the method sendMessage() is triggered to actually send a HipChat notification. After that a short on/off cycle is checked to look for heating and if it goes off longer than 5 minutes, the coffee maker is idle. The trick is generally to not detect another stage in the cycle if that stage is currently active. That usually removes the issue of sending a message continuously instead of once.

Sending a message

The YUN provides multiple ways of sending an HTTP request, but the easiest thing is to trigger a bash script inside Linux, instead of using Arduino directly. Finding that out was the solution to Road block #3. Throughout the internet, keyboard warriors warn about using Arduino’s string object and how inefficient it is. I started by generating a cURL request with a single String object, but that never worked. It turns out that I ran up against what appeared to be a memory limitation inside Arduino that probably limits string length to 256 characters. The cURL command I wanted to run was about 260 and would exit without error, but without actually sending a message. The solution to that problem is to move the cURL command to a separate bash script on the SD card and trigger that script from Arduino instead.

The last problem was that cURL would try to verify the SSL certificate that HipChat was using but was unable to. While probably not proper, the easiest thing is to disable that check using the -k flag in the command. After that, I set up the monitor and 10 minutes after brewing started, this appeared in our All Teams chat:
hipchat-coffee-bot-message
After a few days of operation, it seems to be fairly accurate and people have responded well to the little machine we call Coffee Bot.

Next steps

As mentioned I was hoping to be able to retrieve coffee levels from the current interval but since our particular coffee maker doesn’t do that, I’ll have to add a scale. With weight data available, I could know right away that a new pot is brewing and how many cups are left. It would even be possible to make the HipChat integration accept requests to know how many cups are left. You could write /coffee cups to know the number or /coffee time to know how long it’s been heating.

We also have tea drinkers so it would be reasonably straightforward to add a tea pot sensor. That cycle is even easier because it is either on and heating or off.

Finally I might make the hardware collection a little prettier for the shelf by maybe 3D printing an enclosure and making a proper PCB but that’s now pretty far down my list of electronics projects.