Interior Weather – Coding a Twitter Switch using Processing and Arduino

Here is the code to connect Processing to the Twitter Live Stream and have Processing sending Serial to an Arduino:
/// TWITTER //////////////////////////////////////////////////////////////////////////////////////

////// ALONE – Connecting to the Twitter API
static String OAuthConsumerKeyA = “NDgkkBotZeXcoAPMOsemA”;
static String OAuthConsumerSecretA = “XusVVwearPNjGwhYpYkgnlAM4WRSwu2EnGVHjQdSaOw”;
static String AccessTokenA = “102763568-uuNmUvzeOBFNyDaX1GA1XyW5TTiUekQKhLRYjuhV”;
static String AccessTokenSecretA = “TUpkVOftKcLAjsFVnzkjJV9bB30bRSbixgOF983appk”;

////// SAD – Connecting to the Twitter API
static String OAuthConsumerKeyB = “Z1PMOJj7f3jP0LCvl32ng”;
static String OAuthConsumerSecretB = “WRehjiit2gEk1TOb2kDvKbG8Wdwg2Qg0IoDYCZof7ek”;
static String AccessTokenB = “981269682-0kVLQO6tmaZKC8UOm49spp4K1u3e9q4vptKMcPMw”;
static String AccessTokenSecretB = “d1VLGSeKALv2dz61xx9eLdhpCqTZGuCDE1ms311w”;

////// ANGRY – Connecting to the Twitter API
static String OAuthConsumerKeyC = “”;
static String OAuthConsumerSecretC = “”;
static String AccessTokenC = “”;
static String AccessTokenSecretC = “”;

////// AFRAID – Connecting to the Twitter API
static String OAuthConsumerKeyD = “”;
static String OAuthConsumerSecretD = “”;
static String AccessTokenD = “”;
static String AccessTokenSecretD = “”;

////// Declaring the Twitter Factories
TwitterStream twitterA = new TwitterStreamFactory().getInstance();
TwitterStream twitterB = new TwitterStreamFactory().getInstance();
TwitterStream twitterC = new TwitterStreamFactory().getInstance();
TwitterStream twitterD = new TwitterStreamFactory().getInstance();

////// Find I FEEL ALONE in the live Twitter live stream
String keywordsA[] = { “I feel alone”};
String alone = “”;
ArrayList allTweetsAlone = new ArrayList();
boolean aloneOn;

////// Find I AM SAD in the live Twitter live stream
String keywordsB[] = {“I am sad”};
String sad = “”;
ArrayList allTweetsSad = new ArrayList();
boolean sadOn;

////// Find I AM ANGRY in the live Twitter live stream
String keywordsC[] = {“I am angry”};
String angry = “”;
ArrayList allTweetsAngry = new ArrayList();
boolean angryOn;

////// Find I AM HAPPY in the live Twitter live stream
String keywordsD[] = {“I am afraid of”};
String afraid = “”;
ArrayList allTweetsAfraid = new ArrayList();
boolean afraidOn;

/// ARDUINO //////////////////////////////////////////////////////////////////////////////////////

////// Declaring for the Serial communication
import processing.serial.*;
Serial port;

/// MINIM ////////////////////////////////////////////////////////////////////////////////////////
import ddf.minim.*;

Minim minim;
AudioPlayer[] track = new AudioPlayer [9];

/// SETUP ////////////////////////////////////////////////////////////////////////////////////////

void setup () {

size(500, 500);

///// Communicating with Twitter
connectTwitter();
twitterA.addListener(listenerA);
twitterA.filter(new FilterQuery().track(keywordsA));

twitterB.addListener(listenerB);
twitterB.filter(new FilterQuery().track(keywordsB));

twitterC.addListener(listenerC);
twitterC.filter(new FilterQuery().track(keywordsC));

twitterD.addListener(listenerD);
twitterD.filter(new FilterQuery().track(keywordsD));

////// Serial Communicatione
println(Serial.list());
port = new Serial(this, Serial.list()[8], 115200);
println(Serial.list());

////// MINIM
minim = new Minim(this);

for (int i = 0; i < track.length; i++) {
track [i] = minim.loadFile( i + “.mp3″);
}
}

/// DRAW /////////////////////////////////////////////////////////////////////////////////////////

