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