yoppa.org


Blog

Processing 2.0とTwitter4Jで、ProcessingからTweetしてみる

多摩美の授業で、Arduinoで取得したセンサーの値を、Processing経由でTwitterへ投稿するサンプルを作成する必要がありいろいろ調べてみた。昨年度は、Stewgateというプロキシを経由してやってみたのだけれど、できればProcessingで完結したい。というわけで調べてみた。

Processingのフォーラムで調べてみると、Twitter4JというJavaのライブラリを使うといいらしい。ただ、どうやってライブラリをProcessingに追加するかなどがいまいちあいまい… ということでまとめてみた。

まず、Twitter4Jのページから、最新版のライブラリをダウンロード。その中にあるlibフォルダ以下にある.jarファイルがProcessingで必要なものとなる。これらをまとめて、Processing側からLibraryとして認識されるよう”library.properties”などを記述。これでProcessingからLibraryとして認識されるはず。下記からProcessing用にまとめたものをダウンロードできます。

このZipファイルを解凍した”twitter4j”フォルダを、~/Document/Processing/libraries/ 以下にコピー。これで準備完了。

あとは、TwitterのDeveloper向けページで新規にアプリを作成し、”Consumer key”, “Consumer secret”, “Access token”, “Access token secret”をそれぞれ取得。また、Settingの項目の”Application Type”を”Read and Write”にしておくこと。

下記のテストコードで確認できます。画面をクリックすると、Tweetするはず。

 
import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.http.*;
import twitter4j.internal.util.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;

String msg = "Automatically posted from Processing";

String consumerKey = "xxxx";
String consumerSecret = "xxxx";
String accessToken = "xxxx";
String accessSecret = "xxxx";
Twitter myTwitter;

int ms;
color bg = color(0, 0, 0);

void setup() {
  size(400, 400);
  frameRate(60);

  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey(consumerKey);
  cb.setOAuthConsumerSecret(consumerSecret);
  cb.setOAuthAccessToken(accessToken);
  cb.setOAuthAccessTokenSecret(accessSecret);
  myTwitter = new TwitterFactory(cb.build()).getInstance();
}

void draw() {
  background(bg);
  if (millis() - ms > 1000) {
    bg = color(0, 0, 0);
  }
}

void mousePressed() {
  try {
    Status st = myTwitter.updateStatus(msg + ", " + getDate());
    println("Successfully updated the status to [" + st.getText() + "].");
    ms = millis();
    bg = color(0, 0, 255);
  }
  catch (TwitterException e) {
    println(e.getStatusCode());
    bg = color(255, 0, 0);
  }
}

String getDate() {
  String monthStr, dayStr, hourStr, minuteStr, secondStr;

  if (month() < 10) {
    monthStr = "0" + month();
  } 
  else {
    monthStr = "" + month();
  }
  if (day() < 10) {
    dayStr = "0" + day();
  } 
  else {
    dayStr = "" + day();
  }
  if (hour() < 10) {
    hourStr = "0" + hour();
  } 
  else {
    hourStr = "" + hour();
  }
  if (minute() < 10) {
    minuteStr = "0" + minute();
  } 
  else {
    minuteStr = "" + minute();
  }
  if (second() < 10) {
    secondStr = "0" + second();
  } 
  else {
    secondStr = "" + second();
  }

  return monthStr + "/" + dayStr + ", " + hourStr + ":" + minuteStr + ":" + secondStr;
}

追記

Arduinoと連携して、3分に一度、周囲の環境情報(このサンプルでは照度と温度)をつぶやくサンプルつくったので、こちらも掲載。センサーは、ArduinoのAnalog In 0に、照度センサー(NJL7502L)の値を、Analog In 1に温度センサー(LM61BIZ)の値を入れてます。

こんな感じでつぶやくはず!!

Arduino側

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

void loop() {
  int in0 = analogRead(0);
  int in1 = analogRead(1);
  
  Serial.print(in0);
  Serial.print('\t');
  Serial.println(in1);
  
  delay(180000);
}

Processing側

import processing.serial.*;
import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.http.*;
import twitter4j.internal.util.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;

String consumerKey = "xxxx";
String consumerSecret = "xxxx";
String accessToken = "xxxx";
String accessSecret = "xxxx";

Twitter myTwitter;
Serial myPort;

float illumi, tempr;

int ms;
color bg = color(0, 0, 0);

void setup() {
  size(400, 400);
  frameRate(60);

  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey(consumerKey);
  cb.setOAuthConsumerSecret(consumerSecret);
  cb.setOAuthAccessToken(accessToken);
  cb.setOAuthAccessTokenSecret(accessSecret);
  myTwitter = new TwitterFactory(cb.build()).getInstance();

  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
}

void draw() {
  background(bg);
  if (millis() - ms > 1000) {
    bg = color(0, 0, 0);
  }
}

void tweetValue() {
  try {
    Status st = myTwitter.updateStatus("My room illumination is " + illumi + ", temprture is " + tempr + ", " + getDate());
    println("Successfully updated the status to [" + st.getText() + "].");
    ms = millis();
    bg = color(0, 0, 255);
  }
  catch (TwitterException e) {
    println(e.getStatusCode());
    bg = color(255, 0, 0);
  }
}

void serialEvent(Serial p) {
  String inString0 = myPort.readStringUntil('\t');
  illumi = float(inString0);

  String inString1 = myPort.readStringUntil('\n');
  tempr = float(inString1) * 500.0 /1024.0 -60.0;

  tweetValue();
}

void mouseReleased() {
  tweetValue();
}

String getDate() {
  String monthStr, dayStr, hourStr, minuteStr, secondStr;

  if (month() < 10) {
    monthStr = "0" + month();
  } 
  else {
    monthStr = "" + month();
  }
  if (day() < 10) {
    dayStr = "0" + day();
  } 
  else {
    dayStr = "" + day();
  }
  if (hour() < 10) {
    hourStr = "0" + hour();
  } 
  else {
    hourStr = "" + hour();
  }
  if (minute() < 10) {
    minuteStr = "0" + minute();
  } 
  else {
    minuteStr = "" + minute();
  }
  if (second() < 10) {
    secondStr = "0" + second();
  } 
  else {
    secondStr = "" + second();
  }

  return monthStr + "/" + dayStr + ", " + hourStr + ":" + minuteStr + ":" + secondStr;
}
tadokoro — 03 July 2012 11:49
Disqusのコメントシステムが、かっこ良くなってる!