Instantly share code, notes, and snippets.
Created
December 14, 2011 02:45
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save rhathaway/1475020 to your computer and use it in GitHub Desktop.
wunderground + buttons: memory error
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 arduinoPort; | |
String oldValue; | |
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/[api key]/conditions/q/HI/Honolulu.json"); | |
SFOweather = client.GET("/api/[api key]/conditions/q/CA/San_Francisco.json"); | |
CHIweather = client.GET("/api/[api key]/conditions/q/IL/Chicago.json"); | |
NYCweather = client.GET("/api/[api key]/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 = 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(); | |
} | |
} | |
void readButtonPress(HttpRequest request, Serial arduinoPort) { | |
byte[] inBuffer = new byte[50]; | |
while (arduinoPort.available () > 0) { | |
inBuffer = arduinoPort.readBytes(); | |
arduinoPort.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 | |
if (myString != oldValue) { | |
if (myString.equals("HNL")) { // compare arduino input with string | |
println("now displaying weather for Honolulu, HI"); | |
request = HNLweather; // assign JSON url to http request | |
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 (myString.equals("SFO")) { | |
println("now displaying weather for San Francisco, CA"); | |
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 (myString.equals("CHI")) { | |
println("now displaying weather for Chicago, IL"); | |
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 (myString.equals("NYC")) { | |
println("now displaying weather for New York, NY"); | |
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); | |
} | |
myString = oldValue; | |
} | |
} | |
} | |
} | |
void draw(HttpRequest request) { | |
readButtonPress(request, arduinoPort); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Console output:
HttpClient: Connecting to api.wunderground.com on port 80
HttpClient: Connecting to api.wunderground.com on port 80
HttpClient: Connecting to api.wunderground.com on port 80
HttpClient: Connecting to api.wunderground.com on port 80
HttpClient: GET http://api.wunderground.com:80/api/d6961237291397f0/conditions/q/NY/New_York_City.json HTTP/1.1
HttpClient: GET http://api.wunderground.com:80/api/d6961237291397f0/conditions/q/CA/San_Francisco.json HTTP/1.1
HttpClient: GET http://api.wunderground.com:80/api/d6961237291397f0/conditions/q/HI/Honolulu.json HTTP/1.1
HttpClient: GET http://api.wunderground.com:80/api/d6961237291397f0/conditions/q/IL/Chicago.json HTTP/1.1
Invalid memory access of location 0xb1ab8b70 eip=0x228f290c