Initial version, CyaSSL Twilio example

Dependencies:   HTTPClient EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "HTTPClient.h"
00004 
00005 EthernetInterface eth;
00006 HTTPClient http;
00007 
00008 static char str[1500] ;
00009 
00010 void twilio_main(void *av)
00011 {
00012     int ret ;
00013 
00014 #define ALLOWANCE 100
00015 #define SAFIX     10
00016 
00017     static char twilio_AccountSid[] = "TWILIO_ACCOUNT_SID" ;
00018     static char twilio_AuthToken[]  = "AUTH_TOKEN" ;
00019 
00020     static const char twilio_From[] = "PHONE_NUMBER_FROM" ;    
00021     static const char twilio_To[]   = "PHONE_NUMBER_TO" ;
00022     static const char twilio_Url[] = "http://twimlbin.com/TWIML_PATH" ;
00023     
00024     static const char twilio_api[] = "https://api.twilio.com/2010-04-01/Accounts/%s/%s.%s" ;
00025     static const char twilio_Method[]   = "GET" ;
00026     static const char twilio_FBMethod[] = "GET" ;
00027     static const char twilio_StatusCBM[]= "GET" ;
00028     static const char twilio_Record[]   = "false" ;    
00029 
00030     static char twilio_request[] = "Calls" ;
00031     static char twilio_twiML[] = "xml" ;
00032 
00033     static char uri[ 
00034         sizeof(twilio_AccountSid)
00035         +sizeof(twilio_AuthToken)
00036         +sizeof(twilio_api)
00037         +SAFIX+ALLOWANCE] ;
00038     static char HeaderLines[] =
00039         "User-Agent: curl/7.33.0\n"
00040         "Host: api.twilio.com\n"
00041         "Accept: */*\n" ;
00042 
00043     //POST data
00044     HTTPMap params;
00045     HTTPText inText(str, 1500);
00046 
00047     params.put("From", twilio_From);
00048     params.put("To", twilio_To);
00049     params.put("Url", twilio_Url);
00050     params.put("Method", twilio_Method);    
00051     params.put("FaulbackMethod", twilio_FBMethod);
00052     params.put("StatusCallbackMethod", twilio_StatusCBM);
00053     params.put("Record", twilio_Record);
00054     
00055     printf("\nTrying to call %s\n", twilio_To);
00056     sprintf(uri, twilio_api, twilio_AccountSid, twilio_request, twilio_twiML) ;
00057     printf("uri=%s\n", uri) ;
00058 
00059     http.basicAuth(twilio_AccountSid, twilio_AuthToken);
00060     http.setHeader(HeaderLines) ;
00061     http.setSSLversion(1) ; /* TLSv1.0 */
00062     
00063     ret = http.post(uri, params, &inText);
00064     if (!ret) {
00065         printf("Executed POST successfully - read %d characters\n", strlen(str));
00066         printf("Result: %s\n", str);
00067     } else {
00068         printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00069     }
00070     eth.disconnect();
00071 
00072 }
00073 
00074 int main() {  
00075     int ret ;
00076     int av ;
00077     
00078     EthernetInterface eth;
00079 
00080     printf("Twilio Client Starting,...\n") ;
00081 
00082     ret = eth.init(); //Use DHCP
00083     while(1) {
00084         ret = eth.connect();
00085         if(ret == 0)break ;
00086         Thread::wait(100);
00087     }
00088     printf("IP = %s\n", eth.getIPAddress());
00089     
00090     //#define BOARD_FRDM_K64F
00091     #ifdef BOARD_FRDM_K64F
00092     #define STACK_SIZE 10000
00093     Thread t(twilio_main, NULL, osPriorityNormal, STACK_SIZE);
00094     #else
00095     twilio_main(&av) ;
00096     #endif
00097     
00098     while (true) {
00099         Thread::wait(1000);
00100     } 
00101 }