void draw () {
background(0);

// ALONE // H
if (aloneOn == true) {
ellipse (width/2, height/2, 30, 30);
port.write(‘H’);
if (track[7].position() > track[7].length()-1000) {
aloneOn = false;
track[7].rewind();
}
else if (track[7].isPlaying() ==false) {
track[7].play();
port.write(‘H’);
}
}

// SAD // L
if (sadOn == true) {
ellipse (30, 250, 30, 30);
port.write(‘L’);
if (track[6].position() > track[6].length()-1000) {
sadOn = false;
track[6].rewind();
}
else if (track[6].isPlaying() ==false) {
track[6].play();
port.write(‘L’);
}
}

// ANGRY // M
if (angryOn == true) {
ellipse (250, 30, 30, 30);
port.write(‘M’);
if (track[1].position() > track[1].length()-1000) {
angryOn = false;
track[1].rewind();
}
else if (track[1].isPlaying() ==false) {
track[1].play();
port.write(‘M’);
}
}

// AFRAID // N
if (afraidOn == true) {
ellipse (420, 250, 30, 30);
port.write(‘N’);
if (track[8].position() > track[8].length()-500) {
afraidOn = false;
track[8].rewind();
}
else if (track[8].isPlaying() ==false) {
track[8].play();
port.write(‘N’);
}
}

if(aloneOn == false && sadOn == false && angryOn == false && afraidOn == false) {
track[5].play();
if(track[5].position() > track[5].length()-500) {
track[5].rewind();
}
}

}
////////////////////////////////////////////////////////////////////////////////////////////////////

