Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sensmon couchdb

Sensmon couchdb

Motokazu Nishimura

December 12, 2010
Tweet

More Decks by Motokazu Nishimura

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • @motokazu ( Motokazu Nishimura ) • CouchDBͱͷग़ձ͍ •

    ̎೥લ͙Β͍ʹग़ձ͏ • “relax”ͱ͍͏ϑϨʔζʹײ໏Λ͏͚Δ • javascriptͷੈքɺJSON • ͦͷҰํͰɺɺϑΟδΧϧίϯϐϡʔςΟϯάˍηϯαʔͰ༡ͼ࢝Ίͨ • ओʹArduinoΛ࢖ͬͯηϯγϯά • ՈͷதΛޮ཰Խ͢Δ͜ͱ͕໨ඪ 2010೥12݄12೔೔༵೔
  2. ՈͷதΛϞχλϦϯάϒʔϜ • ੜ׆ͱޫ೤අ͸ɺ੾ͬͯ΋੾Εͳ͍ؔ܎ • ͦΕͳΒ͖ͬͪΓ؅ཧ͍ͨ͠ʂ • બ୒ࢶΛߟ͑ͯΈΔ • ΦʔϧిԽʂɹՈΛ૯߹తʹվ૷ •

    Կ͔ܭଌ૷ஔΛಋೖͯ͠ϞχλϦϯά (Google PowerMater) SSL+XML • ࣗ෼Ͱηϯαʔ࢓ࠐΈϞχλϦϯά 2010೥12݄12೔೔༵೔
  3. ͪͳΈʹɺ͓͍͘Βʁ • ΦʔϧిԽɹ※ͦ΋ͦ΋ɺआՈͰ͸վ଄ෆՄೳ • ૷ஔɿՈɹɿ਺ඦສʙ਺ઍສ • ϞχλϦϯάɿςϨϏ • ෦෼తʹ૷ஔ •

    ૷ஔɿGoogle PowerMaterʹܨ͕ΔిྗϝʔλʔͳͲ ʢ೔ຊͰ͸࢖͑ͳ͍ɻɹ਺ສʣ http://www.google.com/powermeter/about/partners.html • ϞχλϦϯάɿGoogle PowerMater ͳͲ • ࡞Δ • ૷ஔɿɹArduino + ethernet shield + ηϯαʔ ɹɿ਺ઍԁ • ϞχλϦϯάɿ CouchDB !! (+ Couchapp) 2010೥12݄12೔೔༵೔
  4. ηϯαʔσʔλͷऔಘ : Arduino + ηϯαʔ • Arduinoͱ͸ • ࠷ۙྲྀߦͷφΠεͳϚΠίϯϘʔυ •

    ϋʔυ͕ۤखͰ΋ιϑτ΢ΣΞͳؾ෼Ͱѻ͑Δ • ؆୯ʹADม׵ͯ͘͠ΕΔ • 0vʙ5vͷೖྗΛ 0ʙ1023ͷσδλϧ਺஋΁ม׵ analogReadͰಡΉ • Cݴޠ • Pinʹܨ͙͚ͩ Arduino 2010೥12݄12೔೔༵೔
  5. ηϯαʔΛܨ͍Ͱ͔ΒCouchDBʹૹΔ·ͰͷྲྀΕ • ηϯαʔͷVout(ిѹग़ྗ)Λanalog 2pinʹܨ͙ • analogReadͰಡΈࠐΜͰɺσδλϧʹϚοϓ • sprintf Ͱ jsonΛ࡞Δ

    • strlenͰ content-lengthΛऔಘ • CoucnDBʹhttpͰ઀ଓͯ͠jsonΛPOST sprintf(json,"{%cstime%c: %ld, %csolarpower%c: %0d.%d, %ctemperature%c: %d}", q,q,now(), q,q,(int)solarpower,solarpower1, q,q,temperature); int solarValue = analogRead(solarPin); sprintf(clength,"Content-Length: %d",strlen(json)); 2010೥12݄12೔೔༵೔
  6. ࢀߟɿૹ৴͢Δͱ͜Ζ Client client(server, 5984); // connect couchdb Serial.println("connecting couchdb..."); if(client.connect()){

    Serial.println("connected"); client.println("POST /sensmon HTTP/1.1"); client.println("Host: sheevabian"); client.println("Authorization: Basic xxx="); client.println("Content-Type: application/json"); sprintf(clength,"Content-Length: %d",strlen(json)); client.println(clength); client.println("Connection: Close"); client.println(); client.print(json); Serial.println("POST OK"); } else { Serial.println("connection failed"); return false; } client.stop(); 2010೥12݄12೔೔༵೔
  7. CouchDBʹηϯαʔσʔλΛཷΊࠐΉ • ηϯαʔͱ͍͑͹ {key:࣌ؒ , value: ηϯγϯά৘ใ} • DBΛ࡞੒ curl

    -X PUT http://localhost:5984/sensmon (ͱ͍͍ͳ͕Βɺ࣮ࡍ͸couchapp pushͰ࡞Δ) • ͱʹ͔͘ཷΊࠐΈɺviewͰѻ͏ {"_id":"676a376bc707cd7da0cb2c78050044c4","_rev":"1-a92b94af015ef8b1bed2df56f7e037c8","stime": 1291595210,"solarpower":0.63,"temperature":22}} 2010೥12݄12೔೔༵೔
  8. sensmon • άϥϑͷදࣔʹ FlotϥΠϒϥϦΛར༻ ɹɹhttp://code.google.com/p/flot/ • ͱʹ͔͘σʔλΛ͢΂ͯऔಘ (_view/all )Flotʹ౉͢ <script

    src="/_utils/script/jquery.js?1.3.1"></script> ... $.ajax({ type: 'GET', dataType: 'json', url: "/sensmon/_design/sensmon/_view/all", success: function(data){ $.each(data.rows, function(id, val){ // temperature var tmp = [val.value.stime, val.value.temperature]; tempd.push(tmp); } ... datasets.push({"label":"Temperature",data:tempd, yaxis: 1 }); ... var plot = $.plot($("#placeholder"), datasets , options); } 2010೥12݄12೔೔༵೔
  9. sensmon-calendar • mapreduce + group=true $.ajax({ type: 'GET', dataType: 'json',

    url: "/sensmon/_design/sensmon-calendar/_view/summary_temp_day?group=true", var key = [d.getFullYear(),(d.getMonth()+1),d.getDate()]; emit(key, doc.solarpower); {"count":count,"max":max,"min":min,"ave":ave} map reduce {"count":count,"max":max,"min":min,"ave":ave} {"count":count,"max":max,"min":min,"ave":ave} {"count":count,"max":max,"min":min,"ave":ave} {"count":count,"max":max,"min":min,"ave":ave} {"count":count,"max":max,"min":min,"ave":ave} {"count":count,"max":max,"min":min,"ave":ave} {"count":count,"max":max,"min":min,"ave":ave} rereduce {"count":count,"max":max,"min":min,"ave":ave} {"count":count,"max":max,"min":min,"ave":ave} {"count":count,"max":max,"min":min,"ave":ave} values.forEach(function(val){ max = Math.max(max,val.max); min = Math.min(min,val.min); ave += val.ave; count += val.count; }); ave = ave/values.length; return {"count":count,"max":max,"min":min,"ave":ave}; 2010೥12݄12೔೔༵೔