前回。
まずはIFTTT MAKERチャネルの登録
前回までに、任意のタイミングで配電盤を流れる電流値(っぽい値)を測定することができました。せっかく測定した値をそのままにしておくのはもったいなかったので、まずはIFTTTを使ってGoogle DriveのSpreadsheetsに値をアップするようにしました。
コードは後ほど載せますが、あるURLにJSONをHTTP POSTすると、それがトリガーになり、任意のアクションを割り当てることができます。
Google Drive上で、取得できた値を整形すると、こんな感じでグラフ化できます(手動ですが)。とりあえず1日の消費電流量の遷移を見ることができました。
問題は電源
ここまではいい感じに実装を進めてきたのですが、ここで問題が。配電盤の近くにコンセントがなく、ESP8266ボードの電源をどうするのかという問題が。最近のモバイルバッテリーは、電力の出力先の負荷があまりにも低くなると、給電を止めてしまう賢い(賢くない)機能があるので使えず。悩んだ挙句、ちょっと前に使ってた、エネループ(単3×2)をUSBの5Vに昇圧してくれるデバイスを使うことに。
SANYO eneloop USB出力付ハンディ電源(単3形2個セット) KBC-D1BS
これだと給電が止まることが無くいい感じに動作。
さらなる問題
このエネループ、あんまり電池が持たず・・・。ESP8266のDEEP SLEEPを使っているものの、半日持たないというポンコツっぷり。悔しいので以下の仕様変更を敢行。
- LEDは常時消灯
- 30秒間隔での測定→1分間隔に
- 休止中は今までどおりDEEP SLEEPを活用
すると、40時間ほどエネループが持つようになりました。ただ、逆にまだ40時間ほどしか持ちません。1分おきに測定をしていますが、その度にAPに接続するのがダメなのかも・・・。
クソコード
こんな感じです。 MAKERチャネルは、JSONにデータを一気に3つまで詰め込むことができるようです。が、今は1つしか詰めていません。
#include <ESP8266WiFi.h> #include <WiFiClient.h> char ssid[] = "XXXXXXXX"; char password[] = "xxxxxxxx"; char host[] = "maker.ifttt.com"; char url[] = "/trigger/xxxx/with/key/xxxxxx"; int port = 80; void setup() { Serial.begin(115200); Serial.println(""); } void loop() { connectWifi(); httpPostJson(getCurrent()); ESP.deepSleep(60 * 1000 * 1000, WAKE_RF_DEFAULT); delay(1000); } void connectWifi() { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println("connected!"); } double getCurrent() { Serial.println("get current..."); long total = 0.0; int sample = 1000; for (int i = 0; i < sample; i++) { int sensorValue = analogRead(A0); float voltage = sensorValue * (3.2 / 1023.0); int voltageInt = voltage * 100; int current = (voltageInt - 158) * 30; total += current * current; } float result = total / sample / 10000.0; return sqrt(result); } void httpPostJson(double data) { WiFiClient wifiClient; String json = "{\"value1\":\"" + String(data) + "\"}"; if (!wifiClient.connect(host, port)) { Serial.println("connection failed"); return; } Serial.println("http post..."); wifiClient.println(String("POST ") + url + " HTTP/1.1"); wifiClient.println(String("Host: ") + host); wifiClient.println("Cache-Control: no-cache"); wifiClient.println("Content-Type: application/JSON"); wifiClient.print("Content-Length: "); wifiClient.println(json.length()); wifiClient.println(); wifiClient.println(json); }
Learning Esp8266: Build the Internet of Things With the Arduino Ide and Raspberry Pi