Coffee Machine Starter

From Developer Wiki

Contents

Purpose

The first person into the office never liked having to turn on the coffee pot and wait for the coffee to brew. Therefore we thought "why not have the coffee drinkers text in a command to start the machine on their way into the office? That way the coffee is fresh as soon a the first person arrives!" Eager to solve any such beverage-related problem of critical importance with our own technology, our team (specifically Adrian Tymes and Evan Small) sprang into action.

Solution

A relay toggles the power to the coffeepot. This relay is connected to the parallel port of a computer which has a program running to routinely poll a datafile on the server for a ON/OFF status. The datafile on the server is updated to indicate "ON" whenever the first person texts in the special keyword. The power would be toggled off at 5pm daily and the request queue cleared at midnight.

Note: the keyword, actual filename and path have been obscured to prevent the public from controlling our coffeepot!

Use Case

Between 6am and 5pm, the first person to text the special keyword would cause the relay to switch power on to the coffee machine and return a text message saying "Turning on coffeepot now". Anyone texting this keyword after would receive the message "The coffeepot was already activated today by user ID XXXXXX".

Problem: The last person in the office must remember to refill the pot and ensure the power switch on the machine is ON, even though the relay has the power switched off at 5pm.

Implementation

Under the keyword's API tab, the Dynamic Content URL was

http://www.mozes.com/cgi-bin/coffeesample.cgi?userid=<userid>

coffeesample.cgi is listed below:

#!/usr/bin/perl
use CGI;
my $datafile = "/xxx/xxx/xxxxxx/coffeedata.txt";
my ($userid,$time) = (0,0);
if (-e $datafile) {
 open(RD,$datafile);
 my $line = <RD>;
 close(RD);
 if ($line =~ /(\d+),(\d+)/) {
   $userid = $1;
   $time = $2;
 }
}
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
my ($s,$m,$this_hour,$this_mday,$this_mon,$this_year,$wd,$yd,$i) =
 localtime(time());
my $query = new CGI;
if ($query->param("result") > 0) {
 print "Content-Type: text/plain\r\n\r\n";
 if (($mday == $this_mday) && ($mon == $this_mon) && ($year == $this_year) &&
     ($this_hour >= 6) && ($this_hour <= 16)) {
   print "1\n";
 } else {
   print "0\n";
 }
} else {
 my $result = "";
 if (($this_hour < 6) || ($this_hour > 16)) {
   $result = "You may only activate the coffeepot between 6 AM and 5 PM.";
 } elsif (($mday == $this_mday) && ($mon == $this_mon) &&
     ($year == $this_year)) {
   $result = "The coffeepot was already activated today by user ID " . $userid;
 } else {
   $userid = $query->param("userid");
   $userid =~ s/\D//gs;
   if ($userid > 0) {
     $result = "Turning on coffeepot now.";
     open(WR,">" . $datafile);
     print WR $userid . "," . time() . "\n";
     close(WR);
   } else {
     $result = "Invalid userid.";
   }
 }
 print "Content-Type: text/xml\r\n\r\n" .
   "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n" .
   "<MozesDynamicContent>\n" .
   "<Result>" . $result . "</Result>\n" .
   "</MozesDynamicContent>\n";
}

In the keyword Mobile tab

<DynamicResult>

And in the Description field in the Details tab

<DynamicResult>

Evaluation

The system worked well for a few days but was hampered by a few problems:

  1. having someone remember to clean and refill the pot at the end of the day. It didn't always happen.
  2. having the relay receiving from the parallel port was somewhat problematic, as there seemed to be occasional random pulses sent by the computer
  3. the polling program would occasionally quit due to unexplained Windows issues

Finally, one morning in a fit of caffeine withdrawal, the relay was ripped out and bypassed by a coffee-drinker who had run out of patience.

An improved version would include the following:

  1. web server running on the computer rather than rely on it polling a separate server
  2. using Phidgets as the power toggle interface would eliminate the unreliable parallel port and make use of the USB port instead.
  3. using Linux, or some other stable server platform, instead of Windows
  4. change the message to say when the coffeepot was turned on, rather than by whom.
  5. tie the data to a database, rather than a flat file
  6. tie employee performance evaluations to end-of-day coffeepot preparations. well, maybe not...
  7. set up a robot to clean and refill the pot at the end of the day
  8. Or send sms reminders b4 end of day to employees that coffee pot must be cleaned and refilled at end of day.