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:
Fri Aug 12 16:15:00 2016 +0000
Revision:
2:1df4b48824b1
Parent:
1:26b8af61d0ac
Child:
3:ff2bf7a1ece8
Sending data to IBM with ALS every 2 seconds for debug.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BlueShadow 2:1df4b48824b1 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 2:1df4b48824b1 13 * - Rohm Electronics Sensor Board
BlueShadow 2:1df4b48824b1 14 * - Expansion board (LSM6DS0
BlueShadow 2:1df4b48824b1 15 * 3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis
BlueShadow 2:1df4b48824b1 16 * magnetometer, HTS221 humidity and temperature sensor and LPS25HB
BlueShadow 2:1df4b48824b1 17 * pressure sensor)
BlueShadow 2:1df4b48824b1 18 *
BlueShadow 2:1df4b48824b1 19 * What this program does:
BlueShadow 2:1df4b48824b1 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 2:1df4b48824b1 25 * - you need to set the "VENDOR" and "MODEL"
BlueShadow 2:1df4b48824b1 26
BlueShadow 2:1df4b48824b1 27 * - you need to set the "do_cloud_post" flag to true for this to
BlueShadow 2:1df4b48824b1 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 2:1df4b48824b1 44 Sample Program Description:
BlueShadow 2:1df4b48824b1 45 This Program will enable to Multi-Tech Dragonfly platform to utilize ROHM's Multi-sensor Shield Board.
BlueShadow 2:1df4b48824b1 46 This program will initialize all sensors on the shield and then read back the sensor data.
BlueShadow 2:1df4b48824b1 47 Data will then be output to the UART Debug Terminal every 1 second.
BlueShadow 2:1df4b48824b1 48
BlueShadow 2:1df4b48824b1 49 Sample Program Author:
BlueShadow 2:1df4b48824b1 50 ROHM USDC
BlueShadow 2:1df4b48824b1 51
BlueShadow 2:1df4b48824b1 52 Additional Resources:
BlueShadow 2:1df4b48824b1 53 ROHM Sensor Shield GitHub Repository: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield
mfiore 0:6ce1803b7a03 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 2:1df4b48824b1 61 #include <sstream>
BlueShadow 2:1df4b48824b1 62
BlueShadow 2:1df4b48824b1 63
BlueShadow 2:1df4b48824b1 64
BlueShadow 2:1df4b48824b1 65 //-------- Customise these values -----------
BlueShadow 2:1df4b48824b1 66
BlueShadow 2:1df4b48824b1 67 #define ORG "quickstart" // your organization or "quickstart"
BlueShadow 2:1df4b48824b1 68 #define DEVICE_TYPE "dragonflytype" // use this default for quickstart or customize to your registered device type
BlueShadow 2:1df4b48824b1 69 #define DEVICE_ID "dragonfly01" // use this default for quickstart or customize to your registered device id
BlueShadow 2:1df4b48824b1 70 #define TOKEN "pauljaeger" // not used with "quickstart"
BlueShadow 2:1df4b48824b1 71 #define EVENT "myEvent" // use this default or customize to your event type
BlueShadow 2:1df4b48824b1 72 //-------- Customise the above values --------
BlueShadow 2:1df4b48824b1 73
BlueShadow 2:1df4b48824b1 74
BlueShadow 2:1df4b48824b1 75 //char BlueMix_HEADER[] = "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
BlueShadow 2:1df4b48824b1 76 char BlueMix_HEADER[] = "Content-Type: application/json";
BlueShadow 2:1df4b48824b1 77
BlueShadow 2:1df4b48824b1 78 const char BlueMix_URL[] = "http://" ORG ".internetofthings.ibmcloud.com/api/v0002/device/types/" DEVICE_TYPE "/devices/" DEVICE_ID "/events/" EVENT;
BlueShadow 2:1df4b48824b1 79 // replace typeID and deviceId with "" and delete ()
BlueShadow 2:1df4b48824b1 80 // replace $eventId = IBM will define in class.
BlueShadow 2:1df4b48824b1 81
BlueShadow 2:1df4b48824b1 82
BlueShadow 2:1df4b48824b1 83 //*****************************************************************************************************************************************************
BlueShadow 2:1df4b48824b1 84 DigitalOut Led1Out(LED1);
BlueShadow 2:1df4b48824b1 85
mfiore 0:6ce1803b7a03 86
mfiore 1:26b8af61d0ac 87 // This line controls the regulator's battery charger.
mfiore 1:26b8af61d0ac 88 // BC_NCE = 0 enables the battery charger
mfiore 1:26b8af61d0ac 89 // BC_NCE = 1 disables the battery charger
mfiore 1:26b8af61d0ac 90 DigitalOut bc_nce(PB_2);
mfiore 1:26b8af61d0ac 91
mfiore 0:6ce1803b7a03 92 bool init_mtsas();
mfiore 0:6ce1803b7a03 93 char* httpResToStr(HTTPResult res);
mfiore 0:6ce1803b7a03 94
mfiore 0:6ce1803b7a03 95 // The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio.
mfiore 0:6ce1803b7a03 96 mts::MTSSerialFlowControl* io;
mfiore 0:6ce1803b7a03 97 // The Cellular object represents the cellular radio.
mfiore 0:6ce1803b7a03 98 mts::Cellular* radio;
mfiore 0:6ce1803b7a03 99
mfiore 0:6ce1803b7a03 100 // An APN is required for GSM radios.
BlueShadow 2:1df4b48824b1 101 static const char apn[] = "iot.aer.net";
mfiore 0:6ce1803b7a03 102
mfiore 0:6ce1803b7a03 103 bool radio_ok = false;
mfiore 0:6ce1803b7a03 104
BlueShadow 2:1df4b48824b1 105
BlueShadow 2:1df4b48824b1 106 #define RPR0521 //RPR0521 Ambient Light Sensor.
BlueShadow 2:1df4b48824b1 107 //Define Pins for I2C Interface
BlueShadow 2:1df4b48824b1 108 I2C i2c(I2C_SDA, I2C_SCL);
BlueShadow 2:1df4b48824b1 109 bool RepStart = true;
BlueShadow 2:1df4b48824b1 110 bool NoRepStart = false;
BlueShadow 2:1df4b48824b1 111
BlueShadow 2:1df4b48824b1 112 #ifdef RPR0521
BlueShadow 2:1df4b48824b1 113 int RPR0521_addr_w = 0x70; //7bit addr = 0x38, with write bit 0
BlueShadow 2:1df4b48824b1 114 int RPR0521_addr_r = 0x71; //7bit addr = 0x38, with read bit 1
BlueShadow 2:1df4b48824b1 115 char RPR0521_ModeControl[2] = {0x41, 0xE6};
BlueShadow 2:1df4b48824b1 116 char RPR0521_ALSPSControl[2] = {0x42, 0x03};
BlueShadow 2:1df4b48824b1 117 char RPR0521_Persist[2] = {0x43, 0x20};
BlueShadow 2:1df4b48824b1 118 char RPR0521_Addr_ReadData = 0x44;
BlueShadow 2:1df4b48824b1 119 char RPR0521_Content_ReadData[6];
BlueShadow 2:1df4b48824b1 120 int RPR0521_PS_RAWOUT = 0; //this is an output
BlueShadow 2:1df4b48824b1 121 float RPR0521_PS_OUT = 0;
BlueShadow 2:1df4b48824b1 122 int RPR0521_ALS_D0_RAWOUT = 0;
BlueShadow 2:1df4b48824b1 123 int RPR0521_ALS_D1_RAWOUT = 0;
BlueShadow 2:1df4b48824b1 124 float RPR0521_ALS_DataRatio = 0;
BlueShadow 2:1df4b48824b1 125 float RPR0521_ALS_OUT = 0; //this is an output
BlueShadow 2:1df4b48824b1 126 float RPR0521_ALS[2]; // is this ok taking an int to the [0] value and float to [1]???????????
BlueShadow 2:1df4b48824b1 127 #endif
BlueShadow 2:1df4b48824b1 128
BlueShadow 2:1df4b48824b1 129 bool init_mtsas();
BlueShadow 2:1df4b48824b1 130 void ReadRPR0521_ALS ();
BlueShadow 2:1df4b48824b1 131 char* httpResToStr(HTTPResult res);
BlueShadow 2:1df4b48824b1 132
BlueShadow 2:1df4b48824b1 133
BlueShadow 2:1df4b48824b1 134 namespace patch // I have no idea why this is in the code *********************
BlueShadow 2:1df4b48824b1 135 {
BlueShadow 2:1df4b48824b1 136 template < typename T > std::string to_string( const T& n )
BlueShadow 2:1df4b48824b1 137 {
BlueShadow 2:1df4b48824b1 138 std::ostringstream stm ;
BlueShadow 2:1df4b48824b1 139 stm << n ;
BlueShadow 2:1df4b48824b1 140 return stm.str() ;
BlueShadow 2:1df4b48824b1 141 }
BlueShadow 2:1df4b48824b1 142 }
BlueShadow 2:1df4b48824b1 143
BlueShadow 2:1df4b48824b1 144
BlueShadow 2:1df4b48824b1 145 /****************************************************************************************************
BlueShadow 2:1df4b48824b1 146 // main
BlueShadow 2:1df4b48824b1 147 ****************************************************************************************************/
BlueShadow 2:1df4b48824b1 148
BlueShadow 2:1df4b48824b1 149 int main()
BlueShadow 2:1df4b48824b1 150 {
mfiore 1:26b8af61d0ac 151 // Disable the battery charger unless a battery is attached.
mfiore 1:26b8af61d0ac 152 bc_nce = 1;
BlueShadow 2:1df4b48824b1 153
BlueShadow 2:1df4b48824b1 154 static int post_interval_ms = 2000; //************* I don't want to wait 30 seconds ************************/
BlueShadow 2:1df4b48824b1 155
mfiore 0:6ce1803b7a03 156 // Change the baud rate of the debug port from the default 9600 to 115200.
mfiore 0:6ce1803b7a03 157 Serial debug(USBTX, USBRX);
BlueShadow 2:1df4b48824b1 158
mfiore 0:6ce1803b7a03 159 debug.baud(115200);
BlueShadow 2:1df4b48824b1 160
mfiore 0:6ce1803b7a03 161 //Sets the log level to INFO, higher log levels produce more log output.
mfiore 0:6ce1803b7a03 162 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
BlueShadow 2:1df4b48824b1 163 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
BlueShadow 2:1df4b48824b1 164
BlueShadow 2:1df4b48824b1 165
BlueShadow 2:1df4b48824b1 166
BlueShadow 2:1df4b48824b1 167 //****************************************************************************************************
BlueShadow 2:1df4b48824b1 168 // Initialize I2C Devices ************
BlueShadow 2:1df4b48824b1 169 //****************************************************************************************************/
BlueShadow 2:1df4b48824b1 170
BlueShadow 2:1df4b48824b1 171 #ifdef RPR0521
BlueShadow 2:1df4b48824b1 172 i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false);
BlueShadow 2:1df4b48824b1 173 i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false);
BlueShadow 2:1df4b48824b1 174 i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false);
BlueShadow 2:1df4b48824b1 175 #endif
BlueShadow 2:1df4b48824b1 176
BlueShadow 2:1df4b48824b1 177 //****************************************************************************************************/
BlueShadow 2:1df4b48824b1 178 // Initialization Radio Section **********************************************************
BlueShadow 2:1df4b48824b1 179 //****************************************************************************************************/
mfiore 0:6ce1803b7a03 180 logInfo("initializing cellular radio");
mfiore 0:6ce1803b7a03 181 radio_ok = init_mtsas();
mfiore 0:6ce1803b7a03 182 if (! radio_ok) {
mfiore 0:6ce1803b7a03 183 while (true) {
mfiore 0:6ce1803b7a03 184 logError("failed to initialize cellular radio");
mfiore 0:6ce1803b7a03 185 wait(1);
mfiore 0:6ce1803b7a03 186 }
mfiore 0:6ce1803b7a03 187 }
BlueShadow 2:1df4b48824b1 188
BlueShadow 2:1df4b48824b1 189
mfiore 0:6ce1803b7a03 190 logInfo("setting APN");
mfiore 0:6ce1803b7a03 191 if (radio->setApn(apn) != MTS_SUCCESS)
mfiore 0:6ce1803b7a03 192 logError("failed to set APN to \"%s\"", apn);
BlueShadow 2:1df4b48824b1 193 logInfo("APN set successful");
BlueShadow 2:1df4b48824b1 194
BlueShadow 2:1df4b48824b1 195 Timer post_timer;
BlueShadow 2:1df4b48824b1 196 post_timer.start();
BlueShadow 2:1df4b48824b1 197 int timeStamp;
BlueShadow 2:1df4b48824b1 198
BlueShadow 2:1df4b48824b1 199 logInfo("Entering loop");
BlueShadow 2:1df4b48824b1 200 while (true) {
BlueShadow 2:1df4b48824b1 201 if (post_timer.read_ms() > post_interval_ms ) { // can this be changed to seconds?
BlueShadow 2:1df4b48824b1 202 timeStamp = post_timer.read_ms();
BlueShadow 2:1df4b48824b1 203 logDebug("timer read %d", timeStamp);
BlueShadow 2:1df4b48824b1 204 logDebug("timer value %d", post_interval_ms );
BlueShadow 2:1df4b48824b1 205
BlueShadow 2:1df4b48824b1 206 #ifdef RPR0521 //als digital
BlueShadow 2:1df4b48824b1 207 ReadRPR0521_ALS ();
BlueShadow 2:1df4b48824b1 208 int sensor_data = RPR0521_ALS[0];
BlueShadow 2:1df4b48824b1 209 int proximity_data = RPR0521_ALS[1];
BlueShadow 2:1df4b48824b1 210 #else
BlueShadow 2:1df4b48824b1 211 int sensor_data = -1;
BlueShadow 2:1df4b48824b1 212 int proximity_data = -1;
BlueShadow 2:1df4b48824b1 213 #endif
BlueShadow 2:1df4b48824b1 214 logDebug("\r\nPosting sensor data %d",sensor_data);
BlueShadow 2:1df4b48824b1 215 std::string sensor_data_str = patch::to_string(sensor_data);
BlueShadow 2:1df4b48824b1 216 logDebug("Sensor data string: %s",sensor_data_str.c_str());
BlueShadow 2:1df4b48824b1 217
BlueShadow 2:1df4b48824b1 218 // http_tx.clear();
BlueShadow 2:1df4b48824b1 219
BlueShadow 2:1df4b48824b1 220 logInfo("bringing up the link");
BlueShadow 2:1df4b48824b1 221 if (! radio->connect()) {
BlueShadow 2:1df4b48824b1 222 logError("failed to bring up the link");
BlueShadow 2:1df4b48824b1 223 //return 0;
BlueShadow 2:1df4b48824b1 224 } else {
BlueShadow 2:1df4b48824b1 225
BlueShadow 2:1df4b48824b1 226 // HTTPClient object used for HTTP requests.
BlueShadow 2:1df4b48824b1 227 HTTPClient http;
BlueShadow 2:1df4b48824b1 228
BlueShadow 2:1df4b48824b1 229 // Enable strict certificate validation.
BlueShadow 2:1df4b48824b1 230 http.setPeerVerification(VERIFY_PEER);
BlueShadow 2:1df4b48824b1 231
BlueShadow 2:1df4b48824b1 232 // Load certificates defined in ssl_certificates.h.
BlueShadow 2:1df4b48824b1 233 // See comments in ssl_certificates.h for information on how to get and format root certificates.
BlueShadow 2:1df4b48824b1 234 if (http.addRootCACertificate(ssl_certificates) != HTTP_OK)
BlueShadow 2:1df4b48824b1 235 logError("loading SSL certificates failed");
BlueShadow 2:1df4b48824b1 236
BlueShadow 2:1df4b48824b1 237 // HTTP POST example - QUickstart
BlueShadow 2:1df4b48824b1 238
BlueShadow 2:1df4b48824b1 239 char http_rx_buf[1024];
BlueShadow 2:1df4b48824b1 240 char http_tx_buf[1024];
BlueShadow 2:1df4b48824b1 241
BlueShadow 2:1df4b48824b1 242 //sprintf(http_tx_buf,"\"Light\": \"%d\" \"Proximity\": \"%d\"",sensor_data, proximity_data);
BlueShadow 2:1df4b48824b1 243 memset(http_tx_buf, 0, sizeof(http_tx_buf));
BlueShadow 2:1df4b48824b1 244 memset(http_rx_buf, 0, sizeof(http_rx_buf));
BlueShadow 2:1df4b48824b1 245 snprintf(http_tx_buf, sizeof(http_tx_buf), "{ \"Light\": \"%d\" }", sensor_data);
BlueShadow 2:1df4b48824b1 246 logDebug("%s",http_tx_buf);
BlueShadow 2:1df4b48824b1 247 HTTPResult res;
BlueShadow 2:1df4b48824b1 248
BlueShadow 2:1df4b48824b1 249 // char http_tx_buf[] = "{ \"temperature\": \"715\" }";
BlueShadow 2:1df4b48824b1 250 // char http_tx_buf[] = "{ \"temperature\": \"" + sensor_data_str.c_str() + "\" }"; //failed to compile
BlueShadow 2:1df4b48824b1 251 // char http_tx_buf[] = "{ \"temperature\": \"715\"";
BlueShadow 2:1df4b48824b1 252 // strcat (http_tx_buf,sensor_data_str.c_str());
BlueShadow 2:1df4b48824b1 253 // strcat (http_tx_buf, "\" }");
BlueShadow 2:1df4b48824b1 254 // logDebug ("\r\n %s %d \r\n", http_tx_buf, sizeof(http_tx_buf));
BlueShadow 2:1df4b48824b1 255
BlueShadow 2:1df4b48824b1 256
BlueShadow 2:1df4b48824b1 257 //string txx_buf = "{ \"temperature\": \"";
BlueShadow 2:1df4b48824b1 258 //strcat (txx_buf,sensor_data_str.c_str());
BlueShadow 2:1df4b48824b1 259 //strcat (txx_buf, "\" }");
BlueShadow 2:1df4b48824b1 260 //logDebug ("\r\n %s %d \r\n", txx_buf.c_str(), txx_buf.size());
BlueShadow 2:1df4b48824b1 261 //http_tx_buf = txx_bug.c_str();
BlueShadow 2:1df4b48824b1 262
BlueShadow 2:1df4b48824b1 263
BlueShadow 2:1df4b48824b1 264 // IHTTPDataIn object - will contain data received from server.
BlueShadow 2:1df4b48824b1 265 HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf));
BlueShadow 2:1df4b48824b1 266
BlueShadow 2:1df4b48824b1 267 // IHTTPDataOut object - contains data to be posted to server.
BlueShadow 2:1df4b48824b1 268 // HTTPJson automatically adds the JSON content-type header to the request.
BlueShadow 2:1df4b48824b1 269 HTTPJson http_tx(http_tx_buf, strlen(http_tx_buf)+1);
BlueShadow 2:1df4b48824b1 270
BlueShadow 2:1df4b48824b1 271 // Make a HTTP POST request to http://httpbin.org/
BlueShadow 2:1df4b48824b1 272 res = http.post("http://quickstart.internetofthings.ibmcloud.com/api/v0002/device/types/dragonflytype/devices/dragonfly01/events/myEvent", http_tx, &http_rx);
BlueShadow 2:1df4b48824b1 273 if (res != HTTP_OK)
BlueShadow 2:1df4b48824b1 274 logError("HTTPS POST to Bluemix failed [%d][%s]", res, httpResToStr(res));
BlueShadow 2:1df4b48824b1 275 else
BlueShadow 2:1df4b48824b1 276 logInfo("HTTPS POST to Bluemix succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf);
BlueShadow 2:1df4b48824b1 277
BlueShadow 2:1df4b48824b1 278 //logInfo("finished - bringing down link");
BlueShadow 2:1df4b48824b1 279 radio->disconnect();
BlueShadow 2:1df4b48824b1 280 post_timer.reset();
BlueShadow 2:1df4b48824b1 281 }
mfiore 0:6ce1803b7a03 282 }
BlueShadow 2:1df4b48824b1 283
BlueShadow 2:1df4b48824b1 284 //return 0;
mfiore 0:6ce1803b7a03 285 }
BlueShadow 2:1df4b48824b1 286 //radio->disconnect();
mfiore 0:6ce1803b7a03 287 }
mfiore 0:6ce1803b7a03 288
BlueShadow 2:1df4b48824b1 289 bool init_mtsas()
BlueShadow 2:1df4b48824b1 290 {
mfiore 0:6ce1803b7a03 291 io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
mfiore 0:6ce1803b7a03 292 if (! io)
mfiore 0:6ce1803b7a03 293 return false;
BlueShadow 2:1df4b48824b1 294
mfiore 0:6ce1803b7a03 295 // radio default baud rate is 115200
mfiore 0:6ce1803b7a03 296 io->baud(115200);
mfiore 0:6ce1803b7a03 297 radio = mts::CellularFactory::create(io);
mfiore 0:6ce1803b7a03 298 if (! radio)
mfiore 0:6ce1803b7a03 299 return false;
BlueShadow 2:1df4b48824b1 300
mfiore 0:6ce1803b7a03 301 // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created
mfiore 0:6ce1803b7a03 302 Transport::setTransport(radio);
BlueShadow 2:1df4b48824b1 303
mfiore 0:6ce1803b7a03 304 return true;
mfiore 0:6ce1803b7a03 305 }
mfiore 0:6ce1803b7a03 306
BlueShadow 2:1df4b48824b1 307 char* httpResToStr(HTTPResult res)
BlueShadow 2:1df4b48824b1 308 {
mfiore 0:6ce1803b7a03 309 switch(res) {
mfiore 0:6ce1803b7a03 310 case HTTP_PROCESSING:
mfiore 0:6ce1803b7a03 311 return "HTTP_PROCESSING";
mfiore 0:6ce1803b7a03 312 case HTTP_PARSE:
mfiore 0:6ce1803b7a03 313 return "HTTP_PARSE";
mfiore 0:6ce1803b7a03 314 case HTTP_DNS:
mfiore 0:6ce1803b7a03 315 return "HTTP_DNS";
mfiore 0:6ce1803b7a03 316 case HTTP_PRTCL:
mfiore 0:6ce1803b7a03 317 return "HTTP_PRTCL";
mfiore 0:6ce1803b7a03 318 case HTTP_NOTFOUND:
mfiore 0:6ce1803b7a03 319 return "HTTP_NOTFOUND";
mfiore 0:6ce1803b7a03 320 case HTTP_REFUSED:
mfiore 0:6ce1803b7a03 321 return "HTTP_REFUSED";
mfiore 0:6ce1803b7a03 322 case HTTP_ERROR:
mfiore 0:6ce1803b7a03 323 return "HTTP_ERROR";
mfiore 0:6ce1803b7a03 324 case HTTP_TIMEOUT:
mfiore 0:6ce1803b7a03 325 return "HTTP_TIMEOUT";
mfiore 0:6ce1803b7a03 326 case HTTP_CONN:
mfiore 0:6ce1803b7a03 327 return "HTTP_CONN";
mfiore 0:6ce1803b7a03 328 case HTTP_CLOSED:
mfiore 0:6ce1803b7a03 329 return "HTTP_CLOSED";
mfiore 0:6ce1803b7a03 330 case HTTP_REDIRECT:
mfiore 0:6ce1803b7a03 331 return "HTTP_REDIRECT";
mfiore 0:6ce1803b7a03 332 case HTTP_OK:
mfiore 0:6ce1803b7a03 333 return "HTTP_OK";
mfiore 0:6ce1803b7a03 334 default:
mfiore 0:6ce1803b7a03 335 return "HTTP Result unknown";
mfiore 0:6ce1803b7a03 336 }
mfiore 0:6ce1803b7a03 337 }
BlueShadow 2:1df4b48824b1 338
BlueShadow 2:1df4b48824b1 339
BlueShadow 2:1df4b48824b1 340 #ifdef RPR0521 //als digital
BlueShadow 2:1df4b48824b1 341 void ReadRPR0521_ALS ()
BlueShadow 2:1df4b48824b1 342 {
BlueShadow 2:1df4b48824b1 343 i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart);
BlueShadow 2:1df4b48824b1 344 i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart);
BlueShadow 2:1df4b48824b1 345
BlueShadow 2:1df4b48824b1 346 RPR0521_ALS[1] = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]);
BlueShadow 2:1df4b48824b1 347 RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]);
BlueShadow 2:1df4b48824b1 348 RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]);
BlueShadow 2:1df4b48824b1 349 RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT;
BlueShadow 2:1df4b48824b1 350
BlueShadow 2:1df4b48824b1 351 if(RPR0521_ALS_DataRatio < (float)0.595) {
BlueShadow 2:1df4b48824b1 352 RPR0521_ALS[0] = ((float)1.682*(float)RPR0521_ALS_D0_RAWOUT - (float)1.877*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 2:1df4b48824b1 353 } else if(RPR0521_ALS_DataRatio < (float)1.015) {
BlueShadow 2:1df4b48824b1 354 RPR0521_ALS[0] = ((float)0.644*(float)RPR0521_ALS_D0_RAWOUT - (float)0.132*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 2:1df4b48824b1 355 } else if(RPR0521_ALS_DataRatio < (float)1.352) {
BlueShadow 2:1df4b48824b1 356 RPR0521_ALS[0] = ((float)0.756*(float)RPR0521_ALS_D0_RAWOUT - (float)0.243*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 2:1df4b48824b1 357 } else if(RPR0521_ALS_DataRatio < (float)3.053) {
BlueShadow 2:1df4b48824b1 358 RPR0521_ALS[0] = ((float)0.766*(float)RPR0521_ALS_D0_RAWOUT - (float)0.25*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 2:1df4b48824b1 359 } else {
BlueShadow 2:1df4b48824b1 360 RPR0521_ALS[0] = 0;
BlueShadow 2:1df4b48824b1 361 }
BlueShadow 2:1df4b48824b1 362 logDebug("RPR-0521 ALS/PROX Sensor Data:\r\n");
BlueShadow 2:1df4b48824b1 363 logDebug(" ALS = %0.2f lx\r\n", RPR0521_ALS[0]);
BlueShadow 2:1df4b48824b1 364 logDebug(" PROX= %0.2f ADC Counts\r\n", RPR0521_ALS[1]); //defined as a float but is an unsigned.
BlueShadow 2:1df4b48824b1 365
BlueShadow 2:1df4b48824b1 366 }
BlueShadow 2:1df4b48824b1 367 #endif