![](/media/cache/profiles/5f14615696649541a025d3d0f8e0447f.jpg.50x50_q85.jpg)
ワークショップ用のサンプルプログラムです。
IFTTT/ifttt.cpp@0:a967c8649563, 2016-04-29 (annotated)
- Committer:
- jksoft
- Date:
- Fri Apr 29 21:07:24 2016 +0000
- Revision:
- 0:a967c8649563
??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:a967c8649563 | 1 | /* mbed Microcontroller Library |
jksoft | 0:a967c8649563 | 2 | * Copyright (c) 2006-2013 ARM Limited |
jksoft | 0:a967c8649563 | 3 | * |
jksoft | 0:a967c8649563 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jksoft | 0:a967c8649563 | 5 | * you may not use this file except in compliance with the License. |
jksoft | 0:a967c8649563 | 6 | * You may obtain a copy of the License at |
jksoft | 0:a967c8649563 | 7 | * |
jksoft | 0:a967c8649563 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jksoft | 0:a967c8649563 | 9 | * |
jksoft | 0:a967c8649563 | 10 | * Unless required by applicable law or agreed to in writing, software |
jksoft | 0:a967c8649563 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jksoft | 0:a967c8649563 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jksoft | 0:a967c8649563 | 13 | * See the License for the specific language governing permissions and |
jksoft | 0:a967c8649563 | 14 | * limitations under the License. |
jksoft | 0:a967c8649563 | 15 | */ |
jksoft | 0:a967c8649563 | 16 | |
jksoft | 0:a967c8649563 | 17 | #include "mbed.h" |
jksoft | 0:a967c8649563 | 18 | #include "ifttt.h" |
jksoft | 0:a967c8649563 | 19 | //#include <string> |
jksoft | 0:a967c8649563 | 20 | #include "SoftSerialSendOnry.h" |
jksoft | 0:a967c8649563 | 21 | |
jksoft | 0:a967c8649563 | 22 | extern SoftSerialSendOnry pc; |
jksoft | 0:a967c8649563 | 23 | |
jksoft | 0:a967c8649563 | 24 | #if 0 |
jksoft | 0:a967c8649563 | 25 | #define DBG(x, ...) pc.printf("[IFTTT : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
jksoft | 0:a967c8649563 | 26 | #define WARN(x, ...) pc.printf("[IFTTT : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
jksoft | 0:a967c8649563 | 27 | #define ERR(x, ...) pc.printf("[IFTTT : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
jksoft | 0:a967c8649563 | 28 | #else |
jksoft | 0:a967c8649563 | 29 | #define DBG(x, ...) //wait_us(10); |
jksoft | 0:a967c8649563 | 30 | #define WARN(x, ...) //wait_us(10); |
jksoft | 0:a967c8649563 | 31 | #define ERR(x, ...) |
jksoft | 0:a967c8649563 | 32 | #endif |
jksoft | 0:a967c8649563 | 33 | |
jksoft | 0:a967c8649563 | 34 | #if 0 |
jksoft | 0:a967c8649563 | 35 | #define INFO(x, ...) pc.printf("[IFTTT : INFO]"x" \r\n",##__VA_ARGS__); |
jksoft | 0:a967c8649563 | 36 | #else |
jksoft | 0:a967c8649563 | 37 | #define INFO(x, ...) |
jksoft | 0:a967c8649563 | 38 | #endif |
jksoft | 0:a967c8649563 | 39 | |
jksoft | 0:a967c8649563 | 40 | // |
jksoft | 0:a967c8649563 | 41 | // Initialize object with Event, Key, and valid socket. |
jksoft | 0:a967c8649563 | 42 | // TODO: accept hostname parameter / implement DNS lookup |
jksoft | 0:a967c8649563 | 43 | // |
jksoft | 0:a967c8649563 | 44 | IFTTT::IFTTT(const char * event, const char * key, TCPSocketConnection * s) |
jksoft | 0:a967c8649563 | 45 | { |
jksoft | 0:a967c8649563 | 46 | // Error Check |
jksoft | 0:a967c8649563 | 47 | if(sizeof(event) > IFTTT_MAX_SIZE_EVENTNAME) { |
jksoft | 0:a967c8649563 | 48 | ERR("Given event > IFTTT_MAX_SIZE_EVENTNAME, increase the max event string size in ifttt.h"); |
jksoft | 0:a967c8649563 | 49 | } |
jksoft | 0:a967c8649563 | 50 | if(sizeof(key) > IFTTT_MAX_SIZE_SECRETKEY) { |
jksoft | 0:a967c8649563 | 51 | ERR("Given key > IFTTT_MAX_SIZE_SECRETKEY, increase the max secret key string size in ifttt.h"); |
jksoft | 0:a967c8649563 | 52 | } |
jksoft | 0:a967c8649563 | 53 | // Copy event name and secret key into object instance |
jksoft | 0:a967c8649563 | 54 | strcpy(this->eventName,event); |
jksoft | 0:a967c8649563 | 55 | strcpy(this->secretKey,key); |
jksoft | 0:a967c8649563 | 56 | |
jksoft | 0:a967c8649563 | 57 | // Set up Socket |
jksoft | 0:a967c8649563 | 58 | if(NULL == s) { |
jksoft | 0:a967c8649563 | 59 | WARN("Given Socket Pointer is NULL, will try opening a socket."); |
jksoft | 0:a967c8649563 | 60 | } |
jksoft | 0:a967c8649563 | 61 | this->socket = s; |
jksoft | 0:a967c8649563 | 62 | |
jksoft | 0:a967c8649563 | 63 | // Set up Host / Port |
jksoft | 0:a967c8649563 | 64 | this->port = IFTTT_PORT; |
jksoft | 0:a967c8649563 | 65 | this->host = IFTTT_IP; |
jksoft | 0:a967c8649563 | 66 | |
jksoft | 0:a967c8649563 | 67 | // Initialize ingredient values to empty strings. |
jksoft | 0:a967c8649563 | 68 | v1 = ""; |
jksoft | 0:a967c8649563 | 69 | v2 = ""; |
jksoft | 0:a967c8649563 | 70 | v3 = ""; |
jksoft | 0:a967c8649563 | 71 | } |
jksoft | 0:a967c8649563 | 72 | |
jksoft | 0:a967c8649563 | 73 | // |
jksoft | 0:a967c8649563 | 74 | // Add ingredients to be sent. |
jksoft | 0:a967c8649563 | 75 | // |
jksoft | 0:a967c8649563 | 76 | bool |
jksoft | 0:a967c8649563 | 77 | IFTTT::addIngredients( char * value1, char * value2, char * value3) |
jksoft | 0:a967c8649563 | 78 | { |
jksoft | 0:a967c8649563 | 79 | // update internal pointers. If variable not given then pass an empty string |
jksoft | 0:a967c8649563 | 80 | v1 = (NULL == value1)?"":value1; |
jksoft | 0:a967c8649563 | 81 | v2 = (NULL == value2)?"":value2; |
jksoft | 0:a967c8649563 | 82 | v3 = (NULL == value3)?"":value3; |
jksoft | 0:a967c8649563 | 83 | return true; |
jksoft | 0:a967c8649563 | 84 | } |
jksoft | 0:a967c8649563 | 85 | |
jksoft | 0:a967c8649563 | 86 | // |
jksoft | 0:a967c8649563 | 87 | // This function sends data to maker.ifttt.org via GET query commands |
jksoft | 0:a967c8649563 | 88 | // return true on sucess, false on fail |
jksoft | 0:a967c8649563 | 89 | // |
jksoft | 0:a967c8649563 | 90 | bool IFTTT::get() |
jksoft | 0:a967c8649563 | 91 | { |
jksoft | 0:a967c8649563 | 92 | // Connect to maker.ifttt.org |
jksoft | 0:a967c8649563 | 93 | int retry = 0; |
jksoft | 0:a967c8649563 | 94 | for(retry=0; retry<IFTTT_MAX_RETRY; retry++) { |
jksoft | 0:a967c8649563 | 95 | int ret = this->socket->connect(this->host, this->port); |
jksoft | 0:a967c8649563 | 96 | if(ret == 0) { |
jksoft | 0:a967c8649563 | 97 | DBG("Successfully Connected socket to host"); |
jksoft | 0:a967c8649563 | 98 | break ; |
jksoft | 0:a967c8649563 | 99 | } |
jksoft | 0:a967c8649563 | 100 | } |
jksoft | 0:a967c8649563 | 101 | if(retry == IFTTT_MAX_RETRY) { |
jksoft | 0:a967c8649563 | 102 | this->socket->close(); |
jksoft | 0:a967c8649563 | 103 | ERR("Could not connect socket to host\r\n"); |
jksoft | 0:a967c8649563 | 104 | return false; |
jksoft | 0:a967c8649563 | 105 | } |
jksoft | 0:a967c8649563 | 106 | |
jksoft | 0:a967c8649563 | 107 | // Prep data to send |
jksoft | 0:a967c8649563 | 108 | // TODO: verify / modify data to be query string compliant (convert spaces to '+', convert non-alpha-numberic characters to the proper encoding... etc) |
jksoft | 0:a967c8649563 | 109 | char str[IFTTT_MAX_SIZE_STRING] = {0}; |
jksoft | 0:a967c8649563 | 110 | 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); |
jksoft | 0:a967c8649563 | 111 | DBG("String to send is:\n\r%s",str); |
jksoft | 0:a967c8649563 | 112 | |
jksoft | 0:a967c8649563 | 113 | // Send Data |
jksoft | 0:a967c8649563 | 114 | DBG("Sending GET data..."); |
jksoft | 0:a967c8649563 | 115 | int check = 0; |
jksoft | 0:a967c8649563 | 116 | check = this->socket->send_all(str,sizeof(str)); |
jksoft | 0:a967c8649563 | 117 | if(check) { |
jksoft | 0:a967c8649563 | 118 | DBG("Sent Sucessfully %d bytes",check); |
jksoft | 0:a967c8649563 | 119 | } else { |
jksoft | 0:a967c8649563 | 120 | ERR("Sending failed"); |
jksoft | 0:a967c8649563 | 121 | return false; |
jksoft | 0:a967c8649563 | 122 | } |
jksoft | 0:a967c8649563 | 123 | DBG("Waiting on reply ... \r\n"); |
jksoft | 0:a967c8649563 | 124 | int ret = this->socket->receive(str,50); |
jksoft | 0:a967c8649563 | 125 | str[ret]=0; |
jksoft | 0:a967c8649563 | 126 | DBG("Received String : %s",str); |
jksoft | 0:a967c8649563 | 127 | this->socket->close(); |
jksoft | 0:a967c8649563 | 128 | |
jksoft | 0:a967c8649563 | 129 | return true; |
jksoft | 0:a967c8649563 | 130 | } |
jksoft | 0:a967c8649563 | 131 | |
jksoft | 0:a967c8649563 | 132 | // |
jksoft | 0:a967c8649563 | 133 | // This function sends JSON encoded data encoded in a POST packet, |
jksoft | 0:a967c8649563 | 134 | // |
jksoft | 0:a967c8649563 | 135 | bool IFTTT::post() |
jksoft | 0:a967c8649563 | 136 | { |
jksoft | 0:a967c8649563 | 137 | // Connect to maker.ifttt.org |
jksoft | 0:a967c8649563 | 138 | int retry = 0; |
jksoft | 0:a967c8649563 | 139 | for(retry=0; retry<IFTTT_MAX_RETRY; retry++) { |
jksoft | 0:a967c8649563 | 140 | int ret = this->socket->connect(this->host, this->port); |
jksoft | 0:a967c8649563 | 141 | if(ret == 0) { |
jksoft | 0:a967c8649563 | 142 | DBG("Successfully Connected socket to host"); |
jksoft | 0:a967c8649563 | 143 | break ; |
jksoft | 0:a967c8649563 | 144 | } |
jksoft | 0:a967c8649563 | 145 | } |
jksoft | 0:a967c8649563 | 146 | if(retry == IFTTT_MAX_RETRY) { |
jksoft | 0:a967c8649563 | 147 | this->socket->close(); |
jksoft | 0:a967c8649563 | 148 | ERR("Could not connect socket to host\r\n"); |
jksoft | 0:a967c8649563 | 149 | return false; |
jksoft | 0:a967c8649563 | 150 | } |
jksoft | 0:a967c8649563 | 151 | |
jksoft | 0:a967c8649563 | 152 | // Prep data to send, the Assembled POST packet should look like this |
jksoft | 0:a967c8649563 | 153 | // |
jksoft | 0:a967c8649563 | 154 | // |
jksoft | 0:a967c8649563 | 155 | //POST /trigger/<eventName>/with/key/<secretKey> HTTP/1.1 |
jksoft | 0:a967c8649563 | 156 | //Host: maker.ifttt.com |
jksoft | 0:a967c8649563 | 157 | //Content-Length: <length of POST data> |
jksoft | 0:a967c8649563 | 158 | //Content-Type: application/json |
jksoft | 0:a967c8649563 | 159 | // |
jksoft | 0:a967c8649563 | 160 | //{"value1":"<v1>","value2":"<v2>","value3":"<v3>"} |
jksoft | 0:a967c8649563 | 161 | // |
jksoft | 0:a967c8649563 | 162 | // |
jksoft | 0:a967c8649563 | 163 | char str[IFTTT_MAX_SIZE_STRING] = {0}; |
jksoft | 0:a967c8649563 | 164 | char header[100] = {0}; |
jksoft | 0:a967c8649563 | 165 | sprintf(header, "POST /trigger/%s/with/key/%s HTTP/1.1\r\n",eventName,secretKey); |
jksoft | 0:a967c8649563 | 166 | const char * host = "Host: maker.ifttt.com\r\n"; |
jksoft | 0:a967c8649563 | 167 | char contentLen[50] = {0}; |
jksoft | 0:a967c8649563 | 168 | const char * contentType = "Content-Type: application/json\r\n\r\n"; |
jksoft | 0:a967c8649563 | 169 | char valueData [150] = {0}; |
jksoft | 0:a967c8649563 | 170 | sprintf(valueData,"{\"value1\":\"%s\",\"value2\":\"%s\",\"value3\":\"%s\"}\r\n",v1,v2,v3); |
jksoft | 0:a967c8649563 | 171 | sprintf(contentLen,"Content-Length: %d\r\n",strlen(valueData)); |
jksoft | 0:a967c8649563 | 172 | sprintf(str,"%s%s%s%s%s",header,host,contentLen,contentType,valueData); |
jksoft | 0:a967c8649563 | 173 | |
jksoft | 0:a967c8649563 | 174 | DBG("String to send is:\n\r%s",str); |
jksoft | 0:a967c8649563 | 175 | |
jksoft | 0:a967c8649563 | 176 | // Send Data |
jksoft | 0:a967c8649563 | 177 | DBG("Sending POST data..."); |
jksoft | 0:a967c8649563 | 178 | int check = 0; |
jksoft | 0:a967c8649563 | 179 | check = this->socket->send_all(str,strlen(str)); |
jksoft | 0:a967c8649563 | 180 | if(check) { |
jksoft | 0:a967c8649563 | 181 | DBG("Sent Sucessfully %d bytes",check); |
jksoft | 0:a967c8649563 | 182 | } else { |
jksoft | 0:a967c8649563 | 183 | ERR("Sending failed"); |
jksoft | 0:a967c8649563 | 184 | return false; |
jksoft | 0:a967c8649563 | 185 | } |
jksoft | 0:a967c8649563 | 186 | DBG("Waiting on reply ... \r\n"); |
jksoft | 0:a967c8649563 | 187 | int ret = this->socket->receive(str,IFTTT_MAX_SIZE_STRING); |
jksoft | 0:a967c8649563 | 188 | str[ret]=0; |
jksoft | 0:a967c8649563 | 189 | DBG("Received String : %s",str); |
jksoft | 0:a967c8649563 | 190 | this->socket->close(); |
jksoft | 0:a967c8649563 | 191 | |
jksoft | 0:a967c8649563 | 192 | return true; |
jksoft | 0:a967c8649563 | 193 | } |
jksoft | 0:a967c8649563 | 194 | |
jksoft | 0:a967c8649563 | 195 | // |
jksoft | 0:a967c8649563 | 196 | // Send trigger and any values associated to maker.ifttt.com |
jksoft | 0:a967c8649563 | 197 | // currently unsecured (sends over HTTP, not https) |
jksoft | 0:a967c8649563 | 198 | // |
jksoft | 0:a967c8649563 | 199 | bool |
jksoft | 0:a967c8649563 | 200 | IFTTT::trigger(int triggerType) |
jksoft | 0:a967c8649563 | 201 | { |
jksoft | 0:a967c8649563 | 202 | int ret = 0; |
jksoft | 0:a967c8649563 | 203 | switch(triggerType) { |
jksoft | 0:a967c8649563 | 204 | case IFTTT_GET: |
jksoft | 0:a967c8649563 | 205 | DBG("Sending Data as GET request"); |
jksoft | 0:a967c8649563 | 206 | ret = get(); |
jksoft | 0:a967c8649563 | 207 | break; |
jksoft | 0:a967c8649563 | 208 | case IFTTT_POST: |
jksoft | 0:a967c8649563 | 209 | DBG("Sending Data as POST request"); |
jksoft | 0:a967c8649563 | 210 | ret = post(); |
jksoft | 0:a967c8649563 | 211 | break; |
jksoft | 0:a967c8649563 | 212 | |
jksoft | 0:a967c8649563 | 213 | default: |
jksoft | 0:a967c8649563 | 214 | WARN("Invalid type, defaulting to sending data as POST request"); |
jksoft | 0:a967c8649563 | 215 | ret = post(); |
jksoft | 0:a967c8649563 | 216 | break; |
jksoft | 0:a967c8649563 | 217 | } |
jksoft | 0:a967c8649563 | 218 | DBG("Sending Data return code : %d",ret); |
jksoft | 0:a967c8649563 | 219 | if(ret){ |
jksoft | 0:a967c8649563 | 220 | INFO("Successfully triggered event: '%s' with v1='%s', v2='%s', v3='%s' !",eventName,v1,v2,v3); |
jksoft | 0:a967c8649563 | 221 | } |
jksoft | 0:a967c8649563 | 222 | return ret; |
jksoft | 0:a967c8649563 | 223 | } |