Initial version, CyaSSL Twilio example

Dependencies:   HTTPClient EthernetInterface mbed-rtos mbed

This simple Twilio example works with HTTPS based Twilio API from mbed, makes a call to your phone and talks text speech message.

For using this example:

- Prepare mbed with Ethernet connection.

- Go to http://twilio.com, and sign up.

- Copy Twilio Sid, Token, phone number and TwiML URL in twilio.cpp.

static char twilio_AccountSid[] = "TWILIO_ACCOUNT_SID" ;
static char twilio_AuthToken[]  = "AUTH_TOKEN" ;
static const char twilio_From[] = "PHONE_NUMBER_FROM" ;
static const char twilio_To[] = "PHONE_NUMBER_TO" ;
static const char twilio_Url[]  = "http://twimlbin.com/TWIML_PATH" ;

- The demo makes a call from Twilio to your phone. Set the phone numbers onto twilio_From and twilio_To.

- Set up a TwiML web page defining Twilio action, set the url to twilio_Url.

TwiML first step example:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Say language="ja-JP">もしもし。聞こえますか?こちらツイリオです</Say>
</Response>

- Build, download it to mbed, and you will get a call on your phone.

Have fun!

For FRDM-k64f: http://mbed.org/users/wolfSSL/code/CyaSSL-Twilio-FRDM/

日本語:http://mbed.org/users/wolfSSL/code/CyaSSL-Twilio/wiki/CyaSSL-Twilio-jp

