Lecture 11

Example Code / Projects:

Play a sound, in Processing:

Read and display sensor values, from an Arduino, in Processing:

Send control signals to actuators, on an Arduino, from Processing:

Use Processing to send a tweet:

In Processing, check Twitter for the use of a word:

When a sensor is activated on an Arduino, tell Processing to send a tweet:

Detect when a Twitter user sends a tweet, and trigger an actuator on an Arduino:

From Virtual to Digital and Back

Zip File for today:

http://cmuems.com/2012/resources/arduinoProcessing.zip (6MB)

Arduino Input to Processing:

Arduino Code:

int sensorValue0;
int sensorValue1;

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  sensorValue0 = analogRead(A0);
  sensorValue1 = analogRead(A1);
  Serial.print(sensorValue0);
  Serial.print(",");
  Serial.println(sensorValue1);

  delay(10);
}

Processing code:

import processing.serial.*;

int sensor0 = 0;
int sensor1 = 0;

Serial myPort;

void setup() {
  size(200, 200);

  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
}

void draw() {
  // do your stuff here!
}

void serialEvent(Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // split the string on the commas and convert the
    // resulting substrings into an integer array:
    int[] sensors = int(split(inString, ","));
    // if the array has at least two elements, you know
    // you got the whole thing.  Put the numbers in the
    // sensor variables:
    if (sensors.length >=2) {
      sensor0 = sensors[0];
      sensor1 = sensors[1];
    }
  }
}

Assignment

(Due Monday December 3):

  • Connect your virtual and physical worlds, AND/OR
  • Use an unfamiliar component to make something interesting.

For example,

  • Make an object which responds to a Tweet in an interesting way, or/and which sends a Tweet for an interesting reason. For example, you might have a small box in your fridge, that tweets whenever someone opens the door and the light goes on. Give careful thought to where your project “lives”, and who (or what) it responds to.

And of course,

  • Create a YouTube video which documents your project. In a blog post, embed your video, and write some text (~100-150 words) about your project: what it does, why, and how. Also provide code (processing, arduino), a Fritzing diagram, and an image/photo if appropriate.

Feel free to consider using Pachube as a source for live data streams.
Consider using IFTTT (If This Then That) as a way of connecting your universes.


Overview

In this unit, we’re exploring the idea of the “Internet of Things“, and Things that Tweet, by making networked objects. These are things — artworks or what-have-you — that are connected to the Internet. They may act on signals they receive, or they may transmit signals from things they sense, or both. They can even act on a signal from the other side of the world! We’ll take advantage of open protocols like Twitter, open-source arts engineering tools like Processing and Arduino, and API’s like Twitter4j and the Java String API in order to do our work:

In order to do this project, you’ll need to have a Twitter account, and then create an authorized “App”. Basically, Twitter wants you to have fun, but they also want to keep track of what you’re doing, so that you don’t send lots of spam. (Computer programs are good at that.) You’ll need to get credentials by doing the following:

  • Get a Twitter account
  • Go to https://dev.twitter.com/apps/new
  • Fill out the description of your application (e.g. “Caroline’sEMS2App”)
  • Agree to the developer’s license, fill out the CAPTCHA, etc…
  • Change your app settings (Settings tab) to “Read and Write”
  • Copy and paste your credentials: OAuthConsumerKey and OAuthConsumerSecret
  • Request, then copy and paste your AccessToken and AccessTokenSecret
  • Twitter4j will require those credentials in order for you to do stuff.

Here are TWO PIECES OF IMPORTANT INFO:

  • Please don’t use my (Golan’s) credentials, which happen to appear in the code examples linked below. I don’t want to be blacklisted because you sent a bunch of spam. Please get your own.
  • Limit your query rate! Twitter is pretty concerned with limiting the rates of queries you make. Your app can get blacklisted (for 15 days!) if you exceed 350 queries per hour. That’s why I have delay(12000) in the code examples that fetch data from Twitter.

DEMO CODE

(Please note that you’ll need to substitute in your own Twitter-Developer credentials for these to work.) See zip file (above) for most recent versions.

  1. Here’s a simple demo of Tweeting from Processing.
  2. Here’s code for Arduino + Processing, such that pressing a button on the Arduino sends a tweet via Processing.
  3. Here’s an Arduino+Processing project that moves a servo in response to a tweet. The servo is set to a position which comes from a number parsed from the Tweet.
  4. Here’s a Processing project that checks Twitter periodically to see if someone — anyone — has recently Tweeted a certain word. (I chose the word “finagle” because it doesn’t come up too often.) When that word is Tweeted, a signal is sent to an Arduino.

Strings

Here are some simple examples of String parsing… for example, if you were trying to parse Tweets:

//--------------------------------------------------------
// Testing to see if a Tweet contains a certain word
String tweetString = "I sure love bees";
String testString = "love";

int locationOfTestStringInTweet = tweetString.indexOf(testString);
if (locationOfTestStringInTweet != -1) {
  println ("Test string exists! (At character index: " + locationOfTestStringInTweet + ")");
}

//--------------------------------------------------------
// Converting a single number (encoded as a String) into an integer
String singleIntegerString = "123"; // that's a String, not an integer, folks

// If you want to be really safe, you have to make sure it's actually an integer.
// We do that with the try/catch business, which takes care of errors.
try {
  int integerFetchedFromString = Integer.parseInt (singleIntegerString);
  println ("The encoded integer = " + integerFetchedFromString) ;
}
catch(NumberFormatException e) {
  println("That wasn't an integer, silly!"); // you gots problems
}

//--------------------------------------------------------
// Parsing some numbers from a tweet.
// Here, I'm relying on an assumption that our tweet is correctly formatted, so no try/catch....
String tweetMixingStringsAndNumbers = "127 180 255 happy";
String arrayOfWords[] = split (tweetMixingStringsAndNumbers, " ");
int valueA = Integer.parseInt (arrayOfWords[0]);
int valueB = Integer.parseInt (arrayOfWords[1]);
int valueC = Integer.parseInt (arrayOfWords[2]);
String mood = arrayOfWords[3];
println("Commands: " + valueA + " " + valueB + " " + valueC);