Created
December 15, 2011 00:05
-
-
Save rhathaway/1479196 to your computer and use it in GitHub Desktop.
final wunderground buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.francisli.processing.http.*; | |
import processing.serial.*; | |
Serial myPort; // arduino port | |
String oldValue; | |
PFont cityFont, timeFont, weatherFont, titleFont, subtitleFont; | |
PImage backgroundImg; | |
PImage conditionImg; | |
PImage windImg; | |
String locationInfo, currentTime, currentConditions, iconURL, wind, windDirection; | |
float tempF, tempC, windSpeed; | |
HttpClient client; | |
HttpRequest weatherRequest; | |
void setup() { | |
size(1024, 768); | |
frameRate(24); | |
background(255); | |
cityFont = loadFont("TradeGothicLTStd-48.vlw"); | |
timeFont = loadFont("TradeGothicLTStd-18.vlw"); | |
weatherFont = loadFont("TradeGothicLTStd-22.vlw"); | |
titleFont = loadFont("TradeGothicLTStd-60.vlw"); | |
subtitleFont = loadFont("TradeGothicLTStd-30.vlw"); | |
textAlign(CENTER, CENTER); | |
textFont(titleFont); | |
fill(0, 200); | |
text("UNDER THE WEATHER?", 512, 330); | |
textFont(subtitleFont); | |
text("local weather conditions for four U.S. cities", 512, 395); | |
textFont(timeFont); | |
text("(coming soon: emotional reactions via twitter)", 512, 430); | |
textFont(subtitleFont); | |
fill(183, 67, 63); | |
text("to start: press a button on the breadboard", 512, 670); | |
backgroundImg = loadImage("weatherbg.jpg"); | |
// set up the communication between the Arduino and the Processing app | |
myPort = new Serial(this, Serial.list()[0], 9600); | |
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 | |
fill(111,40,86); | |
weatherRequest = client.GET("/api/[api_key]/conditions/q/HI/Honolulu.json"); | |
//// and for twitter | |
//twitterRequest = twitterClient.GET("asdfasdf"); | |
} | |
if (myString.equals("S")) { /// do the other cities | |
fill(113,28,21); | |
weatherRequest = client.GET("/api/[api_key]/conditions/q/CA/San_Francisco.json"); | |
} | |
if (myString.equals("C")) { | |
fill(37,47,139); | |
weatherRequest = client.GET("/api/[api_key]/conditions/q/IL/Chicago.json"); | |
} | |
if (myString.equals("N")) { | |
fill(24,100,82); | |
weatherRequest = client.GET("/api/[api_key]/conditions/q/NY/New_York_City.json"); | |
} | |
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); | |
text(locationInfo, 36, 240); | |
textFont(timeFont); | |
text(currentTime, 36, 290); | |
conditionImg = loadImage(iconURL); | |
image(conditionImg, 36, 320); | |
textFont(weatherFont); | |
text(tempF + "° F / " + tempC + "° C", 100, 324); | |
text(currentConditions, 100, 350); | |
//windImg = loadImage("wind-icon.gif"); | |
//image(windImg, 36, 400); | |
text("WIND: " + wind, 36, 390); | |
text("SPEED/DIRECTION: " + windSpeed + " MPH from the " + windDirection, 36, 415); | |
} | |
} | |
void draw() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment