Twilio call to your phone example
Dependencies: EthernetInterface HTTPClient mbed-rtos mbed
Revision 0:215c9ef8ad80, committed 2014-12-08
- Comitter:
- wolfSSL
- Date:
- Mon Dec 08 12:13:59 2014 +0000
- Commit message:
- Initial version
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Mon Dec 08 12:13:59 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/wolfSSL/code/EthernetInterface/#a477218b744e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient.lib Mon Dec 08 12:13:59 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/wolfSSL/code/HTTPClient/#a9ecee69c6b5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Dec 08 12:13:59 2014 +0000 @@ -0,0 +1,98 @@ +/* twilio.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "mbed.h" +#include "EthernetInterface.h" +#include "HTTPClient.h" + +#define KEYS "https://192.168.49.57/574d76fcb/keys.txt" + +#define BUFFSIZE (50+1) + +extern void twilio_call(char *twilio_AccountSid, + char *twilio_AuthToken, + char *twilio_From, + char *twilio_To, + char *twilio_Url) ; + +extern HTTPClient http; +EthernetInterface eth; + +void thread_main(void const *av) +{ + int ret ; + int i ; + static char recvBuff[BUFFSIZE*6] ; + static char dummy1[BUFFSIZE], dummy2[BUFFSIZE] ; + static char twilio_AccountSid[BUFFSIZE] ; + static char twilio_AuthToken[BUFFSIZE] ; + static char twilio_From[BUFFSIZE] ; + static char twilio_To[BUFFSIZE] ; + static char twilio_Url[BUFFSIZE] ; + + memset(recvBuff, '\0', sizeof(recvBuff)) ; + ret = http.get(KEYS, recvBuff, sizeof(recvBuff)); + if (ret) { + printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + return ; + } + + sscanf(recvBuff, "%50s\n%50s\n%50s\n%50s\n%50s\n%50s", dummy1, dummy2, + twilio_AccountSid, twilio_AuthToken, twilio_From, twilio_Url) ; + printf("id=%s\nToken=%s\nFrom=%s\nUrl=%s\n\n", + twilio_AccountSid, twilio_AuthToken, twilio_From, twilio_Url) ; + + printf("Phone Number:") ; + for(i=0; i<BUFFSIZE; i++) { + if((twilio_To[i] = getchar()) == '\r') { + twilio_To[i] = '\0' ; + putchar('\n') ; + break ; + } else putchar(twilio_To[i]) ; + } + + twilio_call(twilio_AccountSid, twilio_AuthToken, twilio_From, twilio_To, twilio_Url) ; + + while (true) { + wait(10.0); + } +} + +int main() { + int ret ; + void *av ; + + ret = eth.init(); //Use DHCP + printf("Twilio Client Starting,...\n") ; + + while(1) { + ret = eth.connect(); + if(ret == 0)break ; + wait(0.1); + } + printf("IP = %s\n", eth.getIPAddress()); + + #define STACK_SIZE 20000 + Thread t( thread_main, NULL, osPriorityNormal, STACK_SIZE); + + while(1) + wait(10.0) ; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon Dec 08 12:13:59 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#f1ef95efa5ad
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Dec 08 12:13:59 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/twilio.cpp Mon Dec 08 12:13:59 2014 +0000 @@ -0,0 +1,87 @@ +/* twilio.c + * + * Copyright (C) 2006-2014 wolfSSL Inc. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + + +#include "mbed.h" +#include "EthernetInterface.h" +#include "HTTPClient.h" + +HTTPClient http; + +static char str[1500] ; + +void twilio_call(char *twilio_AccountSid, + char *twilio_AuthToken, + char *twilio_From, + char *twilio_To, + char *twilio_Url) +{ + int ret ; + +#define ALLOWANCE 100 +#define SAFIX 10 + + static const char twilio_api[] = "https://api.twilio.com/2010-04-01/Accounts/%s/%s.%s" ; + static const char twilio_Method[] = "GET" ; + static const char twilio_FBMethod[] = "GET" ; + static const char twilio_StatusCBM[]= "GET" ; + static const char twilio_Record[] = "false" ; + + static char twilio_request[] = "Calls" ; + static char twilio_twiML[] = "xml" ; + + static char uri[ + sizeof(twilio_AccountSid) + +sizeof(twilio_AuthToken) + +sizeof(twilio_api) + +SAFIX+ALLOWANCE] ; + static char HeaderLines[] = + "User-Agent: CyaSSL-Twilio,1.0\n" + "Host: api.twilio.com\n" + "Accept: */*\n" ; + + //POST data + HTTPMap params; + HTTPText inText(str, 1500); + + params.put("From", twilio_From); + params.put("To", twilio_To); + params.put("Url", twilio_Url); + params.put("Method", twilio_Method); + params.put("FaulbackMethod", twilio_FBMethod); + params.put("StatusCallbackMethod", twilio_StatusCBM); + params.put("Record", twilio_Record); + + printf("\nTrying to call %s\n", twilio_To); + sprintf(uri, twilio_api, twilio_AccountSid, twilio_request, twilio_twiML) ; + + http.basicAuth(twilio_AccountSid, twilio_AuthToken); + http.setHeader(HeaderLines) ; + http.setSSLversion(1) ; /* TLSv1.0 */ + + ret = http.post(uri, params, &inText); + if (!ret) { + printf("Executed POST successfully - read %d characters\n", strlen(str)); + printf("Result: %s\n", str); + } else { + printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + } +}