注釈

こんにちは、SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Communityへようこそ!Facebook上で、仲間と一緒にRaspberry Pi、Arduino、ESP32をさらに深く探求しましょう。

なぜ参加するのか?

  • 専門的なサポート:購入後の問題や技術的な課題をコミュニティやチームの助けを借りて解決。

  • 学びと共有:スキルを向上させるためのヒントやチュートリアルを交換。

  • 限定プレビュー:新製品発表や予告編に早期アクセス。

  • 特別割引:最新製品の特別割引を楽しむ。

  • フェスティブプロモーションとプレゼント:プレゼントやホリデープロモーションに参加。

👉 私たちと一緒に探索と創造を始める準備はできましたか?[ここ]をクリックして、今すぐ参加しましょう!

レッスン49: IFTTTを使用した振動アラートシステム

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

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

必要なコンポーネント

このプロジェクトでは、以下のコンポーネントが必要です。

一式キットを購入するのが便利です。こちらのリンクをご覧ください:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

以下のリンクから別々に購入することもできます。

Component Introduction

Purchase Link

Arduino UNO R3 or R4

購入

ブレッドボード

購入

ESP8266モジュール

-

振動センサーモジュール (SW-420)

-

配線

../_images/Lesson_49_Iot_vibration_alert_system_uno_bb.png

IFTTTの設定

IFTTT は、2011年に設立された民間企業で、オンラインデジタルオートメーションプラットフォームを提供しています。これらのプラットフォームは、クロスプラットフォームのifステートメントを視覚的に作成するインターフェースを提供し、2020年現在、1,800万人のユーザーが利用しています。

../_images/04-ifttt_intro.png

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

../_images/04-ifttt_intro_2A.png

1) IFTTTのサインアップ

ブラウザに「https://ifttt.com」と入力し、ページ中央にある「Get started」ボタンをクリックします。情報を入力してアカウントを作成します。

../_images/04-ifttt_signup.png

「Back」をクリックしてクイックスタートを終了し、IFTTTのホームページに戻り、ページをリフレッシュして再度ログインします。

../_images/04-ifttt_signup_2.png

2) アプレットの作成

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

../_images/04-ifttt_create_applet_1_shadow.png

If This trigger

「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 action

「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

コード

  1. universal-maker-sensor-kit\arduino_uno\Lesson_49_Vibration_alert_system``のパスにある ``Lesson_49_Vibration_alert_system.ino ファイルを開くか、このコードを **Arduino IDE**にコピーします。

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

      String mySSID = "your_ssid";     // WiFi SSID
    String myPWD = "your_password";  // WiFi Password
    
  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

コード解析

キットに付属しているESP8266モジュールは、すでにATファームウェアが書き込まれています。したがって、ESP8266モジュールはATコマンドを通じて制御できます。このプロジェクトでは、ソフトウェアシリアルを使用してArduino UnoボードとESP8266モジュール間の通信を可能にします。Arduino Unoボードは、ネットワーク接続とリクエスト送信のためにESP8266モジュールにATコマンドを送信します。詳細は|link_esp8266_at|をご参照ください。

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("--------------------------------------");
      }
    

参考