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:46:22 2016 +0000
Revision:
7:3d2a3fd4a523
Parent:
6:8f1ad9d2193e
Class ready example for Bluemix, NXP Freescale sensor, Aeris, Dragonfly

Who changed what in which revision?

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