Skip to content

Instantly share code, notes, and snippets.

@sunekaae
Last active August 29, 2015 14:19
Show Gist options
  • Save sunekaae/8903cd140c0e20774f09 to your computer and use it in GitHub Desktop.
Save sunekaae/8903cd140c0e20774f09 to your computer and use it in GitHub Desktop.
Processing calls an API, parses a a JSON value from the response and shows it on screen.Extend with a) have it call your spark-core to get relevant data value, and b) based on the value decide what sound to play
import http.requests.*;
// Sune for Hyper Hardware
// install the "HTTP Requests" library: "Sketc" > "Import Library" > "Add Library" and type "HTTP Requests".
// https://github.com/runemadsen/HTTP-Requests-for-Processing
int timer;
void setup() {
}
void draw() {
if (millis() - timer >= 2000) {
// every 2 seconds...
// example of API which returns some JSON data: http://openweathermap.org/current
GetRequest get = new GetRequest("http://api.openweathermap.org/data/2.5/weather?q=Stockholm,Sweden");
get.send();
String response = get.getContent();
println("Reponse Content: " + response);
// example of JSON parsing: http://forum.processing.org/two/search?Search=json+example
JSONObject ob = parseJSONObject(response);
JSONObject main = ob.getJSONObject("main");
int temperature = main.getInt("temp");
println("temperature: " + temperature);
background(random(255));
text(temperature, 10, 30);
timer = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment