IFTTTのWebhooksにデータ送信するクライアント側のスクリプト集
Arduino ESP8266 (ESP-WROOM-02) http 版
平文でIFTTTサーバにデータを送信するため、IFTTTのKEYが途中でキャプチャされて盗まれる可能性がある。 どうしてもhttpを使う必要がある場合を除き、後述の https 版を使うほうがよい
/*******************
IFTTT Webhookにhttpでデータ送信
******************/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// ルーターのSSID
const char* ssid = "◯◯◯◯◯";
// ルーターのパスワード
const char* password = "▲▲▲▲▲";
// IFTTT
const char* IFTTT_EVENT = "googledrive-ifttt-test01";
const char* IFTTT_KEY = "chJAmeuHie28jUdiaHYYYKZiwl0msTm47Jl-jYsf4eY";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(500); // 0.5秒待つ
/******************
Wifiに接続
*****************/
Serial.print(F("\n\nConnecting to "));
Serial.println(ssid);
int error_count = 0;
WiFi.begin(ssid, password);
// Wifi接続成功 待ち
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
// 0.5秒ごとに20回試行してWifiに接続できなかったら、スリープに入る
if (++error_count > 20)
{
Serial.println(F("Wifi eror\ngoing deep sleep ..."));
ESP.deepSleep(30 * 1000 * 1000); // 単位 usec : 30秒後にスリープから復帰する
delay(1000); // deepSleepに入るまでのダミー
}
}
Serial.print(F("\nWiFi connected\n"));
// Print the IP address
Serial.println(WiFi.localIP());
/******************
IFTTT Webhookにデータを送信
*****************/
double val01 = 12.5;
double val02 = -3.02;
double val03 = 5500;
char url[256];
sprintf(url, "http://maker.ifttt.com/trigger/%s/with/key/%s?"
"value1=%f&value2=%f&value3=%f",
IFTTT_EVENT, IFTTT_KEY, val01, val02, val03);
Serial.println(url);
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
// HTTP_CODE_OK = 200 の場合
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.printf("WARNING: http post, code = %d", httpCode);
}
}
else {
Serial.println(F("ERROR: http post"));
}
http.end();
/******************
Deep Sleepに入る
*****************/
Serial.println(F("going deep sleep ..."));
// 1分(60秒)スリープ
ESP.deepSleep( 60 * 1000 * 1000); // 単位 usec
delay(1000); // deepSleepに入るまでのダミー
}
void loop() {
// loop関数に入ることはないが、何らかの原因で呼びだされた場合のための処理
delay(10 * 1000);
}
引数の送信を、上の例ではGET(URLパラメーター)で行っているが、POSTで行う場合は次のようにすればよい
char url[256]; char param[256]; sprintf(url, "http://maker.ifttt.com/trigger/%s/with/key/%s", IFTTT_EVENT, IFTTT_KEY); sprintf(param, "value1=%f&value2=%f&value3=%f", val01, val02, val03); Serial.println(url); Serial.println(param); HTTPClient http; http.begin(url); int httpCode = http.POST(param); if (httpCode > 0) {
Arduino ESP8266 (ESP-WROOM-02) https 版
/*******************
IFTTT Webhookにhttpsでデータ送信
******************/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
// ルーターのSSID
const char* ssid = "◯◯◯◯◯";
// ルーターのパスワード
const char* password = "▲▲▲▲▲";
// IFTTT
const char* IFTTT_EVENT = "googledrive-ifttt-test01";
const char* IFTTT_KEY = "chJAmeuHie28jUdiaHYYYKZiwl0msTm47Jl-jYsf4eY";
void setup() {
Serial.begin(115200);
delay(500); // 0.5秒待つ
/******************
Wifiに接続
*****************/
Serial.print(F("\n\nConnecting to "));
Serial.println(ssid);
int error_count = 0;
WiFi.begin(ssid, password);
// Wifi接続成功 待ち
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
// 0.5秒ごとに20回試行してWifiに接続できなかったら、スリープに入る
if (++error_count > 20)
{
Serial.println(F("Wifi eror\ngoing deep sleep ..."));
ESP.deepSleep(30 * 1000 * 1000); // 単位 usec : 30秒後にスリープから復帰する
delay(1000); // deepSleepに入るまでのダミー
}
}
Serial.print(F("\nWiFi connected\n"));
// Print the IP address
Serial.println(WiFi.localIP());
/******************
IFTTT Webhookにデータを送信
*****************/
double val01 = 12.5;
double val02 = -3.02;
double val03 = 5500;
WiFiClientSecure https;
if (!https.connect("maker.ifttt.com", 443))
{
Serial.println(F("ERROR: https connect"));
}
else
{
https.printf("GET /trigger/%s/with/key/%s?"
"value1=%f&value2=%f&value3=%f HTTP/1.1\r\n"
"Host: maker.ifttt.com\r\n"
"User-Agent: ESP8266\r\n"
"Connection: close\r\n\r\n"
, IFTTT_EVENT, IFTTT_KEY, val01, val02, val03);
https.flush();
while (https.connected())
{
while(https.available())
{
String line = https.readStringUntil('\n');
Serial.println(line);
}
}
https.stop();
}
/******************
Deep Sleepに入る
*****************/
Serial.println(F("going deep sleep ..."));
// 1分(60秒)スリープ
ESP.deepSleep( 60 * 1000 * 1000); // 単位 usec
delay(1000); // deepSleepに入るまでのダミー
}
void loop() {
// loop関数に入ることはないが、何らかの原因で呼びだされた場合のための処理
delay(10 * 1000);
}
PC Linux, Raspberry Pi のPython
GET (URLパラメーター)でデータ送信する場合
import requests
# IFTTT
IFTTT_EVENT = 'googledrive-ifttt-test01'
IFTTT_KEY = 'chJAmeuHie28jUdiaHYYYKZiwl0msTm47Jl-jYsf4eY'
val01 = 12.5
val02 = -3.02
val03 = 5500
#URL
URL_STR = 'https://maker.ifttt.com/trigger/' + IFTTT_EVENT + '/with/key/' + \
IFTTT_KEY + '?value1=' + str(val01) + '&value2=' + str(val02) + \
'&value3=' + str(val03)
response = requests.get( URL_STR )
print( response.text )
POSTでデータ送信する場合
import requests # IFTTT IFTTT_EVENT = 'googledrive-ifttt-test01' IFTTT_KEY = 'chJAmeuHie28jUdiaHYYYKZiwl0msTm47Jl-jYsf4eY' val01 = 12.5 val02 = -3.02 val03 = 5500 hashparam = { 'value1' : str(val01), 'value2' : str(val02), 'value3' : str(val03) } #URL URL_STR = 'https://maker.ifttt.com/trigger/' + IFTTT_EVENT + '/with/key/' + IFTTT_KEY response = requests.get( URL_STR, params=hashparam ) print( response.text )
PC Linux, Raspberry Pi のPerl
GET (URLパラメーター)でデータ送信する場合
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
{
my $val01 = 12.5;
my $val02 = -3.02;
my $val03 = 5500;
# IFTTT
my $IFTTT_EVENT = 'googledrive-ifttt-test01';
my $IFTTT_KEY = 'chJAmeuHie28jUdiaHYYYKZiwl0msTm47Jl-jYsf4eY';
my $ua = new LWP::UserAgent();
my $response = $ua->post( "https://maker.ifttt.com/trigger/$IFTTT_EVENT/with/key/$IFTTT_KEY".
."?value1=$val01&value2=$val02&value3=$val03" );
print $response->as_string;
exit;
}
POSTでデータ送信する場合
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
{
my $val01 = 12.5;
my $val02 = -3.02;
my $val03 = 5500;
# IFTTT
my $IFTTT_EVENT = 'googledrive-ifttt-test01';
my $IFTTT_KEY = 'chJAmeuHie28jUdiaHYYYKZiwl0msTm47Jl-jYsf4eY';
my %param = (
"value1" => $val01,
"value2" => $val02,
"value3" => $val03,
);
my $ua = new LWP::UserAgent();
my $response = $ua->post( "https://maker.ifttt.com/trigger/$IFTTT_EVENT/with/key/$IFTTT_KEY",
\%param );
print $response->as_string;
exit;
}
