Created
April 19, 2017 17:07
-
-
Save pral2a/2dc20704963747c6fd15a1d28b584989 to your computer and use it in GitHub Desktop.
Smart Citizen API basic processing example
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. | |
*/ | |
long lastUpdate = 0; | |
long updateInterval = 20; | |
String resourceURL = "http://data.smartcitizen.me/v0/devices/3325"; | |
void setup() { | |
getData(); | |
} | |
void draw() { | |
if (millis() - lastUpdate > (updateInterval*1000)) { | |
getData(); | |
} | |
} | |
void getData() { | |
JSONObject sckData = loadJSONObject(resourceURL); | |
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 lightSensor = sensors.getJSONObject(0); // You can get properties from an specific datapoint to do whatever you want. | |
float light = lightSensor.getFloat("value"); | |
JSONObject humSensor = sensors.getJSONObject(2); // You can get properties from an specific datapoint to do whatever you want. | |
float hum = humSensor.getFloat("value"); | |
JSONObject tempSensor = sensors.getJSONObject(3); // You can get properties from an specific datapoint to do whatever you want. | |
float temp = tempSensor.getFloat("value"); | |
JSONObject no2Sensor = sensors.getJSONObject(4); // You can get properties from an specific datapoint to do whatever you want. | |
float no2 = no2Sensor.getFloat("value"); | |
JSONObject coSensor = sensors.getJSONObject(5); // You can get properties from an specific datapoint to do whatever you want. | |
float co = coSensor.getFloat("value"); | |
JSONObject noiseSensor = sensors.getJSONObject(7); // You can get properties from an specific datapoint to do whatever you want. | |
float noise = noiseSensor.getFloat("value"); | |
println("lastPost >> " + timeStamp + " @ Temp: " + temp + " ªC | " + hum + " % rel | " + co + " kOhm | " + no2 + " kOhm | " + light + " lux | " + noise + " dB"); | |
lastUpdate = millis(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment