MultiTech
/
Dragonfly_Cellular_HTTP_Example
Does HTTP GET and POST requests using the onboard cellular radio.
main.cpp
- Committer:
- mfiore
- Date:
- 2016-02-26
- Revision:
- 1:85048c56310d
- Parent:
- 0:093d58465743
File content as of revision 1:85048c56310d:
/** Dragonfly Cellular HTTP Example * Configures the cellular radio, brings up the cellular link, and does HTTP GET and POST requests. * * NOTE: This example changes the baud rate of the debug port to 115200 baud! */ #include "mbed.h" #include "mtsas.h" // This line controls the regulator's battery charger. // BC_NCE = 0 enables the battery charger // BC_NCE = 1 disables the battery charger DigitalOut bc_nce(PB_2); bool init_mtsas(); char* httpResToStr(HTTPResult res); // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio. mts::MTSSerialFlowControl* io; // The Cellular object represents the cellular radio. mts::Cellular* radio; // An APN is required for GSM radios. static const char apn[] = ""; bool radio_ok = false; int main() { // Disable the battery charger unless a battery is attached. bc_nce = 1; // Change the baud rate of the debug port from the default 9600 to 115200. Serial debug(USBTX, USBRX); debug.baud(115200); //Sets the log level to INFO, higher log levels produce more log output. //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL); logInfo("initializing cellular radio"); radio_ok = init_mtsas(); if (! radio_ok) { while (true) { logError("failed to initialize cellular radio"); wait(1); } } logInfo("setting APN"); if (radio->setApn(apn) != MTS_SUCCESS) logError("failed to set APN to \"%s\"", apn); logInfo("bringing up the link"); if (! radio->connect()) { logError("failed to bring up the link"); } else { // HTTPClient object used for HTTP requests. HTTPClient http; // HTTP GET example. { char http_rx_buf[1024]; HTTPResult res; // IHTTPDataIn object - will contain data received from server. HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); // Make a HTTP GET request to http://httpbin.org/ res = http.get("http://httpbin.org/get", &http_rx); if (res != HTTP_OK) logError("HTTP GET failed [%d][%s]", res, httpResToStr(res)); else logInfo("HTTP GET succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); } // HTTP POST example. { char http_rx_buf[1024]; HTTPResult res; char http_tx_buf[] = "{ \"name\": \"temp_1\", \"temperature\": 75 }"; // IHTTPDataIn object - will contain data received from server. HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); // IHTTPDataOut object - contains data to be posted to server. // HTTPJson automatically adds the JSON content-type header to the request. HTTPJson http_tx(http_tx_buf, sizeof(http_tx_buf)); // Make a HTTP POST request to http://httpbin.org/ res = http.post("http://httpbin.org/post", http_tx, &http_rx); if (res != HTTP_OK) logError("HTTP POST failed [%d][%s]", res, httpResToStr(res)); else logInfo("HTTP POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); } } logInfo("finished - bringing down link"); radio->disconnect(); return 0; } bool init_mtsas() { io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); if (! io) return false; // radio default baud rate is 115200 io->baud(115200); radio = mts::CellularFactory::create(io); if (! radio) return false; // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created Transport::setTransport(radio); return true; } char* httpResToStr(HTTPResult res) { switch(res) { case HTTP_PROCESSING: return "HTTP_PROCESSING"; case HTTP_PARSE: return "HTTP_PARSE"; case HTTP_DNS: return "HTTP_DNS"; case HTTP_PRTCL: return "HTTP_PRTCL"; case HTTP_NOTFOUND: return "HTTP_NOTFOUND"; case HTTP_REFUSED: return "HTTP_REFUSED"; case HTTP_ERROR: return "HTTP_ERROR"; case HTTP_TIMEOUT: return "HTTP_TIMEOUT"; case HTTP_CONN: return "HTTP_CONN"; case HTTP_CLOSED: return "HTTP_CLOSED"; case HTTP_REDIRECT: return "HTTP_REDIRECT"; case HTTP_OK: return "HTTP_OK"; default: return "HTTP Result unknown"; } }