IFTTTを使用した振動警報システム

このプロジェクトでは、Arduinoボード(Uno R4またはR3)、ESP8266モジュール、そして振動センサー(SW-420)を使用して振動検出システムを構築します。振動が検出されると、システムはIFTTTサーバーにHTTPリクエストを送信し、通知やメールの送信など、様々なアクションをトリガーすることが可能です。

短時間に過度なアラートを防ぐため、システムはHTTPリクエストを最低2分(120000ミリ秒)の間隔で送信するようにプログラムされています。この間隔は、ユーザーのニーズに応じて調整することができます。

1. 配線の構築

注釈

ESP8266モジュールは安定した動作環境を提供するために高い電流が必要ですので、9Vのバッテリーが接続されていることを確認してください。

../_images/04-Wiring_Vibration_alert_system.png

2. IFTTTの設定

IFTTT は2011年に設立された民間の商業企業で、オンラインのデジタル自動化プラットフォームをサービスとして提供しています。このプラットフォームは、2020年時点で1800万人のユーザーに、多様なプラットフォーム間でif文を作成するための視覚的なインターフェースを提供しています。

../_images/04-ifttt_intro.png

IFTTTは「If This Then That」の略で、特定の条件が満たされた場合に別の何かが起こるという仕組みです。"if this" 部分はトリガーと呼ばれ、"then that" 部分はアクションと呼ばれます。スマートホームデバイス、ソーシャルメディア、配信アプリなどを連携させ、自動化されたタスクを実行します。

https://images.contentful.com/mrsnpomeucef/78eXVQ2gXnWkCNCykr7Mdt/8b188790f1fda2ed84ab17afe06cf145/img-welcome-02_x2.jpg

2.1 IFTTTへのサインアップ

お使いのブラウザで "https://ifttt.com" にアクセスし、ページの中央にある「Get started」ボタンをクリックしてください。アカウントを作成するために必要な情報を入力します。

../_images/04-ifttt_signup.png

「Back」をクリックしてクイックスタートを終了し、IFTTTのホームページに戻ってページを更新し、再度ログインしてください。

../_images/04-ifttt_signup_2.png

2.2 アプレットの作成

「Create」をクリックしてアプレットの作成を開始します。

../_images/04-ifttt_create_applet_1_shadow.png

If This トリガー

「If This」の隣にある「Add」をクリックしてトリガーを追加します。

../_images/04-ifttt_create_applet_2_shadow.png

「webhook」を検索し、「Webhooks」をクリックします。

../_images/04-ifttt_create_applet_3_shadow.png

次に表示されるページで「Receive a web request」をクリックします。

../_images/04-ifttt_create_applet_4_shadow.png

「Event Name」には「vibration_detected」と入力します。

../_images/04-ifttt_create_applet_5_shadow.png

Then That アクション

「Then That」の隣にある「Add」をクリックしてアクションを追加します。

../_images/04-ifttt_create_applet_6_shadow.png

「email」と検索し、「Email」をクリックします。

../_images/04-ifttt_create_applet_7_shadow.png

次に表示されるページで「Send me a email」をクリックします。

../_images/04-ifttt_create_applet_8_shadow.png

振動が検出されたときに送信されるメールの件名と内容を設定します。

参考として、件名は「[ESP-01] Detected vibration!!!」、内容は「Detected vibration, please confirm the situation promptly! {{OccurredAt}}」と設定されています。メールを送信する際には、{{OccurredAt}} はイベントが発生した時刻に自動的に置き換えられます。

../_images/04-ifttt_create_applet_9_shadow.png

以下の手順に従って、アプレットの作成を完了します。

../_images/04-ifttt_create_applet_10_shadow.png ../_images/04-ifttt_create_applet_11_shadow.png ../_images/04-ifttt_create_applet_12_shadow.png

3. コードの実行

  1. ultimate-sensor-kit\iot_project\wifi\04-Vibration_alert_system パス内の 04-Vibration_alert_system.ino ファイルを開くか、このコードを Arduino IDE にコピーします。

  2. 使用しているWiFiの mySSIDmyPWD を入力する必要があります。

    String mySSID = "your_ssid";     // WiFiのSSID
    String myPWD = "your_password";  // WiFiのパスワード
    
  3. イベント名とご自身のAPIキーを設定した URL も変更する必要があります。

    String URL = "/trigger/vibration_detected/with/key/xxxxxxxxxxxxxxxxxx";
    
    ../_images/04-ifttt_apikey_1_shadow.png ../_images/04-ifttt_apikey_2_shadow.png

    この場所で 必ず秘密にしておくべき独自のAPIキー を見つけることができます。イベント名は vibration_detected として入力します。最終的なURLはウェブページの下部に表示されます。このURLをコピーしてください。

    ../_images/04-ifttt_apikey_3_shadow.png ../_images/04-ifttt_apikey_4_shadow.png
  4. ボードとポートを正しく選択した後、 書き込み ボタンをクリックします。

  5. シリアルモニターを開き(ボーレートは 9600 に設定)、成功した接続などのプロンプトが表示されるまで待ちます。

    ../_images/04-ready_shadow.png