Committer:
wolfSSL
Date:
Sat Jul 12 07:50:22 2014 +0000
Revision:
0:bd0e8172f67f
Initial commit, CyaSSL example for Twilio

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:bd0e8172f67f 1 #include "mbed.h"
wolfSSL 0:bd0e8172f67f 2 #include "EthernetInterface.h"
wolfSSL 0:bd0e8172f67f 3 #include "HTTPClient.h"
wolfSSL 0:bd0e8172f67f 4
wolfSSL 0:bd0e8172f67f 5 EthernetInterface eth;
wolfSSL 0:bd0e8172f67f 6 HTTPClient http;
wolfSSL 0:bd0e8172f67f 7
wolfSSL 0:bd0e8172f67f 8 static char str[1500] ;
wolfSSL 0:bd0e8172f67f 9
wolfSSL 0:bd0e8172f67f 10 void twilio_main(void *av)
wolfSSL 0:bd0e8172f67f 11 {
wolfSSL 0:bd0e8172f67f 12 int ret ;
wolfSSL 0:bd0e8172f67f 13
wolfSSL 0:bd0e8172f67f 14 #define ALLOWANCE 100
wolfSSL 0:bd0e8172f67f 15 #define SAFIX 10
wolfSSL 0:bd0e8172f67f 16
wolfSSL 0:bd0e8172f67f 17 static char twilio_AccountSid[] = "TWILIO_ACCOUNT_SID" ;
wolfSSL 0:bd0e8172f67f 18 static char twilio_AuthToken[] = "AUTH_TOKEN" ;
wolfSSL 0:bd0e8172f67f 19
wolfSSL 0:bd0e8172f67f 20 static const char twilio_From[] = "PHONE_NUMBER_FROM" ;
wolfSSL 0:bd0e8172f67f 21 static const char twilio_To[] = "PHONE_NUMBER_TO" ;
wolfSSL 0:bd0e8172f67f 22 static const char twilio_Url[] = "http://twimlbin.com/TWIML_PATH" ;
wolfSSL 0:bd0e8172f67f 23
wolfSSL 0:bd0e8172f67f 24 static const char twilio_api[] = "https://api.twilio.com/2010-04-01/Accounts/%s/%s.%s" ;
wolfSSL 0:bd0e8172f67f 25 static const char twilio_Method[] = "GET" ;
wolfSSL 0:bd0e8172f67f 26 static const char twilio_FBMethod[] = "GET" ;
wolfSSL 0:bd0e8172f67f 27 static const char twilio_StatusCBM[]= "GET" ;
wolfSSL 0:bd0e8172f67f 28 static const char twilio_Record[] = "false" ;
wolfSSL 0:bd0e8172f67f 29
wolfSSL 0:bd0e8172f67f 30 static char twilio_request[] = "Calls" ;
wolfSSL 0:bd0e8172f67f 31 static char twilio_twiML[] = "xml" ;
wolfSSL 0:bd0e8172f67f 32
wolfSSL 0:bd0e8172f67f 33 static char uri[
wolfSSL 0:bd0e8172f67f 34 sizeof(twilio_AccountSid)
wolfSSL 0:bd0e8172f67f 35 +sizeof(twilio_AuthToken)
wolfSSL 0:bd0e8172f67f 36 +sizeof(twilio_api)
wolfSSL 0:bd0e8172f67f 37 +SAFIX+ALLOWANCE] ;
wolfSSL 0:bd0e8172f67f 38 static char HeaderLines[] =
wolfSSL 0:bd0e8172f67f 39 "User-Agent: curl/7.33.0\n"
wolfSSL 0:bd0e8172f67f 40 "Host: api.twilio.com\n"
wolfSSL 0:bd0e8172f67f 41 "Accept: */*\n" ;
wolfSSL 0:bd0e8172f67f 42
wolfSSL 0:bd0e8172f67f 43 //POST data
wolfSSL 0:bd0e8172f67f 44 HTTPMap params;
wolfSSL 0:bd0e8172f67f 45 HTTPText inText(str, 1500);
wolfSSL 0:bd0e8172f67f 46
wolfSSL 0:bd0e8172f67f 47 params.put("From", twilio_From);
wolfSSL 0:bd0e8172f67f 48 params.put("To", twilio_To);
wolfSSL 0:bd0e8172f67f 49 params.put("Url", twilio_Url);
wolfSSL 0:bd0e8172f67f 50 params.put("Method", twilio_Method);
wolfSSL 0:bd0e8172f67f 51 params.put("FaulbackMethod", twilio_FBMethod);
wolfSSL 0:bd0e8172f67f 52 params.put("StatusCallbackMethod", twilio_StatusCBM);
wolfSSL 0:bd0e8172f67f 53 params.put("Record", twilio_Record);
wolfSSL 0:bd0e8172f67f 54
wolfSSL 0:bd0e8172f67f 55 printf("\nTrying to call %s\n", twilio_To);
wolfSSL 0:bd0e8172f67f 56 sprintf(uri, twilio_api, twilio_AccountSid, twilio_request, twilio_twiML) ;
wolfSSL 0:bd0e8172f67f 57 printf("uri=%s\n", uri) ;
wolfSSL 0:bd0e8172f67f 58
wolfSSL 0:bd0e8172f67f 59 http.basicAuth(twilio_AccountSid, twilio_AuthToken);
wolfSSL 0:bd0e8172f67f 60 http.setHeader(HeaderLines) ;
wolfSSL 0:bd0e8172f67f 61 http.setSSLversion(1) ; /* TLSv1.0 */
wolfSSL 0:bd0e8172f67f 62
wolfSSL 0:bd0e8172f67f 63 ret = http.post(uri, params, &inText);
wolfSSL 0:bd0e8172f67f 64 if (!ret) {
wolfSSL 0:bd0e8172f67f 65 printf("Executed POST successfully - read %d characters\n", strlen(str));
wolfSSL 0:bd0e8172f67f 66 printf("Result: %s\n", str);
wolfSSL 0:bd0e8172f67f 67 } else {
wolfSSL 0:bd0e8172f67f 68 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
wolfSSL 0:bd0e8172f67f 69 }
wolfSSL 0:bd0e8172f67f 70 eth.disconnect();
wolfSSL 0:bd0e8172f67f 71
wolfSSL 0:bd0e8172f67f 72 }
wolfSSL 0:bd0e8172f67f 73
wolfSSL 0:bd0e8172f67f 74 int main() {
wolfSSL 0:bd0e8172f67f 75 int ret ;
wolfSSL 0:bd0e8172f67f 76 int av ;
wolfSSL 0:bd0e8172f67f 77
wolfSSL 0:bd0e8172f67f 78 EthernetInterface eth;
wolfSSL 0:bd0e8172f67f 79
wolfSSL 0:bd0e8172f67f 80 printf("Twilio Client Starting,...\n") ;
wolfSSL 0:bd0e8172f67f 81
wolfSSL 0:bd0e8172f67f 82 ret = eth.init(); //Use DHCP
wolfSSL 0:bd0e8172f67f 83 while(1) {
wolfSSL 0:bd0e8172f67f 84 ret = eth.connect();
wolfSSL 0:bd0e8172f67f 85 if(ret == 0)break ;
wolfSSL 0:bd0e8172f67f 86 Thread::wait(100);
wolfSSL 0:bd0e8172f67f 87 }
wolfSSL 0:bd0e8172f67f 88 printf("IP = %s\n", eth.getIPAddress());
wolfSSL 0:bd0e8172f67f 89
wolfSSL 0:bd0e8172f67f 90 //#define BOARD_FRDM_K64F
wolfSSL 0:bd0e8172f67f 91 #ifdef BOARD_FRDM_K64F
wolfSSL 0:bd0e8172f67f 92 #define STACK_SIZE 10000
wolfSSL 0:bd0e8172f67f 93 Thread t(twilio_main, NULL, osPriorityNormal, STACK_SIZE);
wolfSSL 0:bd0e8172f67f 94 #else
wolfSSL 0:bd0e8172f67f 95 twilio_main(&av) ;
wolfSSL 0:bd0e8172f67f 96 #endif
wolfSSL 0:bd0e8172f67f 97
wolfSSL 0:bd0e8172f67f 98 while (true) {
wolfSSL 0:bd0e8172f67f 99 Thread::wait(1000);
wolfSSL 0:bd0e8172f67f 100 }
wolfSSL 0:bd0e8172f67f 101 }