The Coffee machine on Twitter

In: arduino|physical computing|processing

17 May 2009

coffeemachine

After @BarackObama, @BritneySpears and @oprah it is now time for the coffeemachine to get on Twitter and add some meaning full words.

What does it do?

It will tweet “the coffee machine says: I’m making coffee” if you turn on the coffeemachine, if you switch it off it will tweet “the coffee machine says: someone switched me off”.

How does it work?

This is an overview of the hardware/software/services I used:
Sensor > Arduino > Processing > PHP > Twitter

A LDR (light dependent resistor) is glued on the on/off led of the coffeemachine. When the coffeemachine is switched on the led turns on, the ldr detects this light. This LDR is connected to an Arduino microcontroller, Processing reads the value from the Arduino using Firmata and Processing sends this to a php file which will send it to Twitter.

How to build your own?

Although it looks a bit overwhelming it is very easy to build your own tweeting ‘whatever’ thing, All hardware, software and scripts I used to build this are opensource. I just hacked everything together.
You will need the following:

Source files:

» Download Processing code + PHP source files

What do I need to do to get this working on my computer?

I use the Arduino Library for Processing and the Firmata firmware for the Arduino (included in Arduino 12 or higher).

1) Unzip the file ‘processing-arduino2.zip’ and copy the “arduino” folder into the “libraries” sub-folder of your Processing Sketchbook. (You can find the location of your Sketchbook by opening the Processing Preferences. If you haven’t made a “libraries” sub-folder, create one.)

2) Start the Arduino application and open the Examples > Library-Firmata > StandardFirmata sketch, and upload it to the Arduino board.

3) Upload the php files on your (php enabled) server in a folder called ‘twittertest’ (for example)
Open the file ‘coffeemachine_processing.pde’ with Processing, and edit the following lines:
- change the variable:
twitterPassword = "yourTwitterPassword";
- change the portnumber for the Arduino:
arduino = new Arduino(this, Arduino.list()[2]);
- change the url:
new URL("http://www.yourdomain.com/twittertest/insertTwitterMsg.php?pass="+twitterPassword+"&msg="+msg).openStream();

4) Open the file ‘insertTwitterMsg.php’ with a text editor and change the variable ‘$twitterUsername’ in your Twitter username.?

5) Open the file ‘ coffeemachine_processing.pde’ in Processing and press ‘run’. Now you should have your own Tweeting arduino sensor.

Instead of a coffeemachine there are hundreds of other possibilities, you can connect almost every sensor or object to Twitter, and build your own ‘internet of things’.
Next week I will post a ‘Who’s at the Office’ Processing script that scans computernames in a local network, so stay tuned…

The Processing source file ‘coffeemachine_processing.pde’:

/*
coffeemachineProcessing - receives Arduino input from sensor and sends a message to Twitter
Date: 05-2009
Copyleft by Eric Holm 

http://ericholm.nl/blog

*/

// import the Serial port library
import processing.serial.*;
// import the firmata arduino library
import cc.arduino.*;

Arduino arduino;
int sensorPin = 0;    // Analog input pin
int sensorValue = 0;  // value read from analog input
Boolean isBusy = false;
String twitterPassword;

void setup() {
  // CHANGE YOUR TWITTER PASSWORD
  twitterPassword = "yourTwitterPassword";

  // CHANGE YOUR PORT NUMBER
  arduino = new Arduino(this, Arduino.list()[2]); // v2
  arduino.pinMode(sensorPin, Arduino.INPUT);
}

void draw() {
  sensorValue = arduino.analogRead(sensorPin); // read the analog value
  // if the analog value is below a certain value
  if (sensorValue < 930) {
    switchCoffee(true);
  }
  // if the analog value is above a certain value
  if (sensorValue > 800) {
    switchCoffee(false);
  }
  // for testing the value
  println(sensorValue);
  // delay for 5 seconds
  delay(5000);
} 

public void switchCoffee(Boolean switchState){
  if (switchState) {
    if (isBusy == false) {
      // switch on the led on port 13 of the arduino
      arduino.digitalWrite(13, Arduino.HIGH);
      // for testing inside the Processing IDE
      println("the coffee machine says: I'm making coffee");
      sendToTwitter("I%20am%20making%20coffee");
      isBusy = true;
    }
  } else {
  if (isBusy == true) {
     // switch on the led on port 13 of the arduino
     arduino.digitalWrite(13, Arduino.LOW);
     // for testing inside the Processing IDE
     println("the coffee machine says: someone switched me off");
     sendToTwitter("someone%20switched%20me%20off");
     isBusy = false;
    }
  }
}

public void sendToTwitter(String msg){
  try {
    // calls the PHP script on your own domain with the 'msg' string as a parameter
    new URL("http://www.yourdomain.com/twittertest/insertTwitterMsg.php?pass="+twitterPassword+"&msg="+msg).openStream();
    // wait 3 seconds..
    delay(3000);
  }
  catch (Exception theException) {
    theException.printStackTrace();
  }
}

3 Responses to The Coffee machine on Twitter

Avatar

Hello twitter « Ramon Snellink

June 2nd, 2009 at 18:45

[...] Het leek me leuk om te zien hoe dat in z’n werk gaat, of dat enigszins uitvoerbaar is voor een programmeer-leek als ik. Het heerlijke aan het internet is dat er veel mensen zijn die het geen probleem vinden om hun kennis te delen, sterker nog, ze leggen het op zo’n manier uit dat zelfs de leek er nog mee uit de voeten kan. Ik zoek op “twitter” en “arduino” op internet, en voila, een tutorial. [...]

Avatar

Twitter Trackbacks for The Coffee machine on Twitter - digital things and physical computing [ericholm.nl] on Topsy.com

August 31st, 2009 at 12:29

[...] The Coffee machine on Twitter – digital things and physical computing ericholm.nl/blog/2009/05/the-coffee-machine-on-twitter – view page – cached #RSS 2.0 RSS .92 Atom 0.3 digital things and physical computing » The Coffee machine on Twitter Comments Feed digital things and physical computing Arduino Introduction presentation Hot100 – Picnic08 Who’s at the office Twitter script — From the page [...]

Avatar

elim

September 4th, 2009 at 20:02

I can see the message show up on my processing, but nothing to Twitter.
Any ideas why? Thanks a lot!!

Comment Form

About this blog

The webblog of Eric Holm, designer at Kimogo in Amsterdam. interactive media projects, Flash games and interactive installations.

Archives