Skip to content

Instantly share code, notes, and snippets.

@rhathaway
Created December 14, 2011 05:57
Show Gist options
  • Save rhathaway/1475452 to your computer and use it in GitHub Desktop.
Save rhathaway/1475452 to your computer and use it in GitHub Desktop.
buttons controlling wunderground data
import com.francisli.processing.http.*;
import processing.serial.*;
Serial myPort; // arduino port
String oldValue;
PFont cityFont, timeFont, weatherFont;
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);
// 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");
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/d6961237291397f0/conditions/q/HI/Honolulu.json");
}
if (myString.equals("S")) { /// do the other cities
weatherRequest = client.GET("/api/d6961237291397f0/conditions/q/CA/San_Francisco.json");
}
if (myString.equals("C")) {
weatherRequest = client.GET("/api/d6961237291397f0/conditions/q/IL/Chicago.json");
}
if (myString.equals("N")) {
weatherRequest = client.GET("/api/d6961237291397f0/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, 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() {
}
@rhathaway
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment