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