Does HTTPS GET and POST requests using the onboard cellular radio.

Dependencies:   mbed mtsas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /** Dragonfly Cellular HTTPS Example
00002  * Configures the cellular radio, brings up the cellular link, and does HTTPS GET and POST requests.
00003  * To do HTTPS requests with a certain server, the root certificate used to validate that server's certificate must be installed. See ssl_certificates.h for information on how to get the proper root certificate.
00004  *
00005  * NOTE: This example changes the baud rate of the debug port to 115200 baud!
00006  */
00007 
00008 #include "mbed.h"
00009 #include "mtsas.h"
00010 #include "ssl_certificates.h"
00011 
00012 // This line controls the regulator's battery charger.
00013 // BC_NCE = 0 enables the battery charger
00014 // BC_NCE = 1 disables the battery charger
00015 DigitalOut bc_nce(PB_2);
00016 
00017 bool init_mtsas();
00018 char* httpResToStr(HTTPResult res);
00019 
00020 // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio.
00021 mts::MTSSerialFlowControl* io;
00022 // The Cellular object represents the cellular radio.
00023 mts::Cellular* radio;
00024 
00025 // An APN is required for GSM radios.
00026 static const char apn[] = "";
00027 
00028 bool radio_ok = false;
00029 
00030 int main() {
00031     // Disable the battery charger unless a battery is attached.
00032     bc_nce = 1;
00033     
00034     // Change the baud rate of the debug port from the default 9600 to 115200.
00035     Serial debug(USBTX, USBRX);
00036     debug.baud(115200);
00037     
00038     //Sets the log level to INFO, higher log levels produce more log output.
00039     //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
00040     mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
00041     
00042     logInfo("initializing cellular radio");
00043     radio_ok = init_mtsas();
00044     if (! radio_ok) {
00045         while (true) {
00046             logError("failed to initialize cellular radio");
00047             wait(1);
00048         }
00049     }
00050     
00051     logInfo("setting APN");
00052     if (radio->setApn(apn) != MTS_SUCCESS)
00053         logError("failed to set APN to \"%s\"", apn);
00054         
00055     logInfo("bringing up the link");
00056     if (! radio->connect()) {
00057         logError("failed to bring up the link");
00058     } else {
00059         
00060         // HTTPClient object used for HTTP requests.
00061         HTTPClient http;
00062         
00063         // Enable strict certificate validation.
00064         http.setPeerVerification(VERIFY_PEER);
00065         
00066         // Load certificates defined in ssl_certificates.h.
00067         // See comments in ssl_certificates.h for information on how to get and format root certificates.
00068         if (http.addRootCACertificate(ssl_certificates) != HTTP_OK)
00069             logError("loading SSL certificates failed");
00070         
00071         // HTTP GET example - httpbin.org
00072         {
00073             char http_rx_buf[1024];
00074             HTTPResult res;
00075             
00076             // IHTTPDataIn object - will contain data received from server.
00077             HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf));
00078             
00079             // Make a HTTP GET request to http://httpbin.org/
00080             res = http.get("https://httpbin.org/get", &http_rx);
00081             if (res != HTTP_OK)
00082                 logError("HTTPS GET failed [%d][%s]", res, httpResToStr(res));
00083             else
00084                 logInfo("HTTPS GET succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf);
00085         }
00086         
00087         // HTTP POST example - httpbin.org
00088         {
00089             char http_rx_buf[1024];
00090             HTTPResult res;
00091             
00092             char http_tx_buf[] = "{ \"name\": \"temp_1\", \"temperature\": 75 }";
00093             
00094             // IHTTPDataIn object - will contain data received from server.
00095             HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf));
00096             
00097             // IHTTPDataOut object - contains data to be posted to server.
00098             // HTTPJson automatically adds the JSON content-type header to the request.
00099             HTTPJson http_tx(http_tx_buf, sizeof(http_tx_buf));
00100             
00101             // Make a HTTP POST request to http://httpbin.org/
00102             res = http.post("https://httpbin.org/post", http_tx, &http_rx);
00103             if (res != HTTP_OK)
00104                 logError("HTTPS POST failed [%d][%s]", res, httpResToStr(res));
00105             else
00106                 logInfo("HTTPS POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf);
00107         }
00108     }
00109     
00110     logInfo("finished - bringing down link");
00111     radio->disconnect();
00112     
00113     return 0;
00114 }
00115 
00116 bool init_mtsas() {
00117     io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
00118     if (! io)
00119         return false;
00120         
00121     // radio default baud rate is 115200
00122     io->baud(115200);
00123     radio = mts::CellularFactory::create(io);
00124     if (! radio)
00125         return false;
00126         
00127     // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created
00128     Transport::setTransport(radio);
00129     
00130     return true;
00131 }
00132 
00133 char* httpResToStr(HTTPResult res) {
00134     switch(res) {
00135         case HTTP_PROCESSING:
00136             return "HTTP_PROCESSING";
00137         case HTTP_PARSE:
00138             return "HTTP_PARSE";
00139         case HTTP_DNS:
00140             return "HTTP_DNS";
00141         case HTTP_PRTCL:
00142             return "HTTP_PRTCL";
00143         case HTTP_NOTFOUND:
00144             return "HTTP_NOTFOUND";
00145         case HTTP_REFUSED:
00146             return "HTTP_REFUSED";
00147         case HTTP_ERROR:
00148             return "HTTP_ERROR";
00149         case HTTP_TIMEOUT:
00150             return "HTTP_TIMEOUT";
00151         case HTTP_CONN:
00152             return "HTTP_CONN";
00153         case HTTP_CLOSED:
00154             return "HTTP_CLOSED";
00155         case HTTP_REDIRECT:
00156             return "HTTP_REDIRECT";
00157         case HTTP_OK:
00158             return "HTTP_OK";
00159         default:
00160             return "HTTP Result unknown";
00161     }
00162 }