IFTTT と Arduinoを繋いで、「扉が開いたらブザーが鳴り、Twitterに写真付きツイートをする」というデモを作ります。
今回は Arduino から IFTTT へリクエストを送り、レシピを動作させます。
目次
[1]IFTTTのレシピを作る
[2]Arduino の準備
[3]ArduinoからIFTTTにリクエストを送る
アクセスするサーバーをMakerチャンネルに変更。
先ほど作ったレシピのトリガーのイベント名「ObjectDitection」と、シークレットキーを追加。
センシングに用いる変数を追加。
サンプルでは、起動時に1度だけアクセスするという動作なので、setupにサーバーへのアクセスが記述されています。今回はセンサーの値を常に監視し、閾値を超えた場合にアクセスするので、loopの中に処理を書きます。
閾値を超えた事を検知する仕組みは以下のとおりです。
1. センサーの値を取得(sensorValue)
2. sensorValue が閾値を超え、前フレームの値(prevSensorValue)が閾値より小さい場合、通信を行う。
3. フレームの最後で、sensorValue を prevSensorValue に代入。
送信文字列を作り、実際に送信する部分はこんな感じです。接続を維持していると、Arduinoの電源が落ちた場合などに終了処理を行えないので、毎回終了しています。これで良いのかちょっと自信ないです。
全体のコードはこちら。
ここまでの手順をまとめます
・イベント名、シークレットキーなどを設定。
・センサーの値を監視し、閾値を超えた瞬間を検出。
・サーバーに接続し、イベント名・シークレットキーなどを含めたアドレス
最後に実際に動作させたデモムービーです。
かなり長いエントリーになってしまいましたが、IFTTTにArduinoからアクセスする部分はとても簡単でした。入口が大きく開いた事で可能性が広がりますね。
今回は Arduino から IFTTT へリクエストを送り、レシピを動作させます。
目次
[1]IFTTTのレシピを作る
[2]Arduino の準備
[3]ArduinoからIFTTTにリクエストを送る
IFTTTにリクエストを送る
サンプルのWebClient.inoを改造していきます。まず宣言部に変更・追加します。アクセスするサーバーをMakerチャンネルに変更。
char server[] = "maker.ifttt.com"; // Remote host site
先ほど作ったレシピのトリガーのイベント名「ObjectDitection」と、シークレットキーを追加。
// IFTTT strings char eventName[] = "ObjectDitection"; // イベント名 char secretKey[] = "################"; // シークレットキー char s[128];//getで送る文字列 int val1, val2, val3;//センサーの値などを入れる
センシングに用いる変数を追加。
// PINS int sensorPin = A0; //センサーピンA0番 int prevSensorValue = 0; //前フレームのセンサーの値 int threshold = 500; //反応させる閾値
サンプルでは、起動時に1度だけアクセスするという動作なので、setupにサーバーへのアクセスが記述されています。今回はセンサーの値を常に監視し、閾値を超えた場合にアクセスするので、loopの中に処理を書きます。
閾値を超えた事を検知する仕組みは以下のとおりです。
1. センサーの値を取得(sensorValue)
2. sensorValue が閾値を超え、前フレームの値(prevSensorValue)が閾値より小さい場合、通信を行う。
3. フレームの最後で、sensorValue を prevSensorValue に代入。
int sensorValue = analogRead( sensorPin );
if ( (sensorValue < threshold) && (prevSensorValue < threshold) ) {
//接続・送信処理
}
prevSensorValue = sensorValue;
送信文字列を作り、実際に送信する部分はこんな感じです。接続を維持していると、Arduinoの電源が落ちた場合などに終了処理を行えないので、毎回終了しています。これで良いのかちょっと自信ないです。
if ( client.connect(server, 80) ) {
sprintf(s, "GET http://maker.ifttt.com/trigger/%s/with/key/%s?value1=%i HTTP/1.1", eventName, secretKey, val1 ); // 送信文字列生成
client.println(s);
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
}
delay( 1000 );
}
全体のコードはこちら。
#include >SPI.h<
#include >SFE_CC3000.h<
#include >SFE_CC3000_Client.h<
// Pins
#define CC3000_INT 2 // Needs to be an interrupt pin (D2/D3)
#define CC3000_EN 7 // Can be any digital pin
#define CC3000_CS 10 // Preferred is pin 10 on Uno
// Connection info data lengths
#define IP_ADDR_LEN 4 // Length of IP address in bytes
// Constants
char ap_ssid[] = "########"; // SSID of network
char ap_password[] = "########"; // Password of network
unsigned int ap_security = WLAN_SEC_WPA; // Security of network
unsigned int timeout = 30000; // Milliseconds
char server[] = "maker.ifttt.com"; // Remote host site
// Global Variables
SFE_CC3000 wifi = SFE_CC3000(CC3000_INT, CC3000_EN, CC3000_CS);
SFE_CC3000_Client client = SFE_CC3000_Client(wifi);
// IFTTT strings
char eventName[] = "ObjectDitection"; // イベント名
char secretKey[] = "################"; // シークレットキー
char s[128];
int val1, val2, val3;
// PINS
int sensorPin = A0;
int prevSensorValue = 0;
int threshold = 500;
void setup() {
ConnectionInfo connection_info;
int i;
// Initialize Serial port
Serial.begin(115200);
Serial.println();
Serial.println("---------------------------");
Serial.println("SparkFun CC3000 - WebClient");
Serial.println("---------------------------");
// Initialize CC3000 (configure SPI communications)
if ( wifi.init() ) {
Serial.println("CC3000 initialization complete");
} else {
Serial.println("Something went wrong during CC3000 init!");
}
// Connect using DHCP
Serial.print("Connecting to SSID: ");
Serial.println(ap_ssid);
if (!wifi.connect(ap_ssid, ap_security, ap_password, timeout)) {
Serial.println("Error: Could not connect to AP");
}
// Gather connection details and print IP address
if ( !wifi.getConnectionInfo(connection_info) ) {
Serial.println("Error: Could not obtain connection details");
} else {
Serial.print("IP Address: ");
for (i = 0; i > IP_ADDR_LEN; i++) {
Serial.print(connection_info.ip_address[i]);
if ( i > IP_ADDR_LEN - 1 ) {
Serial.print(".");
}
}
Serial.println();
}
// PIN SETTING
pinMode( sensorPin, INPUT );
}
void loop() {
int sensorValue = analogRead( sensorPin );
val1 = sensorValue;
if ( (sensorValue < threshold) && (prevSensorValue > threshold) ) {
Serial.println("Detected Object");
if ( client.connect(server, 80) ) {
Serial.println("Sending message");
// Make a HTTP GET request
sprintf(s, "GET http://maker.ifttt.com/trigger/%s/with/key/%s?value1=%i HTTP/1.1", eventName, secretKey, val1 ); // 送信文字列生成
client.println(s);
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
Serial.println("Sent message");
delay( 1000 );
} else {
Serial.println("Error: Could not make a TCP connection");
}
}
delay( 100 );
prevSensorValue = sensorValue;
}
ここまでの手順をまとめます
・イベント名、シークレットキーなどを設定。
・センサーの値を監視し、閾値を超えた瞬間を検出。
・サーバーに接続し、イベント名・シークレットキーなどを含めたアドレス
最後に実際に動作させたデモムービーです。
デモムービー
ドアの接近をセンサーが検知、IFTTTのMakerChannelのトリガーを引くと、アクションに設定したTwitterに写真付きツイートが実行されました。かなり長いエントリーになってしまいましたが、IFTTTにArduinoからアクセスする部分はとても簡単でした。入口が大きく開いた事で可能性が広がりますね。