Skip to content

Instantly share code, notes, and snippets.

@rhathaway
Created December 15, 2011 23:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rhathaway/1483478 to your computer and use it in GitHub Desktop.
wunderground + twitter: another attempt
class Glyph {
int x, y, glyphsize;
String text;
Glyph() {
x = 410;
y = 50;
glyphsize = 10;
}
void paint() {
smooth();
noStroke();
textFont(tweetFont, 12);
text(text, x, y);
y = y + 1;
//color(255, 255, 255, 100);
//ellipse(x + i * 10, y, glyphsize, glyphsize);
}
}
class Tweets {
String text;
float x, y, angle;
Tweets() {
HttpClient twitterClient;
x = random(width);
y = random(height);
angle = random(360);
twitterClient = new HttpClient(this, "search.twitter.com");
//// using SSL is optional, but recommended
twitterClient.useSSL = true;
//// turn on OAuth request signing
twitterClient.useOAuth = true;
//// set up OAuth signing parameters
twitterClient.oauthConsumerKey = "---";
twitterClient.oauthConsumerSecret = "---";
twitterClient.oauthAccessToken = "---";
twitterClient.oauthAccessTokenSecret = "---";
//// set up POST params
HashMap params1 = new HashMap();
// tweets about the weather within 15mi of honolulu
params1.put("q", "weather geocode:21.306944,-157.858333,15mi");
HashMap params2 = new HashMap();
// tweets about the weather within 15mi of san francisco
params2.put("q", "weather geocode:37.781157,-122.398720,15mi");
HashMap params3 = new HashMap();
// tweets about the weather within 15mi of chicago
params3.put("q", "weather geocode:41.85003,-87.650052,15mi");
HashMap params4 = new HashMap();
// tweets about the weather within 15mi of nyc
params4.put("q", "weather geocode:40.712602,-74.005972,15mi");
//// variable assignment for tweets- we instantiate a new array of Glyph objects with size 50
tweetGlyphs = new Glyph[50];
for (int i = 0; i < tweetGlyphs.length; i++) {
tweetGlyphs[i] = new Glyph();
}
}
void responseReceived(HttpRequest request, HttpResponse response) {
println(response.statusCode + ": " + response.statusMessage);
println(response.getContentAsString());
JSONObject content = response.getContentAsJSONObject();
JSONObject dinoResults = content.get("results");
glyphs = new Glyph[dinoResults.size()];
for (int i = 0; i < dinoResults.size(); i++) {
JSONObject tweet = dinoResults.get(i);
TweetGlyph tg = new TweetGlyph();
tg.text = tweet.get("text").stringValue();
glyphs[i] = tg;
}
}
void paint() {
textFont(tweetFont, 12);
text(text, x, y);
//// this is code that MOVES the glylph
x = x + 2 * cos(radians(angle));
y = y + 2 * sin(radians(angle));
if (y > height) {
y = 0;
}
if (y < 0) {
y = height;
}
if (x > width) {
x = 0;
}
if (x < 0) {
x = width;
}
}
}
import com.francisli.processing.http.*;
import processing.serial.*;
//// variable declaration of an array of Glyph objects, called glyphs
Glyph[] glyphs;
Serial myPort; // arduino port
String oldValue;
PFont cityFont, timeFont, weatherFont, tweetFont;
PImage backgroundImg;
PImage conditionImg;
PImage windImg;
String locationInfo, currentTime, currentConditions, iconURL, wind, windDirection;
float tempF, tempC, windSpeed;
HttpClient client, twitterClient;
HttpRequest weatherRequest, twitterRequest;
HashMap params1, params2, params3, params4;
void setup() {
size(1024, 768);
frameRate(24);
background(255);
// set up the communication between the Arduino and the Processing app
myPort = new Serial(this, Serial.list()[0], 9600);
cityFont = loadFont("UbuntuCondensed-Regular-45.vlw");
timeFont = loadFont("UbuntuCondensed-Regular-18.vlw");
weatherFont = loadFont("UbuntuCondensed-Regular-22.vlw");
tweetFont = loadFont("UbuntuCondensed-Regular-18.vlw");
backgroundImg = loadImage("weatherbg.jpg");
client = new HttpClient(this, "api.wunderground.com");
}
void serialEvent(Serial myPort) {
byte[] inBuffer = new byte[50];
while (myPort.available () > 0) {
inBuffer = myPort.readBytes();
myPort.readBytes(inBuffer);
if (inBuffer != null) {
String myString = new String(inBuffer); // read string from arduino
println(myString); // pin 2: HNL, pin 4: SFO, pin 6: CHI, pin 8: NYC
//// do stuff to read the new data
if (!myString.equals(oldValue)) {
if (myString.equals("H")) {
/// set up and dispatch a request for the weather
weatherRequest = client.GET("/api/[api_key]/conditions/q/HI/Honolulu.json");
//// and for twitter
twitterRequest = twitterClient.GET("/search.json", params1);
}
if (myString.equals("S")) { /// do the other cities
weatherRequest = client.GET("/api/[api_key]/conditions/q/CA/San_Francisco.json");
twitterRequest = twitterClient.GET("/search.json", params2);
}
if (myString.equals("C")) {
weatherRequest = client.GET("/api/[api_key]/conditions/q/IL/Chicago.json");
twitterRequest = twitterClient.GET("/search.json", params3);
}
if (myString.equals("N")) {
weatherRequest = client.GET("/api/[api_key]/conditions/q/NY/New_York_City.json");
twitterRequest = twitterClient.GET("/search.json", params4);
}
oldValue = myString;
}
}
}
}
void responseReceived(HttpRequest request, HttpResponse response) {
//// check for HTTP 200 success code
if (response.statusCode == 200) {
println(response.getContentAsString());
JSONObject results = response.getContentAsJSONObject();
locationInfo = results.get("current_observation").get("display_location").get("full").stringValue();
currentTime = results.get("current_observation").get("observation_time_rfc822").stringValue();
tempF = results.get("current_observation").get("temp_f").floatValue();
tempC = results.get("current_observation").get("temp_c").floatValue();
currentConditions = results.get("current_observation").get("weather").stringValue();
iconURL = results.get("current_observation").get("icon_url").stringValue();
wind = results.get("current_observation").get("wind_string").stringValue();
windDirection = results.get("current_observation").get("wind_dir").stringValue();
windSpeed = results.get("current_observation").get("wind_mph").floatValue();
println(locationInfo);
println(currentTime);
println("Current conditions: " + currentConditions);
println("Temperature: " + tempF + "° F / " + tempC + "° C");
println(iconURL);
println("Wind conditions: " + wind);
println("Wind speed/direction: " + windSpeed + " MPH from the " + windDirection);
imageMode(CORNERS);
image(backgroundImg, 0, 0);
textAlign(LEFT, TOP);
textFont(cityFont, 45);
fill(0, 200);
text(locationInfo, 15, 15);
textFont(timeFont, 18);
text(currentTime, 15, 65);
conditionImg = loadImage(iconURL);
image(conditionImg, 15, 110);
textFont(weatherFont, 22);
text(tempF + "° F / " + tempC + "° C", 75, 110);
text(currentConditions, 75, 135);
windImg = loadImage("wind-icon.gif");
image(windImg, 15, 185);
text("WIND: " + wind, 75, 185, 300, 60);
text("SPEED/DIRECTION: ", 75, 250);
text(windSpeed + " MPH from the " + windDirection, 75, 275);
}
}
void draw() {
//// loop through each Glyph in the array and let it paint itself to the screen
for (int i = 0; i < glyphs.length; i = i + 1) {
glyphs[i].paint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment