Skip to content

Instantly share code, notes, and snippets.

@rhathaway
Created December 15, 2011 23:09
Show Gist options
  • Save rhathaway/1483408 to your computer and use it in GitHub Desktop.
Save rhathaway/1483408 to your computer and use it in GitHub Desktop.
wunderground + twitter: one attempt
class Glyph {
int x, y, glyphsize;
Glyph() {
x = 410;
y = 50;
glyphsize = 10;
}
void paint(int i) {
smooth();
noStroke();
color(255, 255, 255, 100);
ellipse(x + i * 10, y, glyphsize, glyphsize);
}
void paint2(int i) {
smooth();
noStroke();
color(255, 255, 255, 100);
ellipse(x + i * 10, y + 400, glyphsize, glyphsize);
}
}
import com.francisli.processing.http.*;
import processing.serial.*;
Serial arduinoPort;
//// variable declaration of an two arrays of Glyph objects
Glyph[] happyGlyphs;
Glyph[] sadGlyphs;
// twitter stuff
HttpClient weatherClient, twitterClient;
HttpRequest happySearch;
HttpRequest sadSearch;
JSONObject content;
// wunderground stuff
PFont cityFont, timeFont, weatherFont;
PImage backgroundImg;
PImage conditionImg;
PImage windImg;
String locationInfo, currentTime, currentConditions, iconURL, wind, windDirection;
float tempF, tempC, windSpeed;
// variables for specific city data
String HNLinfo, HNLtime, HNLconditions, HNLwind, HNLwindDirection, HNLiconURL;
float HNLtempF, HNLtempC, HNLwindSpeed;
String SFOinfo, SFOtime, SFOconditions, SFOwind, SFOwindDirection, SFOiconURL;
float SFOtempF, SFOtempC, SFOwindSpeed;
String CHIinfo, CHItime, CHIconditions, CHIwind, CHIwindDirection, CHIiconURL;
float CHItempF, CHItempC, CHIwindSpeed;
String NYCinfo, NYCtime, NYCconditions, NYCwind, NYCwindDirection, NYCiconURL;
float NYCtempF, NYCtempC, NYCwindSpeed;
HttpRequest HNLweather, SFOweather, CHIweather, NYCweather;
void setup() {
size(1024, 768);
frameRate(24);
arduinoPort = new Serial(this, Serial.list()[0], 9600);
wundergroundRequest();
twitterRequest();
}
void wundergroundRequest() {
cityFont = loadFont("UbuntuCondensed-Regular-45.vlw");
timeFont = loadFont("UbuntuCondensed-Regular-18.vlw");
weatherFont = loadFont("UbuntuCondensed-Regular-22.vlw");
backgroundImg = loadImage("weatherbg.jpg");
weatherClient = new HttpClient(this, "api.wunderground.com");
HNLweather = weatherClient.GET("/api/[api_key]/conditions/q/HI/Honolulu.json");
SFOweather = weatherClient.GET("/api/[api_key]/conditions/q/CA/San_Francisco.json");
CHIweather = weatherClient.GET("/api/[api_key]/conditions/q/IL/Chicago.json");
NYCweather = weatherClient.GET("/api/[api_key]/conditions/q/NY/New_York_City.json");
}
void twitterRequest() {
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 params = new HashMap();
// positive tweets about the weather within 15mi of san francisco
params.put("q", "weather :) geocode:37.781157,-122.398720,15mi");
happySearch = twitterClient.GET("/search.json", params);
HashMap params2 = new HashMap();
// negative tweets about the weather within 15mi of san francisco
params2.put("q", "weather :( geocode:37.781157,-122.398720,15mi");
sadSearch = twitterClient.GET("/search.json", params2);
//// variable assignment- we instantiate a new array of Glyph objects with size 200
happyGlyphs = new Glyph[200];
for (int i = 0; i < happyGlyphs.length; i = i + 1) {
happyGlyphs[i] = new Glyph();
}
sadGlyphs = new Glyph[200];
for (int i = 0; i < sadGlyphs.length; i = i + 1) {
sadGlyphs[i] = new Glyph();
}
}
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 info
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();
if (request == HNLweather) { // Honolulu weather
HNLinfo = locationInfo.toUpperCase();
HNLtime = currentTime;
HNLtempF = tempF;
HNLtempC = tempC;
HNLconditions = currentConditions;
HNLiconURL = iconURL;
HNLwind = wind;
HNLwindDirection = windDirection;
HNLwindSpeed = windSpeed;
println(HNLinfo);
println(HNLtime);
println("Current conditions: " + HNLconditions);
println("Temperature: " + HNLtempF + "° F / " + HNLtempC + "° C");
println(HNLiconURL);
println("Wind conditions: " + HNLwind);
println("Wind speed/direction: " + HNLwindSpeed + " MPH from the " + HNLwindDirection);
background(255);
imageMode(CORNERS);
image(backgroundImg, 0, 0);
textAlign(LEFT, TOP);
textFont(cityFont, 45);
fill(0, 200);
text(HNLinfo, 15, 15);
textFont(timeFont, 18);
text(HNLtime, 15, 65);
conditionImg = loadImage(HNLiconURL);
image(conditionImg, 15, 110);
textFont(weatherFont, 22);
text(HNLtempF + "° F / " + HNLtempC + "° C", 75, 110);
text(HNLconditions, 75, 135);
windImg = loadImage("wind-icon.gif");
image(windImg, 15, 185);
text("WIND: " + HNLwind, 75, 185, 300, 60);
text("SPEED/DIRECTION: ", 75, 250);
text(HNLwindSpeed + " MPH from the " + HNLwindDirection, 75, 275);
}
if (request == SFOweather) { // San Francisco weather
SFOinfo = locationInfo.toUpperCase();
SFOtime = currentTime;
SFOtempF = tempF;
SFOtempC = tempC;
SFOconditions = currentConditions;
SFOiconURL = iconURL;
SFOwind = wind;
SFOwindDirection = windDirection;
SFOwindSpeed = windSpeed;
println(SFOinfo);
println(SFOtime);
println("Current conditions: " + SFOconditions);
println("Temperature: " + SFOtempF + "° F / " + SFOtempC + "° C");
println(SFOiconURL);
println("Wind conditions: " + SFOwind);
println("Wind speed/direction: " + SFOwindSpeed + " MPH from the " + SFOwindDirection);
background(255);
imageMode(CORNERS);
image(backgroundImg, 0, 0);
textAlign(LEFT, TOP);
textFont(cityFont, 45);
fill(0, 200);
text(SFOinfo, 15, 15);
textFont(timeFont, 18);
text(SFOtime, 15, 65);
conditionImg = loadImage(SFOiconURL);
image(conditionImg, 15, 110);
textFont(weatherFont, 22);
text(SFOtempF + "° F / " + SFOtempC + "° C", 75, 110);
text(SFOconditions, 75, 135);
windImg = loadImage("wind-icon.gif");
image(windImg, 15, 185);
text("WIND: " + SFOwind, 75, 185, 300, 60);
text("SPEED/DIRECTION: ", 75, 250);
text(SFOwindSpeed + " MPH from the " + SFOwindDirection, 75, 275);
}
if (request == CHIweather) { // Chicago weather
CHIinfo = locationInfo.toUpperCase();
CHItime = currentTime;
CHItempF = tempF;
CHItempC = tempC;
CHIconditions = currentConditions;
CHIiconURL = iconURL;
CHIwind = wind;
CHIwindDirection = windDirection;
CHIwindSpeed = windSpeed;
println(CHIinfo);
println(CHItime);
println("Current conditions: " + CHIconditions);
println("Temperature: " + CHItempF + "° F / " + CHItempC + "° C");
println(CHIiconURL);
println("Wind conditions: " + CHIwind);
println("Wind speed/direction: " + CHIwindSpeed + " MPH from the " + CHIwindDirection);
background(255);
imageMode(CORNERS);
image(backgroundImg, 0, 0);
textAlign(LEFT, TOP);
textFont(cityFont, 45);
fill(0, 200);
text(CHIinfo, 15, 15);
textFont(timeFont, 18);
text(CHItime, 15, 65);
conditionImg = loadImage(CHIiconURL);
image(conditionImg, 15, 110);
textFont(weatherFont, 22);
text(CHItempF + "° F / " + CHItempC + "° C", 75, 110);
text(CHIconditions, 75, 135);
windImg = loadImage("wind-icon.gif");
image(windImg, 15, 185);
text("WIND: " + CHIwind, 75, 185, 300, 60);
text("SPEED/DIRECTION: ", 75, 250);
text(CHIwindSpeed + " MPH from the " + CHIwindDirection, 75, 275);
}
if (request == NYCweather) { // NYC weather
NYCinfo = locationInfo.toUpperCase();
NYCtime = currentTime;
NYCtempF = tempF;
NYCtempC = tempC;
NYCconditions = currentConditions;
NYCiconURL = iconURL;
NYCwind = wind;
NYCwindDirection = windDirection;
NYCwindSpeed = windSpeed;
println(NYCinfo);
println(NYCtime);
println("Current conditions: " + NYCconditions);
println("Temperature: " + NYCtempF + "° F / " + NYCtempC + "° C");
println(NYCiconURL);
println("Wind conditions: " + NYCwind);
println("Wind speed/direction: " + NYCwindSpeed + " MPH from the " + NYCwindDirection);
background(255);
imageMode(CORNERS);
image(backgroundImg, 0, 0);
textAlign(LEFT, TOP);
textFont(cityFont, 45);
fill(0, 200);
text(NYCinfo, 15, 15);
textFont(timeFont, 18);
text(NYCtime, 15, 65);
conditionImg = loadImage(NYCiconURL);
image(conditionImg, 15, 110);
textFont(weatherFont, 22);
text(NYCtempF + "° F / " + NYCtempC + "° C", 75, 110);
text(NYCconditions, 75, 135);
windImg = loadImage("wind-icon.gif");
image(windImg, 15, 185);
text("WIND: " + NYCwind, 75, 185, 300, 60);
text("SPEED/DIRECTION: ", 75, 250);
text(NYCwindSpeed + " MPH from the " + NYCwindDirection, 75, 275);
}
// twitter stuff
if (request == happySearch) {
JSONObject happyResults = content.get("results");
happyGlyphs = new Glyph[happyResults.size()];
for (int i = 0; i < happyResults.size(); i++) {
JSONObject tweet = happyResults.get(i);
Glyph tg = new Glyph();
// tg.text = tweet.get("text").stringValue();
happyGlyphs[i] = tg;
}
}
else if (request == sadSearch) {
JSONObject sadResults = content.get("results");
sadGlyphs = new Glyph[sadResults.size()];
for (int i = 0; i < sadResults.size(); i++) {
JSONObject tweet = sadResults.get(i);
Glyph tg = new Glyph();
// tg.text = tweet.get("text").stringValue();
sadGlyphs[i] = tg;
}
}
}
}
void draw () {
smooth();
println(happyGlyphs.length);
for (int i = 0; i < happyGlyphs.length; i = i + 1) {
happyGlyphs[i].paint(i);
}
println(sadGlyphs.length);
for (int i = 0; i < sadGlyphs.length; i = i + 1) {
sadGlyphs[i].paint2(i);
}
byte[] inBuffer = new byte[50];
while (arduinoPort.available () > 0) {
inBuffer = arduinoPort.readBytes();
arduinoPort.readBytes(inBuffer);
if (inBuffer != null) {
String myString = new String(inBuffer);
println(myString);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment