|
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); |
|
|
|
// 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("HNL")) { |
|
/// set up and dispatch a request for the weather |
|
weatherRequest = client.GET("/api/d6961237291397f0/conditions/q/HI/Honolulu.json"); |
|
//// and for twitter |
|
//twitterRequest = twitterClient.GET("asdfasdf"); |
|
} |
|
if (myString.equals("SFO")) { /// do the other cities |
|
weatherRequest = client.GET("/api/d6961237291397f0/conditions/q/CA/San_Francisco.json"); |
|
} |
|
if (myString.equals("CHI")) { |
|
weatherRequest = client.GET("/api/d6961237291397f0/conditions/q/IL/Chicago.json"); |
|
} |
|
if (myString.equals("NYC")) { |
|
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(); |
|
} |
|
} |
|
|
|
|
|
|
|
void draw() { |
|
serialEvent(myPort); |
|
|
|
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); |
|
|
|
background(255); |
|
|
|
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); |
|
} |
Console output:
null
null
Current conditions: null
Temperature: 0.0° F / 0.0° C
null
Wind conditions: null
Wind speed/direction: 0.0 MPH from the null
Exception in thread "Animation Thread" java.lang.NullPointerException
at processing.core.PGraphics.text(Unknown Source)
at processing.core.PApplet.text(Unknown Source)
at buttons_weatherdata_serialevent.draw(buttons_weatherdata_serialevent.java:129)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:680)