No Description
Dependencies: mbed mtsas picojson
Fork of Dragonfly_Cellular_HTTP_Example by
main.cpp@3:c6a13980819b, 2017-08-29 (annotated)
- Committer:
- dabraham
- Date:
- Tue Aug 29 00:23:04 2017 +0000
- Revision:
- 3:c6a13980819b
- Parent:
- 2:e8fe29290fc1
Project Created
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" |
dabraham | 2:e8fe29290fc1 | 9 | #include "picojson.h" |
dabraham | 2:e8fe29290fc1 | 10 | //#include "Cellular.h" |
dabraham | 2:e8fe29290fc1 | 11 | #include "MTSLog.h" |
dabraham | 2:e8fe29290fc1 | 12 | #include <stdio.h> |
dabraham | 2:e8fe29290fc1 | 13 | #include <string> |
dabraham | 2:e8fe29290fc1 | 14 | #include <iostream> |
dabraham | 2:e8fe29290fc1 | 15 | #include <sstream> |
mfiore | 0:093d58465743 | 16 | |
mfiore | 1:85048c56310d | 17 | // This line controls the regulator's battery charger. |
mfiore | 1:85048c56310d | 18 | // BC_NCE = 0 enables the battery charger |
mfiore | 1:85048c56310d | 19 | // BC_NCE = 1 disables the battery charger |
mfiore | 1:85048c56310d | 20 | DigitalOut bc_nce(PB_2); |
mfiore | 1:85048c56310d | 21 | |
mfiore | 0:093d58465743 | 22 | bool init_mtsas(); |
mfiore | 0:093d58465743 | 23 | char* httpResToStr(HTTPResult res); |
dabraham | 2:e8fe29290fc1 | 24 | string id="1"; |
dabraham | 2:e8fe29290fc1 | 25 | int interval; |
mfiore | 0:093d58465743 | 26 | |
mfiore | 0:093d58465743 | 27 | // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio. |
mfiore | 0:093d58465743 | 28 | mts::MTSSerialFlowControl* io; |
mfiore | 0:093d58465743 | 29 | // The Cellular object represents the cellular radio. |
mfiore | 0:093d58465743 | 30 | mts::Cellular* radio; |
mfiore | 0:093d58465743 | 31 | |
mfiore | 0:093d58465743 | 32 | // An APN is required for GSM radios. |
dabraham | 2:e8fe29290fc1 | 33 | static const char apn[] = "rogers-core-appl1.apn"; |
mfiore | 0:093d58465743 | 34 | |
mfiore | 0:093d58465743 | 35 | bool radio_ok = false; |
mfiore | 0:093d58465743 | 36 | |
mfiore | 0:093d58465743 | 37 | int main() { |
dabraham | 2:e8fe29290fc1 | 38 | while(true){ |
mfiore | 1:85048c56310d | 39 | // Disable the battery charger unless a battery is attached. |
mfiore | 1:85048c56310d | 40 | bc_nce = 1; |
mfiore | 1:85048c56310d | 41 | |
mfiore | 0:093d58465743 | 42 | // Change the baud rate of the debug port from the default 9600 to 115200. |
mfiore | 0:093d58465743 | 43 | Serial debug(USBTX, USBRX); |
dabraham | 2:e8fe29290fc1 | 44 | debug.baud(9600); |
mfiore | 0:093d58465743 | 45 | |
mfiore | 0:093d58465743 | 46 | //Sets the log level to INFO, higher log levels produce more log output. |
mfiore | 0:093d58465743 | 47 | //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE |
mfiore | 0:093d58465743 | 48 | mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL); |
mfiore | 0:093d58465743 | 49 | |
mfiore | 0:093d58465743 | 50 | logInfo("initializing cellular radio"); |
mfiore | 0:093d58465743 | 51 | radio_ok = init_mtsas(); |
mfiore | 0:093d58465743 | 52 | if (! radio_ok) { |
mfiore | 0:093d58465743 | 53 | while (true) { |
mfiore | 0:093d58465743 | 54 | logError("failed to initialize cellular radio"); |
mfiore | 0:093d58465743 | 55 | wait(1); |
mfiore | 0:093d58465743 | 56 | } |
mfiore | 0:093d58465743 | 57 | } |
mfiore | 0:093d58465743 | 58 | |
mfiore | 0:093d58465743 | 59 | logInfo("setting APN"); |
mfiore | 0:093d58465743 | 60 | if (radio->setApn(apn) != MTS_SUCCESS) |
mfiore | 0:093d58465743 | 61 | logError("failed to set APN to \"%s\"", apn); |
mfiore | 0:093d58465743 | 62 | |
mfiore | 0:093d58465743 | 63 | logInfo("bringing up the link"); |
mfiore | 0:093d58465743 | 64 | if (! radio->connect()) { |
mfiore | 0:093d58465743 | 65 | logError("failed to bring up the link"); |
mfiore | 0:093d58465743 | 66 | } else { |
dabraham | 2:e8fe29290fc1 | 67 | string imei = radio->getEquipmentIdentifier(); |
dabraham | 2:e8fe29290fc1 | 68 | logInfo("The IMEI is \r\n%s", imei); |
mfiore | 0:093d58465743 | 69 | // HTTPClient object used for HTTP requests. |
mfiore | 0:093d58465743 | 70 | HTTPClient http; |
mfiore | 0:093d58465743 | 71 | |
mfiore | 0:093d58465743 | 72 | // HTTP GET example. |
mfiore | 0:093d58465743 | 73 | { |
mfiore | 0:093d58465743 | 74 | char http_rx_buf[1024]; |
mfiore | 0:093d58465743 | 75 | HTTPResult res; |
mfiore | 0:093d58465743 | 76 | |
mfiore | 0:093d58465743 | 77 | // IHTTPDataIn object - will contain data received from server. |
mfiore | 0:093d58465743 | 78 | HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); |
mfiore | 0:093d58465743 | 79 | |
mfiore | 0:093d58465743 | 80 | // Make a HTTP GET request to http://httpbin.org/ |
dabraham | 2:e8fe29290fc1 | 81 | string url = "http://locationtracker-env.fu8xgvitxi.ca-central-1.elasticbeanstalk.com/GetDevice/" + id; |
dabraham | 2:e8fe29290fc1 | 82 | const char *URL = url.c_str(); |
dabraham | 2:e8fe29290fc1 | 83 | res = http.get(URL, &http_rx); |
mfiore | 0:093d58465743 | 84 | if (res != HTTP_OK) |
mfiore | 0:093d58465743 | 85 | logError("HTTP GET failed [%d][%s]", res, httpResToStr(res)); |
dabraham | 2:e8fe29290fc1 | 86 | else{ |
mfiore | 0:093d58465743 | 87 | logInfo("HTTP GET succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); |
dabraham | 2:e8fe29290fc1 | 88 | string resStr(http_rx_buf); |
dabraham | 2:e8fe29290fc1 | 89 | logInfo("The received responce is \r\n%s", resStr); |
dabraham | 2:e8fe29290fc1 | 90 | |
dabraham | 2:e8fe29290fc1 | 91 | picojson::value responseValue; |
dabraham | 2:e8fe29290fc1 | 92 | const char *jsonResponse = resStr.c_str(); |
dabraham | 2:e8fe29290fc1 | 93 | string json = picojson::parse(responseValue, jsonResponse, jsonResponse + strlen(jsonResponse)); |
dabraham | 2:e8fe29290fc1 | 94 | logInfo("The received responce is =%s\r\n%s", json.c_str()); |
dabraham | 2:e8fe29290fc1 | 95 | //printf("res error? %s\r\n", err.c_str()); |
dabraham | 2:e8fe29290fc1 | 96 | logInfo("device_serial =%s\r\n" , responseValue.get("device_serial").get<string>().c_str()); |
dabraham | 2:e8fe29290fc1 | 97 | //printf("number =%f\r\n" , responseValue.get("number").get<double>()); |
dabraham | 2:e8fe29290fc1 | 98 | //printf("integer =%d\r\n" , (int)v.get("integer").get<double>()); |
dabraham | 2:e8fe29290fc1 | 99 | interval = (int)responseValue.get("tracking_interval").get<double>() * 60; |
dabraham | 2:e8fe29290fc1 | 100 | } |
mfiore | 0:093d58465743 | 101 | } |
mfiore | 0:093d58465743 | 102 | |
dabraham | 2:e8fe29290fc1 | 103 | |
mfiore | 0:093d58465743 | 104 | // HTTP POST example. |
mfiore | 0:093d58465743 | 105 | { |
mfiore | 0:093d58465743 | 106 | char http_rx_buf[1024]; |
mfiore | 0:093d58465743 | 107 | HTTPResult res; |
mfiore | 0:093d58465743 | 108 | |
dabraham | 2:e8fe29290fc1 | 109 | // Enable GPS |
dabraham | 2:e8fe29290fc1 | 110 | radio->GPSenable(); |
dabraham | 2:e8fe29290fc1 | 111 | |
dabraham | 2:e8fe29290fc1 | 112 | // Get GPS position |
dabraham | 2:e8fe29290fc1 | 113 | Cellular::gpsData gps_data = radio->GPSgetPosition(); |
dabraham | 2:e8fe29290fc1 | 114 | bool gps_success = gps_data.success; |
dabraham | 2:e8fe29290fc1 | 115 | string latitude = gps_data.latitude; |
dabraham | 2:e8fe29290fc1 | 116 | string longitude = gps_data.longitude; |
dabraham | 2:e8fe29290fc1 | 117 | float hdop = gps_data.hdop; |
dabraham | 2:e8fe29290fc1 | 118 | float altitude = gps_data.altitude; |
dabraham | 2:e8fe29290fc1 | 119 | float fix = gps_data.fix; |
dabraham | 2:e8fe29290fc1 | 120 | string cog = gps_data.cog; |
dabraham | 2:e8fe29290fc1 | 121 | float kmhr = gps_data.kmhr; |
dabraham | 2:e8fe29290fc1 | 122 | float knots = gps_data.knots; |
dabraham | 2:e8fe29290fc1 | 123 | string timestamp = gps_data.timestamp; |
dabraham | 2:e8fe29290fc1 | 124 | string battery_percentage = "50"; |
dabraham | 2:e8fe29290fc1 | 125 | string device_id = "1"; |
dabraham | 2:e8fe29290fc1 | 126 | string device_serial = "MTQ-01"; |
dabraham | 2:e8fe29290fc1 | 127 | |
dabraham | 2:e8fe29290fc1 | 128 | stringstream ss; |
dabraham | 2:e8fe29290fc1 | 129 | ss << gps_data.satellites; |
dabraham | 2:e8fe29290fc1 | 130 | string satellites = ss.str(); |
dabraham | 2:e8fe29290fc1 | 131 | |
dabraham | 2:e8fe29290fc1 | 132 | if(gps_success == true){ |
dabraham | 2:e8fe29290fc1 | 133 | picojson::object gps_attributes; |
dabraham | 2:e8fe29290fc1 | 134 | |
dabraham | 2:e8fe29290fc1 | 135 | gps_attributes["latitude"] = picojson::value(latitude); |
dabraham | 2:e8fe29290fc1 | 136 | gps_attributes["longitude"] = picojson::value(longitude); |
dabraham | 2:e8fe29290fc1 | 137 | gps_attributes["hdop"] = picojson::value(hdop); |
dabraham | 2:e8fe29290fc1 | 138 | gps_attributes["altitude"] = picojson::value(altitude); |
dabraham | 2:e8fe29290fc1 | 139 | gps_attributes["fix"] = picojson::value(fix); |
dabraham | 2:e8fe29290fc1 | 140 | gps_attributes["cog"] = picojson::value(cog); |
dabraham | 2:e8fe29290fc1 | 141 | gps_attributes["kmhr"] = picojson::value(kmhr); |
dabraham | 2:e8fe29290fc1 | 142 | gps_attributes["knots"] = picojson::value(knots); |
dabraham | 2:e8fe29290fc1 | 143 | gps_attributes["satellites"] = picojson::value(satellites); |
dabraham | 2:e8fe29290fc1 | 144 | gps_attributes["time_stamp"] = picojson::value(timestamp); |
dabraham | 2:e8fe29290fc1 | 145 | gps_attributes["battery_percentage"] = picojson::value(battery_percentage); |
dabraham | 2:e8fe29290fc1 | 146 | gps_attributes["device_id"] = picojson::value(device_id); |
dabraham | 2:e8fe29290fc1 | 147 | gps_attributes["device_serial"] = picojson::value(device_serial); |
dabraham | 2:e8fe29290fc1 | 148 | |
dabraham | 2:e8fe29290fc1 | 149 | const char *GPSJSONString = picojson::value(gps_attributes).serialize().c_str(); // serialize the json_msg and convert the string result to char* using c_str() |
dabraham | 2:e8fe29290fc1 | 150 | logInfo("The current JSON value is %s", GPSJSONString); |
dabraham | 2:e8fe29290fc1 | 151 | |
dabraham | 2:e8fe29290fc1 | 152 | char http_tx_buf[1024]; |
dabraham | 2:e8fe29290fc1 | 153 | strcpy(http_tx_buf, GPSJSONString); // convert the char* result(GPSJSONString) to char[](http_tx_buf) |
mfiore | 0:093d58465743 | 154 | |
mfiore | 0:093d58465743 | 155 | // IHTTPDataIn object - will contain data received from server. |
mfiore | 0:093d58465743 | 156 | HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); |
mfiore | 0:093d58465743 | 157 | |
mfiore | 0:093d58465743 | 158 | // IHTTPDataOut object - contains data to be posted to server. |
mfiore | 0:093d58465743 | 159 | // HTTPJson automatically adds the JSON content-type header to the request. |
dabraham | 2:e8fe29290fc1 | 160 | HTTPJson http_tx(http_tx_buf, sizeof(http_tx_buf)); |
mfiore | 0:093d58465743 | 161 | |
mfiore | 0:093d58465743 | 162 | // Make a HTTP POST request to http://httpbin.org/ |
dabraham | 2:e8fe29290fc1 | 163 | res = http.post("http://locationtracker-env.fu8xgvitxi.ca-central-1.elasticbeanstalk.com/PostGPS", http_tx, &http_rx); |
mfiore | 0:093d58465743 | 164 | if (res != HTTP_OK) |
mfiore | 0:093d58465743 | 165 | logError("HTTP POST failed [%d][%s]", res, httpResToStr(res)); |
mfiore | 0:093d58465743 | 166 | else |
mfiore | 0:093d58465743 | 167 | logInfo("HTTP POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); |
dabraham | 2:e8fe29290fc1 | 168 | |
mfiore | 0:093d58465743 | 169 | } |
dabraham | 2:e8fe29290fc1 | 170 | else{ |
dabraham | 2:e8fe29290fc1 | 171 | logInfo("GPS Location failed"); |
dabraham | 2:e8fe29290fc1 | 172 | } |
dabraham | 2:e8fe29290fc1 | 173 | } |
mfiore | 0:093d58465743 | 174 | } |
mfiore | 0:093d58465743 | 175 | |
mfiore | 0:093d58465743 | 176 | logInfo("finished - bringing down link"); |
dabraham | 2:e8fe29290fc1 | 177 | radio->GPSdisable(); |
mfiore | 0:093d58465743 | 178 | radio->disconnect(); |
dabraham | 2:e8fe29290fc1 | 179 | wait(interval); |
dabraham | 2:e8fe29290fc1 | 180 | } |
mfiore | 0:093d58465743 | 181 | return 0; |
mfiore | 0:093d58465743 | 182 | } |
mfiore | 0:093d58465743 | 183 | |
dabraham | 2:e8fe29290fc1 | 184 | |
mfiore | 0:093d58465743 | 185 | bool init_mtsas() { |
mfiore | 0:093d58465743 | 186 | io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); |
mfiore | 0:093d58465743 | 187 | if (! io) |
mfiore | 0:093d58465743 | 188 | return false; |
mfiore | 0:093d58465743 | 189 | |
mfiore | 0:093d58465743 | 190 | // radio default baud rate is 115200 |
mfiore | 0:093d58465743 | 191 | io->baud(115200); |
mfiore | 0:093d58465743 | 192 | radio = mts::CellularFactory::create(io); |
mfiore | 0:093d58465743 | 193 | if (! radio) |
mfiore | 0:093d58465743 | 194 | return false; |
mfiore | 0:093d58465743 | 195 | |
mfiore | 0:093d58465743 | 196 | // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created |
mfiore | 0:093d58465743 | 197 | Transport::setTransport(radio); |
mfiore | 0:093d58465743 | 198 | |
mfiore | 0:093d58465743 | 199 | return true; |
mfiore | 0:093d58465743 | 200 | } |
mfiore | 0:093d58465743 | 201 | |
mfiore | 0:093d58465743 | 202 | char* httpResToStr(HTTPResult res) { |
mfiore | 0:093d58465743 | 203 | switch(res) { |
mfiore | 0:093d58465743 | 204 | case HTTP_PROCESSING: |
mfiore | 0:093d58465743 | 205 | return "HTTP_PROCESSING"; |
mfiore | 0:093d58465743 | 206 | case HTTP_PARSE: |
mfiore | 0:093d58465743 | 207 | return "HTTP_PARSE"; |
mfiore | 0:093d58465743 | 208 | case HTTP_DNS: |
mfiore | 0:093d58465743 | 209 | return "HTTP_DNS"; |
mfiore | 0:093d58465743 | 210 | case HTTP_PRTCL: |
mfiore | 0:093d58465743 | 211 | return "HTTP_PRTCL"; |
mfiore | 0:093d58465743 | 212 | case HTTP_NOTFOUND: |
mfiore | 0:093d58465743 | 213 | return "HTTP_NOTFOUND"; |
mfiore | 0:093d58465743 | 214 | case HTTP_REFUSED: |
mfiore | 0:093d58465743 | 215 | return "HTTP_REFUSED"; |
mfiore | 0:093d58465743 | 216 | case HTTP_ERROR: |
mfiore | 0:093d58465743 | 217 | return "HTTP_ERROR"; |
mfiore | 0:093d58465743 | 218 | case HTTP_TIMEOUT: |
mfiore | 0:093d58465743 | 219 | return "HTTP_TIMEOUT"; |
mfiore | 0:093d58465743 | 220 | case HTTP_CONN: |
mfiore | 0:093d58465743 | 221 | return "HTTP_CONN"; |
mfiore | 0:093d58465743 | 222 | case HTTP_CLOSED: |
mfiore | 0:093d58465743 | 223 | return "HTTP_CLOSED"; |
mfiore | 0:093d58465743 | 224 | case HTTP_REDIRECT: |
mfiore | 0:093d58465743 | 225 | return "HTTP_REDIRECT"; |
mfiore | 0:093d58465743 | 226 | case HTTP_OK: |
mfiore | 0:093d58465743 | 227 | return "HTTP_OK"; |
mfiore | 0:093d58465743 | 228 | default: |
mfiore | 0:093d58465743 | 229 | return "HTTP Result unknown"; |
mfiore | 0:093d58465743 | 230 | } |
mfiore | 0:093d58465743 | 231 | } |