ARDUINO 3A – Talking to the Cloud

03B – ARDUINO > TWEETING

We can then use the flexibility of Processing to connect sensor data to services online such as Twitter. This example shows how to take the potentiometer circuit and code you’ve just made, and connect it to Twitter via Processing. It is set up so that when your potentiometer is turned up to its highest level, the Processing sketch will publish a tweet for you. This example enables you to create things that tweet updates.

TWITTER SETUP:

In to use the twitter API you need to register the details as a developer and get authorization keys. You do this as follows:

1. Visit https://apps.twitter.com/ and login with your twitter username and password.
2. Click the “Create a new application” button” on the top right.
3. Fill in the form. (You can use any old URL that you have – for example your purchase url)
4. Click on “Keys and Access Tokens”. You should see the first two of the four things that we need: the Consumer key and the Consumer secret.
5. Next to “Access Level” click “Modify Permssions” and choose the access you want.
6. Return to the “Keys” tab and click the button that says “Create my access token”.
7. Fill in these four values as strings in the fields indicated in the Processing sketch.

PROCESSING SETUP:

1. Download the Twitter4j library here. 
2. Unzip library and copy the folder the folder named ‘libraries’ in the Processing folder
3. Rename the whole library folder so that it is called ‘twitter4j’
4. Go into this folder and rename the folder called ‘lib’ so it is called ‘library’ instead.

Update this code with your Twitter oAuth info and compile in Processing. It will tweet every time a byte of 255 is sent in from Arduino.

import processing.serial.*; //import serial library
import twitter4j.conf.*; //import twitter library
import twitter4j.api.*; 
import twitter4j.*; 
import java.util.*;  
 
ConfigurationBuilder cb;
Query query;
Twitter twitter;
Serial myPort;  // Create object from Serial class
int inByte;
 
void setup() {
  size(800, 600);      // window size
  println(Serial.list());// List all the available serial ports
  // change the number below to match your port. For me it was my 3rd port (port 2) but check what your consol says.
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
}
 
void draw() {
  background(#081640);  
  if (inByte==255) {
    println("Tweeting");
    login();
    tweet();
  }
}
 
void serialEvent(Serial myPort){
    // get the byte:
  inByte = myPort.read();
  // print it:
  println(inByte);
}
 
void login() {
  cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey("YourCosumerKey");
  cb.setOAuthConsumerSecret("YourConsumerSecret");
  cb.setOAuthAccessToken("YourAccessToken");
 cb.setOAuthAccessTokenSecret("YourAccessTokenSecret");
}
 
void tweet() {
  Twitter twitter = new TwitterFactory(cb.build()).getInstance();
  try {
    Status status = twitter.updateStatus("I AM A TWEETING POTENTIOMETER #SUNYSS15");
    System.out.println("Successfully updated the status to [" + status.getText() + "].");
  }
  catch (TwitterException te) {
    println("Couldn't connect: " + te);
  };
}

ARDUINO

Use the arduino code and circuit from the first exercise (03A) to send serial data to Processing.

03C – SEARCHING TWITTER AND PINGING ARDUINO

We can work the other way and listen on twitter for particular posts, and when hearing a new post with a particular tag, ping Arduino to trigger something.
PROCESSING CODE:

// Updated 1/27/15
// See http://twitter4j.org/en/code-examples.html for more examples and details.
 
// 1. Visit https://apps.twitter.com/ and login with your twitter username and password.
// 2. Click the "Create a new application" button" on the top right.
// 3. Fill in the form.
// 4. Click on "Keys and Access Tokens". You should see the first two of the four things
//    that we need: the Consumer key and the Consumer secret. 
// 5. Next to "Access Level" click "Modify Permssions" and choose the access you want.
// 6. Return to the "Keys" tab and click the button that says "Create my access token".
// 7. Fill in these four values as strings in the cb fields below.
 
import twitter4j.conf.*; //import twitter library files
import twitter4j.api.*; 
import twitter4j.*; 
import java.util.*;  
 
ConfigurationBuilder cb;
Query query;
Twitter twitter;
 
//array for tweets
ArrayList statuses; 
 
import processing.serial.*;
 
Serial myPort;
 
//size of previous array
int psize;
//size of current array
int csize;
 
void setup() {
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[2], 9600);
  myPort.buffer(1); //change this to whatever port your computer is using 
}
 
void draw() { 
}
 
void mousePressed() {
  login();
  search();
}
void login() {
  cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey("ConsumerKey");
  cb.setOAuthConsumerSecret("ConsumerSecret");
  cb.setOAuthAccessToken("AccessToken");
  cb.setOAuthAccessTokenSecret("AccessTokenSecret");
}
 
void search() {
  Twitter twitter = new TwitterFactory(cb.build()).getInstance();
  Query query = new Query("#SUNYSS15"); //search twitter for hashtag
 
  try {
    QueryResult result = twitter.search(query);
    statuses = (ArrayList) result.getTweets();
 
    for (int i = 0; i < statuses.size (); i++) {
      Status s = (Status) statuses.get(i);
      String user = s.getUser().getScreenName();
      String msg = s.getText();
      Date d = s.getCreatedAt();
      println("Tweet by " + user + " at " + d + ": " + msg);
    };
  }
  catch (TwitterException te) {
    println("Couldn't connect: " + te);
  };
  csize=(statuses.size());  //get current arrayList size
  //if csize> psize, then there is a new tweet
  if(csize>psize){ //if new tweet
    myPort.write(1); 
    println("1");//send a 1 to processing
  }else{
    myPort.write(0);
     println("0"); //send a 0 to procssing
  }
 
  psize=csize; //set previous size to whatever current size is
}

ARDUINO CODE:
Arduino will flash an LED on Pin 13 if a 1 is sent from Processing.

int ledPin = 13;      // select the pin for the LED
 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  // put your main code here, to run repeatedly:
  int input = Serial.read();  // read serial 
  if(input==1){
     digitalWrite(ledPin, HIGH);
     delay(1000);
 
     digitalWrite(ledPin, LOW); 
     delay(1000);
  }else{
    digitalWrite(ledPin, LOW); 
  }
 
}

03D – CONNECTING ARDUINO TO IFTTT (IF THIS THEN THAT)

Here is a tutorial on how to do this using an online data service called Lithouse.

This means you can connect Arduino to trigger various online services – any that you can use in an IFTTT recipe. Although this tutorial is using a motion sensor, you could replace this with any sensor in your kit. IE. a button, light sensor, heat sensor etc.

It is more difficult to go the other way around and use an IFTTT recipe to trigger an arduino component. But if you need to do this, it could be set up via IFTTT triggering a tweet with a particular hashtag, and having processing then send serial data to arduino (like in the example above).