Avnet FAE Training class using Dragonfly, Aeris SIM, Exostie Cloud, Rohm Sensor board

Dependencies:   MbedJSONValue mbed mtsas

Fork of UUU_MultiTech_Dragonfly_Sprint_SF by Paul Jaeger

Committer:
BlueShadow
Date:
Wed Jul 27 23:31:42 2016 +0000
Revision:
10:75acd8caa0d3
Parent:
9:6475e1b83491
MultiTech Dragonfly utilizing Rohm Sensor board, Aeris SIM, Exosite Cloud for Avent FAE training event.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:a44e71488e1f 1 /*************************************************************************
BlueShadow 8:e78dcfad254b 2 * Dragonfly Example program for 2016 Sprint Exosite Training
BlueShadow 6:7946b5c2376a 3 *
mfiore 0:a44e71488e1f 4 * The following hardware is required to successfully run this program:
mfiore 0:a44e71488e1f 5 * - MultiTech UDK2 (4" square white PCB with Arduino headers, antenna
mfiore 0:a44e71488e1f 6 * connector, micro USB ports, and 40-pin connector for Dragonfly)
mfiore 0:a44e71488e1f 7 * - MultiTech Dragonfly (1"x2" green PCB with Telit radio)
BlueShadow 8:e78dcfad254b 8 * - Rohm Electronics Sensor Board
BlueShadow 8:e78dcfad254b 9
BlueShadow 8:e78dcfad254b 10 * - Expansion board (LSM6DS0
mfiore 0:a44e71488e1f 11 * 3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis
mfiore 0:a44e71488e1f 12 * magnetometer, HTS221 humidity and temperature sensor and LPS25HB
mfiore 0:a44e71488e1f 13 * pressure sensor)
mfiore 0:a44e71488e1f 14 *
mfiore 0:a44e71488e1f 15 * What this program does:
BlueShadow 8:e78dcfad254b 16 * - reads data from all sensors on board
mfiore 0:a44e71488e1f 17 * - prints all sensor data to debug port on a periodic basis
BlueShadow 8:e78dcfad254b 18 * - optionally send data to Exosite
BlueShadow 8:e78dcfad254b 19 * - All data is sent to a specific location determined by the student login.
BlueShadow 8:e78dcfad254b 20 * - Exosite cloud platform (user must create own account and configure a device
BlueShadow 8:e78dcfad254b 21 * - you need to set the "VENDOR" and "MODEL"
BlueShadow 8:e78dcfad254b 22
mfiore 5:a946ef74a8c4 23 * - you need to set the "do_cloud_post" flag to true for this to
mfiore 5:a946ef74a8c4 24 * work
mfiore 0:a44e71488e1f 25 *
mfiore 0:a44e71488e1f 26 * Setup:
mfiore 0:a44e71488e1f 27 * - Correctly insert SIM card into Dragonfly
mfiore 0:a44e71488e1f 28 * - Seat the Dragonfly on the UDK2 board
mfiore 0:a44e71488e1f 29 * - Connect an antenna to the connector on the Dragonfly labled "M"
mfiore 0:a44e71488e1f 30 * - Stack the Base Shield on the UDK2 Arduino headers
mfiore 0:a44e71488e1f 31 * - Stack the MEMs board on top of the Base Shield
mfiore 0:a44e71488e1f 32 * - Plug in the power cable
mfiore 0:a44e71488e1f 33 * - Plug a micro USB cable into the port below and slightly to the
mfiore 0:a44e71488e1f 34 * left of the Dragonfly (NOT the port on the Dragonfly)
mfiore 0:a44e71488e1f 35 *
mfiore 0:a44e71488e1f 36 * Go have fun and make something cool!
mfiore 0:a44e71488e1f 37 *
mfiore 0:a44e71488e1f 38 ************************************************************************/
BlueShadow 6:7946b5c2376a 39 /*
BlueShadow 6:7946b5c2376a 40 Sample Program Description:
BlueShadow 6:7946b5c2376a 41 This Program will enable to Multi-Tech Dragonfly platform to utilize ROHM's Multi-sensor Shield Board.
BlueShadow 6:7946b5c2376a 42 This program will initialize all sensors on the shield and then read back the sensor data.
BlueShadow 6:7946b5c2376a 43 Data will then be output to the UART Debug Terminal every 1 second.
BlueShadow 6:7946b5c2376a 44
BlueShadow 6:7946b5c2376a 45 Sample Program Author:
BlueShadow 6:7946b5c2376a 46 ROHM USDC
BlueShadow 6:7946b5c2376a 47
BlueShadow 6:7946b5c2376a 48 Additional Resources:
BlueShadow 6:7946b5c2376a 49 ROHM Sensor Shield GitHub Repository: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield
BlueShadow 6:7946b5c2376a 50 */
BlueShadow 6:7946b5c2376a 51
BlueShadow 6:7946b5c2376a 52
mfiore 0:a44e71488e1f 53 #include "mbed.h"
mfiore 0:a44e71488e1f 54 #include "mtsas.h"
mfiore 0:a44e71488e1f 55 #include <string>
BlueShadow 8:e78dcfad254b 56 #include <sstream>
BlueShadow 8:e78dcfad254b 57
BlueShadow 10:75acd8caa0d3 58 #define EXOSITE_CIK "< YOUR NUMBER HERE >"
BlueShadow 8:e78dcfad254b 59
BlueShadow 10:75acd8caa0d3 60 char EXOSITE_HEADER[] = "X-Exosite-CIK: " EXOSITE_CIK "\r\nContent-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
BlueShadow 10:75acd8caa0d3 61 const char EXOSITE_URL[] = "https://m2.exosite.com:443/onep:v1/stack/alias?state";
BlueShadow 10:75acd8caa0d3 62
BlueShadow 8:e78dcfad254b 63
BlueShadow 8:e78dcfad254b 64 DigitalOut Led1Out(LED1);
mfiore 0:a44e71488e1f 65
mfiore 0:a44e71488e1f 66 // Debug serial port
mfiore 0:a44e71488e1f 67 static Serial debug(USBTX, USBRX);
mfiore 0:a44e71488e1f 68
mfiore 0:a44e71488e1f 69 // MTSSerialFlowControl - serial link between processor and radio
mfiore 1:a049d113e250 70 static MTSSerialFlowControl* io;
mfiore 0:a44e71488e1f 71
mfiore 0:a44e71488e1f 72 // Cellular - radio object for cellular operations (SMS, TCP, etc)
mfiore 0:a44e71488e1f 73 Cellular* radio;
mfiore 0:a44e71488e1f 74
mfiore 0:a44e71488e1f 75 // APN associated with SIM card
mfiore 4:730b61258422 76 // this APN should work for the AT&T SIM that came with your Dragonfly
BlueShadow 10:75acd8caa0d3 77 static const std::string apn = "< YOUR CELL PROVIDER HERE >";
mfiore 5:a946ef74a8c4 78
mfiore 5:a946ef74a8c4 79 // set to true if you want to post to the cloud
mfiore 5:a946ef74a8c4 80 //bool do_cloud_post = false;
mfiore 5:a946ef74a8c4 81 bool do_cloud_post = true;
mfiore 5:a946ef74a8c4 82
mfiore 0:a44e71488e1f 83 // variables for sensor data
mfiore 0:a44e71488e1f 84 float temp_celsius;
mfiore 0:a44e71488e1f 85 float humidity_percent;
mfiore 0:a44e71488e1f 86 float pressure_mbar;
mfiore 0:a44e71488e1f 87 float moisture_percent;
mfiore 0:a44e71488e1f 88 int32_t mag_mgauss[3];
mfiore 0:a44e71488e1f 89 int32_t acc_mg[3];
mfiore 0:a44e71488e1f 90 int32_t gyro_mdps[3];
mfiore 0:a44e71488e1f 91
mfiore 0:a44e71488e1f 92 // misc variables
BlueShadow 9:6475e1b83491 93 static char wall_of_dash[] = "--------------------------------------------------";
BlueShadow 10:75acd8caa0d3 94
BlueShadow 10:75acd8caa0d3 95 static int post_interval_ms = 30000; //************* I don't want to wait 30 seconds ************************/
mfiore 0:a44e71488e1f 96 int debug_baud = 115200;
mfiore 0:a44e71488e1f 97
BlueShadow 6:7946b5c2376a 98
BlueShadow 6:7946b5c2376a 99
BlueShadow 6:7946b5c2376a 100 /****************************************************************************************************
BlueShadow 6:7946b5c2376a 101
BlueShadow 6:7946b5c2376a 102 ****************************************************************************************************/
BlueShadow 6:7946b5c2376a 103
BlueShadow 6:7946b5c2376a 104 //Macros for checking each of the different Sensor Devices
BlueShadow 6:7946b5c2376a 105 #define AnalogTemp //BDE0600
BlueShadow 6:7946b5c2376a 106 #define AnalogUV //ML8511
BlueShadow 6:7946b5c2376a 107 #define HallSensor //BU52011
BlueShadow 6:7946b5c2376a 108 #define RPR0521 //RPR0521
BlueShadow 6:7946b5c2376a 109 #define KMX62 //KMX61, Accel/Mag
BlueShadow 6:7946b5c2376a 110 #define COLOR //BH1745
BlueShadow 6:7946b5c2376a 111 #define KX022 //KX022, Accel Only
BlueShadow 6:7946b5c2376a 112 #define Pressure //BM1383
BlueShadow 6:7946b5c2376a 113
BlueShadow 6:7946b5c2376a 114
BlueShadow 6:7946b5c2376a 115 //Define Pins for I2C Interface
BlueShadow 6:7946b5c2376a 116 I2C i2c(I2C_SDA, I2C_SCL);
BlueShadow 6:7946b5c2376a 117 bool RepStart = true;
BlueShadow 6:7946b5c2376a 118 bool NoRepStart = false;
BlueShadow 6:7946b5c2376a 119
BlueShadow 6:7946b5c2376a 120 //Define Sensor Variables
BlueShadow 6:7946b5c2376a 121 #ifdef AnalogTemp
BlueShadow 6:7946b5c2376a 122 AnalogIn BDE0600_Temp(PC_4); //Mapped to A2
BlueShadow 6:7946b5c2376a 123 uint16_t BDE0600_Temp_value;
BlueShadow 6:7946b5c2376a 124 float BDE0600_output;
BlueShadow 6:7946b5c2376a 125 #endif
BlueShadow 6:7946b5c2376a 126
BlueShadow 6:7946b5c2376a 127 #ifdef AnalogUV
BlueShadow 6:7946b5c2376a 128 AnalogIn ML8511_UV(PC_1); //Mapped to A4
BlueShadow 6:7946b5c2376a 129 uint16_t ML8511_UV_value;
BlueShadow 6:7946b5c2376a 130 float ML8511_output;
BlueShadow 6:7946b5c2376a 131 #endif
BlueShadow 6:7946b5c2376a 132
BlueShadow 6:7946b5c2376a 133 #ifdef HallSensor
BlueShadow 6:7946b5c2376a 134 DigitalIn Hall_GPIO0(PC_8);
BlueShadow 6:7946b5c2376a 135 DigitalIn Hall_GPIO1(PB_5);
BlueShadow 6:7946b5c2376a 136 int Hall_Return1;
BlueShadow 6:7946b5c2376a 137 int Hall_Return0;
BlueShadow 6:7946b5c2376a 138 int32_t Hall_Return[2];
BlueShadow 6:7946b5c2376a 139 #endif
BlueShadow 6:7946b5c2376a 140
BlueShadow 6:7946b5c2376a 141 #ifdef RPR0521
BlueShadow 6:7946b5c2376a 142 int RPR0521_addr_w = 0x70; //7bit addr = 0x38, with write bit 0
BlueShadow 6:7946b5c2376a 143 int RPR0521_addr_r = 0x71; //7bit addr = 0x38, with read bit 1
BlueShadow 6:7946b5c2376a 144 char RPR0521_ModeControl[2] = {0x41, 0xE6};
BlueShadow 6:7946b5c2376a 145 char RPR0521_ALSPSControl[2] = {0x42, 0x03};
BlueShadow 6:7946b5c2376a 146 char RPR0521_Persist[2] = {0x43, 0x20};
BlueShadow 6:7946b5c2376a 147 char RPR0521_Addr_ReadData = 0x44;
BlueShadow 6:7946b5c2376a 148 char RPR0521_Content_ReadData[6];
BlueShadow 6:7946b5c2376a 149 int RPR0521_PS_RAWOUT = 0; //this is an output
BlueShadow 6:7946b5c2376a 150 float RPR0521_PS_OUT = 0;
BlueShadow 6:7946b5c2376a 151 int RPR0521_ALS_D0_RAWOUT = 0;
BlueShadow 6:7946b5c2376a 152 int RPR0521_ALS_D1_RAWOUT = 0;
BlueShadow 6:7946b5c2376a 153 float RPR0521_ALS_DataRatio = 0;
BlueShadow 6:7946b5c2376a 154 float RPR0521_ALS_OUT = 0; //this is an output
BlueShadow 6:7946b5c2376a 155 float RPR0521_ALS[2]; // is this ok taking an int to the [0] value and float to [1]???????????
BlueShadow 6:7946b5c2376a 156 #endif
BlueShadow 6:7946b5c2376a 157
BlueShadow 6:7946b5c2376a 158 #ifdef KMX62
BlueShadow 6:7946b5c2376a 159 int KMX62_addr_w = 0x1C; //7bit addr = 0x38, with write bit 0
BlueShadow 6:7946b5c2376a 160 int KMX62_addr_r = 0x1D; //7bit addr = 0x38, with read bit 1
BlueShadow 6:7946b5c2376a 161 char KMX62_CNTL2[2] = {0x3A, 0x5F};
BlueShadow 6:7946b5c2376a 162 char KMX62_Addr_Accel_ReadData = 0x0A;
BlueShadow 6:7946b5c2376a 163 char KMX62_Content_Accel_ReadData[6];
BlueShadow 6:7946b5c2376a 164 char KMX62_Addr_Mag_ReadData = 0x10;
BlueShadow 6:7946b5c2376a 165 char KMX62_Content_Mag_ReadData[6];
BlueShadow 6:7946b5c2376a 166 short int MEMS_Accel_Xout = 0;
BlueShadow 6:7946b5c2376a 167 short int MEMS_Accel_Yout = 0;
BlueShadow 6:7946b5c2376a 168 short int MEMS_Accel_Zout = 0;
BlueShadow 6:7946b5c2376a 169 double MEMS_Accel_Conv_Xout = 0;
BlueShadow 6:7946b5c2376a 170 double MEMS_Accel_Conv_Yout = 0;
BlueShadow 6:7946b5c2376a 171 double MEMS_Accel_Conv_Zout = 0;
BlueShadow 6:7946b5c2376a 172
BlueShadow 6:7946b5c2376a 173 short int MEMS_Mag_Xout = 0;
BlueShadow 6:7946b5c2376a 174 short int MEMS_Mag_Yout = 0;
BlueShadow 6:7946b5c2376a 175 short int MEMS_Mag_Zout = 0;
BlueShadow 6:7946b5c2376a 176 float MEMS_Mag_Conv_Xout = 0;
BlueShadow 6:7946b5c2376a 177 float MEMS_Mag_Conv_Yout = 0;
BlueShadow 6:7946b5c2376a 178 float MEMS_Mag_Conv_Zout = 0;
BlueShadow 6:7946b5c2376a 179
BlueShadow 6:7946b5c2376a 180 double MEMS_Accel[3];
BlueShadow 6:7946b5c2376a 181 float MEMS_Mag[3];
BlueShadow 6:7946b5c2376a 182 #endif
BlueShadow 6:7946b5c2376a 183
BlueShadow 6:7946b5c2376a 184 #ifdef COLOR
BlueShadow 6:7946b5c2376a 185 int BH1745_addr_w = 0x72; //write
BlueShadow 6:7946b5c2376a 186 int BH1745_addr_r = 0x73; //read
BlueShadow 6:7946b5c2376a 187 char BH1745_persistence[2] = {0x61, 0x03};
BlueShadow 6:7946b5c2376a 188 char BH1745_mode1[2] = {0x41, 0x00};
BlueShadow 6:7946b5c2376a 189 char BH1745_mode2[2] = {0x42, 0x92};
BlueShadow 6:7946b5c2376a 190 char BH1745_mode3[2] = {0x43, 0x02};
BlueShadow 6:7946b5c2376a 191 char BH1745_Content_ReadData[6];
BlueShadow 6:7946b5c2376a 192 char BH1745_Addr_color_ReadData = 0x50;
BlueShadow 6:7946b5c2376a 193 int BH1745_Red;
BlueShadow 6:7946b5c2376a 194 int BH1745_Blue;
BlueShadow 6:7946b5c2376a 195 int BH1745_Green;
BlueShadow 6:7946b5c2376a 196 int32_t BH1745[3]; //Red, Blue Green matrix
BlueShadow 6:7946b5c2376a 197 #endif
BlueShadow 6:7946b5c2376a 198
BlueShadow 6:7946b5c2376a 199 #ifdef KX022
BlueShadow 6:7946b5c2376a 200 int KX022_addr_w = 0x3C; //write
BlueShadow 6:7946b5c2376a 201 int KX022_addr_r = 0x3D; //read
BlueShadow 6:7946b5c2376a 202 char KX022_Accel_CNTL1[2] = {0x18, 0x41};
BlueShadow 6:7946b5c2376a 203 char KX022_Accel_ODCNTL[2] = {0x1B, 0x02};
BlueShadow 6:7946b5c2376a 204 char KX022_Accel_CNTL3[2] = {0x1A, 0xD8};
BlueShadow 6:7946b5c2376a 205 char KX022_Accel_TILT_TIMER[2] = {0x22, 0x01};
BlueShadow 6:7946b5c2376a 206 char KX022_Accel_CNTL2[2] = {0x18, 0xC1};
BlueShadow 6:7946b5c2376a 207 char KX022_Content_ReadData[6];
BlueShadow 6:7946b5c2376a 208 char KX022_Addr_Accel_ReadData = 0x06;
BlueShadow 6:7946b5c2376a 209 float KX022_Accel_X;
BlueShadow 6:7946b5c2376a 210 float KX022_Accel_Y;
BlueShadow 6:7946b5c2376a 211 float KX022_Accel_Z;
BlueShadow 6:7946b5c2376a 212 short int KX022_Accel_X_RawOUT = 0;
BlueShadow 6:7946b5c2376a 213 short int KX022_Accel_Y_RawOUT = 0;
BlueShadow 6:7946b5c2376a 214 short int KX022_Accel_Z_RawOUT = 0;
BlueShadow 6:7946b5c2376a 215 int KX022_Accel_X_LB = 0;
BlueShadow 6:7946b5c2376a 216 int KX022_Accel_X_HB = 0;
BlueShadow 6:7946b5c2376a 217 int KX022_Accel_Y_LB = 0;
BlueShadow 6:7946b5c2376a 218 int KX022_Accel_Y_HB = 0;
BlueShadow 6:7946b5c2376a 219 int KX022_Accel_Z_LB = 0;
BlueShadow 6:7946b5c2376a 220 int KX022_Accel_Z_HB = 0;
BlueShadow 6:7946b5c2376a 221 float KX022_Accel[3];
BlueShadow 6:7946b5c2376a 222 #endif
BlueShadow 6:7946b5c2376a 223
BlueShadow 6:7946b5c2376a 224 #ifdef Pressure
BlueShadow 6:7946b5c2376a 225 int Press_addr_w = 0xBA; //write
BlueShadow 6:7946b5c2376a 226 int Press_addr_r = 0xBB; //read
BlueShadow 6:7946b5c2376a 227 char PWR_DOWN[2] = {0x12, 0x01};
BlueShadow 6:7946b5c2376a 228 char SLEEP[2] = {0x13, 0x01};
BlueShadow 6:7946b5c2376a 229 char Mode_Control[2] = {0x14, 0xC4};
BlueShadow 6:7946b5c2376a 230 char Press_Content_ReadData[6];
BlueShadow 6:7946b5c2376a 231 char Press_Addr_ReadData =0x1A;
BlueShadow 6:7946b5c2376a 232 int BM1383_Temp_highByte;
BlueShadow 6:7946b5c2376a 233 int BM1383_Temp_lowByte;
BlueShadow 6:7946b5c2376a 234 int BM1383_Pres_highByte;
BlueShadow 6:7946b5c2376a 235 int BM1383_Pres_lowByte;
BlueShadow 6:7946b5c2376a 236 int BM1383_Pres_leastByte;
BlueShadow 6:7946b5c2376a 237 short int BM1383_Temp_Out;
BlueShadow 6:7946b5c2376a 238 float BM1383_Temp_Conv_Out;
BlueShadow 6:7946b5c2376a 239 float BM1383_Pres_Conv_Out;
BlueShadow 6:7946b5c2376a 240 float_t BM1383[2]; // Temp is 0 and Pressure is 1
BlueShadow 6:7946b5c2376a 241 float BM1383_Var;
BlueShadow 6:7946b5c2376a 242 float BM1383_Deci;
BlueShadow 6:7946b5c2376a 243 #endif
BlueShadow 6:7946b5c2376a 244
BlueShadow 6:7946b5c2376a 245 /****************************************************************************************************
mfiore 0:a44e71488e1f 246 // function prototypes
BlueShadow 6:7946b5c2376a 247 ****************************************************************************************************/
mfiore 0:a44e71488e1f 248 bool init_mtsas();
BlueShadow 6:7946b5c2376a 249 void ReadAnalogTemp();
BlueShadow 6:7946b5c2376a 250 void ReadAnalogUV ();
BlueShadow 6:7946b5c2376a 251 void ReadHallSensor ();
BlueShadow 6:7946b5c2376a 252 void ReadCOLOR ();
BlueShadow 6:7946b5c2376a 253 void ReadRPR0521_ALS ();
BlueShadow 6:7946b5c2376a 254 void ReadKMX62_Accel ();
BlueShadow 6:7946b5c2376a 255 void ReadKMX62_Mag ();
BlueShadow 6:7946b5c2376a 256 void ReadPressure ();
BlueShadow 6:7946b5c2376a 257 void ReadKX022();
BlueShadow 8:e78dcfad254b 258 char* httpResToStr(HTTPResult res);
BlueShadow 8:e78dcfad254b 259
BlueShadow 8:e78dcfad254b 260 namespace patch
BlueShadow 8:e78dcfad254b 261 {
BlueShadow 9:6475e1b83491 262 template < typename T > std::string to_string( const T& n )
BlueShadow 9:6475e1b83491 263 {
BlueShadow 9:6475e1b83491 264 std::ostringstream stm ;
BlueShadow 9:6475e1b83491 265 stm << n ;
BlueShadow 9:6475e1b83491 266 return stm.str() ;
BlueShadow 9:6475e1b83491 267 }
BlueShadow 8:e78dcfad254b 268 }
mfiore 0:a44e71488e1f 269
BlueShadow 6:7946b5c2376a 270 /****************************************************************************************************
mfiore 0:a44e71488e1f 271 // main
BlueShadow 6:7946b5c2376a 272 ****************************************************************************************************/
BlueShadow 6:7946b5c2376a 273 int main()
BlueShadow 6:7946b5c2376a 274 {
mfiore 0:a44e71488e1f 275 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
mfiore 0:a44e71488e1f 276 debug.baud(debug_baud);
BlueShadow 6:7946b5c2376a 277
BlueShadow 6:7946b5c2376a 278 // Initialization Radio Section **********************************************************
BlueShadow 8:e78dcfad254b 279 logInfo("initializing cellular radio");
BlueShadow 8:e78dcfad254b 280 if (!init_mtsas()) {
BlueShadow 8:e78dcfad254b 281 while (true) {
BlueShadow 8:e78dcfad254b 282 logError("failed to initialize cellular radio");
BlueShadow 8:e78dcfad254b 283 wait(1);
BlueShadow 8:e78dcfad254b 284 }
BlueShadow 8:e78dcfad254b 285 }
BlueShadow 9:6475e1b83491 286
BlueShadow 8:e78dcfad254b 287 logInfo("Configuring http...\r\n");
BlueShadow 8:e78dcfad254b 288 HTTPClient http;
BlueShadow 8:e78dcfad254b 289 HTTPResult result;
BlueShadow 8:e78dcfad254b 290 http.setHeader(EXOSITE_HEADER);
BlueShadow 9:6475e1b83491 291
BlueShadow 8:e78dcfad254b 292 char http_rx_buf[1024];
BlueShadow 8:e78dcfad254b 293 // IHTTPDataIn object - will contain data received from server.
BlueShadow 8:e78dcfad254b 294 HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf));
BlueShadow 9:6475e1b83491 295
BlueShadow 8:e78dcfad254b 296 // IHTTPDataOut object - contains data to be posted to server.
BlueShadow 8:e78dcfad254b 297 HTTPMap http_tx;
BlueShadow 9:6475e1b83491 298
BlueShadow 9:6475e1b83491 299
BlueShadow 8:e78dcfad254b 300 //****************************************************************************************************
BlueShadow 8:e78dcfad254b 301 // Initialize I2C Devices ************
BlueShadow 8:e78dcfad254b 302 //****************************************************************************************************/
BlueShadow 6:7946b5c2376a 303
BlueShadow 9:6475e1b83491 304 #ifdef RPR0521
BlueShadow 9:6475e1b83491 305 i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false);
BlueShadow 9:6475e1b83491 306 i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false);
BlueShadow 9:6475e1b83491 307 i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false);
BlueShadow 9:6475e1b83491 308 #endif
BlueShadow 9:6475e1b83491 309
BlueShadow 9:6475e1b83491 310 #ifdef KMX62
BlueShadow 9:6475e1b83491 311 i2c.write(KMX62_addr_w, &KMX62_CNTL2[0], 2, false);
BlueShadow 9:6475e1b83491 312 #endif
BlueShadow 9:6475e1b83491 313
BlueShadow 9:6475e1b83491 314 #ifdef COLOR
BlueShadow 9:6475e1b83491 315 i2c.write(BH1745_addr_w, &BH1745_persistence[0], 2, false);
BlueShadow 9:6475e1b83491 316 i2c.write(BH1745_addr_w, &BH1745_mode1[0], 2, false);
BlueShadow 9:6475e1b83491 317 i2c.write(BH1745_addr_w, &BH1745_mode2[0], 2, false);
BlueShadow 9:6475e1b83491 318 i2c.write(BH1745_addr_w, &BH1745_mode3[0], 2, false);
BlueShadow 9:6475e1b83491 319 #endif
BlueShadow 9:6475e1b83491 320
BlueShadow 9:6475e1b83491 321 #ifdef KX022
BlueShadow 9:6475e1b83491 322 i2c.write(KX022_addr_w, &KX022_Accel_CNTL1[0], 2, false);
BlueShadow 9:6475e1b83491 323 i2c.write(KX022_addr_w, &KX022_Accel_ODCNTL[0], 2, false);
BlueShadow 9:6475e1b83491 324 i2c.write(KX022_addr_w, &KX022_Accel_CNTL3[0], 2, false);
BlueShadow 9:6475e1b83491 325 i2c.write(KX022_addr_w, &KX022_Accel_TILT_TIMER[0], 2, false);
BlueShadow 9:6475e1b83491 326 i2c.write(KX022_addr_w, &KX022_Accel_CNTL2[0], 2, false);
BlueShadow 9:6475e1b83491 327 #endif
BlueShadow 9:6475e1b83491 328
BlueShadow 9:6475e1b83491 329 #ifdef Pressure
BlueShadow 9:6475e1b83491 330 i2c.write(Press_addr_w, &PWR_DOWN[0], 2, false);
BlueShadow 9:6475e1b83491 331 i2c.write(Press_addr_w, &SLEEP[0], 2, false);
BlueShadow 9:6475e1b83491 332 i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
BlueShadow 9:6475e1b83491 333 #endif
BlueShadow 9:6475e1b83491 334
BlueShadow 6:7946b5c2376a 335
BlueShadow 8:e78dcfad254b 336 //****************************************************************************************************/
BlueShadow 8:e78dcfad254b 337 //End I2C Initialization Section **********************************************************
BlueShadow 8:e78dcfad254b 338 //****************************************************************************************************/
BlueShadow 9:6475e1b83491 339
BlueShadow 8:e78dcfad254b 340 Timer post_timer;
BlueShadow 9:6475e1b83491 341 post_timer.start();
BlueShadow 8:e78dcfad254b 342 logInfo("Setup complete.");
BlueShadow 8:e78dcfad254b 343 logInfo("Waiting for %d ms to trigger connect...", post_interval_ms);
BlueShadow 6:7946b5c2376a 344
BlueShadow 9:6475e1b83491 345 //*******************************************************************************************************/
BlueShadow 9:6475e1b83491 346 //Beging loop for Main
BlueShadow 9:6475e1b83491 347 //*******************************************************************************************************/
BlueShadow 9:6475e1b83491 348
BlueShadow 8:e78dcfad254b 349 while (true) {
BlueShadow 8:e78dcfad254b 350 if (post_timer.read_ms() > post_interval_ms && do_cloud_post) {
BlueShadow 8:e78dcfad254b 351 logInfo("bringing up the link");
BlueShadow 9:6475e1b83491 352
BlueShadow 9:6475e1b83491 353 #ifdef AnalogTemp
BlueShadow 9:6475e1b83491 354 ReadAnalogTemp ();
BlueShadow 9:6475e1b83491 355 #endif
BlueShadow 9:6475e1b83491 356
BlueShadow 9:6475e1b83491 357 #ifdef AnalogUV
BlueShadow 9:6475e1b83491 358 ReadAnalogUV ();
BlueShadow 9:6475e1b83491 359 #endif
BlueShadow 9:6475e1b83491 360
BlueShadow 9:6475e1b83491 361 #ifdef HallSensor
BlueShadow 9:6475e1b83491 362 ReadHallSensor ();
BlueShadow 9:6475e1b83491 363 #endif
BlueShadow 9:6475e1b83491 364
BlueShadow 9:6475e1b83491 365 #ifdef COLOR
BlueShadow 9:6475e1b83491 366 ReadCOLOR ();
BlueShadow 9:6475e1b83491 367 #endif
BlueShadow 9:6475e1b83491 368
BlueShadow 9:6475e1b83491 369 #ifdef RPR0521 //als digital
BlueShadow 9:6475e1b83491 370 ReadRPR0521_ALS ();
BlueShadow 9:6475e1b83491 371 #endif
BlueShadow 9:6475e1b83491 372
BlueShadow 9:6475e1b83491 373 #ifdef Pressure
BlueShadow 9:6475e1b83491 374 ReadPressure();
BlueShadow 9:6475e1b83491 375 #endif
BlueShadow 9:6475e1b83491 376
BlueShadow 9:6475e1b83491 377 #ifdef KMX62
BlueShadow 9:6475e1b83491 378 ReadKMX62_Accel ();
BlueShadow 9:6475e1b83491 379 ReadKMX62_Mag ();
BlueShadow 9:6475e1b83491 380 #endif
BlueShadow 9:6475e1b83491 381
BlueShadow 9:6475e1b83491 382 #ifdef KX022
BlueShadow 9:6475e1b83491 383 ReadKX022 ();
BlueShadow 9:6475e1b83491 384 #endif
BlueShadow 9:6475e1b83491 385
BlueShadow 9:6475e1b83491 386 logDebug("%s", wall_of_dash);
BlueShadow 9:6475e1b83491 387 logDebug("SENSOR DATA");
BlueShadow 9:6475e1b83491 388 logDebug("temperature: %0.2f C", BM1383[0]);
BlueShadow 9:6475e1b83491 389 logDebug("analog uv: %.1f mW/cm2", ML8511_output);
BlueShadow 9:6475e1b83491 390 logDebug("ambient Light %0.3f", RPR0521_ALS[0]);
BlueShadow 9:6475e1b83491 391 logDebug("proximity count %0.3f", RPR0521_ALS[1]);
BlueShadow 9:6475e1b83491 392 logDebug("hall effect: South %d\t North %d", Hall_Return[0],Hall_Return[1]);
BlueShadow 9:6475e1b83491 393 logDebug("pressure: %0.2f hPa", BM1383[1]);
BlueShadow 9:6475e1b83491 394 logDebug("magnetometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tuT", MEMS_Mag[0], MEMS_Mag[1], MEMS_Mag[2]);
BlueShadow 9:6475e1b83491 395 logDebug("accelerometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tg", MEMS_Accel[0], MEMS_Accel[1], MEMS_Accel[2]);
BlueShadow 9:6475e1b83491 396 logDebug("color:\r\n\tred: %ld\tgrn: %ld\tblu: %ld\t", BH1745[0], BH1745[1], BH1745[2]);
BlueShadow 9:6475e1b83491 397 logDebug("%s", wall_of_dash);
BlueShadow 9:6475e1b83491 398
BlueShadow 9:6475e1b83491 399
BlueShadow 9:6475e1b83491 400 //*******************************************************************************************************/
BlueShadow 9:6475e1b83491 401 //Connect to the radio to send data
BlueShadow 9:6475e1b83491 402 //*******************************************************************************************************/
BlueShadow 9:6475e1b83491 403
BlueShadow 8:e78dcfad254b 404 if (radio->connect()) {
BlueShadow 9:6475e1b83491 405 int sensor_data = RPR0521_ALS[0];
BlueShadow 10:75acd8caa0d3 406 logDebug("posting sensor data %d",sensor_data);
BlueShadow 6:7946b5c2376a 407
BlueShadow 10:75acd8caa0d3 408 std::string sensor_data_str = patch::to_string(sensor_data);
BlueShadow 10:75acd8caa0d3 409 logDebug("Sensor data string: %s",sensor_data_str.c_str());
BlueShadow 10:75acd8caa0d3 410
BlueShadow 10:75acd8caa0d3 411 http_tx.clear();
BlueShadow 10:75acd8caa0d3 412 http_tx.put("< YOUR EXOSITE DISPLAY VARIABLE HERE >", sensor_data_str.c_str());
BlueShadow 9:6475e1b83491 413
BlueShadow 8:e78dcfad254b 414 // Make HTTP POST request
BlueShadow 8:e78dcfad254b 415 result = http.post(EXOSITE_URL, http_tx, &http_rx);
BlueShadow 10:75acd8caa0d3 416 // logDebug("%s %s",EXOSITE_URL, EXOSITE_HEADER);
BlueShadow 8:e78dcfad254b 417 if (result != HTTP_OK) {
BlueShadow 8:e78dcfad254b 418 logError("HTTP POST failed [%d][%s]", result, httpResToStr(result));
BlueShadow 8:e78dcfad254b 419 } else {
BlueShadow 8:e78dcfad254b 420 logInfo("HTTP POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf);
BlueShadow 8:e78dcfad254b 421 }
BlueShadow 6:7946b5c2376a 422
mfiore 4:730b61258422 423 radio->disconnect();
mfiore 4:730b61258422 424 } else {
mfiore 4:730b61258422 425 logError("establishing PPP link failed");
mfiore 4:730b61258422 426 }
BlueShadow 6:7946b5c2376a 427
mfiore 4:730b61258422 428 post_timer.reset();
BlueShadow 9:6475e1b83491 429
BlueShadow 8:e78dcfad254b 430 logInfo("Waiting for %d ms to trigger connect...", post_interval_ms);
mfiore 4:730b61258422 431 }
mfiore 0:a44e71488e1f 432 }
mfiore 0:a44e71488e1f 433 }
mfiore 0:a44e71488e1f 434
BlueShadow 8:e78dcfad254b 435
mfiore 1:a049d113e250 436 // init functions
BlueShadow 6:7946b5c2376a 437 bool init_mtsas()
BlueShadow 6:7946b5c2376a 438 {
mfiore 1:a049d113e250 439 io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
mfiore 1:a049d113e250 440 if (! io)
mfiore 1:a049d113e250 441 return false;
BlueShadow 6:7946b5c2376a 442
mfiore 1:a049d113e250 443 io->baud(115200);
mfiore 1:a049d113e250 444 radio = CellularFactory::create(io);
mfiore 1:a049d113e250 445 if (! radio)
mfiore 1:a049d113e250 446 return false;
BlueShadow 6:7946b5c2376a 447
mfiore 1:a049d113e250 448 Code ret = radio->setApn(apn);
mfiore 1:a049d113e250 449 if (ret != MTS_SUCCESS)
mfiore 1:a049d113e250 450 return false;
BlueShadow 6:7946b5c2376a 451
mfiore 5:a946ef74a8c4 452 Transport::setTransport(radio);
BlueShadow 6:7946b5c2376a 453
mfiore 1:a049d113e250 454 return true;
mfiore 1:a049d113e250 455 }
mfiore 1:a049d113e250 456
BlueShadow 9:6475e1b83491 457 char* httpResToStr(HTTPResult result)
BlueShadow 9:6475e1b83491 458 {
BlueShadow 8:e78dcfad254b 459 switch(result) {
BlueShadow 8:e78dcfad254b 460 case HTTP_PROCESSING:
BlueShadow 8:e78dcfad254b 461 return "HTTP_PROCESSING";
BlueShadow 8:e78dcfad254b 462 case HTTP_PARSE:
BlueShadow 8:e78dcfad254b 463 return "HTTP_PARSE";
BlueShadow 8:e78dcfad254b 464 case HTTP_DNS:
BlueShadow 8:e78dcfad254b 465 return "HTTP_DNS";
BlueShadow 8:e78dcfad254b 466 case HTTP_PRTCL:
BlueShadow 8:e78dcfad254b 467 return "HTTP_PRTCL";
BlueShadow 8:e78dcfad254b 468 case HTTP_NOTFOUND:
BlueShadow 8:e78dcfad254b 469 return "HTTP_NOTFOUND";
BlueShadow 8:e78dcfad254b 470 case HTTP_REFUSED:
BlueShadow 8:e78dcfad254b 471 return "HTTP_REFUSED";
BlueShadow 8:e78dcfad254b 472 case HTTP_ERROR:
BlueShadow 8:e78dcfad254b 473 return "HTTP_ERROR";
BlueShadow 8:e78dcfad254b 474 case HTTP_TIMEOUT:
BlueShadow 8:e78dcfad254b 475 return "HTTP_TIMEOUT";
BlueShadow 8:e78dcfad254b 476 case HTTP_CONN:
BlueShadow 8:e78dcfad254b 477 return "HTTP_CONN";
BlueShadow 8:e78dcfad254b 478 case HTTP_CLOSED:
BlueShadow 8:e78dcfad254b 479 return "HTTP_CLOSED";
BlueShadow 8:e78dcfad254b 480 case HTTP_REDIRECT:
BlueShadow 8:e78dcfad254b 481 return "HTTP_REDIRECT";
BlueShadow 8:e78dcfad254b 482 case HTTP_OK:
BlueShadow 8:e78dcfad254b 483 return "HTTP_OK";
BlueShadow 8:e78dcfad254b 484 default:
BlueShadow 8:e78dcfad254b 485 return "HTTP Result unknown";
BlueShadow 6:7946b5c2376a 486 }
mfiore 0:a44e71488e1f 487 }
mfiore 0:a44e71488e1f 488
BlueShadow 9:6475e1b83491 489
BlueShadow 9:6475e1b83491 490 //************************************************************************************************/
BlueShadow 9:6475e1b83491 491 // Sensor data acquisition functions
BlueShadow 9:6475e1b83491 492 //************************************************************************************************/
BlueShadow 9:6475e1b83491 493 #ifdef AnalogTemp
BlueShadow 9:6475e1b83491 494 void ReadAnalogTemp ()
BlueShadow 9:6475e1b83491 495 {
BlueShadow 9:6475e1b83491 496 BDE0600_Temp_value = BDE0600_Temp.read_u16();
BlueShadow 9:6475e1b83491 497
BlueShadow 9:6475e1b83491 498 BDE0600_output = (float)BDE0600_Temp_value * (float)0.000050354; //(value * (3.3V/65535))
BlueShadow 9:6475e1b83491 499 BDE0600_output = (BDE0600_output-(float)1.753)/((float)-0.01068) + (float)30;
BlueShadow 9:6475e1b83491 500
BlueShadow 9:6475e1b83491 501 // printf("BDE0600 Analog Temp Sensor Data:\r\n");
BlueShadow 9:6475e1b83491 502 // printf(" Temp = %.2f C\r\n", BDE0600_output);
BlueShadow 9:6475e1b83491 503 }
BlueShadow 9:6475e1b83491 504 #endif
BlueShadow 9:6475e1b83491 505
BlueShadow 9:6475e1b83491 506 #ifdef AnalogUV
BlueShadow 9:6475e1b83491 507 void ReadAnalogUV ()
BlueShadow 9:6475e1b83491 508 {
BlueShadow 9:6475e1b83491 509 ML8511_UV_value = ML8511_UV.read_u16();
BlueShadow 9:6475e1b83491 510 ML8511_output = (float)ML8511_UV_value * (float)0.000050354; //(value * (3.3V/65535)) //Note to self: when playing with this, a negative value is seen... Honestly, I think this has to do with my ADC converstion...
BlueShadow 9:6475e1b83491 511 ML8511_output = (ML8511_output-(float)2.2)/((float)0.129) + 10; // Added +5 to the offset so when inside (aka, no UV, readings show 0)... this is the wrong approach... and the readings don't make sense... Fix this.
BlueShadow 9:6475e1b83491 512
BlueShadow 9:6475e1b83491 513 // printf("ML8511 Analog UV Sensor Data:\r\n");
BlueShadow 9:6475e1b83491 514 // printf(" UV = %.1f mW/cm2\r\n", ML8511_output);
BlueShadow 9:6475e1b83491 515
BlueShadow 9:6475e1b83491 516 }
BlueShadow 9:6475e1b83491 517 #endif
BlueShadow 9:6475e1b83491 518
BlueShadow 9:6475e1b83491 519
BlueShadow 9:6475e1b83491 520 #ifdef HallSensor
BlueShadow 9:6475e1b83491 521 void ReadHallSensor ()
BlueShadow 9:6475e1b83491 522 {
BlueShadow 9:6475e1b83491 523
BlueShadow 9:6475e1b83491 524 Hall_Return[0] = Hall_GPIO0;
BlueShadow 9:6475e1b83491 525 Hall_Return[1] = Hall_GPIO1;
BlueShadow 9:6475e1b83491 526
BlueShadow 9:6475e1b83491 527 // printf("BU52011 Hall Switch Sensor Data:\r\n");
BlueShadow 9:6475e1b83491 528 // printf(" South Detect = %d\r\n", Hall_Return[0]);
BlueShadow 9:6475e1b83491 529 // printf(" North Detect = %d\r\n", Hall_Return[1]);
BlueShadow 9:6475e1b83491 530
BlueShadow 9:6475e1b83491 531
BlueShadow 9:6475e1b83491 532 }
BlueShadow 9:6475e1b83491 533 #endif
BlueShadow 9:6475e1b83491 534
BlueShadow 9:6475e1b83491 535 #ifdef COLOR
BlueShadow 9:6475e1b83491 536 void ReadCOLOR ()
BlueShadow 9:6475e1b83491 537 {
BlueShadow 9:6475e1b83491 538
BlueShadow 9:6475e1b83491 539 //Read color data from the IC
BlueShadow 9:6475e1b83491 540 i2c.write(BH1745_addr_w, &BH1745_Addr_color_ReadData, 1, RepStart);
BlueShadow 9:6475e1b83491 541 i2c.read(BH1745_addr_r, &BH1745_Content_ReadData[0], 6, NoRepStart);
BlueShadow 9:6475e1b83491 542
BlueShadow 9:6475e1b83491 543 //separate all data read into colors
BlueShadow 9:6475e1b83491 544 BH1745[0] = (BH1745_Content_ReadData[1]<<8) | (BH1745_Content_ReadData[0]);
BlueShadow 9:6475e1b83491 545 BH1745[1] = (BH1745_Content_ReadData[3]<<8) | (BH1745_Content_ReadData[2]);
BlueShadow 9:6475e1b83491 546 BH1745[2] = (BH1745_Content_ReadData[5]<<8) | (BH1745_Content_ReadData[4]);
BlueShadow 9:6475e1b83491 547
BlueShadow 9:6475e1b83491 548 //Output Data into UART
BlueShadow 9:6475e1b83491 549 // printf("BH1745 COLOR Sensor Data:\r\n");
BlueShadow 9:6475e1b83491 550 // printf(" Red = %d ADC Counts\r\n",BH1745[0]);
BlueShadow 9:6475e1b83491 551 // printf(" Green = %d ADC Counts\r\n",BH1745[1]);
BlueShadow 9:6475e1b83491 552 // printf(" Blue = %d ADC Counts\r\n",BH1745[2]);
BlueShadow 9:6475e1b83491 553
BlueShadow 9:6475e1b83491 554 }
BlueShadow 9:6475e1b83491 555 #endif
BlueShadow 9:6475e1b83491 556
BlueShadow 9:6475e1b83491 557 #ifdef RPR0521 //als digital
BlueShadow 9:6475e1b83491 558 void ReadRPR0521_ALS ()
BlueShadow 9:6475e1b83491 559 {
BlueShadow 9:6475e1b83491 560 i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart);
BlueShadow 9:6475e1b83491 561 i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart);
BlueShadow 9:6475e1b83491 562
BlueShadow 9:6475e1b83491 563 RPR0521_ALS[1] = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]);
BlueShadow 9:6475e1b83491 564 RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]);
BlueShadow 9:6475e1b83491 565 RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]);
BlueShadow 9:6475e1b83491 566 RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT;
BlueShadow 9:6475e1b83491 567
BlueShadow 9:6475e1b83491 568 if(RPR0521_ALS_DataRatio < (float)0.595) {
BlueShadow 9:6475e1b83491 569 RPR0521_ALS[0] = ((float)1.682*(float)RPR0521_ALS_D0_RAWOUT - (float)1.877*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 9:6475e1b83491 570 } else if(RPR0521_ALS_DataRatio < (float)1.015) {
BlueShadow 9:6475e1b83491 571 RPR0521_ALS[0] = ((float)0.644*(float)RPR0521_ALS_D0_RAWOUT - (float)0.132*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 9:6475e1b83491 572 } else if(RPR0521_ALS_DataRatio < (float)1.352) {
BlueShadow 9:6475e1b83491 573 RPR0521_ALS[0] = ((float)0.756*(float)RPR0521_ALS_D0_RAWOUT - (float)0.243*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 9:6475e1b83491 574 } else if(RPR0521_ALS_DataRatio < (float)3.053) {
BlueShadow 9:6475e1b83491 575 RPR0521_ALS[0] = ((float)0.766*(float)RPR0521_ALS_D0_RAWOUT - (float)0.25*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 9:6475e1b83491 576 } else {
BlueShadow 9:6475e1b83491 577 RPR0521_ALS[0] = 0;
BlueShadow 9:6475e1b83491 578 }
BlueShadow 9:6475e1b83491 579 // printf("RPR-0521 ALS/PROX Sensor Data:\r\n");
BlueShadow 9:6475e1b83491 580 // printf(" ALS = %0.2f lx\r\n", RPR0521_ALS[0]);
BlueShadow 9:6475e1b83491 581 // printf(" PROX= %0.2f ADC Counts\r\n", RPR0521_ALS[1]); //defined as a float but is an unsigned.
BlueShadow 9:6475e1b83491 582
BlueShadow 9:6475e1b83491 583 }
BlueShadow 9:6475e1b83491 584 #endif
BlueShadow 9:6475e1b83491 585
BlueShadow 9:6475e1b83491 586 #ifdef KMX62
BlueShadow 9:6475e1b83491 587 void ReadKMX62_Accel ()
BlueShadow 9:6475e1b83491 588 {
BlueShadow 9:6475e1b83491 589 //Read Accel Portion from the IC
BlueShadow 9:6475e1b83491 590 i2c.write(KMX62_addr_w, &KMX62_Addr_Accel_ReadData, 1, RepStart);
BlueShadow 9:6475e1b83491 591 i2c.read(KMX62_addr_r, &KMX62_Content_Accel_ReadData[0], 6, NoRepStart);
BlueShadow 9:6475e1b83491 592
BlueShadow 9:6475e1b83491 593 //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
BlueShadow 9:6475e1b83491 594 // However, because we need the signed value, we will adjust the value when converting to "g"
BlueShadow 9:6475e1b83491 595 MEMS_Accel_Xout = (KMX62_Content_Accel_ReadData[1]<<8) | (KMX62_Content_Accel_ReadData[0]);
BlueShadow 9:6475e1b83491 596 MEMS_Accel_Yout = (KMX62_Content_Accel_ReadData[3]<<8) | (KMX62_Content_Accel_ReadData[2]);
BlueShadow 9:6475e1b83491 597 MEMS_Accel_Zout = (KMX62_Content_Accel_ReadData[5]<<8) | (KMX62_Content_Accel_ReadData[4]);
BlueShadow 9:6475e1b83491 598
BlueShadow 9:6475e1b83491 599 //Note: Conversion to G is as follows:
BlueShadow 9:6475e1b83491 600 // Axis_ValueInG = MEMS_Accel_axis / 1024
BlueShadow 9:6475e1b83491 601 // However, since we did not remove the LSB previously, we need to divide by 4 again
BlueShadow 9:6475e1b83491 602 // Thus, we will divide the output by 4096 (1024*4) to convert and cancel out the LSB
BlueShadow 9:6475e1b83491 603 MEMS_Accel[0] = ((float)MEMS_Accel_Xout/4096/2);
BlueShadow 9:6475e1b83491 604 MEMS_Accel[1] = ((float)MEMS_Accel_Yout/4096/2);
BlueShadow 9:6475e1b83491 605 MEMS_Accel[2] = ((float)MEMS_Accel_Zout/4096/2);
BlueShadow 9:6475e1b83491 606
BlueShadow 9:6475e1b83491 607 // Return Data to UART
BlueShadow 9:6475e1b83491 608 // printf("KMX62 Accel+Mag Sensor Data:\r\n");
BlueShadow 9:6475e1b83491 609 // printf(" AccX= %0.2f g\r\n", MEMS_Accel[0]);
BlueShadow 9:6475e1b83491 610 // printf(" AccY= %0.2f g\r\n", MEMS_Accel[1]);
BlueShadow 9:6475e1b83491 611 // printf(" AccZ= %0.2f g\r\n", MEMS_Accel[2]);
BlueShadow 9:6475e1b83491 612
BlueShadow 9:6475e1b83491 613 }
BlueShadow 9:6475e1b83491 614
BlueShadow 9:6475e1b83491 615 void ReadKMX62_Mag ()
BlueShadow 9:6475e1b83491 616 {
BlueShadow 9:6475e1b83491 617
BlueShadow 9:6475e1b83491 618 //Read Mag portion from the IC
BlueShadow 9:6475e1b83491 619 i2c.write(KMX62_addr_w, &KMX62_Addr_Mag_ReadData, 1, RepStart);
BlueShadow 9:6475e1b83491 620 i2c.read(KMX62_addr_r, &KMX62_Content_Mag_ReadData[0], 6, NoRepStart);
BlueShadow 9:6475e1b83491 621
BlueShadow 9:6475e1b83491 622 //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
BlueShadow 9:6475e1b83491 623 // However, because we need the signed value, we will adjust the value when converting to "g"
BlueShadow 9:6475e1b83491 624 MEMS_Mag_Xout = (KMX62_Content_Mag_ReadData[1]<<8) | (KMX62_Content_Mag_ReadData[0]);
BlueShadow 9:6475e1b83491 625 MEMS_Mag_Yout = (KMX62_Content_Mag_ReadData[3]<<8) | (KMX62_Content_Mag_ReadData[2]);
BlueShadow 9:6475e1b83491 626 MEMS_Mag_Zout = (KMX62_Content_Mag_ReadData[5]<<8) | (KMX62_Content_Mag_ReadData[4]);
BlueShadow 9:6475e1b83491 627
BlueShadow 9:6475e1b83491 628 //Note: Conversion to G is as follows:
BlueShadow 9:6475e1b83491 629 // Axis_ValueInG = MEMS_Accel_axis / 1024
BlueShadow 9:6475e1b83491 630 // However, since we did not remove the LSB previously, we need to divide by 4 again
BlueShadow 9:6475e1b83491 631 // Thus, we will divide the output by 4095 (1024*4) to convert and cancel out the LSB
BlueShadow 9:6475e1b83491 632 MEMS_Mag[0] = (float)MEMS_Mag_Xout/4096*(float)0.146;
BlueShadow 9:6475e1b83491 633 MEMS_Mag[1] = (float)MEMS_Mag_Yout/4096*(float)0.146;
BlueShadow 9:6475e1b83491 634 MEMS_Mag[2] = (float)MEMS_Mag_Zout/4096*(float)0.146;
BlueShadow 9:6475e1b83491 635
BlueShadow 9:6475e1b83491 636 // Return Data to UART
BlueShadow 9:6475e1b83491 637 // printf(" MagX= %0.2f uT\r\n", MEMS_Mag[0]);
BlueShadow 9:6475e1b83491 638 // printf(" MagY= %0.2f uT\r\n", MEMS_Mag[1]);
BlueShadow 9:6475e1b83491 639 // printf(" MagZ= %0.2f uT\r\n", MEMS_Mag[2]);
BlueShadow 9:6475e1b83491 640
BlueShadow 9:6475e1b83491 641 }
BlueShadow 9:6475e1b83491 642 #endif
BlueShadow 9:6475e1b83491 643
BlueShadow 9:6475e1b83491 644 #ifdef KX022
BlueShadow 9:6475e1b83491 645 void ReadKX022 ()
BlueShadow 9:6475e1b83491 646 {
BlueShadow 9:6475e1b83491 647
BlueShadow 9:6475e1b83491 648 //Read KX022 Portion from the IC
BlueShadow 9:6475e1b83491 649 i2c.write(KX022_addr_w, &KX022_Addr_Accel_ReadData, 1, RepStart);
BlueShadow 9:6475e1b83491 650 i2c.read(KX022_addr_r, &KX022_Content_ReadData[0], 6, NoRepStart);
BlueShadow 9:6475e1b83491 651
BlueShadow 9:6475e1b83491 652 //Format Data
BlueShadow 9:6475e1b83491 653 KX022_Accel_X_RawOUT = (KX022_Content_ReadData[1]<<8) | (KX022_Content_ReadData[0]);
BlueShadow 9:6475e1b83491 654 KX022_Accel_Y_RawOUT = (KX022_Content_ReadData[3]<<8) | (KX022_Content_ReadData[2]);
BlueShadow 9:6475e1b83491 655 KX022_Accel_Z_RawOUT = (KX022_Content_ReadData[5]<<8) | (KX022_Content_ReadData[4]);
BlueShadow 9:6475e1b83491 656
BlueShadow 9:6475e1b83491 657 //Scale Data
BlueShadow 9:6475e1b83491 658 KX022_Accel[0] = (float)KX022_Accel_X_RawOUT / 16384;
BlueShadow 9:6475e1b83491 659 KX022_Accel[1] = (float)KX022_Accel_Y_RawOUT / 16384;
BlueShadow 9:6475e1b83491 660 KX022_Accel[2] = (float)KX022_Accel_Z_RawOUT / 16384;
BlueShadow 9:6475e1b83491 661
BlueShadow 9:6475e1b83491 662 //Return Data through UART
BlueShadow 9:6475e1b83491 663 // printf("KX022 Accelerometer Sensor Data: \r\n");
BlueShadow 9:6475e1b83491 664 // printf(" AccX= %0.2f g\r\n", KX022_Accel[0]);
BlueShadow 9:6475e1b83491 665 // printf(" AccY= %0.2f g\r\n", KX022_Accel[1]);
BlueShadow 9:6475e1b83491 666 // printf(" AccZ= %0.2f g\r\n", KX022_Accel[2]);
BlueShadow 9:6475e1b83491 667
BlueShadow 9:6475e1b83491 668 }
BlueShadow 9:6475e1b83491 669 #endif
BlueShadow 9:6475e1b83491 670
BlueShadow 9:6475e1b83491 671
BlueShadow 9:6475e1b83491 672 #ifdef Pressure
BlueShadow 9:6475e1b83491 673 void ReadPressure ()
BlueShadow 9:6475e1b83491 674 {
BlueShadow 9:6475e1b83491 675
BlueShadow 9:6475e1b83491 676 i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
BlueShadow 9:6475e1b83491 677 i2c.read(Press_addr_r, &Press_Content_ReadData[0], 6, NoRepStart);
BlueShadow 9:6475e1b83491 678
BlueShadow 9:6475e1b83491 679 BM1383_Temp_Out = (Press_Content_ReadData[0]<<8) | (Press_Content_ReadData[1]);
BlueShadow 9:6475e1b83491 680 BM1383[0] = (float)BM1383_Temp_Out/32;
BlueShadow 9:6475e1b83491 681
BlueShadow 9:6475e1b83491 682 BM1383_Var = (Press_Content_ReadData[2]<<3) | (Press_Content_ReadData[3] >> 5);
BlueShadow 9:6475e1b83491 683 BM1383_Deci = ((Press_Content_ReadData[3] & 0x1f) << 6 | ((Press_Content_ReadData[4] >> 2)));
BlueShadow 9:6475e1b83491 684 BM1383_Deci = (float)BM1383_Deci* (float)0.00048828125; //0.00048828125 = 2^-11
BlueShadow 9:6475e1b83491 685 BM1383[1] = (BM1383_Var + BM1383_Deci); //question pending here...
BlueShadow 9:6475e1b83491 686
BlueShadow 9:6475e1b83491 687 // printf("BM1383 Pressure Sensor Data:\r\n");
BlueShadow 9:6475e1b83491 688 // printf(" Temperature= %0.2f C\r\n", BM1383[0]);
BlueShadow 9:6475e1b83491 689 // printf(" Pressure = %0.2f hPa\r\n", BM1383[1]);
BlueShadow 9:6475e1b83491 690
BlueShadow 9:6475e1b83491 691 }
BlueShadow 9:6475e1b83491 692 #endif