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