MultiTech Dragonfly with ROHM Sensor board sending data to IBM BlueMix Quickstart

Dependencies:   mbed mtsas FXAS21002 FXOS8700 mbed-rtos

Fork of AvnetWorking_IBM_QuickStart by Paul Jaeger

Committer:
BlueShadow
Date:
Tue Aug 23 02:17:54 2016 +0000
Revision:
6:8f1ad9d2193e
Parent:
5:dbedb2422089
Child:
7:3d2a3fd4a523
Freescale NXP accel data sending to IBM Bluemix QuickStart - 5 data sets then termination.  www.Bluemix.com  Quickstart data look for dragonfly22; This uses Aeris APN

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BlueShadow 6:8f1ad9d2193e 1 /** Combination of MultiTech HTTPS Example using json with ROHM Sennsor board and IBM Bluemix
BlueShadow 2:1df4b48824b1 2 *
BlueShadow 2:1df4b48824b1 3 * Configures the Sensor board for Ambient Light, cellular radio, brings up the cellular link,
BlueShadow 2:1df4b48824b1 4 * and does HTTPS POST requests.
mfiore 0:6ce1803b7a03 5 * To do HTTPS requests with a certain server, the root certificate used to validate that server's certificate must be installed. See ssl_certificates.h for information on how to get the proper root certificate.
mfiore 0:6ce1803b7a03 6 *
BlueShadow 2:1df4b48824b1 7 *
BlueShadow 2:1df4b48824b1 8 *
BlueShadow 2:1df4b48824b1 9 * The following hardware is required to successfully run this program:
BlueShadow 2:1df4b48824b1 10 * - MultiTech UDK2 (4" square white PCB with Arduino headers, antenna
BlueShadow 2:1df4b48824b1 11 * connector, micro USB ports, and 40-pin connector for Dragonfly)
BlueShadow 2:1df4b48824b1 12 * - MultiTech Dragonfly (1"x2" green PCB with Telit radio)
BlueShadow 6:8f1ad9d2193e 13 * - Rohm Electronics Sensor Board
BlueShadow 6:8f1ad9d2193e 14 * - Expansion board (LSM6DS0
BlueShadow 6:8f1ad9d2193e 15 * 3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis
BlueShadow 6:8f1ad9d2193e 16 * magnetometer, HTS221 humidity and temperature sensor and LPS25HB
BlueShadow 6:8f1ad9d2193e 17 * pressure sensor)
BlueShadow 2:1df4b48824b1 18 *
BlueShadow 2:1df4b48824b1 19 * What this program does:
BlueShadow 6:8f1ad9d2193e 20 * - reads data from all sensors on board
BlueShadow 2:1df4b48824b1 21 * - prints all sensor data to debug port on a periodic basis
BlueShadow 2:1df4b48824b1 22 * - optionally send data to BlueMix
BlueShadow 2:1df4b48824b1 23 * - All data is sent to a specific location determined by the student login.
BlueShadow 2:1df4b48824b1 24 * - BlueMix cloud platform (user must create own account and configure a device
BlueShadow 6:8f1ad9d2193e 25 * - you need to set the "VENDOR" and "MODEL"
BlueShadow 6:8f1ad9d2193e 26
BlueShadow 6:8f1ad9d2193e 27 * - you need to set the "do_cloud_post" flag to true for this to
BlueShadow 6:8f1ad9d2193e 28 * work
BlueShadow 2:1df4b48824b1 29 *
BlueShadow 2:1df4b48824b1 30 * Setup:
BlueShadow 2:1df4b48824b1 31 * - Correctly insert SIM card into Dragonfly
BlueShadow 2:1df4b48824b1 32 * - Seat the Dragonfly on the UDK2 board
BlueShadow 2:1df4b48824b1 33 * - Connect an antenna to the connector on the Dragonfly labled "M"
BlueShadow 2:1df4b48824b1 34 * - Stack the Base Shield on the UDK2 Arduino headers
BlueShadow 2:1df4b48824b1 35 * - Stack the MEMs board on top of the Base Shield
BlueShadow 2:1df4b48824b1 36 * - Plug in the power cable
BlueShadow 2:1df4b48824b1 37 * - Plug a micro USB cable into the port below and slightly to the
BlueShadow 2:1df4b48824b1 38 * left of the Dragonfly (NOT the port on the Dragonfly)
BlueShadow 2:1df4b48824b1 39 *
BlueShadow 2:1df4b48824b1 40 * Go have fun and make something cool!
BlueShadow 2:1df4b48824b1 41 *
BlueShadow 2:1df4b48824b1 42 ************************************************************************/
BlueShadow 2:1df4b48824b1 43 /*
BlueShadow 6:8f1ad9d2193e 44 Sample Program Description:
BlueShadow 6:8f1ad9d2193e 45 This Program will enable to Multi-Tech Dragonfly platform to utilize ROHM's Multi-sensor Shield Board.
BlueShadow 6:8f1ad9d2193e 46 This program will initialize all sensors on the shield and then read back the sensor data.
BlueShadow 6:8f1ad9d2193e 47 Data will then be output to the UART Debug Terminal every 1 second.
BlueShadow 6:8f1ad9d2193e 48
BlueShadow 6:8f1ad9d2193e 49 Sample Program Author:
BlueShadow 6:8f1ad9d2193e 50 ROHM USDC
BlueShadow 6:8f1ad9d2193e 51
BlueShadow 6:8f1ad9d2193e 52 Additional Resources:
BlueShadow 6:8f1ad9d2193e 53 ROHM Sensor Shield GitHub Repository: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield
BlueShadow 6:8f1ad9d2193e 54 * NOTE: This example changes the baud rate of the debug port to 115200 baud!
mfiore 0:6ce1803b7a03 55 */
mfiore 0:6ce1803b7a03 56
mfiore 0:6ce1803b7a03 57 #include "mbed.h"
mfiore 0:6ce1803b7a03 58 #include "mtsas.h"
mfiore 0:6ce1803b7a03 59 #include "ssl_certificates.h"
BlueShadow 2:1df4b48824b1 60 #include <string> // added for string manipulation
BlueShadow 6:8f1ad9d2193e 61 #include <sstream>
BlueShadow 6:8f1ad9d2193e 62 #include "FXAS21002.h"
BlueShadow 6:8f1ad9d2193e 63 #include "FXOS8700.h"
BlueShadow 2:1df4b48824b1 64
BlueShadow 6:8f1ad9d2193e 65 // Initialize pins for I2C communication for sensors. Set jumpers J6,J7 in FRDM-STBC-AGM01 board accordingly.
BlueShadow 6:8f1ad9d2193e 66 FXOS8700 accel(D14,D15);
BlueShadow 6:8f1ad9d2193e 67 FXOS8700 mag(D14,D15);
BlueShadow 6:8f1ad9d2193e 68 FXAS21002 gyro(D14,D15);
BlueShadow 2:1df4b48824b1 69
BlueShadow 6:8f1ad9d2193e 70
BlueShadow 6:8f1ad9d2193e 71 char streamAcc[] = "acc_rms"; // Stream you want to push to
BlueShadow 6:8f1ad9d2193e 72 char streamMag[] = "mag_rms"; // Stream you want to push to
BlueShadow 6:8f1ad9d2193e 73 char streamGyr[] = "gyr_rms"; // Stream you want to push to
BlueShadow 2:1df4b48824b1 74
BlueShadow 2:1df4b48824b1 75
BlueShadow 2:1df4b48824b1 76 //*****************************************************************************************************************************************************
BlueShadow 2:1df4b48824b1 77 DigitalOut Led1Out(LED1);
BlueShadow 2:1df4b48824b1 78
BlueShadow 6:8f1ad9d2193e 79
BlueShadow 6:8f1ad9d2193e 80 // This line controls the regulator's battery charger.
BlueShadow 6:8f1ad9d2193e 81 // BC_NCE = 0 enables the battery charger
BlueShadow 6:8f1ad9d2193e 82 // BC_NCE = 1 disables the battery charger
mfiore 1:26b8af61d0ac 83 DigitalOut bc_nce(PB_2);
mfiore 1:26b8af61d0ac 84
mfiore 0:6ce1803b7a03 85 bool init_mtsas();
mfiore 0:6ce1803b7a03 86 char* httpResToStr(HTTPResult res);
mfiore 0:6ce1803b7a03 87
mfiore 0:6ce1803b7a03 88 // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio.
mfiore 0:6ce1803b7a03 89 mts::MTSSerialFlowControl* io;
mfiore 0:6ce1803b7a03 90 // The Cellular object represents the cellular radio.
mfiore 0:6ce1803b7a03 91 mts::Cellular* radio;
mfiore 0:6ce1803b7a03 92
mfiore 0:6ce1803b7a03 93 // An APN is required for GSM radios.
BlueShadow 2:1df4b48824b1 94 static const char apn[] = "iot.aer.net";
mfiore 0:6ce1803b7a03 95
mfiore 0:6ce1803b7a03 96 bool radio_ok = false;
mfiore 0:6ce1803b7a03 97
BlueShadow 6:8f1ad9d2193e 98 bool init_mtsas();
BlueShadow 2:1df4b48824b1 99 char* httpResToStr(HTTPResult res);
BlueShadow 2:1df4b48824b1 100
BlueShadow 2:1df4b48824b1 101 /****************************************************************************************************
BlueShadow 2:1df4b48824b1 102 // main
BlueShadow 2:1df4b48824b1 103 ****************************************************************************************************/
BlueShadow 2:1df4b48824b1 104
BlueShadow 2:1df4b48824b1 105 int main()
BlueShadow 2:1df4b48824b1 106 {
BlueShadow 2:1df4b48824b1 107
BlueShadow 5:dbedb2422089 108 accel.accel_config();
BlueShadow 5:dbedb2422089 109 mag.mag_config();
BlueShadow 5:dbedb2422089 110 gyro.gyro_config();
BlueShadow 2:1df4b48824b1 111
BlueShadow 5:dbedb2422089 112 float accel_data[3];
BlueShadow 6:8f1ad9d2193e 113 //float mag_data[3];
BlueShadow 6:8f1ad9d2193e 114 //float gyro_data[3];
BlueShadow 2:1df4b48824b1 115
BlueShadow 6:8f1ad9d2193e 116 printf("Begin Data Acquisition from FXOS8700 and FXAS21002....\r\n\r\n");
BlueShadow 6:8f1ad9d2193e 117 wait(0.5);
BlueShadow 5:dbedb2422089 118
BlueShadow 5:dbedb2422089 119
BlueShadow 5:dbedb2422089 120
BlueShadow 6:8f1ad9d2193e 121 // Disable the battery charger unless a battery is attached.
BlueShadow 6:8f1ad9d2193e 122 bc_nce = 1;
BlueShadow 6:8f1ad9d2193e 123
BlueShadow 6:8f1ad9d2193e 124
BlueShadow 6:8f1ad9d2193e 125 // Change the baud rate of the debug port from the default 9600 to 115200.
BlueShadow 6:8f1ad9d2193e 126 Serial debug(USBTX, USBRX);
BlueShadow 6:8f1ad9d2193e 127 debug.baud(115200);
BlueShadow 2:1df4b48824b1 128
mfiore 0:6ce1803b7a03 129 //Sets the log level to INFO, higher log levels produce more log output.
mfiore 0:6ce1803b7a03 130 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
BlueShadow 2:1df4b48824b1 131 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
BlueShadow 2:1df4b48824b1 132
BlueShadow 2:1df4b48824b1 133
BlueShadow 2:1df4b48824b1 134 //****************************************************************************************************/
BlueShadow 2:1df4b48824b1 135 // Initialization Radio Section **********************************************************
BlueShadow 2:1df4b48824b1 136 //****************************************************************************************************/
mfiore 0:6ce1803b7a03 137 logInfo("initializing cellular radio");
mfiore 0:6ce1803b7a03 138 radio_ok = init_mtsas();
mfiore 0:6ce1803b7a03 139 if (! radio_ok) {
mfiore 0:6ce1803b7a03 140 while (true) {
mfiore 0:6ce1803b7a03 141 logError("failed to initialize cellular radio");
mfiore 0:6ce1803b7a03 142 wait(1);
mfiore 0:6ce1803b7a03 143 }
mfiore 0:6ce1803b7a03 144 }
BlueShadow 2:1df4b48824b1 145
BlueShadow 2:1df4b48824b1 146
mfiore 0:6ce1803b7a03 147 logInfo("setting APN");
mfiore 0:6ce1803b7a03 148 if (radio->setApn(apn) != MTS_SUCCESS)
mfiore 0:6ce1803b7a03 149 logError("failed to set APN to \"%s\"", apn);
BlueShadow 2:1df4b48824b1 150 logInfo("APN set successful");
BlueShadow 2:1df4b48824b1 151
BlueShadow 6:8f1ad9d2193e 152 Timer post_timer;
BlueShadow 6:8f1ad9d2193e 153 post_timer.start();
BlueShadow 6:8f1ad9d2193e 154 static int post_interval_ms = 500; //************* I don't want to wait 30 seconds ************************/
BlueShadow 6:8f1ad9d2193e 155 int timeStamp;
BlueShadow 6:8f1ad9d2193e 156 int countingLoop = 0;
BlueShadow 4:d16e07588838 157
BlueShadow 6:8f1ad9d2193e 158 logInfo("Entering loop");
BlueShadow 6:8f1ad9d2193e 159 while (countingLoop < 5 ) {
BlueShadow 6:8f1ad9d2193e 160 if (post_timer.read_ms() > post_interval_ms ) { // can this be changed to seconds?
BlueShadow 6:8f1ad9d2193e 161 timeStamp = post_timer.read_ms();
BlueShadow 6:8f1ad9d2193e 162 logDebug("timer read %d", timeStamp);
BlueShadow 6:8f1ad9d2193e 163 logDebug("timer value %d", post_interval_ms );
BlueShadow 6:8f1ad9d2193e 164 logDebug("loop count value %d", countingLoop );
BlueShadow 2:1df4b48824b1 165
BlueShadow 6:8f1ad9d2193e 166 accel.acquire_accel_data_g(accel_data);
BlueShadow 6:8f1ad9d2193e 167 float dataX = accel_data[0];
BlueShadow 6:8f1ad9d2193e 168 float dataY = accel_data[1];
BlueShadow 6:8f1ad9d2193e 169 float dataZ = accel_data[2];
BlueShadow 5:dbedb2422089 170
BlueShadow 6:8f1ad9d2193e 171 logDebug("\r\nPosting Accel Readings X: %f Y: %f Z: %f \r\n",dataX,dataY,dataZ);
BlueShadow 4:d16e07588838 172
BlueShadow 6:8f1ad9d2193e 173 logDebug("https://quickstart.internetofthings.ibmcloud.com");
BlueShadow 6:8f1ad9d2193e 174
BlueShadow 6:8f1ad9d2193e 175 //http_tx.clear();
BlueShadow 4:d16e07588838 176
BlueShadow 6:8f1ad9d2193e 177 logInfo("bringing up the link");
BlueShadow 6:8f1ad9d2193e 178 if (! radio->connect()) {
BlueShadow 6:8f1ad9d2193e 179 logError("failed to bring up the link");
BlueShadow 6:8f1ad9d2193e 180 //return 0;
BlueShadow 6:8f1ad9d2193e 181 } else {
BlueShadow 2:1df4b48824b1 182
BlueShadow 2:1df4b48824b1 183 // HTTPClient object used for HTTP requests.
BlueShadow 6:8f1ad9d2193e 184 HTTPClient http;
BlueShadow 2:1df4b48824b1 185
BlueShadow 2:1df4b48824b1 186 // Enable strict certificate validation.
BlueShadow 6:8f1ad9d2193e 187 http.setPeerVerification(VERIFY_PEER);
BlueShadow 2:1df4b48824b1 188
BlueShadow 2:1df4b48824b1 189 // Load certificates defined in ssl_certificates.h.
BlueShadow 2:1df4b48824b1 190 // See comments in ssl_certificates.h for information on how to get and format root certificates.
BlueShadow 6:8f1ad9d2193e 191 if (http.addRootCACertificate(ssl_certificates) != HTTP_OK)
BlueShadow 6:8f1ad9d2193e 192 logError("loading SSL certificates failed");
BlueShadow 2:1df4b48824b1 193
BlueShadow 2:1df4b48824b1 194
BlueShadow 6:8f1ad9d2193e 195 char http_rx_buf[1024];
BlueShadow 6:8f1ad9d2193e 196 char http_tx_buf[1024];
BlueShadow 2:1df4b48824b1 197
BlueShadow 6:8f1ad9d2193e 198 memset(http_tx_buf, 0, sizeof(http_tx_buf));
BlueShadow 6:8f1ad9d2193e 199 memset(http_rx_buf, 0, sizeof(http_rx_buf));
BlueShadow 6:8f1ad9d2193e 200 snprintf(http_tx_buf, sizeof(http_tx_buf), "{ \"dataX\": \"%f\" , \"dataY\": \"%f\" ,\"dataZ\": \"%f\" }", dataX,dataY,dataZ);
BlueShadow 6:8f1ad9d2193e 201 logDebug("%s",http_tx_buf);
BlueShadow 6:8f1ad9d2193e 202 HTTPResult res;
BlueShadow 2:1df4b48824b1 203
BlueShadow 2:1df4b48824b1 204 // IHTTPDataIn object - will contain data received from server.
BlueShadow 6:8f1ad9d2193e 205 HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf));
BlueShadow 2:1df4b48824b1 206
BlueShadow 2:1df4b48824b1 207 // IHTTPDataOut object - contains data to be posted to server.
BlueShadow 2:1df4b48824b1 208 // HTTPJson automatically adds the JSON content-type header to the request.
BlueShadow 6:8f1ad9d2193e 209 HTTPJson http_tx(http_tx_buf, strlen(http_tx_buf)+1);
BlueShadow 2:1df4b48824b1 210
BlueShadow 2:1df4b48824b1 211 // Make a HTTP POST request to http://httpbin.org/
BlueShadow 6:8f1ad9d2193e 212 res = http.post("http://quickstart.internetofthings.ibmcloud.com/api/v0002/device/types/dragonflytype/devices/dragonfly22/events/myEvent", http_tx, &http_rx);
BlueShadow 6:8f1ad9d2193e 213 // res = http.post("http://quickstart.internetofthings.ibmcloud.com/api/v0002/device/types/dragonflytype/devices/ REPLACE WITH DEVICEID /events/myEvent", http_tx, &http_rx);
BlueShadow 6:8f1ad9d2193e 214 if (res != HTTP_OK)
BlueShadow 6:8f1ad9d2193e 215 logError("HTTPS POST to Bluemix failed [%d][%s]", res, httpResToStr(res));
BlueShadow 6:8f1ad9d2193e 216 else
BlueShadow 6:8f1ad9d2193e 217 logInfo("HTTPS POST to Bluemix succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf);
BlueShadow 2:1df4b48824b1 218
BlueShadow 2:1df4b48824b1 219 //logInfo("finished - bringing down link");
BlueShadow 6:8f1ad9d2193e 220 //radio->disconnect();
BlueShadow 2:1df4b48824b1 221 post_timer.reset();
BlueShadow 3:ff2bf7a1ece8 222 countingLoop +=1;
BlueShadow 2:1df4b48824b1 223 }
mfiore 0:6ce1803b7a03 224 }
BlueShadow 2:1df4b48824b1 225
BlueShadow 2:1df4b48824b1 226 //return 0;
mfiore 0:6ce1803b7a03 227 }
BlueShadow 4:d16e07588838 228 radio->disconnect();
BlueShadow 6:8f1ad9d2193e 229 timeStamp = post_timer.read_ms();
BlueShadow 4:d16e07588838 230 logInfo("loop timer = %d", timeStamp);
BlueShadow 4:d16e07588838 231 logInfo("\r\n\n\nEnd Of Line\r\n");
mfiore 0:6ce1803b7a03 232 }
mfiore 0:6ce1803b7a03 233
BlueShadow 2:1df4b48824b1 234 bool init_mtsas()
BlueShadow 2:1df4b48824b1 235 {
mfiore 0:6ce1803b7a03 236 io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
mfiore 0:6ce1803b7a03 237 if (! io)
mfiore 0:6ce1803b7a03 238 return false;
BlueShadow 2:1df4b48824b1 239
mfiore 0:6ce1803b7a03 240 // radio default baud rate is 115200
mfiore 0:6ce1803b7a03 241 io->baud(115200);
mfiore 0:6ce1803b7a03 242 radio = mts::CellularFactory::create(io);
mfiore 0:6ce1803b7a03 243 if (! radio)
mfiore 0:6ce1803b7a03 244 return false;
BlueShadow 2:1df4b48824b1 245
mfiore 0:6ce1803b7a03 246 // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created
mfiore 0:6ce1803b7a03 247 Transport::setTransport(radio);
BlueShadow 2:1df4b48824b1 248
mfiore 0:6ce1803b7a03 249 return true;
mfiore 0:6ce1803b7a03 250 }
mfiore 0:6ce1803b7a03 251
BlueShadow 2:1df4b48824b1 252 char* httpResToStr(HTTPResult res)
BlueShadow 2:1df4b48824b1 253 {
mfiore 0:6ce1803b7a03 254 switch(res) {
mfiore 0:6ce1803b7a03 255 case HTTP_PROCESSING:
mfiore 0:6ce1803b7a03 256 return "HTTP_PROCESSING";
mfiore 0:6ce1803b7a03 257 case HTTP_PARSE:
mfiore 0:6ce1803b7a03 258 return "HTTP_PARSE";
mfiore 0:6ce1803b7a03 259 case HTTP_DNS:
mfiore 0:6ce1803b7a03 260 return "HTTP_DNS";
mfiore 0:6ce1803b7a03 261 case HTTP_PRTCL:
mfiore 0:6ce1803b7a03 262 return "HTTP_PRTCL";
mfiore 0:6ce1803b7a03 263 case HTTP_NOTFOUND:
mfiore 0:6ce1803b7a03 264 return "HTTP_NOTFOUND";
mfiore 0:6ce1803b7a03 265 case HTTP_REFUSED:
mfiore 0:6ce1803b7a03 266 return "HTTP_REFUSED";
mfiore 0:6ce1803b7a03 267 case HTTP_ERROR:
mfiore 0:6ce1803b7a03 268 return "HTTP_ERROR";
mfiore 0:6ce1803b7a03 269 case HTTP_TIMEOUT:
mfiore 0:6ce1803b7a03 270 return "HTTP_TIMEOUT";
mfiore 0:6ce1803b7a03 271 case HTTP_CONN:
mfiore 0:6ce1803b7a03 272 return "HTTP_CONN";
mfiore 0:6ce1803b7a03 273 case HTTP_CLOSED:
mfiore 0:6ce1803b7a03 274 return "HTTP_CLOSED";
mfiore 0:6ce1803b7a03 275 case HTTP_REDIRECT:
mfiore 0:6ce1803b7a03 276 return "HTTP_REDIRECT";
mfiore 0:6ce1803b7a03 277 case HTTP_OK:
mfiore 0:6ce1803b7a03 278 return "HTTP_OK";
mfiore 0:6ce1803b7a03 279 default:
mfiore 0:6ce1803b7a03 280 return "HTTP Result unknown";
mfiore 0:6ce1803b7a03 281 }
mfiore 0:6ce1803b7a03 282 }
BlueShadow 2:1df4b48824b1 283