はじめに
前回の記事でLineThingsを紹介した。「これなら簡単にIoTができるよ!」という感じでいろいろやり方を書いた。しかし、残念ながらもう少し良い方法が見つかってしまった!!
というわけで、今回はIFTTTで簡単にIoTができる方法を紹介する。IFTTTとはあらゆるwebサービスをリンクできるサービスであり、考えているおおよそのことはできる。instagramを投稿したときに自動的にtwitterでも投稿したり、メールを送ったら自動でevernoteに保存したり...
今回はwebhooksとlineを連携させることで、M5stackでボタンを押したらlineに通知がいくシステムを作ってみようと思う。画像が多くなってしまったが、参考にしてほしい。
手順1.アカウント取得
まずはIFTTTのサイトでアカウントを取得しよう。
手順2.This側の作成
それが終了したら自分のアイコンからCreateを選択。
Thisをクリック
検索バーに「webhook」と入力。servicesタブを選び、webhookのアイコンをクリックする。
トリガーを選択。「Receive a web request」。
任意のEventNameを打ち込み、Create trigerをクリックする。
手順3.+That側の作成
今度は+Thatをクリック。
先ほどと同じ要領でやっていく。こんどはlineと検索してlineのアイコンを選択する。
SendMessageを選択。
line notifyから通知...を選択し、Create actionをクリックする。Messageの文章は変更しないでOK。
Finishをクリック。
手順4.makerKeyの確認
Webhooksのアイコンを選択。
Settingsをクリック。
出てきたここの文字列を控えておく。
手順5.プログラムの作成
ここでM5Stackのプログラムを作成する。以下のサイトのコードを参考にした。
ssid
とpassword
はwifiで設定しているものを、makerKey
はさきほど控えておいたものを打ち込もう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
/* * IFTTTを使ってm5stackとlineを連携させるプログラム * 参照元:https://thousandiy.wordpress.com/2017/10/17/esp32_ifttt_line/ */ #include <M5Stack.h> #include <WiFi.h> #include <WiFiClient.h> const char* ssid = "****"; const char* password = "****"; String makerEvent = "push_line"; // Maker Webhooks String makerKey = "****"; // Maker Webhooks const char* server = "maker.ifttt.com"; // Server URL WiFiClient client; bool checkWifiConnected() { // attempt to connect to Wifi network: while (WiFi.status() != WL_CONNECTED) { Serial.print("."); // wait 1 second for re-trying delay(1000); } Serial.print("Connected to "); Serial.println(ssid); return true; } void send(String value1, String value2, String value3) { while (!checkWifiConnected()) { Serial.print("Attempting to connect to WiFi"); WiFi.begin(ssid, password); } Serial.println("\nStarting connection to server..."); if (!client.connect(server, 80)) { Serial.println("Connection failed!"); } else { Serial.println("Connected to server!"); // Make a HTTP request: String url = "/trigger/" + makerEvent + "/with/key/" + makerKey; url += "?value1=" + value1 + "&value2=" + value2 + "&value3=" + value3; client.println("GET " + url + " HTTP/1.1"); client.print("Host: "); client.println(server); client.println("Connection: close"); client.println(); Serial.print("Waiting for response "); //WiFiClientSecure uses a non blocking implementation int count = 0; while (!client.available()) { delay(50); // Serial.print("."); } // if there are incoming bytes available // from the server, read them and print them: while (client.available()) { char c = client.read(); Serial.write(c); } // if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting from server."); client.stop(); } } } void setup() { //Initialize serial and wait for port to open: Serial.begin(115200); delay(100); WiFi.begin(ssid, password); while (!checkWifiConnected()) { WiFi.begin(ssid, password); } } void loop() { M5.update(); if (M5.BtnA.wasReleased()) { send("LINE","test1","test2"); //任意の文字列3つ M5.Lcd.println("send"); } delay(20); } |
手順6.動作
起動後、M5StackのAボタンを押すと、LINEにこんな通知が届く。
やったね
おわりに
やっとIoTが簡単にできる環境が整った! これからどんどんIoTをやっていくぞ!