Example of using the Dragonfly HTTP library with the m2x http api

Dependencies:   MbedJSONValue X_NUCLEO_IKS01A1 mbed mtsas_lat3

Fork of VVV_MultiTech_Dragonfly_ATT_Dallas by Paul Jaeger

Committer:
pferland
Date:
Tue Sep 26 04:56:39 2017 +0000
Revision:
12:493eba0c12c7
Parent:
10:65b7d33f18d9
removed secure data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:a44e71488e1f 1 /*************************************************************************
BlueShadow 8:a09dd040bb4b 2 * Dragonfly Example program of SMS on Freescale sensor board
BlueShadow 8:a09dd040bb4b 3 */
BlueShadow 6:7946b5c2376a 4
mfiore 0:a44e71488e1f 5 #include "mbed.h"
mfiore 0:a44e71488e1f 6 #include "mtsas.h"
mfiore 0:a44e71488e1f 7 #include "MbedJSONValue.h"
mfiore 5:a946ef74a8c4 8 #include "HTTPJson.h"
mfiore 0:a44e71488e1f 9 #include <string>
pferland 10:65b7d33f18d9 10 #include <sstream>
pferland 10:65b7d33f18d9 11 #include "x_nucleo_iks01a1.h"
pferland 10:65b7d33f18d9 12
pferland 10:65b7d33f18d9 13 #define HTTP_RX_BUFFER_SIZE 1024
pferland 10:65b7d33f18d9 14 char http_rx_buf[HTTP_RX_BUFFER_SIZE];
mfiore 0:a44e71488e1f 15
mfiore 0:a44e71488e1f 16 // Debug serial port
mfiore 0:a44e71488e1f 17 static Serial debug(USBTX, USBRX);
mfiore 0:a44e71488e1f 18
mfiore 0:a44e71488e1f 19 // MTSSerialFlowControl - serial link between processor and radio
mfiore 1:a049d113e250 20 static MTSSerialFlowControl* io;
mfiore 0:a44e71488e1f 21
mfiore 0:a44e71488e1f 22 // Cellular - radio object for cellular operations (SMS, TCP, etc)
mfiore 0:a44e71488e1f 23 Cellular* radio;
mfiore 0:a44e71488e1f 24
mfiore 0:a44e71488e1f 25 // APN associated with SIM card
mfiore 4:730b61258422 26 // this APN should work for the AT&T SIM that came with your Dragonfly
pferland 10:65b7d33f18d9 27 static const std::string apn = "broadband";
mfiore 0:a44e71488e1f 28
mfiore 0:a44e71488e1f 29 // Phone number to send SMS messages to
mfiore 5:a946ef74a8c4 30 // just change the x digits - the 1 needs to stay!
pferland 12:493eba0c12c7 31 static const std::string phone_number = " NEED A NUMBER with a 1";
pferland 10:65b7d33f18d9 32
pferland 10:65b7d33f18d9 33 // Keys needed by the m2x API
pferland 10:65b7d33f18d9 34 // see more details here: https://m2x.att.com/developer/documentation/v2/overview
pferland 10:65b7d33f18d9 35 // m2x device ID
pferland 12:493eba0c12c7 36 static const std::string m2x_device_id = "";
pferland 10:65b7d33f18d9 37
pferland 10:65b7d33f18d9 38 // M2X primary API key
pferland 12:493eba0c12c7 39 static const std::string m2x_api_key = "";
mfiore 5:a946ef74a8c4 40
pferland 10:65b7d33f18d9 41 /* Instantiate the expansion board */
pferland 10:65b7d33f18d9 42 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL);
pferland 10:65b7d33f18d9 43
pferland 10:65b7d33f18d9 44 /* Retrieve the composing elements of the expansion board */
pferland 10:65b7d33f18d9 45 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
pferland 10:65b7d33f18d9 46 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
pferland 10:65b7d33f18d9 47 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
pferland 10:65b7d33f18d9 48 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
pferland 10:65b7d33f18d9 49 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
pferland 10:65b7d33f18d9 50 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
pferland 10:65b7d33f18d9 51 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
mfiore 0:a44e71488e1f 52
mfiore 0:a44e71488e1f 53 // variables for sensor data
BlueShadow 8:a09dd040bb4b 54 char streamAcc[] = "acc_rms";
BlueShadow 8:a09dd040bb4b 55 char streamMag[] = "mag_rms";
BlueShadow 8:a09dd040bb4b 56 char streamGyr[] = "gyr_rms";
pferland 10:65b7d33f18d9 57 char streamTemp[] = "temp_c";
pferland 10:65b7d33f18d9 58 char streamPres[] = "pressure";
pferland 10:65b7d33f18d9 59 char streamHum[] = "humidity";
pferland 10:65b7d33f18d9 60 char streamAccel[] = "accel";
mfiore 0:a44e71488e1f 61
mfiore 0:a44e71488e1f 62 // misc variables
BlueShadow 6:7946b5c2376a 63 static int sms_interval_ms = 60000;
BlueShadow 8:a09dd040bb4b 64 static int read_interval_ms = 5000;
BlueShadow 8:a09dd040bb4b 65 static int print_interval_ms = 1500;
mfiore 0:a44e71488e1f 66 int debug_baud = 115200;
mfiore 0:a44e71488e1f 67
BlueShadow 8:a09dd040bb4b 68 bool radio_ok = false;
BlueShadow 6:7946b5c2376a 69
BlueShadow 6:7946b5c2376a 70 /****************************************************************************************************
mfiore 0:a44e71488e1f 71 // function prototypes
BlueShadow 6:7946b5c2376a 72 ****************************************************************************************************/
mfiore 0:a44e71488e1f 73 bool init_mtsas();
pferland 10:65b7d33f18d9 74 char* httpResToStr(HTTPResult res);
mfiore 0:a44e71488e1f 75
BlueShadow 6:7946b5c2376a 76 /****************************************************************************************************
mfiore 0:a44e71488e1f 77 // main
BlueShadow 6:7946b5c2376a 78 ****************************************************************************************************/
BlueShadow 6:7946b5c2376a 79 int main()
BlueShadow 6:7946b5c2376a 80 {
mfiore 0:a44e71488e1f 81 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
mfiore 0:a44e71488e1f 82 debug.baud(debug_baud);
mfiore 0:a44e71488e1f 83 logInfo("starting...");
BlueShadow 6:7946b5c2376a 84
BlueShadow 6:7946b5c2376a 85 /****************************************************************************************************
BlueShadow 6:7946b5c2376a 86 Initialize I2C Devices ************
BlueShadow 6:7946b5c2376a 87 ****************************************************************************************************/
BlueShadow 6:7946b5c2376a 88
pferland 10:65b7d33f18d9 89 int32_t accel_data[3];
pferland 10:65b7d33f18d9 90 float accel_sensitivity;
BlueShadow 8:a09dd040bb4b 91 //float mag_data[3];
BlueShadow 8:a09dd040bb4b 92 //float gyro_data[3];
pferland 10:65b7d33f18d9 93
pferland 10:65b7d33f18d9 94 float dataX = 0.0f;
pferland 10:65b7d33f18d9 95 float dataY = 0.0f;
pferland 10:65b7d33f18d9 96 float dataZ = 1.0f;
pferland 10:65b7d33f18d9 97 float humidity = 0.0f;
pferland 10:65b7d33f18d9 98 float temperature = 0.0f;
pferland 10:65b7d33f18d9 99 float pressure = 0.0f;
BlueShadow 6:7946b5c2376a 100
BlueShadow 6:7946b5c2376a 101 // Initialization Radio Section **********************************************************
BlueShadow 6:7946b5c2376a 102
mfiore 0:a44e71488e1f 103 radio_ok = init_mtsas();
mfiore 0:a44e71488e1f 104 if (! radio_ok)
mfiore 0:a44e71488e1f 105 logError("MTSAS init failed");
mfiore 0:a44e71488e1f 106 else
mfiore 0:a44e71488e1f 107 logInfo("MTSAS is ok");
BlueShadow 6:7946b5c2376a 108
BlueShadow 6:7946b5c2376a 109 //End Radio Initialization Section **********************************************************
pferland 10:65b7d33f18d9 110
BlueShadow 8:a09dd040bb4b 111 Timer sms_timer;
BlueShadow 8:a09dd040bb4b 112 sms_timer.start();
BlueShadow 8:a09dd040bb4b 113 Timer read_timer;
BlueShadow 8:a09dd040bb4b 114 read_timer.start(); // Timer data is set in the Variable seciton see misc variables Timer motion_timer;
mfiore 1:a049d113e250 115 Timer print_timer;
BlueShadow 6:7946b5c2376a 116 print_timer.start();
BlueShadow 6:7946b5c2376a 117 Timer motion_timer;
BlueShadow 6:7946b5c2376a 118 motion_timer.start();
pferland 10:65b7d33f18d9 119
pferland 10:65b7d33f18d9 120 // http object initialization
pferland 10:65b7d33f18d9 121 // HTTPClient object used for HTTP requests.
pferland 10:65b7d33f18d9 122 HTTPClient http;
pferland 10:65b7d33f18d9 123
pferland 10:65b7d33f18d9 124 // Enable strict certificate validation.
pferland 10:65b7d33f18d9 125 // http.setPeerVerification(VERIFY_PEER);
pferland 10:65b7d33f18d9 126
pferland 10:65b7d33f18d9 127 // Load certificates defined in ssl_certificates.h.
pferland 10:65b7d33f18d9 128 // See comments in ssl_certificates.h for information on how to get and format root certificates.
pferland 10:65b7d33f18d9 129 // if (http.addRootCACertificate(ssl_certificates) != HTTP_OK)
pferland 10:65b7d33f18d9 130 // logError("loading SSL certificates failed");
BlueShadow 6:7946b5c2376a 131
mfiore 0:a44e71488e1f 132 while (true) {
BlueShadow 8:a09dd040bb4b 133 if (read_timer.read_ms() > read_interval_ms) {
BlueShadow 6:7946b5c2376a 134
pferland 10:65b7d33f18d9 135 accelerometer->get_x_axes(accel_data);
pferland 10:65b7d33f18d9 136 accelerometer->get_x_sensitivity(&accel_sensitivity);
pferland 10:65b7d33f18d9 137 dataX = accel_data[0] * accel_sensitivity;
pferland 10:65b7d33f18d9 138 dataY = accel_data[1] * accel_sensitivity;
pferland 10:65b7d33f18d9 139 dataZ = accel_data[2] * accel_sensitivity;
pferland 10:65b7d33f18d9 140 humidity_sensor->get_humidity(&humidity);
pferland 10:65b7d33f18d9 141 temp_sensor1->get_temperature(&temperature);
pferland 10:65b7d33f18d9 142 pressure_sensor->get_pressure(&pressure);
BlueShadow 8:a09dd040bb4b 143 read_timer.reset();
mfiore 0:a44e71488e1f 144 }
pferland 10:65b7d33f18d9 145 // send to m2x on same interval as printing
mfiore 0:a44e71488e1f 146 if (print_timer.read_ms() > print_interval_ms) {
BlueShadow 8:a09dd040bb4b 147
mfiore 0:a44e71488e1f 148 logDebug("SENSOR DATA");
BlueShadow 8:a09dd040bb4b 149 logDebug("DataZ %0.3f", dataZ);
BlueShadow 8:a09dd040bb4b 150 logDebug("DataY %0.3f", dataY);
pferland 10:65b7d33f18d9 151 logDebug("DataX %0.3f", dataX);
pferland 10:65b7d33f18d9 152 logDebug("Humidity: %0.2f", humidity);
pferland 10:65b7d33f18d9 153 logDebug("Temperature: %0.1f", temperature);
pferland 10:65b7d33f18d9 154 logDebug("Presure: %f", pressure);
pferland 10:65b7d33f18d9 155
pferland 10:65b7d33f18d9 156 HTTPResult res;
pferland 10:65b7d33f18d9 157 // IHTTPDataIn object - will contain data received from server.
pferland 10:65b7d33f18d9 158 HTTPText http_rx(http_rx_buf, HTTP_RX_BUFFER_SIZE);
pferland 10:65b7d33f18d9 159
pferland 10:65b7d33f18d9 160 std::ostringstream json_packet;
pferland 10:65b7d33f18d9 161 json_packet << "{\"values\": {\"value\" : " \
pferland 10:65b7d33f18d9 162 << temperature << "}}";
pferland 10:65b7d33f18d9 163
pferland 10:65b7d33f18d9 164 // IHTTPDataOut object - contains data to be posted to server.
pferland 10:65b7d33f18d9 165 // HTTPJson automatically adds the JSON content-type header to the request.
pferland 10:65b7d33f18d9 166 char *tx_string = new char[json_packet.str().length()];
pferland 10:65b7d33f18d9 167 sprintf(tx_string, json_packet.str().c_str());
pferland 10:65b7d33f18d9 168 HTTPJson http_tx(tx_string, json_packet.str().length());
pferland 10:65b7d33f18d9 169 std::string m2x_header = "X-M2X-KEY: " + m2x_api_key + "\r\n";
pferland 10:65b7d33f18d9 170 std::string url = "https://api-m2x.att.com/v2/devices/" + \
pferland 10:65b7d33f18d9 171 m2x_device_id + \
pferland 10:65b7d33f18d9 172 "/streams/temperature/values";
pferland 10:65b7d33f18d9 173 http.setHeader(m2x_header.c_str());
pferland 10:65b7d33f18d9 174
pferland 10:65b7d33f18d9 175 http.post(url.c_str(), http_tx, &http_rx);
pferland 10:65b7d33f18d9 176 delete tx_string;
pferland 10:65b7d33f18d9 177 if (res != HTTP_OK)
pferland 10:65b7d33f18d9 178 logError("HTTPS POST failed [%d][%s]", res, httpResToStr(res));
pferland 10:65b7d33f18d9 179 else
pferland 10:65b7d33f18d9 180 logInfo("HTTPS POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf);
pferland 10:65b7d33f18d9 181
mfiore 0:a44e71488e1f 182 print_timer.reset();
pferland 10:65b7d33f18d9 183
mfiore 0:a44e71488e1f 184 }
BlueShadow 6:7946b5c2376a 185
BlueShadow 8:a09dd040bb4b 186 // SMS
BlueShadow 6:7946b5c2376a 187 if (sms_timer.read_ms() > sms_interval_ms) {
BlueShadow 6:7946b5c2376a 188 sms_timer.reset();
BlueShadow 6:7946b5c2376a 189 logInfo("SMS Send Routine");
BlueShadow 8:a09dd040bb4b 190 printf(" In sms routine \r\n");
mfiore 0:a44e71488e1f 191 if (radio_ok) {
mfiore 0:a44e71488e1f 192 MbedJSONValue sms_json;
mfiore 0:a44e71488e1f 193 string sms_str;
BlueShadow 6:7946b5c2376a 194
BlueShadow 9:3dcbe04adfd0 195 sms_json[" edge Gravity: "] = dataY;
BlueShadow 9:3dcbe04adfd0 196 sms_json[" level Gravity: "] = dataZ;
BlueShadow 6:7946b5c2376a 197
mfiore 2:955a63247721 198 sms_str = "SENSOR DATA:\n";
mfiore 0:a44e71488e1f 199 sms_str += sms_json.serialize();
BlueShadow 6:7946b5c2376a 200
mfiore 0:a44e71488e1f 201 logDebug("sending SMS to %s:\r\n%s", phone_number.c_str(), sms_str.c_str());
BlueShadow 9:3dcbe04adfd0 202 if (dataZ<0.6f) { //added
mfiore 0:a44e71488e1f 203 Code ret = radio->sendSMS(phone_number, sms_str);
mfiore 0:a44e71488e1f 204 if (ret != MTS_SUCCESS)
mfiore 0:a44e71488e1f 205 logError("sending SMS failed");
BlueShadow 9:3dcbe04adfd0 206 } //added
mfiore 0:a44e71488e1f 207 }
mfiore 0:a44e71488e1f 208 }
mfiore 0:a44e71488e1f 209 }
mfiore 0:a44e71488e1f 210 }
mfiore 0:a44e71488e1f 211
mfiore 1:a049d113e250 212 // init functions
BlueShadow 6:7946b5c2376a 213 bool init_mtsas()
BlueShadow 6:7946b5c2376a 214 {
mfiore 1:a049d113e250 215 io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
mfiore 1:a049d113e250 216 if (! io)
mfiore 1:a049d113e250 217 return false;
BlueShadow 6:7946b5c2376a 218
mfiore 1:a049d113e250 219 io->baud(115200);
mfiore 1:a049d113e250 220 radio = CellularFactory::create(io);
mfiore 1:a049d113e250 221 if (! radio)
mfiore 1:a049d113e250 222 return false;
BlueShadow 6:7946b5c2376a 223
mfiore 1:a049d113e250 224 Code ret = radio->setApn(apn);
mfiore 1:a049d113e250 225 if (ret != MTS_SUCCESS)
mfiore 1:a049d113e250 226 return false;
BlueShadow 6:7946b5c2376a 227
mfiore 5:a946ef74a8c4 228 Transport::setTransport(radio);
BlueShadow 6:7946b5c2376a 229
mfiore 1:a049d113e250 230 return true;
mfiore 1:a049d113e250 231 }
pferland 10:65b7d33f18d9 232
pferland 10:65b7d33f18d9 233 char* httpResToStr(HTTPResult res) {
pferland 10:65b7d33f18d9 234 switch(res) {
pferland 10:65b7d33f18d9 235 case HTTP_PROCESSING:
pferland 10:65b7d33f18d9 236 return "HTTP_PROCESSING";
pferland 10:65b7d33f18d9 237 case HTTP_PARSE:
pferland 10:65b7d33f18d9 238 return "HTTP_PARSE";
pferland 10:65b7d33f18d9 239 case HTTP_DNS:
pferland 10:65b7d33f18d9 240 return "HTTP_DNS";
pferland 10:65b7d33f18d9 241 case HTTP_PRTCL:
pferland 10:65b7d33f18d9 242 return "HTTP_PRTCL";
pferland 10:65b7d33f18d9 243 case HTTP_NOTFOUND:
pferland 10:65b7d33f18d9 244 return "HTTP_NOTFOUND";
pferland 10:65b7d33f18d9 245 case HTTP_REFUSED:
pferland 10:65b7d33f18d9 246 return "HTTP_REFUSED";
pferland 10:65b7d33f18d9 247 case HTTP_ERROR:
pferland 10:65b7d33f18d9 248 return "HTTP_ERROR";
pferland 10:65b7d33f18d9 249 case HTTP_TIMEOUT:
pferland 10:65b7d33f18d9 250 return "HTTP_TIMEOUT";
pferland 10:65b7d33f18d9 251 case HTTP_CONN:
pferland 10:65b7d33f18d9 252 return "HTTP_CONN";
pferland 10:65b7d33f18d9 253 case HTTP_CLOSED:
pferland 10:65b7d33f18d9 254 return "HTTP_CLOSED";
pferland 10:65b7d33f18d9 255 case HTTP_REDIRECT:
pferland 10:65b7d33f18d9 256 return "HTTP_REDIRECT";
pferland 10:65b7d33f18d9 257 case HTTP_OK:
pferland 10:65b7d33f18d9 258 return "HTTP_OK";
pferland 10:65b7d33f18d9 259 default:
pferland 10:65b7d33f18d9 260 return "HTTP Result unknown";
pferland 10:65b7d33f18d9 261 }
pferland 10:65b7d33f18d9 262 }