Skip to content

Instantly share code, notes, and snippets.

@satzz
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satzz/e8c509f031b2021d2baf to your computer and use it in GitHub Desktop.
Save satzz/e8c509f031b2021d2baf to your computer and use it in GitHub Desktop.
Xivelyでセンサデータなどのリアルタイムなチャートを作る ref: http://qiita.com/satzz@github/items/cef654f8de52ee06eaf5
curl "api.worldweatheronline.com/free/v1/weather.ashx?q=Tokyo&format=json&num_of_days=1&key=YOUR_WEATHER_API_KEY" | ~/bin/jq ' { "datastreams": [ { "id": "temp_tokyo", "current_value": ( .data.current_condition[0].temp_C ) } ] }' > /tmp/temp_tokyo; curl https://api.xively.com/v2/feeds/YOUR_XIVELY_FEED_ID -X PUT -H "X-ApiKey: YOUR_XIVELY_API_KEY" --data @/tmp/temp_tokyo
{
"version": "1.0.0",
"datastreams": [
{
"id": "napion10m0001",
"current_value": " 0.37"
},
{
"id": "napionAnalog0001",
"current_value": "497.49"
},
{
"id": "microphone0001",
"current_value": "396"
},
{
"id": "humidity0001",
"current_value": " 52.60"
},
{
"id": "temperature0001",
"current_value": " 27.60"
},
{
"id": "illuminance0001",
"current_value": "225"
}
]
}
#include <DHT.h>
#define DHTTYPE DHT22
#define DHT22_PIN 7
DHT dht(DHT22_PIN, DHTTYPE);
int napionAnalogPin = A0;
int illuminancePin = A1;
int microphonePin = A2;
int napion10mPin = 6;
int interval = 3; //sec
int loopCount = 100;
String moduleId = "0001";
String feedId = "YOUR_XIVELY_FEED_ID";
String apiKey = "YOUR_XIVELY_API_KEY";
String reqHeader;
void setup() {
Serial.begin(9600);
pinMode(napionAnalogPin, INPUT);
pinMode(illuminancePin, INPUT);
pinMode(microphonePin, INPUT);
pinMode(DHT22_PIN, INPUT);
pinMode(napion10mPin, INPUT);
reqHeader = "PUT /v2/feeds/" + feedId +" HTTP/1.1\r\n" + "X-ApiKey: " + apiKey + "\r\n"+"host: api.xively.com\r\n";
}
int illuminanceValue = 0;
int microphoneValue = 0;
void loop() {
int illuminanceValueMax = 0;
int microphoneValueMax = 0;
long napionAnalogValue = 0;
long napion10mValue = 0;
for (int sec=0; sec<interval; sec++) {
for (int j=0; j<loopCount ; j++) {
delay(10);
illuminanceValue = analogRead(illuminancePin);
if (illuminanceValueMax < illuminanceValue) {
illuminanceValueMax = illuminanceValue;
}
microphoneValue = analogRead(microphonePin);
if (microphoneValueMax < microphoneValue) {
microphoneValueMax = microphoneValue;
}
napion10mValue += digitalRead(napion10mPin);
napionAnalogValue += analogRead(napionAnalogPin);
}
}
float napion10mValueAvg = 1.0*napion10mValue /interval/loopCount;
float napionAnalogValueAvg = 1.0*napionAnalogValue/interval/loopCount;
char napion10mValueAvgChar[10];
char napionAnalogValueAvgChar[10];
dtostrf(napion10mValueAvg, 6, 2, napion10mValueAvgChar);
dtostrf(napionAnalogValueAvg, 6, 2, napionAnalogValueAvgChar);
float h = dht.readHumidity() ;
float t = 5.0/9.0*(dht.readTemperature(true)-32.0); // fahrenheit -> celsius
char ch[10];
char ct[10];
dtostrf(h, 6, 2, ch);
dtostrf(t, 6, 2, ct);
// HTTP request
Serial.print(reqHeader);
String httpBody = "{\"version\":\"1.0.0\",\"datastreams\" : [ \r\n";
httpBody.concat( tuple("napion10m", String(napion10mValueAvgChar)) + "," );
httpBody.concat( tuple("napionAnalog", String(napionAnalogValueAvgChar)) + "," );
httpBody.concat( tuple("microphone", String(microphoneValueMax)) + "," );
httpBody.concat( tuple("humidity", String(ch)) + "," );
httpBody.concat( tuple("temperature", String(ct)) + "," );
httpBody.concat( tuple("illuminance", String(illuminanceValueMax)) + " ] }" );
Serial.print("Content-Length: " + String(httpBody.length()) + "\r\n\r\n");
Serial.print(httpBody);
}
String tuple(String channel, String value) {
return "{\"id\":\"" + channel + moduleId + "\", \"current_value\" : \"" + value + "\"}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment