public
Created

wunderground + twitter: another attempt

  • Download Gist
Glyph.pde
Processing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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);
}
}
Tweets.pde
Processing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
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;
}
}
}
wunderground_twitter_wip3.pde
Processing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
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();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.