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

Dependencies:   mbed mtsas

Committer:
mfiore
Date:
Wed Sep 30 17:45:15 2015 +0000
Revision:
0:093d58465743
Child:
1:85048c56310d
Initial commit - just add APN

Who changed what in which revision?

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