M5 Atom Lite → メール・LINE通知(IFTTT編)
やること
Atom Liteから Webhookで IFTTT へデータ送信
IFTTT上で IF Webhook THEN メール送信(またはLINE通知)を実装
参考
使うもの
1. IFTTTでWebhook→メール送信を設定する
1.1 IFTTTのアカウント登録
IFTTT サイトから右上の「Get started」を選択 https://scrapbox.io/files/646b791f0babb5001b886ebe.png
既存のアカウント(Googleなど)またはメールアドレスを用いてアカウントを登録
https://scrapbox.io/files/646b7921c0a8fd001c0526b2.png
1.2 IFTTTでレシピを作成
1.3 M5 Atom側でwebhookを作成
※書き込むときにはボードをM5 StickVに指定する?
code:AtomLiteMail.ino
const char* ssid = "KAIT-WIFI"; //WifiのSSID ※要書き換え
const char* password = "************"; //Wifiのpass ※要書き換え
String makerEvent = "test1"; // IFTTT上のイベント名 ※要書き換え
String makerKey = "************"; // IFTTT上Webhooksキー ※要書き換え
const char* server = "maker.ifttt.com"; // サーバ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.Btn.wasReleased()) {
send("Mail","test1","test2"); //送信する変数 Value1,Value2,Value3
}
delay(20);
}