Simple IoT Board用のIFTTTのMaker Channelに繋げるためのサンプルです。

Dependencies:   SimpleIoTBoardLib mbed

Files at this revision

API Documentation at this revision

Comitter:
jksoft
Date:
Sun Nov 15 13:44:04 2015 +0000
Commit message:
??

Changed in this revision

IFTTT/ifttt.cpp Show annotated file Show diff for this revision Revisions of this file
IFTTT/ifttt.h Show annotated file Show diff for this revision Revisions of this file
SimpleIoTBoardLib.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 8459cf3d30f7 IFTTT/ifttt.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IFTTT/ifttt.cpp	Sun Nov 15 13:44:04 2015 +0000
@@ -0,0 +1,223 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mbed.h"
+#include "ifttt.h"
+//#include <string>
+#include "SoftSerialSendOnry.h"
+
+extern SoftSerialSendOnry pc;
+
+#if 0
+#define DBG(x, ...)  pc.printf("[IFTTT : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
+#define WARN(x, ...) pc.printf("[IFTTT : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
+#define ERR(x, ...)  pc.printf("[IFTTT : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
+#else
+#define DBG(x, ...) //wait_us(10);
+#define WARN(x, ...) //wait_us(10);
+#define ERR(x, ...)
+#endif
+
+#if 0
+#define INFO(x, ...) pc.printf("[IFTTT : INFO]"x" \r\n",##__VA_ARGS__);
+#else
+#define INFO(x, ...)
+#endif
+
+//
+// Initialize object with Event, Key, and valid socket.
+// TODO: accept hostname parameter / implement DNS lookup
+//
+IFTTT::IFTTT(const char * event, const char * key, TCPSocketConnection * s)
+{
+    // Error Check
+    if(sizeof(event) > IFTTT_MAX_SIZE_EVENTNAME) {
+        ERR("Given event > IFTTT_MAX_SIZE_EVENTNAME, increase the max event string size in ifttt.h");
+    }
+    if(sizeof(key) > IFTTT_MAX_SIZE_SECRETKEY) {
+        ERR("Given key > IFTTT_MAX_SIZE_SECRETKEY, increase the max secret key string size in ifttt.h");
+    }
+    // Copy event name and secret key into object instance
+    strcpy(this->eventName,event);
+    strcpy(this->secretKey,key);
+
+    // Set up Socket
+    if(NULL == s) {
+        WARN("Given Socket Pointer is NULL, will try opening a socket.");
+    }
+    this->socket = s;
+
+    // Set up Host / Port
+    this->port = IFTTT_PORT;
+    this->host = IFTTT_IP;
+    
+    // Initialize ingredient values to empty strings.
+    v1 = "";
+    v2 = "";
+    v3 = "";
+}
+
+//
+// Add ingredients to be sent.
+//
+bool
+IFTTT::addIngredients( char * value1,  char * value2,  char * value3)
+{
+    // update internal pointers. If variable not given then pass an empty string
+    v1 = (NULL == value1)?"":value1;
+    v2 = (NULL == value2)?"":value2;
+    v3 = (NULL == value3)?"":value3;
+    return true;
+}
+
+//
+// This function sends data to maker.ifttt.org via GET query commands
+// return true on sucess, false on fail
+//
+bool IFTTT::get()
+{
+    // Connect to maker.ifttt.org
+    int retry = 0;
+    for(retry=0; retry<IFTTT_MAX_RETRY; retry++) {
+        int ret = this->socket->connect(this->host, this->port);
+        if(ret == 0) {
+            DBG("Successfully Connected socket to host");
+            break ;
+        }
+    }
+    if(retry == IFTTT_MAX_RETRY) {
+        this->socket->close();
+        ERR("Could not connect socket to host\r\n");
+        return false;
+    }
+
+    // Prep data to send
+    // TODO: verify / modify data to be query string compliant (convert spaces to '+', convert non-alpha-numberic characters to the proper encoding... etc)
+    char str[IFTTT_MAX_SIZE_STRING] = {0};
+    sprintf(str, "GET /trigger/%s/with/key/%s/?value1=%s&value2=%s&value3=%s HTTP/1.1\r\nHost: maker.ifttt.com\r\n\r\n",eventName,secretKey,v1,v2,v3);
+    DBG("String to send is:\n\r%s",str);
+
+    // Send Data
+    DBG("Sending GET data...");
+    int check = 0;
+    check = this->socket->send_all(str,sizeof(str));
+    if(check) {
+        DBG("Sent Sucessfully %d bytes",check);
+    } else {
+        ERR("Sending failed");
+        return false;
+    }
+    DBG("Waiting on reply ... \r\n");
+    int ret = this->socket->receive(str,50);
+    str[ret]=0;
+    DBG("Received String : %s",str);
+    this->socket->close();
+
+    return true;
+}
+
+//
+// This function sends JSON encoded data encoded in a POST packet, 
+//
+bool IFTTT::post()
+{
+    // Connect to maker.ifttt.org
+    int retry = 0;
+    for(retry=0; retry<IFTTT_MAX_RETRY; retry++) {
+        int ret = this->socket->connect(this->host, this->port);
+        if(ret == 0) {
+            DBG("Successfully Connected socket to host");
+            break ;
+        }
+    }
+    if(retry == IFTTT_MAX_RETRY) {
+        this->socket->close();
+        ERR("Could not connect socket to host\r\n");
+        return false;
+    }
+
+    // Prep data to send, the Assembled POST packet should look like this
+    //
+    //
+    //POST /trigger/<eventName>/with/key/<secretKey> HTTP/1.1
+    //Host: maker.ifttt.com
+    //Content-Length: <length of POST data>
+    //Content-Type: application/json
+    //
+    //{"value1":"<v1>","value2":"<v2>","value3":"<v3>"}
+    //
+    //
+    char str[IFTTT_MAX_SIZE_STRING] = {0};
+    char header[100] = {0};
+    sprintf(header, "POST /trigger/%s/with/key/%s HTTP/1.1\r\n",eventName,secretKey);
+    const char * host = "Host: maker.ifttt.com\r\n";
+    char contentLen[50] = {0};
+    const char * contentType = "Content-Type: application/json\r\n\r\n";
+    char valueData [150] = {0};
+    sprintf(valueData,"{\"value1\":\"%s\",\"value2\":\"%s\",\"value3\":\"%s\"}\r\n",v1,v2,v3);
+    sprintf(contentLen,"Content-Length: %d\r\n",strlen(valueData));
+    sprintf(str,"%s%s%s%s%s",header,host,contentLen,contentType,valueData);
+
+    DBG("String to send is:\n\r%s",str);
+
+    // Send Data
+    DBG("Sending POST data...");
+    int check = 0;
+    check = this->socket->send_all(str,strlen(str));
+    if(check) {
+        DBG("Sent Sucessfully %d bytes",check);
+    } else {
+        ERR("Sending failed");
+        return false;
+    }
+    DBG("Waiting on reply ... \r\n");
+    int ret = this->socket->receive(str,IFTTT_MAX_SIZE_STRING);
+    str[ret]=0;
+    DBG("Received String : %s",str);
+    this->socket->close();
+
+    return true;
+}
+
+//
+// Send trigger and any values associated to maker.ifttt.com
+// currently  unsecured (sends over HTTP, not https)
+//
+bool
+IFTTT::trigger(int triggerType)
+{
+    int ret = 0;
+    switch(triggerType) {
+        case IFTTT_GET:
+            DBG("Sending Data as GET request");
+            ret = get();
+            break;
+        case IFTTT_POST:
+            DBG("Sending Data as POST request");
+            ret = post();
+            break;
+
+        default:
+            WARN("Invalid type, defaulting to sending data as POST request");
+            ret = post();
+            break;
+    }
+    DBG("Sending Data return code : %d",ret);
+    if(ret){
+    INFO("Successfully triggered event: '%s' with v1='%s', v2='%s', v3='%s' !",eventName,v1,v2,v3);
+    }
+    return ret;
+}
diff -r 000000000000 -r 8459cf3d30f7 IFTTT/ifttt.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IFTTT/ifttt.h	Sun Nov 15 13:44:04 2015 +0000
@@ -0,0 +1,115 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef IFTTT_H
+#define IFTTT_H
+
+#include "mbed.h"
+#include "TCPSocketConnection.h"
+
+#define IFTTT_FAIL -1
+#define IFTTT_SUCCESS 0
+
+#define IFTTT_MAX_RETRY 5
+#define IFTTT_MAX_SIZE_SECRETKEY 50
+#define IFTTT_MAX_SIZE_EVENTNAME 50
+#define IFTTT_MAX_SIZE_STRING 512
+
+#define IFTTT_IP "maker.ifttt.com"
+#define IFTTT_PORT 80
+
+#define IFTTT_GET 0
+#define IFTTT_POST 1
+
+
+/**
+* The IFTTT class (if this then that)
+*/
+class IFTTT
+{
+
+public:
+
+    /**
+    * Constructor, initialize the Event Name and Secret Key to be used
+    *
+    * @param event event name of trigger
+    * @param key secret key provided by the maker channel for your event
+    */
+    IFTTT(const char * event, const char * key, TCPSocketConnection * s = NULL);
+
+    /**
+    * Add ingredients (values) to be sent to maker.ifttt.com
+    *
+    * @param v1 value 1 to send
+    * @param v2 value 2 to send
+    * @param v3 value 3 to send
+    *
+    * @return true if successful, false if failed
+    */
+    bool addIngredients(char * v1 = NULL, char * v2 = NULL, char * v3 = NULL);
+
+    /**
+    * Add ingredients (values) to be sent to maker.ifttt.com
+    *
+    * @param v1 value 1 to send
+    * @param v2 value 2 to send
+    * @param v3 value 3 to send
+    *
+    * @return true if successful, false if failed
+    */
+    bool addIngredients(int v1 = NULL, int v2 = NULL, int v3 = NULL);
+
+    /**
+    * Send data via POST or GET to maker.ifttt.com
+    *
+    * @param iftttType specifies how to send the data. POST by default, GET optional.
+    *
+    * @return true if successful, false if failed
+    */
+    bool trigger(int triggerType = IFTTT_POST);
+
+private:
+
+    /**
+    * Send data to maker.ifttt.com via GET Query parameters
+    *
+    * @param name explanation
+    *
+    * @return true if successful, false if failed
+    */
+    bool get();
+
+    /**
+    * Send data to maker.ifttt.com via POST payload, encoded as JSON
+    *
+    * @param name explanation
+    *
+    * @return true if successful, false if failed
+    */
+    bool post();
+
+    TCPSocketConnection * socket;
+    char eventName[IFTTT_MAX_SIZE_EVENTNAME];
+    char secretKey[IFTTT_MAX_SIZE_SECRETKEY];
+    const char * v1;
+    const char * v2;
+    const char * v3;
+    const char * host;
+    int port;
+
+};
+
+#endif // IFTTT_H
diff -r 000000000000 -r 8459cf3d30f7 SimpleIoTBoardLib.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleIoTBoardLib.lib	Sun Nov 15 13:44:04 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/jksoft/code/SimpleIoTBoardLib/#890c12951e96
diff -r 000000000000 -r 8459cf3d30f7 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Nov 15 13:44:04 2015 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+#include "ESP8266Interface.h"
+#include "TCPSocketConnection.h"
+#include "ifttt.h"
+#include "SoftSerialSendOnry.h"
+
+AnalogIn thermistor(dp13);   /* Temperature sensor connected to Analog Grove connector */
+
+ESP8266Interface wifi(dp16,dp15,dp4,"SSID","Password",115200); // TX,RX,Reset,SSID,Password,Baud
+
+SoftSerialSendOnry pc(dp10); // tx
+
+int main()
+{
+    pc.baud(9600);
+    wifi.init(); //Reset
+    wifi.connect(); //Use DHCP
+    pc.printf("IP Address is %s \n\r", wifi.getIPAddress());
+    TCPSocketConnection socket;
+    
+    // Initialize ifttt object, add up to 3 optional values, trigger event. 
+    IFTTT ifttt("EventName","Secret Key", &socket); // EventName, Secret Key, socket to use
+
+    ifttt.addIngredients("value1","value2","value3");
+    ifttt.trigger(IFTTT_POST);
+}
diff -r 000000000000 -r 8459cf3d30f7 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Nov 15 13:44:04 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9296ab0bfc11
\ No newline at end of file