4. コードの解説

このキットに付属するESP8266モジュールは、すでにATファームウェアが焼き付けられています。そのため、ESP8266はATコマンドによって制御できます。本プロジェクトでは、Arduino UnoボードとESP8266モジュール間の通信を有効にするためにSoftwareSerialを使用します。Arduino Unoボードは、ネットワークへの接続とリクエスト送信のためにESP8266モジュールにATコマンドを送ります。詳細は ESP8266 AT Instruction Set を参照してください。

Unoボードはセンサー値を読み取り、ESP8266モジュールにATコマンドを送信します。ESP8266モジュールはネットワークに接続し、IFTTTサーバーにリクエストを送信します。

  1. ArduinoとESP8266間のシリアル通信のためにSoftwareSerialライブラリをインクルード

    #include <SoftwareSerial.h>
    SoftwareSerial espSerial(2, 3);
    
  2. WiFiの認証情報とIFTTTサーバーの詳細を設定

    String mySSID = "your_ssid";
    String myPWD = "your_password";
    String myHOST = "maker.ifttt.com";
    String myPORT = "80";
    String URL = "/trigger/xxx/with/key/xxxx";
    
  3. 振動センサーとアラート頻度制御のための変数を定義

    unsigned long lastAlertTime = 0;
    const unsigned long postingInterval = 120000L;
    const int sensorPin = 7;
    
  4. setup() 内で、シリアル通信とESP8266モジュールを初期化し、WiFiに接続

    void setup() {
      Serial.begin(9600);
      espSerial.begin(115200);
    
      // Initialize the ESP8266 module
      sendATCommand("AT+RST", 1000, DEBUG);   //Reset the ESP8266 module
      sendATCommand("AT+CWMODE=1", 1000, DEBUG);  //Set the ESP mode as station mode
      sendATCommand("AT+CWJAP=\"" + mySSID + "\",\"" + myPWD + "\"", 3000, DEBUG);  //Connect to WiFi network
    
      while (!espSerial.find("OK")) {
        //Wait for connection
      }
    }
    
  5. loop() 内で、振動を検出し、時間間隔が経過した場合にアラートを送信

    void loop() {
    
      if (digitalRead(sensorPin)) {
        if (lastAlertTime == 0 || millis() - lastAlertTime > postingInterval) {
          Serial.println("Detected vibration!!!");
          sendAlert();  //Send an HTTP request to IFTTT server
        } else {
          Serial.print("Detected vibration!!! ");
          Serial.println("Since an email has been sent recently, no warning email will be sent this time to avoid bombarding your inbox.");
        }
      } else {
        if (DEBUG) {
          Serial.println("Detecting...");
        }
      }
      delay(500);
    }
    
  6. sendAlert() でHTTPリクエストを構築し、ESP8266経由で送信

    void sendAlert() {
    
      String sendData = "GET " + URL + " HTTP/1.1" + "\r\n";
      sendData += "Host: maker.ifttt.com\r\n";
    
      sendATCommand("AT+CIPMUX=0",1000,DEBUG);
      sendATCommand("AT+CIPSTART=...",3000,DEBUG);
      sendATCommand("AT+CIPSEND=" + String(sendData.length()),1000,DEBUG);
      espSerial.println(sendData);
    
    }
    
  7. ATコマンドの処理 sendATCommand()

    この関数は、ESP8266にATコマンドを送信し、レスポンスを収集します。

    void sendATCommand(String command, const int timeout, boolean debug) {
      // Print and send command
      Serial.print("AT Command ==> ");
      Serial.print(command);
      Serial.println();
      espSerial.println(command);  // Send the AT command
    
      // Get the response from the ESP8266 module
      String response = "";
      long int time = millis();
      while ((time + timeout) > millis()) {  // Wait for the response until the timeout
        while (espSerial.available()) {
          char c = espSerial.read();
          response += c;
        }
      }
    
      // Print response if debug mode is on
      if (debug) {
        Serial.println(response);
        Serial.println("--------------------------------------");
      }
    

参考文献