MultiTech
/
Dragonfly_Cellular_HTTP_Example
Does HTTP GET and POST requests using the onboard cellular radio.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /** Dragonfly Cellular HTTP Example 00002 * Configures the cellular radio, brings up the cellular link, and does HTTP GET and POST requests. 00003 * 00004 * NOTE: This example changes the baud rate of the debug port to 115200 baud! 00005 */ 00006 00007 #include "mbed.h" 00008 #include "mtsas.h" 00009 00010 // This line controls the regulator's battery charger. 00011 // BC_NCE = 0 enables the battery charger 00012 // BC_NCE = 1 disables the battery charger 00013 DigitalOut bc_nce(PB_2); 00014 00015 bool init_mtsas(); 00016 char* httpResToStr(HTTPResult res); 00017 00018 // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio. 00019 mts::MTSSerialFlowControl* io; 00020 // The Cellular object represents the cellular radio. 00021 mts::Cellular* radio; 00022 00023 // An APN is required for GSM radios. 00024 static const char apn[] = ""; 00025 00026 bool radio_ok = false; 00027 00028 int main() { 00029 // Disable the battery charger unless a battery is attached. 00030 bc_nce = 1; 00031 00032 // Change the baud rate of the debug port from the default 9600 to 115200. 00033 Serial debug(USBTX, USBRX); 00034 debug.baud(115200); 00035 00036 //Sets the log level to INFO, higher log levels produce more log output. 00037 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE 00038 mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL); 00039 00040 logInfo("initializing cellular radio"); 00041 radio_ok = init_mtsas(); 00042 if (! radio_ok) { 00043 while (true) { 00044 logError("failed to initialize cellular radio"); 00045 wait(1); 00046 } 00047 } 00048 00049 logInfo("setting APN"); 00050 if (radio->setApn(apn) != MTS_SUCCESS) 00051 logError("failed to set APN to \"%s\"", apn); 00052 00053 logInfo("bringing up the link"); 00054 if (! radio->connect()) { 00055 logError("failed to bring up the link"); 00056 } else { 00057 00058 // HTTPClient object used for HTTP requests. 00059 HTTPClient http; 00060 00061 // HTTP GET example. 00062 { 00063 char http_rx_buf[1024]; 00064 HTTPResult res; 00065 00066 // IHTTPDataIn object - will contain data received from server. 00067 HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); 00068 00069 // Make a HTTP GET request to http://httpbin.org/ 00070 res = http.get("http://httpbin.org/get", &http_rx); 00071 if (res != HTTP_OK) 00072 logError("HTTP GET failed [%d][%s]", res, httpResToStr(res)); 00073 else 00074 logInfo("HTTP GET succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); 00075 } 00076 00077 // HTTP POST example. 00078 { 00079 char http_rx_buf[1024]; 00080 HTTPResult res; 00081 00082 char http_tx_buf[] = "{ \"name\": \"temp_1\", \"temperature\": 75 }"; 00083 00084 // IHTTPDataIn object - will contain data received from server. 00085 HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); 00086 00087 // IHTTPDataOut object - contains data to be posted to server. 00088 // HTTPJson automatically adds the JSON content-type header to the request. 00089 HTTPJson http_tx(http_tx_buf, sizeof(http_tx_buf)); 00090 00091 // Make a HTTP POST request to http://httpbin.org/ 00092 res = http.post("http://httpbin.org/post", http_tx, &http_rx); 00093 if (res != HTTP_OK) 00094 logError("HTTP POST failed [%d][%s]", res, httpResToStr(res)); 00095 else 00096 logInfo("HTTP POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); 00097 } 00098 } 00099 00100 logInfo("finished - bringing down link"); 00101 radio->disconnect(); 00102 00103 return 0; 00104 } 00105 00106 bool init_mtsas() { 00107 io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); 00108 if (! io) 00109 return false; 00110 00111 // radio default baud rate is 115200 00112 io->baud(115200); 00113 radio = mts::CellularFactory::create(io); 00114 if (! radio) 00115 return false; 00116 00117 // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created 00118 Transport::setTransport(radio); 00119 00120 return true; 00121 } 00122 00123 char* httpResToStr(HTTPResult res) { 00124 switch(res) { 00125 case HTTP_PROCESSING: 00126 return "HTTP_PROCESSING"; 00127 case HTTP_PARSE: 00128 return "HTTP_PARSE"; 00129 case HTTP_DNS: 00130 return "HTTP_DNS"; 00131 case HTTP_PRTCL: 00132 return "HTTP_PRTCL"; 00133 case HTTP_NOTFOUND: 00134 return "HTTP_NOTFOUND"; 00135 case HTTP_REFUSED: 00136 return "HTTP_REFUSED"; 00137 case HTTP_ERROR: 00138 return "HTTP_ERROR"; 00139 case HTTP_TIMEOUT: 00140 return "HTTP_TIMEOUT"; 00141 case HTTP_CONN: 00142 return "HTTP_CONN"; 00143 case HTTP_CLOSED: 00144 return "HTTP_CLOSED"; 00145 case HTTP_REDIRECT: 00146 return "HTTP_REDIRECT"; 00147 case HTTP_OK: 00148 return "HTTP_OK"; 00149 default: 00150 return "HTTP Result unknown"; 00151 } 00152 }
Generated on Fri Jul 15 2022 05:17:21 by 1.7.2