MultiTech
/
Dragonfly_Cellular_HTTPS_Example
Does HTTPS GET and POST requests using the onboard cellular radio.
main.cpp@0:6ce1803b7a03, 2015-09-30 (annotated)
- Committer:
- mfiore
- Date:
- Wed Sep 30 19:08:10 2015 +0000
- Revision:
- 0:6ce1803b7a03
- Child:
- 1:26b8af61d0ac
initial commit - just set APN
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:6ce1803b7a03 | 1 | /** Dragonfly Cellular HTTPS Example |
mfiore | 0:6ce1803b7a03 | 2 | * Configures the cellular radio, brings up the cellular link, and does HTTPS GET and POST requests. |
mfiore | 0:6ce1803b7a03 | 3 | * 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. |
mfiore | 0:6ce1803b7a03 | 4 | * |
mfiore | 0:6ce1803b7a03 | 5 | * NOTE: This example changes the baud rate of the debug port to 115200 baud! |
mfiore | 0:6ce1803b7a03 | 6 | */ |
mfiore | 0:6ce1803b7a03 | 7 | |
mfiore | 0:6ce1803b7a03 | 8 | #include "mbed.h" |
mfiore | 0:6ce1803b7a03 | 9 | #include "mtsas.h" |
mfiore | 0:6ce1803b7a03 | 10 | #include "ssl_certificates.h" |
mfiore | 0:6ce1803b7a03 | 11 | |
mfiore | 0:6ce1803b7a03 | 12 | bool init_mtsas(); |
mfiore | 0:6ce1803b7a03 | 13 | char* httpResToStr(HTTPResult res); |
mfiore | 0:6ce1803b7a03 | 14 | |
mfiore | 0:6ce1803b7a03 | 15 | // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio. |
mfiore | 0:6ce1803b7a03 | 16 | mts::MTSSerialFlowControl* io; |
mfiore | 0:6ce1803b7a03 | 17 | // The Cellular object represents the cellular radio. |
mfiore | 0:6ce1803b7a03 | 18 | mts::Cellular* radio; |
mfiore | 0:6ce1803b7a03 | 19 | |
mfiore | 0:6ce1803b7a03 | 20 | // An APN is required for GSM radios. |
mfiore | 0:6ce1803b7a03 | 21 | static const char apn[] = ""; |
mfiore | 0:6ce1803b7a03 | 22 | |
mfiore | 0:6ce1803b7a03 | 23 | bool radio_ok = false; |
mfiore | 0:6ce1803b7a03 | 24 | |
mfiore | 0:6ce1803b7a03 | 25 | int main() { |
mfiore | 0:6ce1803b7a03 | 26 | // Change the baud rate of the debug port from the default 9600 to 115200. |
mfiore | 0:6ce1803b7a03 | 27 | Serial debug(USBTX, USBRX); |
mfiore | 0:6ce1803b7a03 | 28 | debug.baud(115200); |
mfiore | 0:6ce1803b7a03 | 29 | |
mfiore | 0:6ce1803b7a03 | 30 | //Sets the log level to INFO, higher log levels produce more log output. |
mfiore | 0:6ce1803b7a03 | 31 | //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE |
mfiore | 0:6ce1803b7a03 | 32 | mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL); |
mfiore | 0:6ce1803b7a03 | 33 | |
mfiore | 0:6ce1803b7a03 | 34 | logInfo("initializing cellular radio"); |
mfiore | 0:6ce1803b7a03 | 35 | radio_ok = init_mtsas(); |
mfiore | 0:6ce1803b7a03 | 36 | if (! radio_ok) { |
mfiore | 0:6ce1803b7a03 | 37 | while (true) { |
mfiore | 0:6ce1803b7a03 | 38 | logError("failed to initialize cellular radio"); |
mfiore | 0:6ce1803b7a03 | 39 | wait(1); |
mfiore | 0:6ce1803b7a03 | 40 | } |
mfiore | 0:6ce1803b7a03 | 41 | } |
mfiore | 0:6ce1803b7a03 | 42 | |
mfiore | 0:6ce1803b7a03 | 43 | logInfo("setting APN"); |
mfiore | 0:6ce1803b7a03 | 44 | if (radio->setApn(apn) != MTS_SUCCESS) |
mfiore | 0:6ce1803b7a03 | 45 | logError("failed to set APN to \"%s\"", apn); |
mfiore | 0:6ce1803b7a03 | 46 | |
mfiore | 0:6ce1803b7a03 | 47 | logInfo("bringing up the link"); |
mfiore | 0:6ce1803b7a03 | 48 | if (! radio->connect()) { |
mfiore | 0:6ce1803b7a03 | 49 | logError("failed to bring up the link"); |
mfiore | 0:6ce1803b7a03 | 50 | } else { |
mfiore | 0:6ce1803b7a03 | 51 | |
mfiore | 0:6ce1803b7a03 | 52 | // HTTPClient object used for HTTP requests. |
mfiore | 0:6ce1803b7a03 | 53 | HTTPClient http; |
mfiore | 0:6ce1803b7a03 | 54 | |
mfiore | 0:6ce1803b7a03 | 55 | // Enable strict certificate validation. |
mfiore | 0:6ce1803b7a03 | 56 | http.setPeerVerification(VERIFY_PEER); |
mfiore | 0:6ce1803b7a03 | 57 | |
mfiore | 0:6ce1803b7a03 | 58 | // Load certificates defined in ssl_certificates.h. |
mfiore | 0:6ce1803b7a03 | 59 | // See comments in ssl_certificates.h for information on how to get and format root certificates. |
mfiore | 0:6ce1803b7a03 | 60 | if (http.addRootCACertificate(ssl_certificates) != HTTP_OK) |
mfiore | 0:6ce1803b7a03 | 61 | logError("loading SSL certificates failed"); |
mfiore | 0:6ce1803b7a03 | 62 | |
mfiore | 0:6ce1803b7a03 | 63 | // HTTP GET example - httpbin.org |
mfiore | 0:6ce1803b7a03 | 64 | { |
mfiore | 0:6ce1803b7a03 | 65 | char http_rx_buf[1024]; |
mfiore | 0:6ce1803b7a03 | 66 | HTTPResult res; |
mfiore | 0:6ce1803b7a03 | 67 | |
mfiore | 0:6ce1803b7a03 | 68 | // IHTTPDataIn object - will contain data received from server. |
mfiore | 0:6ce1803b7a03 | 69 | HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); |
mfiore | 0:6ce1803b7a03 | 70 | |
mfiore | 0:6ce1803b7a03 | 71 | // Make a HTTP GET request to http://httpbin.org/ |
mfiore | 0:6ce1803b7a03 | 72 | res = http.get("https://httpbin.org/get", &http_rx); |
mfiore | 0:6ce1803b7a03 | 73 | if (res != HTTP_OK) |
mfiore | 0:6ce1803b7a03 | 74 | logError("HTTPS GET failed [%d][%s]", res, httpResToStr(res)); |
mfiore | 0:6ce1803b7a03 | 75 | else |
mfiore | 0:6ce1803b7a03 | 76 | logInfo("HTTPS GET succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); |
mfiore | 0:6ce1803b7a03 | 77 | } |
mfiore | 0:6ce1803b7a03 | 78 | |
mfiore | 0:6ce1803b7a03 | 79 | // HTTP POST example - httpbin.org |
mfiore | 0:6ce1803b7a03 | 80 | { |
mfiore | 0:6ce1803b7a03 | 81 | char http_rx_buf[1024]; |
mfiore | 0:6ce1803b7a03 | 82 | HTTPResult res; |
mfiore | 0:6ce1803b7a03 | 83 | |
mfiore | 0:6ce1803b7a03 | 84 | char http_tx_buf[] = "{ \"name\": \"temp_1\", \"temperature\": 75 }"; |
mfiore | 0:6ce1803b7a03 | 85 | |
mfiore | 0:6ce1803b7a03 | 86 | // IHTTPDataIn object - will contain data received from server. |
mfiore | 0:6ce1803b7a03 | 87 | HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); |
mfiore | 0:6ce1803b7a03 | 88 | |
mfiore | 0:6ce1803b7a03 | 89 | // IHTTPDataOut object - contains data to be posted to server. |
mfiore | 0:6ce1803b7a03 | 90 | // HTTPJson automatically adds the JSON content-type header to the request. |
mfiore | 0:6ce1803b7a03 | 91 | HTTPJson http_tx(http_tx_buf, sizeof(http_tx_buf)); |
mfiore | 0:6ce1803b7a03 | 92 | |
mfiore | 0:6ce1803b7a03 | 93 | // Make a HTTP POST request to http://httpbin.org/ |
mfiore | 0:6ce1803b7a03 | 94 | res = http.post("https://httpbin.org/post", http_tx, &http_rx); |
mfiore | 0:6ce1803b7a03 | 95 | if (res != HTTP_OK) |
mfiore | 0:6ce1803b7a03 | 96 | logError("HTTPS POST failed [%d][%s]", res, httpResToStr(res)); |
mfiore | 0:6ce1803b7a03 | 97 | else |
mfiore | 0:6ce1803b7a03 | 98 | logInfo("HTTPS POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); |
mfiore | 0:6ce1803b7a03 | 99 | } |
mfiore | 0:6ce1803b7a03 | 100 | } |
mfiore | 0:6ce1803b7a03 | 101 | |
mfiore | 0:6ce1803b7a03 | 102 | logInfo("finished - bringing down link"); |
mfiore | 0:6ce1803b7a03 | 103 | radio->disconnect(); |
mfiore | 0:6ce1803b7a03 | 104 | |
mfiore | 0:6ce1803b7a03 | 105 | return 0; |
mfiore | 0:6ce1803b7a03 | 106 | } |
mfiore | 0:6ce1803b7a03 | 107 | |
mfiore | 0:6ce1803b7a03 | 108 | bool init_mtsas() { |
mfiore | 0:6ce1803b7a03 | 109 | io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); |
mfiore | 0:6ce1803b7a03 | 110 | if (! io) |
mfiore | 0:6ce1803b7a03 | 111 | return false; |
mfiore | 0:6ce1803b7a03 | 112 | |
mfiore | 0:6ce1803b7a03 | 113 | // radio default baud rate is 115200 |
mfiore | 0:6ce1803b7a03 | 114 | io->baud(115200); |
mfiore | 0:6ce1803b7a03 | 115 | radio = mts::CellularFactory::create(io); |
mfiore | 0:6ce1803b7a03 | 116 | if (! radio) |
mfiore | 0:6ce1803b7a03 | 117 | return false; |
mfiore | 0:6ce1803b7a03 | 118 | |
mfiore | 0:6ce1803b7a03 | 119 | // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created |
mfiore | 0:6ce1803b7a03 | 120 | Transport::setTransport(radio); |
mfiore | 0:6ce1803b7a03 | 121 | |
mfiore | 0:6ce1803b7a03 | 122 | return true; |
mfiore | 0:6ce1803b7a03 | 123 | } |
mfiore | 0:6ce1803b7a03 | 124 | |
mfiore | 0:6ce1803b7a03 | 125 | char* httpResToStr(HTTPResult res) { |
mfiore | 0:6ce1803b7a03 | 126 | switch(res) { |
mfiore | 0:6ce1803b7a03 | 127 | case HTTP_PROCESSING: |
mfiore | 0:6ce1803b7a03 | 128 | return "HTTP_PROCESSING"; |
mfiore | 0:6ce1803b7a03 | 129 | case HTTP_PARSE: |
mfiore | 0:6ce1803b7a03 | 130 | return "HTTP_PARSE"; |
mfiore | 0:6ce1803b7a03 | 131 | case HTTP_DNS: |
mfiore | 0:6ce1803b7a03 | 132 | return "HTTP_DNS"; |
mfiore | 0:6ce1803b7a03 | 133 | case HTTP_PRTCL: |
mfiore | 0:6ce1803b7a03 | 134 | return "HTTP_PRTCL"; |
mfiore | 0:6ce1803b7a03 | 135 | case HTTP_NOTFOUND: |
mfiore | 0:6ce1803b7a03 | 136 | return "HTTP_NOTFOUND"; |
mfiore | 0:6ce1803b7a03 | 137 | case HTTP_REFUSED: |
mfiore | 0:6ce1803b7a03 | 138 | return "HTTP_REFUSED"; |
mfiore | 0:6ce1803b7a03 | 139 | case HTTP_ERROR: |
mfiore | 0:6ce1803b7a03 | 140 | return "HTTP_ERROR"; |
mfiore | 0:6ce1803b7a03 | 141 | case HTTP_TIMEOUT: |
mfiore | 0:6ce1803b7a03 | 142 | return "HTTP_TIMEOUT"; |
mfiore | 0:6ce1803b7a03 | 143 | case HTTP_CONN: |
mfiore | 0:6ce1803b7a03 | 144 | return "HTTP_CONN"; |
mfiore | 0:6ce1803b7a03 | 145 | case HTTP_CLOSED: |
mfiore | 0:6ce1803b7a03 | 146 | return "HTTP_CLOSED"; |
mfiore | 0:6ce1803b7a03 | 147 | case HTTP_REDIRECT: |
mfiore | 0:6ce1803b7a03 | 148 | return "HTTP_REDIRECT"; |
mfiore | 0:6ce1803b7a03 | 149 | case HTTP_OK: |
mfiore | 0:6ce1803b7a03 | 150 | return "HTTP_OK"; |
mfiore | 0:6ce1803b7a03 | 151 | default: |
mfiore | 0:6ce1803b7a03 | 152 | return "HTTP Result unknown"; |
mfiore | 0:6ce1803b7a03 | 153 | } |
mfiore | 0:6ce1803b7a03 | 154 | } |