Last active
March 7, 2016 09:44
-
-
Save kotobuki/b7e6a02c813457821c4d to your computer and use it in GitHub Desktop.
IFTTTのトリガーおよびアクションをESP8266で実行する ref: http://qiita.com/mayfair/items/e761c788a9d8787bc610
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wi-FiアクセスポイントのSSIDとパスワード | |
const char *ssid = "********"; | |
const char *password = "********"; | |
// クライアントID(任意) | |
const char *clientID = "ESP8266"; | |
// Beebotteのチャンネルトークン | |
const char* channelToken = "******************************"; | |
// トピック名("channel/resource"の形式) | |
const char* topic = "ifttt/action"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://maker.ifttt.com/trigger/{event}/with/key/{key} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ "value1" : "***", "value2" : "***", "value3" : "***" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ curl --verbose --header "Content-type: application/json" --request POST --data '{"data": "hello ESP8266"}' https://api.beebotte.com/v1/data/write/ifttt/action?token=****************************** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": "hello ESP8266" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
// PubSubClientライブラリでのパケットサイズは128バイトなのを拡張 | |
#define MQTT_MAX_PACKET_SIZE 1024 | |
#include <PubSubClient.h> | |
#include <ArduinoJson.h> | |
#include "config.h" | |
const char* host = "mqtt.beebotte.com"; | |
// メッセージを受け取ったらシリアルにプリント | |
void callback(char* topic, byte* payload, unsigned int length) { | |
// PubSubClient.hで定義されているMQTTの最大パケットサイズ | |
char buffer[MQTT_MAX_PACKET_SIZE]; | |
snprintf(buffer, sizeof(buffer), "%s", payload); | |
Serial.println("received:"); | |
Serial.print("topic: "); | |
Serial.println(topic); | |
Serial.println(buffer); | |
// 受け取ったJSON形式のペイロードをデコードする | |
StaticJsonBuffer<MQTT_MAX_PACKET_SIZE> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(buffer); | |
if (!root.success()) { | |
Serial.println("parseObject() failed"); | |
return; | |
} | |
const char* parsedPayload = root["data"]; | |
if (parsedPayload != NULL) { | |
Serial.print("payload: "); | |
Serial.println(parsedPayload); | |
} | |
} | |
WiFiClient wifiClient; | |
PubSubClient client(host, 1883, wifiClient); | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(); | |
} | |
void loop() { | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.print("connecting to "); | |
Serial.print(ssid); | |
Serial.println("..."); | |
WiFi.begin(ssid, password); | |
if (WiFi.waitForConnectResult() != WL_CONNECTED) { | |
// Wi-Fiアクスポイントへの接続に失敗したら5秒間待ってリトライ | |
Serial.println("failed to connect"); | |
delay(5000); | |
return; | |
} else { | |
Serial.print("WiFi connected: "); | |
Serial.println(WiFi.localIP()); | |
} | |
} | |
// クライアントがサーバに接続されていなければ | |
if (!client.connected()) { | |
// ユーザ名を指定して接続 | |
String username = "token:"; | |
username += channelToken; | |
client.connect(clientID, username.c_str(), NULL); | |
if (client.connected()) { | |
Serial.println("MQTT connected"); | |
client.setCallback(callback); | |
// トピック名を指定してsubscribe | |
client.subscribe(topic); | |
} else { | |
Serial.print("MQTT connection failed: "); | |
Serial.println(client.state()); | |
delay(5000); | |
} | |
} else { | |
// 既にサーバに接続されていれば通常処理を行う | |
client.loop(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClientSecure.h> | |
// 設定ファイル | |
#include "config.h" | |
const char* host = "maker.ifttt.com"; | |
const char* event = "ping"; | |
const int httpsPort = 443; | |
WiFiClientSecure client; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.print("connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() { | |
Serial.print("connecting to "); | |
Serial.println(host); | |
if (!client.connect(host, httpsPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
// URLを作成 | |
// maker.ifttt.com/trigger/{event}/with/key/{key} | |
String url = "/trigger/"; | |
url += event; | |
url += "/with/key/"; | |
url += key; | |
Serial.print("requesting URL: "); | |
Serial.println(url); | |
// ウェブリクエストを送信 | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
while (client.connected()) { | |
String line = client.readStringUntil('\n'); | |
if (line == "\r") { | |
// ヘッダ部分の受取終了 | |
break; | |
} | |
} | |
String line = client.readStringUntil('\n'); | |
Serial.print("reply: "); | |
Serial.println(line); | |
Serial.println("closing connection"); | |
Serial.println(); | |
// 1分後に再度実行 | |
delay(60000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment