Skip to content

Instantly share code, notes, and snippets.

@rhathaway
Created December 13, 2011 05:57
Show Gist options
  • Save rhathaway/1470817 to your computer and use it in GitHub Desktop.
Save rhathaway/1470817 to your computer and use it in GitHub Desktop.
wunderground + buttons
// modified sample from Programming Interactivity, p.262
int HNLbutton = 2;
int SFObutton = 4;
int CHIbutton = 6;
int NYCbutton = 8;
void setup() {
// open the serial port at 9600 bps:
Serial.begin(9600);
}
void loop() {
if (digitalRead(HNLbutton) == HIGH) {
Serial.print("Honolulu");
}
if (digitalRead(SFObutton) == HIGH) {
Serial.print("San Francisco");
}
if (digitalRead(CHIbutton) == HIGH) {
Serial.print("Chicago");
}
if (digitalRead(NYCbutton) == HIGH) {
Serial.print("New York City");
}
delay(200);
}
// Work in progress: getting button input from Arduino
// using sample code from Programming Interactivity, p. 262
import com.francisli.processing.http.*;
import processing.serial.*;
Serial arduinoPort;
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;
HttpClient client;
HttpRequest HNLweather, SFOweather, CHIweather, NYCweather;
void setup() {
size(1024, 768);
frameRate(24);
// set up the communication between the Arduino and the Processing app
arduinoPort = 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");
HNLweather = client.GET("/api/d6961237291397f0/conditions/q/HI/Honolulu.json");
SFOweather = client.GET("/api/d6961237291397f0/conditions/q/CA/San_Francisco.json");
CHIweather = client.GET("/api/d6961237291397f0/conditions/q/IL/Chicago.json");
NYCweather = client.GET("/api/d6961237291397f0/conditions/q/NY/New_York_City.json");
}
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) {
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) {
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) {
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);
}
}
}
void draw () {
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