LoginSignup
11
9

More than 5 years have passed since last update.

[ESP-WROOM-02]ESP-WROOM-02+土壌湿度センサーとIFTTTでセンサーの値を定期tweetする

Last updated at Posted at 2016-06-05

はじめに

このエントリは、以下のエントリの続きです

育てている盆栽の水やりのタイミングを見える化したかったので、ESP-WROOM-02、土壌湿度センサーを使って、土の中の水分を測ります

今回は、IFTTTを使って、土壌湿度の値をtweetします

電子部品

参考

IFTTTの準備

IFTTTにアクセスしてMy Recipesから、レシピを作っていく

「this」には「Maker」を設定、「that」には「twitter」を設定する
このとき「Maker」で設定したイベント名を、スケッチ(ESP-WROOM-02に書き込むプログラム)で使う

設定後、 https://ifttt.com/maker にアクセスすると、シークレットキーが記載されている
こちらも、スケッチ(ESP-WROOM-02に書き込むプログラム)で使う

センサーの値によってtweet内容を変えたかったので、レシピは3つ用意した

スケッチの作成

ArduinoIDEのメニューのファイル>スケッチの例>ESP8266WiFi>WiFiClientを選択して、サンプルスケッチを使う

ESP-WROOM-02で取得したセンサーの値をIFTTTに渡すを参考にスケッチを作成

センサーの値によってtweetを変えたかったので、event名をif文で分岐した
また、ESPーWROOM-02の機能である、deep sleepを使うことを考え、loopには何も書かず、setup内でのみGETするようにした

setupの最後に「ESP.deepSleep(3600 * 1000 * 1000);」を加え、開発ボードのIO16ピンとRESETピンをつなげる
こうすることで、ESP-WROOM-02の省電力モードであるdeep sleep機能が使えるようになる

arduino
#include <ESP8266WiFi.h>

const char* ssid = "自分のwifi環境のSSID";
const char* password = "自分のwifi環境のパスワード";

const char* host = "maker.ifttt.com";
const char* secretkey = "自分のMakerのkey";

extern "C" {
#include "user_interface.h"
}

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  int moisture = readMoisture();
  String moisture_str = String(moisture);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  char* event = "";
  if (moisture >= 200) {
    event = "hungry_bonsai_full";
  } else if (moisture >= 100 and moisture < 200) {
    event = "hungry_bonsai_soso";
  } else {
    event = "hungry_bonsai_hungry";
  }

  // We now create a URI for the request
  String url = "/trigger/";
  url += event;
  url += "/with/key/";
  url += secretkey;
  url += "?value1=";
  url += moisture_str;

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(10);

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

    // every 60 min
  ESP.deepSleep(3600 * 1000 * 1000);
}

void loop() {
}

int readMoisture() {
  int moisture = 1024 - system_adc_read();
  return moisture;
}

結果

hungry_bonsai.png

@hungry_bonsai

まとめ

IFTTTを使うことで、ESP-WROOM-02とtwitterの連携が簡単にできる
ひとつの関数を呼び出すだけでdeep sleepが使えて便利
あとはdeep sleepが使えれば、盆栽の水分量を見える化できると思う

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9