void stop () {
track[9].close();
minim.stop();

///////// This listens for new tweet with ALONE ///////////////////////////////////////////////////
StatusListener listenerA = new StatusListener() {
public void onStatus(Status status) {

alone = status.getText();
allTweetsAlone.add(alone);

for (int i = 0; i < allTweetsAlone.size(); i++) {
String thisTweet = (String) allTweetsAlone.get(i);
println (“stringAlone: ” + thisTweet);
}
aloneOn = true;
println (“stringAlonesize: ” + allTweetsAlone.size());
};
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println(“Got a status deletion notice id:” + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println(“Got track limitation notice:” + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println(“Got scrub_geo event userId:” + userId + ” upToStatusId:” + upToStatusId);
}

public void onException(Exception ex) {
ex.printStackTrace();
}
};

 

///////// This listens for new tweet with SAD ///////////////////////////////////////////////////
StatusListener listenerB = new StatusListener() {
public void onStatus(Status status) {

sad = status.getText();
allTweetsSad.add(sad);

for (int i = 0; i < allTweetsSad.size(); i++) {
String thisTweet = (String) allTweetsSad.get(i);
println (“stringSAD: ” + thisTweet);
}
sadOn = true;
println (“stringSADsize: ” + allTweetsSad.size());
};
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println(“Got a status deletion notice id:” + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println(“Got track limitation notice:” + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println(“Got scrub_geo event userId:” + userId + ” upToStatusId:” + upToStatusId);
}

public void onException(Exception ex) {
ex.printStackTrace();
}
};

 

///////// This listens for new tweet with SAD ///////////////////////////////////////////////////
StatusListener listenerC = new StatusListener() {
public void onStatus(Status status) {

angry = status.getText();
allTweetsAngry.add(angry);

for (int i = 0; i < allTweetsAngry.size(); i++) {
String thisTweet = (String) allTweetsAngry.get(i);
println (“stringANGRY: ” + thisTweet);
}
angryOn = true;
println (“stringANGRYsize: ” + allTweetsAngry.size());
};
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println(“Got a status deletion notice id:” + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println(“Got track limitation notice:” + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println(“Got scrub_geo event userId:” + userId + ” upToStatusId:” + upToStatusId);
}

public void onException(Exception ex) {
ex.printStackTrace();
}
};

 

///////// This listens for new tweet with HAPPY ///////////////////////////////////////////////////
StatusListener listenerD = new StatusListener() {
public void onStatus(Status status) {

afraid = status.getText();
allTweetsAfraid.add(afraid);

for (int i = 0; i < allTweetsAfraid.size(); i++) {
String thisTweet = (String) allTweetsAfraid.get(i);
println (“stringAfraid: ” + thisTweet);
}
afraidOn = true;
println (“stringAfraidsize: ” + allTweetsAfraid.size());
};
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println(“Got a status deletion notice id:” + statusDeletionNotice.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println(“Got track limitation notice:” + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println(“Got scrub_geo event userId:” + userId + ” upToStatusId:” + upToStatusId);
}

public void onException(Exception ex) {
ex.printStackTrace();
}
};

 

////// Initial connection
void connectTwitter() {

// Connection for ALONE
twitterA.setOAuthConsumer(OAuthConsumerKeyA, OAuthConsumerSecretA);
AccessToken accessTokenA = loadAccessTokenA();
twitterA.setOAuthAccessToken(accessTokenA);

// Connection for SAD
twitterB.setOAuthConsumer(OAuthConsumerKeyB, OAuthConsumerSecretB);
AccessToken accessTokenB = loadAccessTokenB();
twitterB.setOAuthAccessToken(accessTokenB);

// Connection for ANGRY
twitterC.setOAuthConsumer(OAuthConsumerKeyC, OAuthConsumerSecretC);
AccessToken accessTokenC = loadAccessTokenC();
twitterC.setOAuthAccessToken(accessTokenC);

// Connection for Happy
twitterD.setOAuthConsumer(OAuthConsumerKeyD, OAuthConsumerSecretD);
AccessToken accessTokenD = loadAccessTokenD();
twitterD.setOAuthAccessToken(accessTokenD);

}

/////// Loading up the access token

// Access token for ALONE
private static AccessToken loadAccessTokenA() {
return new AccessToken(AccessTokenA, AccessTokenSecretA);
}
// Access token for Sad
private static AccessToken loadAccessTokenB() {
return new AccessToken(AccessTokenB, AccessTokenSecretB);
}
// Access token for ANGRY
private static AccessToken loadAccessTokenC() {
return new AccessToken(AccessTokenC, AccessTokenSecretC);
}
// Access token for HAPPY
private static AccessToken loadAccessTokenD() {
return new AccessToken(AccessTokenD, AccessTokenSecretD);
}

Here is the code to have the Arduino receiving the serial communication from Processing and activating the devices:

 
//MotorA
const int enablePin = 7;
const int motor1Pin = 6;
const int motor2Pin = 5;
//MotorB
const int enableBPin = 4;
const int motorB1Pin = 3;
const int motorB2Pin = 2;

const int ledPinA = 8;
const int ledPinB = 9;
const int ledPinC = 10;
const int ledPinD = 11;
const int ledPinF = 12;

int incomingByte;

void setup() {
Serial.begin(9600);

pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);

pinMode(enableBPin, OUTPUT);
pinMode(motorB1Pin,OUTPUT);
pinMode(motorB2Pin, OUTPUT);

pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(ledPinD, OUTPUT);
pinMode(ledPinF, OUTPUT);

//digitalWrite(enablePin,HIGH);
//digitalWrite(enableBPin,HIGH);

}
void loop(){
if (Serial.available() > 0) {
incomingByte = Serial.read();

if (incomingByte == ‘H’) {
digitalWrite(enablePin,HIGH);
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);

digitalWrite(enableBPin,HIGH);
digitalWrite(motorB1Pin, HIGH);
digitalWrite(motorB2Pin, LOW);

digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinF, LOW);

Serial.println(“ALONE”);
}
if (incomingByte == ‘L’) {
digitalWrite(enablePin,LOW);
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW);

digitalWrite(enableBPin,LOW);
digitalWrite(motorB1Pin, LOW);
digitalWrite(motorB2Pin, LOW);

digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinF, LOW);

Serial.println(“SAD”);
}
if (incomingByte == ‘M’) {
digitalWrite(enablePin,LOW);
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW);

digitalWrite(enableBPin,LOW);
digitalWrite(motorB1Pin, LOW);
digitalWrite(motorB2Pin, LOW);

digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinD, LOW);
digitalWrite(ledPinF, LOW);

Serial.println(“ANGRY”);
}
if (incomingByte == ‘N’) {
digitalWrite(enablePin,LOW);
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW);

digitalWrite(enableBPin,LOW);
digitalWrite(motorB1Pin, LOW);
digitalWrite(motorB2Pin, LOW);

digitalWrite(ledPinA, HIGH);
digitalWrite(ledPinB, HIGH);
digitalWrite(ledPinC, HIGH);
digitalWrite(ledPinD, HIGH);
digitalWrite(ledPinF, HIGH);

Serial.println(“AFRAID”);
}
}

}

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>