Last active
November 30, 2016 19:56
-
-
Save pral2a/34b263dd8a4d489fb7e6bcbc999cb112 to your computer and use it in GitHub Desktop.
SCK OSC
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
/* | |
SmartCitizen API - Procressing.org basic example | |
This example demonstrate how to do a basic call to the SmartCitizen API | |
to retrieve historical data from one device. | |
For more info: | |
http://api.smartcitizen.me/ | |
This code is under public domain. | |
*/ | |
import oscP5.*; | |
import netP5.*; | |
OscP5 oscP5; | |
NetAddress myRemoteLocation; | |
long lastUpdate = 0; | |
long updateInterval = 15; | |
String resourceURL = "http://data.smartcitizen.me/v0/devices/XXXX"; | |
void setup() { | |
/* start oscP5, listening for incoming messages at port 7474 */ | |
oscP5 = new OscP5(this, 7474); | |
/* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters, | |
* an ip address and a port number. myRemoteLocation is used as parameter in | |
* oscP5.send() when sending osc packets to another computer, device, | |
* application. usage see below. for testing purposes the listening port | |
* and the port of the remote location address are the same, hence you will | |
* send messages back to this sketch. | |
*/ | |
myRemoteLocation = new NetAddress("127.0.0.1", 7474); | |
println(">> SmartCitizen API Query URL: " + resourceURL); | |
println(">> Starting data poll..."); | |
getDataAndOSC(); | |
} | |
void draw() { | |
if (millis() - lastUpdate > (updateInterval*1000)) { | |
getDataAndOSC(); | |
} | |
} | |
void getDataAndOSC() { | |
JSONObject sckData = loadJSONObject(resourceURL); | |
//println("sckData "+sckData); | |
JSONObject data = sckData.getJSONObject("data"); // This contains your requested device info and data. | |
String timeStamp = data.getString("recorded_at"); | |
JSONArray sensors = data.getJSONArray("sensors"); // You can get general device information from here | |
JSONObject humSensor = sensors.getJSONObject(1); // Get Hum | |
float hum = humSensor.getFloat("value"); | |
JSONObject tempSensor = sensors.getJSONObject(2); // Get Temp | |
float temp = tempSensor.getFloat("value"); | |
JSONObject noiseSensor = sensors.getJSONObject(3); // Get Light | |
float noise = noiseSensor.getFloat("value"); | |
println("lastPost >> " + timeStamp + " @ Temp: " + temp + " ªC | " + hum + " % rel | " + noise + " dB"); | |
/* osc messages for Max msp */ | |
OscMessage myMessage = new OscMessage("/smartcitizen"); | |
myMessage.add((float)temp); | |
myMessage.add((float)hum); | |
myMessage.add((float)noise); | |
/* send the message */ | |
oscP5.send(myMessage, myRemoteLocation); | |
lastUpdate = millis(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment