Training Class with ROHM Sensor Board and LoRa mDot
Dependencies: MbedJSONValue libmDot mbed-rtos mbed
main.cpp@0:7f1c7e1755bc, 2016-02-26 (annotated)
- Committer:
- BlueShadow
- Date:
- Fri Feb 26 23:07:36 2016 +0000
- Revision:
- 0:7f1c7e1755bc
- Child:
- 1:3ef353c7f925
Brandon's success a M2X solution.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BlueShadow | 0:7f1c7e1755bc | 1 | /************************************************************************* |
BlueShadow | 0:7f1c7e1755bc | 2 | * Originally this was a |
BlueShadow | 0:7f1c7e1755bc | 3 | * Dragonfly Example program for 2015 AT&T Government Solutions Hackathon |
BlueShadow | 0:7f1c7e1755bc | 4 | * |
BlueShadow | 0:7f1c7e1755bc | 5 | * This is in process of being convertered to a mDot processor. mDot has a |
BlueShadow | 0:7f1c7e1755bc | 6 | * limited set of IO that are available to the ROHM board. Most of the |
BlueShadow | 0:7f1c7e1755bc | 7 | * Sensors will be used but the ones that can't have been commented out. |
BlueShadow | 0:7f1c7e1755bc | 8 | * |
BlueShadow | 0:7f1c7e1755bc | 9 | * The following hardware is required to successfully run this program: |
BlueShadow | 0:7f1c7e1755bc | 10 | * - MultiTech UDK2 (4" square white PCB with Arduino headers, antenna |
BlueShadow | 0:7f1c7e1755bc | 11 | * connector, micro USB ports, and 40-pin connector for Dragonfly) |
BlueShadow | 0:7f1c7e1755bc | 12 | * - MultiTech mDot with a LoRa radio |
BlueShadow | 0:7f1c7e1755bc | 13 | * - Seeed Studio Base Shield to elevate the ROHM board connectors away from mDOt |
BlueShadow | 0:7f1c7e1755bc | 14 | * - MEMs Inertial and Environmental Nucleo Expansion board (LSM6DS0 |
BlueShadow | 0:7f1c7e1755bc | 15 | * 3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis |
BlueShadow | 0:7f1c7e1755bc | 16 | * magnetometer, HTS221 humidity and temperature sensor and LPS25HB |
BlueShadow | 0:7f1c7e1755bc | 17 | * pressure sensor) |
BlueShadow | 0:7f1c7e1755bc | 18 | * |
BlueShadow | 0:7f1c7e1755bc | 19 | * What this program does: |
BlueShadow | 0:7f1c7e1755bc | 20 | * - reads data from all sensors on MEMs board and moisture sensor on a |
BlueShadow | 0:7f1c7e1755bc | 21 | * periodic basis |
BlueShadow | 0:7f1c7e1755bc | 22 | * - prints all sensor data to debug port on a periodic basis |
BlueShadow | 0:7f1c7e1755bc | 23 | * - optionally sends LoRa sensor data when the timer expires |
BlueShadow | 0:7f1c7e1755bc | 24 | * THis needs to be written yet. |
BlueShadow | 0:7f1c7e1755bc | 25 | * - optionally sends sensor data to AT&T M2X cloud platform (user must |
BlueShadow | 0:7f1c7e1755bc | 26 | * create own M2X account and configure a device) |
BlueShadow | 0:7f1c7e1755bc | 27 | * - you need to set the "m2x_api_key" field and the "m2x_device_id" |
BlueShadow | 0:7f1c7e1755bc | 28 | * field based on your M2X account for this to work |
BlueShadow | 0:7f1c7e1755bc | 29 | * - you need to set the "do_cloud_post" flag to true for this to |
BlueShadow | 0:7f1c7e1755bc | 30 | * work |
BlueShadow | 0:7f1c7e1755bc | 31 | * |
BlueShadow | 0:7f1c7e1755bc | 32 | * Setup: |
BlueShadow | 0:7f1c7e1755bc | 33 | * - Seat the mDot on the UDK2 board |
BlueShadow | 0:7f1c7e1755bc | 34 | * - Stack the Base Shield on the UDK2 Arduino headers |
BlueShadow | 0:7f1c7e1755bc | 35 | * - Make sure the reference voltage selector switch (next to the A0 |
BlueShadow | 0:7f1c7e1755bc | 36 | * socket) is switched to 3.3V so you don't blow the mDot analog converter |
BlueShadow | 0:7f1c7e1755bc | 37 | * accuracy will suffer as a result when compared to 5V. |
BlueShadow | 0:7f1c7e1755bc | 38 | * - Stack the MEMs board on top of the Base Shield |
BlueShadow | 0:7f1c7e1755bc | 39 | * - Plug in the power cable |
BlueShadow | 0:7f1c7e1755bc | 40 | * - Plug a micro USB cable away from the multiple LED String |
BlueShadow | 0:7f1c7e1755bc | 41 | * |
BlueShadow | 0:7f1c7e1755bc | 42 | * Go have fun and make something cool! |
BlueShadow | 0:7f1c7e1755bc | 43 | * |
BlueShadow | 0:7f1c7e1755bc | 44 | ************************************************************************/ |
BlueShadow | 0:7f1c7e1755bc | 45 | /* |
BlueShadow | 0:7f1c7e1755bc | 46 | Sample Program Description: |
BlueShadow | 0:7f1c7e1755bc | 47 | This Program will enable to Multi-Tech mDot platform to utilize ROHM's Multi-sensor Shield Board. |
BlueShadow | 0:7f1c7e1755bc | 48 | This program will initialize most of the sensors on the shield and then read back the sensor data. |
BlueShadow | 0:7f1c7e1755bc | 49 | Data will then be output to the UART Debug Terminal every 1 second. |
BlueShadow | 0:7f1c7e1755bc | 50 | |
BlueShadow | 0:7f1c7e1755bc | 51 | Sample Program Author: |
BlueShadow | 0:7f1c7e1755bc | 52 | ROHM USDC |
BlueShadow | 0:7f1c7e1755bc | 53 | |
BlueShadow | 0:7f1c7e1755bc | 54 | Additional Resources: |
BlueShadow | 0:7f1c7e1755bc | 55 | ROHM Sensor Shield GitHub Repository: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield |
BlueShadow | 0:7f1c7e1755bc | 56 | */ |
BlueShadow | 0:7f1c7e1755bc | 57 | |
BlueShadow | 0:7f1c7e1755bc | 58 | |
BlueShadow | 0:7f1c7e1755bc | 59 | |
BlueShadow | 0:7f1c7e1755bc | 60 | #include "mbed.h" |
BlueShadow | 0:7f1c7e1755bc | 61 | #include "MbedJSONValue.h" |
BlueShadow | 0:7f1c7e1755bc | 62 | // #include "HTTPJson.h" |
BlueShadow | 0:7f1c7e1755bc | 63 | #include <string> |
BlueShadow | 0:7f1c7e1755bc | 64 | |
BlueShadow | 0:7f1c7e1755bc | 65 | // added the following help files for a mDot not required for Dragonfly. |
BlueShadow | 0:7f1c7e1755bc | 66 | #include "mDot.h" |
BlueShadow | 0:7f1c7e1755bc | 67 | #include "MTSLog.h" |
BlueShadow | 0:7f1c7e1755bc | 68 | #include <vector> |
BlueShadow | 0:7f1c7e1755bc | 69 | #include <algorithm> |
BlueShadow | 0:7f1c7e1755bc | 70 | #include "rtos.h" |
BlueShadow | 0:7f1c7e1755bc | 71 | |
BlueShadow | 0:7f1c7e1755bc | 72 | |
BlueShadow | 0:7f1c7e1755bc | 73 | // Debug serial port |
BlueShadow | 0:7f1c7e1755bc | 74 | static Serial debug(USBTX, USBRX); |
BlueShadow | 0:7f1c7e1755bc | 75 | |
BlueShadow | 0:7f1c7e1755bc | 76 | // see https://m2x.att.com/developer/documentation/v2/overview for M2X API documentation |
BlueShadow | 0:7f1c7e1755bc | 77 | // M2X device ID |
BlueShadow | 0:7f1c7e1755bc | 78 | static const std::string m2x_device_id = ""; |
BlueShadow | 0:7f1c7e1755bc | 79 | |
BlueShadow | 0:7f1c7e1755bc | 80 | // M2X primary API key |
BlueShadow | 0:7f1c7e1755bc | 81 | static const std::string m2x_api_key = ""; |
BlueShadow | 0:7f1c7e1755bc | 82 | |
BlueShadow | 0:7f1c7e1755bc | 83 | // set to true if you want to post to the cloud |
BlueShadow | 0:7f1c7e1755bc | 84 | // you need to have you M2X account set up properly for this to work? |
BlueShadow | 0:7f1c7e1755bc | 85 | bool do_cloud_post = false; |
BlueShadow | 0:7f1c7e1755bc | 86 | //bool do_cloud_post = true; |
BlueShadow | 0:7f1c7e1755bc | 87 | |
BlueShadow | 0:7f1c7e1755bc | 88 | std::string url = "http://api-m2x.att.com/v2/devices/" + m2x_device_id + "/update"; |
BlueShadow | 0:7f1c7e1755bc | 89 | |
BlueShadow | 0:7f1c7e1755bc | 90 | |
BlueShadow | 0:7f1c7e1755bc | 91 | // variables for sensor data |
BlueShadow | 0:7f1c7e1755bc | 92 | float temp_celsius; |
BlueShadow | 0:7f1c7e1755bc | 93 | float humidity_percent; |
BlueShadow | 0:7f1c7e1755bc | 94 | float pressure_mbar; |
BlueShadow | 0:7f1c7e1755bc | 95 | float moisture_percent; |
BlueShadow | 0:7f1c7e1755bc | 96 | int32_t mag_mgauss[3]; |
BlueShadow | 0:7f1c7e1755bc | 97 | int32_t acc_mg[3]; |
BlueShadow | 0:7f1c7e1755bc | 98 | int32_t gyro_mdps[3]; |
BlueShadow | 0:7f1c7e1755bc | 99 | |
BlueShadow | 0:7f1c7e1755bc | 100 | // misc variables |
BlueShadow | 0:7f1c7e1755bc | 101 | static char wall_of_dash[] = "--------------------------------------------------"; |
BlueShadow | 0:7f1c7e1755bc | 102 | bool radio_ok = false; |
BlueShadow | 0:7f1c7e1755bc | 103 | static int thpm_interval_ms = 5000; |
BlueShadow | 0:7f1c7e1755bc | 104 | static int motion_interval_ms = 5000; |
BlueShadow | 0:7f1c7e1755bc | 105 | static int print_interval_ms = 5000; |
BlueShadow | 0:7f1c7e1755bc | 106 | static int sms_interval_ms = 5000; |
BlueShadow | 0:7f1c7e1755bc | 107 | // static int post_interval_ms = 30000; // based on the web stuff that I want to delete. |
BlueShadow | 0:7f1c7e1755bc | 108 | int debug_baud = 115200; |
BlueShadow | 0:7f1c7e1755bc | 109 | |
BlueShadow | 0:7f1c7e1755bc | 110 | |
BlueShadow | 0:7f1c7e1755bc | 111 | /**************************************************************************************************** |
BlueShadow | 0:7f1c7e1755bc | 112 | |
BlueShadow | 0:7f1c7e1755bc | 113 | ****************************************************************************************************/ |
BlueShadow | 0:7f1c7e1755bc | 114 | |
BlueShadow | 0:7f1c7e1755bc | 115 | //Macros for checking each of the different Sensor Devices |
BlueShadow | 0:7f1c7e1755bc | 116 | #define AnalogTemp //BDE0600 |
BlueShadow | 0:7f1c7e1755bc | 117 | // #define AnalogUV //ML8511 // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. |
BlueShadow | 0:7f1c7e1755bc | 118 | #define HallSensor //BU52011 |
BlueShadow | 0:7f1c7e1755bc | 119 | #define RPR0521 //RPR0521 |
BlueShadow | 0:7f1c7e1755bc | 120 | #define KMX62 //KMX61, Accel/Mag |
BlueShadow | 0:7f1c7e1755bc | 121 | #define COLOR //BH1745 |
BlueShadow | 0:7f1c7e1755bc | 122 | #define KX022 //KX022, Accel Only |
BlueShadow | 0:7f1c7e1755bc | 123 | #define Pressure //BM1383 |
BlueShadow | 0:7f1c7e1755bc | 124 | #define SMS //allow SMS messaging now sending LORA!!!! |
BlueShadow | 0:7f1c7e1755bc | 125 | //#define Web //allow M2X communication |
BlueShadow | 0:7f1c7e1755bc | 126 | |
BlueShadow | 0:7f1c7e1755bc | 127 | |
BlueShadow | 0:7f1c7e1755bc | 128 | //Define Pins for I2C Interface |
BlueShadow | 0:7f1c7e1755bc | 129 | I2C i2c(I2C_SDA, I2C_SCL); |
BlueShadow | 0:7f1c7e1755bc | 130 | bool RepStart = true; |
BlueShadow | 0:7f1c7e1755bc | 131 | bool NoRepStart = false; |
BlueShadow | 0:7f1c7e1755bc | 132 | |
BlueShadow | 0:7f1c7e1755bc | 133 | //Define Sensor Variables |
BlueShadow | 0:7f1c7e1755bc | 134 | #ifdef AnalogTemp |
BlueShadow | 0:7f1c7e1755bc | 135 | AnalogIn BDE0600_Temp(PC_1); //Mapped to A2 pin 15 on the mDot |
BlueShadow | 0:7f1c7e1755bc | 136 | uint16_t BDE0600_Temp_value; |
BlueShadow | 0:7f1c7e1755bc | 137 | float BDE0600_output; |
BlueShadow | 0:7f1c7e1755bc | 138 | #endif |
BlueShadow | 0:7f1c7e1755bc | 139 | |
BlueShadow | 0:7f1c7e1755bc | 140 | //#ifdef AnalogUV // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. |
BlueShadow | 0:7f1c7e1755bc | 141 | //AnalogIn ML8511_UV(PA_7); //Mapped to A4 not a pin routed on the UDK to the mDot |
BlueShadow | 0:7f1c7e1755bc | 142 | //uint16_t ML8511_UV_value; |
BlueShadow | 0:7f1c7e1755bc | 143 | //float ML8511_output; |
BlueShadow | 0:7f1c7e1755bc | 144 | //#endif |
BlueShadow | 0:7f1c7e1755bc | 145 | |
BlueShadow | 0:7f1c7e1755bc | 146 | #ifdef HallSensor |
BlueShadow | 0:7f1c7e1755bc | 147 | DigitalIn Hall_GPIO0(PA_4); // assigned to D10 on Arduino, mapped to pin 17 on mDot |
BlueShadow | 0:7f1c7e1755bc | 148 | DigitalIn Hall_GPIO1(PA_7); // assigned to D11 on Arduino, mapped to pin 11 on mDot |
BlueShadow | 0:7f1c7e1755bc | 149 | int Hall_Return1; |
BlueShadow | 0:7f1c7e1755bc | 150 | int Hall_Return0; |
BlueShadow | 0:7f1c7e1755bc | 151 | int32_t Hall_Return[2]; |
BlueShadow | 0:7f1c7e1755bc | 152 | #endif |
BlueShadow | 0:7f1c7e1755bc | 153 | |
BlueShadow | 0:7f1c7e1755bc | 154 | #ifdef RPR0521 |
BlueShadow | 0:7f1c7e1755bc | 155 | int RPR0521_addr_w = 0x70; //7bit addr = 0x38, with write bit 0 |
BlueShadow | 0:7f1c7e1755bc | 156 | int RPR0521_addr_r = 0x71; //7bit addr = 0x38, with read bit 1 |
BlueShadow | 0:7f1c7e1755bc | 157 | char RPR0521_ModeControl[2] = {0x41, 0xE6}; |
BlueShadow | 0:7f1c7e1755bc | 158 | char RPR0521_ALSPSControl[2] = {0x42, 0x03}; |
BlueShadow | 0:7f1c7e1755bc | 159 | char RPR0521_Persist[2] = {0x43, 0x20}; |
BlueShadow | 0:7f1c7e1755bc | 160 | char RPR0521_Addr_ReadData = 0x44; |
BlueShadow | 0:7f1c7e1755bc | 161 | char RPR0521_Content_ReadData[6]; |
BlueShadow | 0:7f1c7e1755bc | 162 | int RPR0521_PS_RAWOUT = 0; //this is an output |
BlueShadow | 0:7f1c7e1755bc | 163 | float RPR0521_PS_OUT = 0; |
BlueShadow | 0:7f1c7e1755bc | 164 | int RPR0521_ALS_D0_RAWOUT = 0; |
BlueShadow | 0:7f1c7e1755bc | 165 | int RPR0521_ALS_D1_RAWOUT = 0; |
BlueShadow | 0:7f1c7e1755bc | 166 | float RPR0521_ALS_DataRatio = 0; |
BlueShadow | 0:7f1c7e1755bc | 167 | float RPR0521_ALS_OUT = 0; //this is an output |
BlueShadow | 0:7f1c7e1755bc | 168 | float RPR0521_ALS[2]; // is this ok taking an int to the [0] value and float to [1]??????????? |
BlueShadow | 0:7f1c7e1755bc | 169 | #endif |
BlueShadow | 0:7f1c7e1755bc | 170 | |
BlueShadow | 0:7f1c7e1755bc | 171 | #ifdef KMX62 |
BlueShadow | 0:7f1c7e1755bc | 172 | int KMX62_addr_w = 0x1C; //7bit addr = 0x38, with write bit 0 |
BlueShadow | 0:7f1c7e1755bc | 173 | int KMX62_addr_r = 0x1D; //7bit addr = 0x38, with read bit 1 |
BlueShadow | 0:7f1c7e1755bc | 174 | char KMX62_CNTL2[2] = {0x3A, 0x5F}; |
BlueShadow | 0:7f1c7e1755bc | 175 | char KMX62_Addr_Accel_ReadData = 0x0A; |
BlueShadow | 0:7f1c7e1755bc | 176 | char KMX62_Content_Accel_ReadData[6]; |
BlueShadow | 0:7f1c7e1755bc | 177 | char KMX62_Addr_Mag_ReadData = 0x10; |
BlueShadow | 0:7f1c7e1755bc | 178 | char KMX62_Content_Mag_ReadData[6]; |
BlueShadow | 0:7f1c7e1755bc | 179 | short int MEMS_Accel_Xout = 0; |
BlueShadow | 0:7f1c7e1755bc | 180 | short int MEMS_Accel_Yout = 0; |
BlueShadow | 0:7f1c7e1755bc | 181 | short int MEMS_Accel_Zout = 0; |
BlueShadow | 0:7f1c7e1755bc | 182 | double MEMS_Accel_Conv_Xout = 0; |
BlueShadow | 0:7f1c7e1755bc | 183 | double MEMS_Accel_Conv_Yout = 0; |
BlueShadow | 0:7f1c7e1755bc | 184 | double MEMS_Accel_Conv_Zout = 0; |
BlueShadow | 0:7f1c7e1755bc | 185 | |
BlueShadow | 0:7f1c7e1755bc | 186 | short int MEMS_Mag_Xout = 0; |
BlueShadow | 0:7f1c7e1755bc | 187 | short int MEMS_Mag_Yout = 0; |
BlueShadow | 0:7f1c7e1755bc | 188 | short int MEMS_Mag_Zout = 0; |
BlueShadow | 0:7f1c7e1755bc | 189 | float MEMS_Mag_Conv_Xout = 0; |
BlueShadow | 0:7f1c7e1755bc | 190 | float MEMS_Mag_Conv_Yout = 0; |
BlueShadow | 0:7f1c7e1755bc | 191 | float MEMS_Mag_Conv_Zout = 0; |
BlueShadow | 0:7f1c7e1755bc | 192 | |
BlueShadow | 0:7f1c7e1755bc | 193 | double MEMS_Accel[3]; |
BlueShadow | 0:7f1c7e1755bc | 194 | float MEMS_Mag[3]; |
BlueShadow | 0:7f1c7e1755bc | 195 | #endif |
BlueShadow | 0:7f1c7e1755bc | 196 | |
BlueShadow | 0:7f1c7e1755bc | 197 | #ifdef COLOR |
BlueShadow | 0:7f1c7e1755bc | 198 | int BH1745_addr_w = 0x72; //write |
BlueShadow | 0:7f1c7e1755bc | 199 | int BH1745_addr_r = 0x73; //read |
BlueShadow | 0:7f1c7e1755bc | 200 | char BH1745_persistence[2] = {0x61, 0x03}; |
BlueShadow | 0:7f1c7e1755bc | 201 | char BH1745_mode1[2] = {0x41, 0x00}; |
BlueShadow | 0:7f1c7e1755bc | 202 | char BH1745_mode2[2] = {0x42, 0x92}; |
BlueShadow | 0:7f1c7e1755bc | 203 | char BH1745_mode3[2] = {0x43, 0x02}; |
BlueShadow | 0:7f1c7e1755bc | 204 | char BH1745_Content_ReadData[6]; |
BlueShadow | 0:7f1c7e1755bc | 205 | char BH1745_Addr_color_ReadData = 0x50; |
BlueShadow | 0:7f1c7e1755bc | 206 | int BH1745_Red; |
BlueShadow | 0:7f1c7e1755bc | 207 | int BH1745_Blue; |
BlueShadow | 0:7f1c7e1755bc | 208 | int BH1745_Green; |
BlueShadow | 0:7f1c7e1755bc | 209 | int32_t BH1745[3]; //Red, Blue Green matrix |
BlueShadow | 0:7f1c7e1755bc | 210 | #endif |
BlueShadow | 0:7f1c7e1755bc | 211 | |
BlueShadow | 0:7f1c7e1755bc | 212 | #ifdef KX022 |
BlueShadow | 0:7f1c7e1755bc | 213 | int KX022_addr_w = 0x3C; //write |
BlueShadow | 0:7f1c7e1755bc | 214 | int KX022_addr_r = 0x3D; //read |
BlueShadow | 0:7f1c7e1755bc | 215 | char KX022_Accel_CNTL1[2] = {0x18, 0x41}; |
BlueShadow | 0:7f1c7e1755bc | 216 | char KX022_Accel_ODCNTL[2] = {0x1B, 0x02}; |
BlueShadow | 0:7f1c7e1755bc | 217 | char KX022_Accel_CNTL3[2] = {0x1A, 0xD8}; |
BlueShadow | 0:7f1c7e1755bc | 218 | char KX022_Accel_TILT_TIMER[2] = {0x22, 0x01}; |
BlueShadow | 0:7f1c7e1755bc | 219 | char KX022_Accel_CNTL2[2] = {0x18, 0xC1}; |
BlueShadow | 0:7f1c7e1755bc | 220 | char KX022_Content_ReadData[6]; |
BlueShadow | 0:7f1c7e1755bc | 221 | char KX022_Addr_Accel_ReadData = 0x06; |
BlueShadow | 0:7f1c7e1755bc | 222 | float KX022_Accel_X; |
BlueShadow | 0:7f1c7e1755bc | 223 | float KX022_Accel_Y; |
BlueShadow | 0:7f1c7e1755bc | 224 | float KX022_Accel_Z; |
BlueShadow | 0:7f1c7e1755bc | 225 | short int KX022_Accel_X_RawOUT = 0; |
BlueShadow | 0:7f1c7e1755bc | 226 | short int KX022_Accel_Y_RawOUT = 0; |
BlueShadow | 0:7f1c7e1755bc | 227 | short int KX022_Accel_Z_RawOUT = 0; |
BlueShadow | 0:7f1c7e1755bc | 228 | int KX022_Accel_X_LB = 0; |
BlueShadow | 0:7f1c7e1755bc | 229 | int KX022_Accel_X_HB = 0; |
BlueShadow | 0:7f1c7e1755bc | 230 | int KX022_Accel_Y_LB = 0; |
BlueShadow | 0:7f1c7e1755bc | 231 | int KX022_Accel_Y_HB = 0; |
BlueShadow | 0:7f1c7e1755bc | 232 | int KX022_Accel_Z_LB = 0; |
BlueShadow | 0:7f1c7e1755bc | 233 | int KX022_Accel_Z_HB = 0; |
BlueShadow | 0:7f1c7e1755bc | 234 | float KX022_Accel[3]; |
BlueShadow | 0:7f1c7e1755bc | 235 | #endif |
BlueShadow | 0:7f1c7e1755bc | 236 | |
BlueShadow | 0:7f1c7e1755bc | 237 | #ifdef Pressure |
BlueShadow | 0:7f1c7e1755bc | 238 | int Press_addr_w = 0xBA; //write |
BlueShadow | 0:7f1c7e1755bc | 239 | int Press_addr_r = 0xBB; //read |
BlueShadow | 0:7f1c7e1755bc | 240 | char PWR_DOWN[2] = {0x12, 0x01}; |
BlueShadow | 0:7f1c7e1755bc | 241 | char SLEEP[2] = {0x13, 0x01}; |
BlueShadow | 0:7f1c7e1755bc | 242 | char Mode_Control[2] = {0x14, 0xC4}; |
BlueShadow | 0:7f1c7e1755bc | 243 | char Press_Content_ReadData[6]; |
BlueShadow | 0:7f1c7e1755bc | 244 | char Press_Addr_ReadData =0x1A; |
BlueShadow | 0:7f1c7e1755bc | 245 | int BM1383_Temp_highByte; |
BlueShadow | 0:7f1c7e1755bc | 246 | int BM1383_Temp_lowByte; |
BlueShadow | 0:7f1c7e1755bc | 247 | int BM1383_Pres_highByte; |
BlueShadow | 0:7f1c7e1755bc | 248 | int BM1383_Pres_lowByte; |
BlueShadow | 0:7f1c7e1755bc | 249 | int BM1383_Pres_leastByte; |
BlueShadow | 0:7f1c7e1755bc | 250 | short int BM1383_Temp_Out; |
BlueShadow | 0:7f1c7e1755bc | 251 | float BM1383_Temp_Conv_Out; |
BlueShadow | 0:7f1c7e1755bc | 252 | float BM1383_Pres_Conv_Out; |
BlueShadow | 0:7f1c7e1755bc | 253 | float_t BM1383[2]; // Temp is 0 and Pressure is 1 |
BlueShadow | 0:7f1c7e1755bc | 254 | float BM1383_Var; |
BlueShadow | 0:7f1c7e1755bc | 255 | float BM1383_Deci; |
BlueShadow | 0:7f1c7e1755bc | 256 | #endif |
BlueShadow | 0:7f1c7e1755bc | 257 | |
BlueShadow | 0:7f1c7e1755bc | 258 | /**************************************************************************************************** |
BlueShadow | 0:7f1c7e1755bc | 259 | // function prototypes |
BlueShadow | 0:7f1c7e1755bc | 260 | ****************************************************************************************************/ |
BlueShadow | 0:7f1c7e1755bc | 261 | bool init_mtsas(); |
BlueShadow | 0:7f1c7e1755bc | 262 | void ReadAnalogTemp(); |
BlueShadow | 0:7f1c7e1755bc | 263 | // void ReadAnalogUV (); // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. |
BlueShadow | 0:7f1c7e1755bc | 264 | void ReadHallSensor (); |
BlueShadow | 0:7f1c7e1755bc | 265 | void ReadCOLOR (); |
BlueShadow | 0:7f1c7e1755bc | 266 | void ReadRPR0521_ALS (); |
BlueShadow | 0:7f1c7e1755bc | 267 | void ReadKMX62_Accel (); |
BlueShadow | 0:7f1c7e1755bc | 268 | void ReadKMX62_Mag (); |
BlueShadow | 0:7f1c7e1755bc | 269 | void ReadPressure (); |
BlueShadow | 0:7f1c7e1755bc | 270 | void ReadKX022(); |
BlueShadow | 0:7f1c7e1755bc | 271 | |
BlueShadow | 0:7f1c7e1755bc | 272 | // these options must match the settings on your Conduit |
BlueShadow | 0:7f1c7e1755bc | 273 | // uncomment the following lines and edit their values to match your configuration |
BlueShadow | 0:7f1c7e1755bc | 274 | static std::string config_network_name = "Arrow123"; |
BlueShadow | 0:7f1c7e1755bc | 275 | static std::string config_network_pass = "Arrow123"; |
BlueShadow | 0:7f1c7e1755bc | 276 | static uint8_t config_frequency_sub_band = 1; |
BlueShadow | 0:7f1c7e1755bc | 277 | |
BlueShadow | 0:7f1c7e1755bc | 278 | /**************************************************************************************************** |
BlueShadow | 0:7f1c7e1755bc | 279 | // main |
BlueShadow | 0:7f1c7e1755bc | 280 | ****************************************************************************************************/ |
BlueShadow | 0:7f1c7e1755bc | 281 | int main() |
BlueShadow | 0:7f1c7e1755bc | 282 | { |
BlueShadow | 0:7f1c7e1755bc | 283 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); //NONE_, FATAL_, ERROR_, WARNING_, INFO_, DEBUG_, TRACE_ |
BlueShadow | 0:7f1c7e1755bc | 284 | debug.baud(debug_baud); |
BlueShadow | 0:7f1c7e1755bc | 285 | logInfo("starting..."); |
BlueShadow | 0:7f1c7e1755bc | 286 | |
BlueShadow | 0:7f1c7e1755bc | 287 | int32_t ret; |
BlueShadow | 0:7f1c7e1755bc | 288 | mDot* dot; |
BlueShadow | 0:7f1c7e1755bc | 289 | |
BlueShadow | 0:7f1c7e1755bc | 290 | /**************************************************************************************************** |
BlueShadow | 0:7f1c7e1755bc | 291 | Initialize LORA ************ |
BlueShadow | 0:7f1c7e1755bc | 292 | ****************************************************************************************************/ |
BlueShadow | 0:7f1c7e1755bc | 293 | // get a mDot handle |
BlueShadow | 0:7f1c7e1755bc | 294 | dot = mDot::getInstance(); |
BlueShadow | 0:7f1c7e1755bc | 295 | |
BlueShadow | 0:7f1c7e1755bc | 296 | // print library version information |
BlueShadow | 0:7f1c7e1755bc | 297 | logInfo("version: %s", dot->getId().c_str()); |
BlueShadow | 0:7f1c7e1755bc | 298 | |
BlueShadow | 0:7f1c7e1755bc | 299 | // reset to default config so we know what state we're in |
BlueShadow | 0:7f1c7e1755bc | 300 | dot->resetConfig(); |
BlueShadow | 0:7f1c7e1755bc | 301 | |
BlueShadow | 0:7f1c7e1755bc | 302 | dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); |
BlueShadow | 0:7f1c7e1755bc | 303 | |
BlueShadow | 0:7f1c7e1755bc | 304 | // set up the mDot with our network information: frequency sub band, network name, and network password |
BlueShadow | 0:7f1c7e1755bc | 305 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
BlueShadow | 0:7f1c7e1755bc | 306 | |
BlueShadow | 0:7f1c7e1755bc | 307 | // frequency sub band is only applicable in the 915 (US) frequency band |
BlueShadow | 0:7f1c7e1755bc | 308 | // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band |
BlueShadow | 0:7f1c7e1755bc | 309 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
BlueShadow | 0:7f1c7e1755bc | 310 | logInfo("setting frequency sub band"); |
BlueShadow | 0:7f1c7e1755bc | 311 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
BlueShadow | 0:7f1c7e1755bc | 312 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
BlueShadow | 0:7f1c7e1755bc | 313 | } |
BlueShadow | 0:7f1c7e1755bc | 314 | |
BlueShadow | 0:7f1c7e1755bc | 315 | logInfo("setting network name"); |
BlueShadow | 0:7f1c7e1755bc | 316 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
BlueShadow | 0:7f1c7e1755bc | 317 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
BlueShadow | 0:7f1c7e1755bc | 318 | } |
BlueShadow | 0:7f1c7e1755bc | 319 | |
BlueShadow | 0:7f1c7e1755bc | 320 | logInfo("setting network password"); |
BlueShadow | 0:7f1c7e1755bc | 321 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
BlueShadow | 0:7f1c7e1755bc | 322 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
BlueShadow | 0:7f1c7e1755bc | 323 | } |
BlueShadow | 0:7f1c7e1755bc | 324 | |
BlueShadow | 0:7f1c7e1755bc | 325 | // a higher spreading factor allows for longer range but lower throughput |
BlueShadow | 0:7f1c7e1755bc | 326 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
BlueShadow | 0:7f1c7e1755bc | 327 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
BlueShadow | 0:7f1c7e1755bc | 328 | logInfo("setting TX spreading factor"); |
BlueShadow | 0:7f1c7e1755bc | 329 | if ((ret = dot->setTxDataRate(mDot::SF_7)) != mDot::MDOT_OK) { |
BlueShadow | 0:7f1c7e1755bc | 330 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
BlueShadow | 0:7f1c7e1755bc | 331 | } |
BlueShadow | 0:7f1c7e1755bc | 332 | |
BlueShadow | 0:7f1c7e1755bc | 333 | // request receive confirmation of packets from the gateway |
BlueShadow | 0:7f1c7e1755bc | 334 | logInfo("enabling ACKs"); |
BlueShadow | 0:7f1c7e1755bc | 335 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
BlueShadow | 0:7f1c7e1755bc | 336 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
BlueShadow | 0:7f1c7e1755bc | 337 | } |
BlueShadow | 0:7f1c7e1755bc | 338 | |
BlueShadow | 0:7f1c7e1755bc | 339 | // save this configuration to the mDot's NVM |
BlueShadow | 0:7f1c7e1755bc | 340 | logInfo("saving config"); |
BlueShadow | 0:7f1c7e1755bc | 341 | if (! dot->saveConfig()) { |
BlueShadow | 0:7f1c7e1755bc | 342 | logError("failed to save configuration"); |
BlueShadow | 0:7f1c7e1755bc | 343 | } |
BlueShadow | 0:7f1c7e1755bc | 344 | //******************************************* |
BlueShadow | 0:7f1c7e1755bc | 345 | // end of configuration |
BlueShadow | 0:7f1c7e1755bc | 346 | //******************************************* |
BlueShadow | 0:7f1c7e1755bc | 347 | |
BlueShadow | 0:7f1c7e1755bc | 348 | // attempt to join the network |
BlueShadow | 0:7f1c7e1755bc | 349 | logInfo("joining network"); |
BlueShadow | 0:7f1c7e1755bc | 350 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
BlueShadow | 0:7f1c7e1755bc | 351 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
BlueShadow | 0:7f1c7e1755bc | 352 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
BlueShadow | 0:7f1c7e1755bc | 353 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
BlueShadow | 0:7f1c7e1755bc | 354 | } |
BlueShadow | 0:7f1c7e1755bc | 355 | |
BlueShadow | 0:7f1c7e1755bc | 356 | |
BlueShadow | 0:7f1c7e1755bc | 357 | /**************************************************************************************************** |
BlueShadow | 0:7f1c7e1755bc | 358 | Initialize I2C Devices ************ |
BlueShadow | 0:7f1c7e1755bc | 359 | ****************************************************************************************************/ |
BlueShadow | 0:7f1c7e1755bc | 360 | |
BlueShadow | 0:7f1c7e1755bc | 361 | #ifdef RPR0521 |
BlueShadow | 0:7f1c7e1755bc | 362 | i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 363 | i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 364 | i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 365 | #endif |
BlueShadow | 0:7f1c7e1755bc | 366 | |
BlueShadow | 0:7f1c7e1755bc | 367 | #ifdef KMX62 |
BlueShadow | 0:7f1c7e1755bc | 368 | i2c.write(KMX62_addr_w, &KMX62_CNTL2[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 369 | #endif |
BlueShadow | 0:7f1c7e1755bc | 370 | |
BlueShadow | 0:7f1c7e1755bc | 371 | #ifdef COLOR |
BlueShadow | 0:7f1c7e1755bc | 372 | i2c.write(BH1745_addr_w, &BH1745_persistence[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 373 | i2c.write(BH1745_addr_w, &BH1745_mode1[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 374 | i2c.write(BH1745_addr_w, &BH1745_mode2[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 375 | i2c.write(BH1745_addr_w, &BH1745_mode3[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 376 | #endif |
BlueShadow | 0:7f1c7e1755bc | 377 | |
BlueShadow | 0:7f1c7e1755bc | 378 | #ifdef KX022 |
BlueShadow | 0:7f1c7e1755bc | 379 | i2c.write(KX022_addr_w, &KX022_Accel_CNTL1[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 380 | i2c.write(KX022_addr_w, &KX022_Accel_ODCNTL[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 381 | i2c.write(KX022_addr_w, &KX022_Accel_CNTL3[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 382 | i2c.write(KX022_addr_w, &KX022_Accel_TILT_TIMER[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 383 | i2c.write(KX022_addr_w, &KX022_Accel_CNTL2[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 384 | #endif |
BlueShadow | 0:7f1c7e1755bc | 385 | |
BlueShadow | 0:7f1c7e1755bc | 386 | #ifdef Pressure |
BlueShadow | 0:7f1c7e1755bc | 387 | i2c.write(Press_addr_w, &PWR_DOWN[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 388 | i2c.write(Press_addr_w, &SLEEP[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 389 | i2c.write(Press_addr_w, &Mode_Control[0], 2, false); |
BlueShadow | 0:7f1c7e1755bc | 390 | #endif |
BlueShadow | 0:7f1c7e1755bc | 391 | //End I2C Initialization Section ********************************************************** |
BlueShadow | 0:7f1c7e1755bc | 392 | |
BlueShadow | 0:7f1c7e1755bc | 393 | |
BlueShadow | 0:7f1c7e1755bc | 394 | Timer thpm_timer; |
BlueShadow | 0:7f1c7e1755bc | 395 | thpm_timer.start(); // Timer data is set in the Variable seciton see misc variables Timer motion_timer; |
BlueShadow | 0:7f1c7e1755bc | 396 | Timer print_timer; |
BlueShadow | 0:7f1c7e1755bc | 397 | print_timer.start(); |
BlueShadow | 0:7f1c7e1755bc | 398 | Timer motion_timer; |
BlueShadow | 0:7f1c7e1755bc | 399 | motion_timer.start(); |
BlueShadow | 0:7f1c7e1755bc | 400 | |
BlueShadow | 0:7f1c7e1755bc | 401 | #ifdef SMS |
BlueShadow | 0:7f1c7e1755bc | 402 | Timer sms_timer; |
BlueShadow | 0:7f1c7e1755bc | 403 | sms_timer.start(); |
BlueShadow | 0:7f1c7e1755bc | 404 | #endif |
BlueShadow | 0:7f1c7e1755bc | 405 | |
BlueShadow | 0:7f1c7e1755bc | 406 | #ifdef Web |
BlueShadow | 0:7f1c7e1755bc | 407 | Timer post_timer; |
BlueShadow | 0:7f1c7e1755bc | 408 | post_timer.start(); |
BlueShadow | 0:7f1c7e1755bc | 409 | #endif |
BlueShadow | 0:7f1c7e1755bc | 410 | |
BlueShadow | 0:7f1c7e1755bc | 411 | while (true) { |
BlueShadow | 0:7f1c7e1755bc | 412 | if (thpm_timer.read_ms() > thpm_interval_ms) { |
BlueShadow | 0:7f1c7e1755bc | 413 | #ifdef AnalogTemp |
BlueShadow | 0:7f1c7e1755bc | 414 | ReadAnalogTemp (); |
BlueShadow | 0:7f1c7e1755bc | 415 | #endif |
BlueShadow | 0:7f1c7e1755bc | 416 | |
BlueShadow | 0:7f1c7e1755bc | 417 | //#ifdef AnalogUV // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. |
BlueShadow | 0:7f1c7e1755bc | 418 | // ReadAnalogUV (); |
BlueShadow | 0:7f1c7e1755bc | 419 | //#endif |
BlueShadow | 0:7f1c7e1755bc | 420 | |
BlueShadow | 0:7f1c7e1755bc | 421 | #ifdef HallSensor |
BlueShadow | 0:7f1c7e1755bc | 422 | ReadHallSensor (); |
BlueShadow | 0:7f1c7e1755bc | 423 | #endif |
BlueShadow | 0:7f1c7e1755bc | 424 | |
BlueShadow | 0:7f1c7e1755bc | 425 | #ifdef COLOR |
BlueShadow | 0:7f1c7e1755bc | 426 | ReadCOLOR (); |
BlueShadow | 0:7f1c7e1755bc | 427 | #endif |
BlueShadow | 0:7f1c7e1755bc | 428 | |
BlueShadow | 0:7f1c7e1755bc | 429 | #ifdef RPR0521 //als digital |
BlueShadow | 0:7f1c7e1755bc | 430 | ReadRPR0521_ALS (); |
BlueShadow | 0:7f1c7e1755bc | 431 | #endif |
BlueShadow | 0:7f1c7e1755bc | 432 | |
BlueShadow | 0:7f1c7e1755bc | 433 | #ifdef Pressure |
BlueShadow | 0:7f1c7e1755bc | 434 | ReadPressure(); |
BlueShadow | 0:7f1c7e1755bc | 435 | #endif |
BlueShadow | 0:7f1c7e1755bc | 436 | thpm_timer.reset(); |
BlueShadow | 0:7f1c7e1755bc | 437 | } |
BlueShadow | 0:7f1c7e1755bc | 438 | |
BlueShadow | 0:7f1c7e1755bc | 439 | if (motion_timer.read_ms() > motion_interval_ms) { |
BlueShadow | 0:7f1c7e1755bc | 440 | #ifdef KMX62 |
BlueShadow | 0:7f1c7e1755bc | 441 | ReadKMX62_Accel (); |
BlueShadow | 0:7f1c7e1755bc | 442 | ReadKMX62_Mag (); |
BlueShadow | 0:7f1c7e1755bc | 443 | #endif |
BlueShadow | 0:7f1c7e1755bc | 444 | |
BlueShadow | 0:7f1c7e1755bc | 445 | #ifdef KX022 |
BlueShadow | 0:7f1c7e1755bc | 446 | ReadKX022 (); |
BlueShadow | 0:7f1c7e1755bc | 447 | #endif |
BlueShadow | 0:7f1c7e1755bc | 448 | motion_timer.reset(); |
BlueShadow | 0:7f1c7e1755bc | 449 | } |
BlueShadow | 0:7f1c7e1755bc | 450 | |
BlueShadow | 0:7f1c7e1755bc | 451 | if (print_timer.read_ms() > print_interval_ms) { |
BlueShadow | 0:7f1c7e1755bc | 452 | logDebug("%s", wall_of_dash); |
BlueShadow | 0:7f1c7e1755bc | 453 | logDebug("SENSOR DATA"); |
BlueShadow | 0:7f1c7e1755bc | 454 | logDebug("temperature: %0.2f C", BM1383[0]); |
BlueShadow | 0:7f1c7e1755bc | 455 | // logDebug("analog uv: %.1f mW/cm2", ML8511_output); // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. |
BlueShadow | 0:7f1c7e1755bc | 456 | logDebug("ambient Light %0.3f", RPR0521_ALS[0]); |
BlueShadow | 0:7f1c7e1755bc | 457 | logDebug("proximity count %0.3f", RPR0521_ALS[1]); |
BlueShadow | 0:7f1c7e1755bc | 458 | logDebug("hall effect: South %d\t North %d", Hall_Return[0],Hall_Return[1]); |
BlueShadow | 0:7f1c7e1755bc | 459 | logDebug("pressure: %0.2f hPa", BM1383[1]); |
BlueShadow | 0:7f1c7e1755bc | 460 | logDebug("magnetometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tuT", MEMS_Mag[0], MEMS_Mag[1], MEMS_Mag[2]); |
BlueShadow | 0:7f1c7e1755bc | 461 | logDebug("accelerometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tg", MEMS_Accel[0], MEMS_Accel[1], MEMS_Accel[2]); |
BlueShadow | 0:7f1c7e1755bc | 462 | logDebug("color:\r\n\tred: %ld\tgrn: %ld\tblu: %ld\t", BH1745[0], BH1745[1], BH1745[2]); |
BlueShadow | 0:7f1c7e1755bc | 463 | logDebug("%s", wall_of_dash); |
BlueShadow | 0:7f1c7e1755bc | 464 | print_timer.reset(); |
BlueShadow | 0:7f1c7e1755bc | 465 | } |
BlueShadow | 0:7f1c7e1755bc | 466 | |
BlueShadow | 0:7f1c7e1755bc | 467 | |
BlueShadow | 0:7f1c7e1755bc | 468 | |
BlueShadow | 0:7f1c7e1755bc | 469 | #ifdef SMS |
BlueShadow | 0:7f1c7e1755bc | 470 | if (sms_timer.read_ms() > sms_interval_ms) { |
BlueShadow | 0:7f1c7e1755bc | 471 | sms_timer.reset(); |
BlueShadow | 0:7f1c7e1755bc | 472 | logInfo("SMS Send Routine"); |
BlueShadow | 0:7f1c7e1755bc | 473 | printf(" In sms routine \r\n"); |
BlueShadow | 0:7f1c7e1755bc | 474 | |
BlueShadow | 0:7f1c7e1755bc | 475 | char send_msg[20]; |
BlueShadow | 0:7f1c7e1755bc | 476 | sprintf(send_msg, "Dilbert,%0.0f", RPR0521_ALS[0]); |
BlueShadow | 0:7f1c7e1755bc | 477 | std::string send_msg_str(send_msg); |
BlueShadow | 0:7f1c7e1755bc | 478 | std::vector<uint8_t> data(send_msg_str.begin(), send_msg_str.end()); |
BlueShadow | 0:7f1c7e1755bc | 479 | |
BlueShadow | 0:7f1c7e1755bc | 480 | // Added the mdot send code here vs that the sms fuction was doing. seemed reasonable. |
BlueShadow | 0:7f1c7e1755bc | 481 | // send the data to the gateway |
BlueShadow | 0:7f1c7e1755bc | 482 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
BlueShadow | 0:7f1c7e1755bc | 483 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
BlueShadow | 0:7f1c7e1755bc | 484 | } else { |
BlueShadow | 0:7f1c7e1755bc | 485 | logInfo("successfully sent data to gateway"); |
BlueShadow | 0:7f1c7e1755bc | 486 | } |
BlueShadow | 0:7f1c7e1755bc | 487 | |
BlueShadow | 0:7f1c7e1755bc | 488 | // End of send LoRa radio code. |
BlueShadow | 0:7f1c7e1755bc | 489 | |
BlueShadow | 0:7f1c7e1755bc | 490 | // if (radio_ok) { |
BlueShadow | 0:7f1c7e1755bc | 491 | // MbedJSONValue sms_json; |
BlueShadow | 0:7f1c7e1755bc | 492 | // string sms_str; |
BlueShadow | 0:7f1c7e1755bc | 493 | // |
BlueShadow | 0:7f1c7e1755bc | 494 | // sms_json["temp_C"] = BDE0600_output; |
BlueShadow | 0:7f1c7e1755bc | 495 | // sms_json["UV"] = ML8511_output; |
BlueShadow | 0:7f1c7e1755bc | 496 | // sms_json["Ambient Light"] = RPR0521_ALS[0]; |
BlueShadow | 0:7f1c7e1755bc | 497 | // sms_json["Prox"] = RPR0521_ALS[1]; |
BlueShadow | 0:7f1c7e1755bc | 498 | // sms_json["pressure_hPa"] = BM1383[1]; |
BlueShadow | 0:7f1c7e1755bc | 499 | // sms_json["mag_mgauss"]["x"] = MEMS_Mag[0]; |
BlueShadow | 0:7f1c7e1755bc | 500 | // sms_json["mag_mgauss"]["y"] = MEMS_Mag[1]; |
BlueShadow | 0:7f1c7e1755bc | 501 | // sms_json["mag_mgauss"]["z"] = MEMS_Mag[2]; |
BlueShadow | 0:7f1c7e1755bc | 502 | // sms_json["acc_mg"]["x"] = MEMS_Accel[0]; |
BlueShadow | 0:7f1c7e1755bc | 503 | // sms_json["acc_mg"]["y"] = MEMS_Accel[1]; |
BlueShadow | 0:7f1c7e1755bc | 504 | // sms_json["acc_mg"]["z"] = MEMS_Accel[2]; |
BlueShadow | 0:7f1c7e1755bc | 505 | // sms_json["Red"] = BH1745[0]; |
BlueShadow | 0:7f1c7e1755bc | 506 | // sms_json["Green"] = BH1745[1]; |
BlueShadow | 0:7f1c7e1755bc | 507 | // sms_json["Blue"] = BH1745[2]; |
BlueShadow | 0:7f1c7e1755bc | 508 | // |
BlueShadow | 0:7f1c7e1755bc | 509 | // sms_str = "SENSOR DATA:\n"; |
BlueShadow | 0:7f1c7e1755bc | 510 | // sms_str += sms_json.serialize(); |
BlueShadow | 0:7f1c7e1755bc | 511 | // |
BlueShadow | 0:7f1c7e1755bc | 512 | // logDebug("sending SMS to %s:\r\n%s", phone_number.c_str(), sms_str.c_str()); |
BlueShadow | 0:7f1c7e1755bc | 513 | // Code ret = radio->sendSMS(phone_number, sms_str); |
BlueShadow | 0:7f1c7e1755bc | 514 | // if (ret != MTS_SUCCESS) |
BlueShadow | 0:7f1c7e1755bc | 515 | // logError("sending SMS failed"); |
BlueShadow | 0:7f1c7e1755bc | 516 | // } |
BlueShadow | 0:7f1c7e1755bc | 517 | } |
BlueShadow | 0:7f1c7e1755bc | 518 | #endif |
BlueShadow | 0:7f1c7e1755bc | 519 | |
BlueShadow | 0:7f1c7e1755bc | 520 | #ifdef Web |
BlueShadow | 0:7f1c7e1755bc | 521 | if (post_timer.read_ms() > post_interval_ms && do_cloud_post) { |
BlueShadow | 0:7f1c7e1755bc | 522 | printf("in web\n\r"); |
BlueShadow | 0:7f1c7e1755bc | 523 | // if (radio->connect()) { |
BlueShadow | 0:7f1c7e1755bc | 524 | // logDebug("posting sensor data"); |
BlueShadow | 0:7f1c7e1755bc | 525 | // |
BlueShadow | 0:7f1c7e1755bc | 526 | // HTTPClient http; |
BlueShadow | 0:7f1c7e1755bc | 527 | // MbedJSONValue http_json_data; |
BlueShadow | 0:7f1c7e1755bc | 528 | // std::string http_json_str; |
BlueShadow | 0:7f1c7e1755bc | 529 | // std::string m2x_header = "X-M2X-KEY: " + m2x_api_key + "\r\n"; |
BlueShadow | 0:7f1c7e1755bc | 530 | // int ret; |
BlueShadow | 0:7f1c7e1755bc | 531 | // char http_response_buf[256]; |
BlueShadow | 0:7f1c7e1755bc | 532 | // HTTPText http_response(http_response_buf, sizeof(http_response_buf)); |
BlueShadow | 0:7f1c7e1755bc | 533 | // |
BlueShadow | 0:7f1c7e1755bc | 534 | // // temp_c, temp_f, humidity, pressure, and moisture are all stream IDs for my device in M2X |
BlueShadow | 0:7f1c7e1755bc | 535 | // // modify these to match your streams or give your streams the same name |
BlueShadow | 0:7f1c7e1755bc | 536 | // http_json_data["values"]["temp_c"] = BDE0600_output; |
BlueShadow | 0:7f1c7e1755bc | 537 | // http_json_data["values"]["UV"] = ML8511_output; |
BlueShadow | 0:7f1c7e1755bc | 538 | // http_json_data["values"]["Ambient Light"] = RPR0521_ALS[0]; |
BlueShadow | 0:7f1c7e1755bc | 539 | // http_json_data["values"]["Prox"] = RPR0521_ALS[1]; |
BlueShadow | 0:7f1c7e1755bc | 540 | // http_json_str = http_json_data.serialize(); |
BlueShadow | 0:7f1c7e1755bc | 541 | // |
BlueShadow | 0:7f1c7e1755bc | 542 | // // add extra header with M2X API key |
BlueShadow | 0:7f1c7e1755bc | 543 | // http.setHeader(m2x_header.c_str()); |
BlueShadow | 0:7f1c7e1755bc | 544 | // |
BlueShadow | 0:7f1c7e1755bc | 545 | // HTTPJson http_json((char*) http_json_str.c_str()); |
BlueShadow | 0:7f1c7e1755bc | 546 | // ret = http.post(url.c_str(), http_json, &http_response); |
BlueShadow | 0:7f1c7e1755bc | 547 | // if (ret != HTTP_OK) |
BlueShadow | 0:7f1c7e1755bc | 548 | // logError("posting data to cloud failed: [%d][%s]", ret, http_response_buf); |
BlueShadow | 0:7f1c7e1755bc | 549 | // else |
BlueShadow | 0:7f1c7e1755bc | 550 | // logDebug("post result [%d][%s]", http.getHTTPResponseCode(), http_response_buf); |
BlueShadow | 0:7f1c7e1755bc | 551 | // |
BlueShadow | 0:7f1c7e1755bc | 552 | // radio->disconnect(); |
BlueShadow | 0:7f1c7e1755bc | 553 | // } else { |
BlueShadow | 0:7f1c7e1755bc | 554 | // logError("establishing PPP link failed"); |
BlueShadow | 0:7f1c7e1755bc | 555 | // } |
BlueShadow | 0:7f1c7e1755bc | 556 | |
BlueShadow | 0:7f1c7e1755bc | 557 | post_timer.reset(); |
BlueShadow | 0:7f1c7e1755bc | 558 | } |
BlueShadow | 0:7f1c7e1755bc | 559 | #endif |
BlueShadow | 0:7f1c7e1755bc | 560 | |
BlueShadow | 0:7f1c7e1755bc | 561 | wait_ms(10); |
BlueShadow | 0:7f1c7e1755bc | 562 | } |
BlueShadow | 0:7f1c7e1755bc | 563 | } |
BlueShadow | 0:7f1c7e1755bc | 564 | |
BlueShadow | 0:7f1c7e1755bc | 565 | |
BlueShadow | 0:7f1c7e1755bc | 566 | |
BlueShadow | 0:7f1c7e1755bc | 567 | // Sensor data acquisition functions |
BlueShadow | 0:7f1c7e1755bc | 568 | /************************************************************************************************/ |
BlueShadow | 0:7f1c7e1755bc | 569 | #ifdef AnalogTemp |
BlueShadow | 0:7f1c7e1755bc | 570 | void ReadAnalogTemp () |
BlueShadow | 0:7f1c7e1755bc | 571 | { |
BlueShadow | 0:7f1c7e1755bc | 572 | BDE0600_Temp_value = BDE0600_Temp.read_u16(); |
BlueShadow | 0:7f1c7e1755bc | 573 | |
BlueShadow | 0:7f1c7e1755bc | 574 | BDE0600_output = (float)BDE0600_Temp_value * (float)0.000050354; //(value * (3.3V/65535)) |
BlueShadow | 0:7f1c7e1755bc | 575 | BDE0600_output = (BDE0600_output-(float)1.753)/((float)-0.01068) + (float)30; |
BlueShadow | 0:7f1c7e1755bc | 576 | |
BlueShadow | 0:7f1c7e1755bc | 577 | // printf("BDE0600 Analog Temp Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 578 | // printf(" Temp = %.2f C\r\n", BDE0600_output); |
BlueShadow | 0:7f1c7e1755bc | 579 | } |
BlueShadow | 0:7f1c7e1755bc | 580 | #endif |
BlueShadow | 0:7f1c7e1755bc | 581 | |
BlueShadow | 0:7f1c7e1755bc | 582 | //#ifdef AnalogUV // analog pin A4 on Arduino connector is not connected to the mDot on the UDK. |
BlueShadow | 0:7f1c7e1755bc | 583 | //void ReadAnalogUV () |
BlueShadow | 0:7f1c7e1755bc | 584 | //{ |
BlueShadow | 0:7f1c7e1755bc | 585 | // ML8511_UV_value = ML8511_UV.read_u16(); |
BlueShadow | 0:7f1c7e1755bc | 586 | // 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 | 0:7f1c7e1755bc | 587 | // 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 | 0:7f1c7e1755bc | 588 | |
BlueShadow | 0:7f1c7e1755bc | 589 | // printf("ML8511 Analog UV Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 590 | // printf(" UV = %.1f mW/cm2\r\n", ML8511_output); |
BlueShadow | 0:7f1c7e1755bc | 591 | |
BlueShadow | 0:7f1c7e1755bc | 592 | //} |
BlueShadow | 0:7f1c7e1755bc | 593 | //#endif |
BlueShadow | 0:7f1c7e1755bc | 594 | |
BlueShadow | 0:7f1c7e1755bc | 595 | |
BlueShadow | 0:7f1c7e1755bc | 596 | #ifdef HallSensor |
BlueShadow | 0:7f1c7e1755bc | 597 | void ReadHallSensor () |
BlueShadow | 0:7f1c7e1755bc | 598 | { |
BlueShadow | 0:7f1c7e1755bc | 599 | |
BlueShadow | 0:7f1c7e1755bc | 600 | Hall_Return[0] = Hall_GPIO0; |
BlueShadow | 0:7f1c7e1755bc | 601 | Hall_Return[1] = Hall_GPIO1; |
BlueShadow | 0:7f1c7e1755bc | 602 | |
BlueShadow | 0:7f1c7e1755bc | 603 | // printf("BU52011 Hall Switch Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 604 | // printf(" South Detect = %d\r\n", Hall_Return[0]); |
BlueShadow | 0:7f1c7e1755bc | 605 | // printf(" North Detect = %d\r\n", Hall_Return[1]); |
BlueShadow | 0:7f1c7e1755bc | 606 | |
BlueShadow | 0:7f1c7e1755bc | 607 | |
BlueShadow | 0:7f1c7e1755bc | 608 | } |
BlueShadow | 0:7f1c7e1755bc | 609 | #endif |
BlueShadow | 0:7f1c7e1755bc | 610 | |
BlueShadow | 0:7f1c7e1755bc | 611 | #ifdef COLOR |
BlueShadow | 0:7f1c7e1755bc | 612 | void ReadCOLOR () |
BlueShadow | 0:7f1c7e1755bc | 613 | { |
BlueShadow | 0:7f1c7e1755bc | 614 | |
BlueShadow | 0:7f1c7e1755bc | 615 | //Read color data from the IC |
BlueShadow | 0:7f1c7e1755bc | 616 | i2c.write(BH1745_addr_w, &BH1745_Addr_color_ReadData, 1, RepStart); |
BlueShadow | 0:7f1c7e1755bc | 617 | i2c.read(BH1745_addr_r, &BH1745_Content_ReadData[0], 6, NoRepStart); |
BlueShadow | 0:7f1c7e1755bc | 618 | |
BlueShadow | 0:7f1c7e1755bc | 619 | //separate all data read into colors |
BlueShadow | 0:7f1c7e1755bc | 620 | BH1745[0] = (BH1745_Content_ReadData[1]<<8) | (BH1745_Content_ReadData[0]); |
BlueShadow | 0:7f1c7e1755bc | 621 | BH1745[1] = (BH1745_Content_ReadData[3]<<8) | (BH1745_Content_ReadData[2]); |
BlueShadow | 0:7f1c7e1755bc | 622 | BH1745[2] = (BH1745_Content_ReadData[5]<<8) | (BH1745_Content_ReadData[4]); |
BlueShadow | 0:7f1c7e1755bc | 623 | |
BlueShadow | 0:7f1c7e1755bc | 624 | //Output Data into UART |
BlueShadow | 0:7f1c7e1755bc | 625 | // printf("BH1745 COLOR Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 626 | // printf(" Red = %d ADC Counts\r\n",BH1745[0]); |
BlueShadow | 0:7f1c7e1755bc | 627 | // printf(" Green = %d ADC Counts\r\n",BH1745[1]); |
BlueShadow | 0:7f1c7e1755bc | 628 | // printf(" Blue = %d ADC Counts\r\n",BH1745[2]); |
BlueShadow | 0:7f1c7e1755bc | 629 | |
BlueShadow | 0:7f1c7e1755bc | 630 | } |
BlueShadow | 0:7f1c7e1755bc | 631 | #endif |
BlueShadow | 0:7f1c7e1755bc | 632 | |
BlueShadow | 0:7f1c7e1755bc | 633 | #ifdef RPR0521 //als digital |
BlueShadow | 0:7f1c7e1755bc | 634 | void ReadRPR0521_ALS () |
BlueShadow | 0:7f1c7e1755bc | 635 | { |
BlueShadow | 0:7f1c7e1755bc | 636 | i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart); |
BlueShadow | 0:7f1c7e1755bc | 637 | i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart); |
BlueShadow | 0:7f1c7e1755bc | 638 | |
BlueShadow | 0:7f1c7e1755bc | 639 | RPR0521_ALS[1] = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]); |
BlueShadow | 0:7f1c7e1755bc | 640 | RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]); |
BlueShadow | 0:7f1c7e1755bc | 641 | RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]); |
BlueShadow | 0:7f1c7e1755bc | 642 | RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT; |
BlueShadow | 0:7f1c7e1755bc | 643 | |
BlueShadow | 0:7f1c7e1755bc | 644 | if(RPR0521_ALS_DataRatio < (float)0.595) { |
BlueShadow | 0:7f1c7e1755bc | 645 | RPR0521_ALS[0] = ((float)1.682*(float)RPR0521_ALS_D0_RAWOUT - (float)1.877*(float)RPR0521_ALS_D1_RAWOUT); |
BlueShadow | 0:7f1c7e1755bc | 646 | } else if(RPR0521_ALS_DataRatio < (float)1.015) { |
BlueShadow | 0:7f1c7e1755bc | 647 | RPR0521_ALS[0] = ((float)0.644*(float)RPR0521_ALS_D0_RAWOUT - (float)0.132*(float)RPR0521_ALS_D1_RAWOUT); |
BlueShadow | 0:7f1c7e1755bc | 648 | } else if(RPR0521_ALS_DataRatio < (float)1.352) { |
BlueShadow | 0:7f1c7e1755bc | 649 | RPR0521_ALS[0] = ((float)0.756*(float)RPR0521_ALS_D0_RAWOUT - (float)0.243*(float)RPR0521_ALS_D1_RAWOUT); |
BlueShadow | 0:7f1c7e1755bc | 650 | } else if(RPR0521_ALS_DataRatio < (float)3.053) { |
BlueShadow | 0:7f1c7e1755bc | 651 | RPR0521_ALS[0] = ((float)0.766*(float)RPR0521_ALS_D0_RAWOUT - (float)0.25*(float)RPR0521_ALS_D1_RAWOUT); |
BlueShadow | 0:7f1c7e1755bc | 652 | } else { |
BlueShadow | 0:7f1c7e1755bc | 653 | RPR0521_ALS[0] = 0; |
BlueShadow | 0:7f1c7e1755bc | 654 | } |
BlueShadow | 0:7f1c7e1755bc | 655 | // printf("RPR-0521 ALS/PROX Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 656 | // printf(" ALS = %0.2f lx\r\n", RPR0521_ALS[0]); |
BlueShadow | 0:7f1c7e1755bc | 657 | // printf(" PROX= %0.2f ADC Counts\r\n", RPR0521_ALS[1]); //defined as a float but is an unsigned. |
BlueShadow | 0:7f1c7e1755bc | 658 | |
BlueShadow | 0:7f1c7e1755bc | 659 | } |
BlueShadow | 0:7f1c7e1755bc | 660 | #endif |
BlueShadow | 0:7f1c7e1755bc | 661 | |
BlueShadow | 0:7f1c7e1755bc | 662 | #ifdef KMX62 |
BlueShadow | 0:7f1c7e1755bc | 663 | void ReadKMX62_Accel () |
BlueShadow | 0:7f1c7e1755bc | 664 | { |
BlueShadow | 0:7f1c7e1755bc | 665 | //Read Accel Portion from the IC |
BlueShadow | 0:7f1c7e1755bc | 666 | i2c.write(KMX62_addr_w, &KMX62_Addr_Accel_ReadData, 1, RepStart); |
BlueShadow | 0:7f1c7e1755bc | 667 | i2c.read(KMX62_addr_r, &KMX62_Content_Accel_ReadData[0], 6, NoRepStart); |
BlueShadow | 0:7f1c7e1755bc | 668 | |
BlueShadow | 0:7f1c7e1755bc | 669 | //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte. |
BlueShadow | 0:7f1c7e1755bc | 670 | // However, because we need the signed value, we will adjust the value when converting to "g" |
BlueShadow | 0:7f1c7e1755bc | 671 | MEMS_Accel_Xout = (KMX62_Content_Accel_ReadData[1]<<8) | (KMX62_Content_Accel_ReadData[0]); |
BlueShadow | 0:7f1c7e1755bc | 672 | MEMS_Accel_Yout = (KMX62_Content_Accel_ReadData[3]<<8) | (KMX62_Content_Accel_ReadData[2]); |
BlueShadow | 0:7f1c7e1755bc | 673 | MEMS_Accel_Zout = (KMX62_Content_Accel_ReadData[5]<<8) | (KMX62_Content_Accel_ReadData[4]); |
BlueShadow | 0:7f1c7e1755bc | 674 | |
BlueShadow | 0:7f1c7e1755bc | 675 | //Note: Conversion to G is as follows: |
BlueShadow | 0:7f1c7e1755bc | 676 | // Axis_ValueInG = MEMS_Accel_axis / 1024 |
BlueShadow | 0:7f1c7e1755bc | 677 | // However, since we did not remove the LSB previously, we need to divide by 4 again |
BlueShadow | 0:7f1c7e1755bc | 678 | // Thus, we will divide the output by 4096 (1024*4) to convert and cancel out the LSB |
BlueShadow | 0:7f1c7e1755bc | 679 | MEMS_Accel[0] = ((float)MEMS_Accel_Xout/4096/2); |
BlueShadow | 0:7f1c7e1755bc | 680 | MEMS_Accel[1] = ((float)MEMS_Accel_Yout/4096/2); |
BlueShadow | 0:7f1c7e1755bc | 681 | MEMS_Accel[2] = ((float)MEMS_Accel_Zout/4096/2); |
BlueShadow | 0:7f1c7e1755bc | 682 | |
BlueShadow | 0:7f1c7e1755bc | 683 | // Return Data to UART |
BlueShadow | 0:7f1c7e1755bc | 684 | // printf("KMX62 Accel+Mag Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 685 | // printf(" AccX= %0.2f g\r\n", MEMS_Accel[0]); |
BlueShadow | 0:7f1c7e1755bc | 686 | // printf(" AccY= %0.2f g\r\n", MEMS_Accel[1]); |
BlueShadow | 0:7f1c7e1755bc | 687 | // printf(" AccZ= %0.2f g\r\n", MEMS_Accel[2]); |
BlueShadow | 0:7f1c7e1755bc | 688 | |
BlueShadow | 0:7f1c7e1755bc | 689 | } |
BlueShadow | 0:7f1c7e1755bc | 690 | |
BlueShadow | 0:7f1c7e1755bc | 691 | void ReadKMX62_Mag () |
BlueShadow | 0:7f1c7e1755bc | 692 | { |
BlueShadow | 0:7f1c7e1755bc | 693 | |
BlueShadow | 0:7f1c7e1755bc | 694 | //Read Mag portion from the IC |
BlueShadow | 0:7f1c7e1755bc | 695 | i2c.write(KMX62_addr_w, &KMX62_Addr_Mag_ReadData, 1, RepStart); |
BlueShadow | 0:7f1c7e1755bc | 696 | i2c.read(KMX62_addr_r, &KMX62_Content_Mag_ReadData[0], 6, NoRepStart); |
BlueShadow | 0:7f1c7e1755bc | 697 | |
BlueShadow | 0:7f1c7e1755bc | 698 | //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte. |
BlueShadow | 0:7f1c7e1755bc | 699 | // However, because we need the signed value, we will adjust the value when converting to "g" |
BlueShadow | 0:7f1c7e1755bc | 700 | MEMS_Mag_Xout = (KMX62_Content_Mag_ReadData[1]<<8) | (KMX62_Content_Mag_ReadData[0]); |
BlueShadow | 0:7f1c7e1755bc | 701 | MEMS_Mag_Yout = (KMX62_Content_Mag_ReadData[3]<<8) | (KMX62_Content_Mag_ReadData[2]); |
BlueShadow | 0:7f1c7e1755bc | 702 | MEMS_Mag_Zout = (KMX62_Content_Mag_ReadData[5]<<8) | (KMX62_Content_Mag_ReadData[4]); |
BlueShadow | 0:7f1c7e1755bc | 703 | |
BlueShadow | 0:7f1c7e1755bc | 704 | //Note: Conversion to G is as follows: |
BlueShadow | 0:7f1c7e1755bc | 705 | // Axis_ValueInG = MEMS_Accel_axis / 1024 |
BlueShadow | 0:7f1c7e1755bc | 706 | // However, since we did not remove the LSB previously, we need to divide by 4 again |
BlueShadow | 0:7f1c7e1755bc | 707 | // Thus, we will divide the output by 4095 (1024*4) to convert and cancel out the LSB |
BlueShadow | 0:7f1c7e1755bc | 708 | MEMS_Mag[0] = (float)MEMS_Mag_Xout/4096*(float)0.146; |
BlueShadow | 0:7f1c7e1755bc | 709 | MEMS_Mag[1] = (float)MEMS_Mag_Yout/4096*(float)0.146; |
BlueShadow | 0:7f1c7e1755bc | 710 | MEMS_Mag[2] = (float)MEMS_Mag_Zout/4096*(float)0.146; |
BlueShadow | 0:7f1c7e1755bc | 711 | |
BlueShadow | 0:7f1c7e1755bc | 712 | // Return Data to UART |
BlueShadow | 0:7f1c7e1755bc | 713 | // printf(" MagX= %0.2f uT\r\n", MEMS_Mag[0]); |
BlueShadow | 0:7f1c7e1755bc | 714 | // printf(" MagY= %0.2f uT\r\n", MEMS_Mag[1]); |
BlueShadow | 0:7f1c7e1755bc | 715 | // printf(" MagZ= %0.2f uT\r\n", MEMS_Mag[2]); |
BlueShadow | 0:7f1c7e1755bc | 716 | |
BlueShadow | 0:7f1c7e1755bc | 717 | } |
BlueShadow | 0:7f1c7e1755bc | 718 | #endif |
BlueShadow | 0:7f1c7e1755bc | 719 | |
BlueShadow | 0:7f1c7e1755bc | 720 | #ifdef KX022 |
BlueShadow | 0:7f1c7e1755bc | 721 | void ReadKX022 () |
BlueShadow | 0:7f1c7e1755bc | 722 | { |
BlueShadow | 0:7f1c7e1755bc | 723 | |
BlueShadow | 0:7f1c7e1755bc | 724 | //Read KX022 Portion from the IC |
BlueShadow | 0:7f1c7e1755bc | 725 | i2c.write(KX022_addr_w, &KX022_Addr_Accel_ReadData, 1, RepStart); |
BlueShadow | 0:7f1c7e1755bc | 726 | i2c.read(KX022_addr_r, &KX022_Content_ReadData[0], 6, NoRepStart); |
BlueShadow | 0:7f1c7e1755bc | 727 | |
BlueShadow | 0:7f1c7e1755bc | 728 | //Format Data |
BlueShadow | 0:7f1c7e1755bc | 729 | KX022_Accel_X_RawOUT = (KX022_Content_ReadData[1]<<8) | (KX022_Content_ReadData[0]); |
BlueShadow | 0:7f1c7e1755bc | 730 | KX022_Accel_Y_RawOUT = (KX022_Content_ReadData[3]<<8) | (KX022_Content_ReadData[2]); |
BlueShadow | 0:7f1c7e1755bc | 731 | KX022_Accel_Z_RawOUT = (KX022_Content_ReadData[5]<<8) | (KX022_Content_ReadData[4]); |
BlueShadow | 0:7f1c7e1755bc | 732 | |
BlueShadow | 0:7f1c7e1755bc | 733 | //Scale Data |
BlueShadow | 0:7f1c7e1755bc | 734 | KX022_Accel[0] = (float)KX022_Accel_X_RawOUT / 16384; |
BlueShadow | 0:7f1c7e1755bc | 735 | KX022_Accel[1] = (float)KX022_Accel_Y_RawOUT / 16384; |
BlueShadow | 0:7f1c7e1755bc | 736 | KX022_Accel[2] = (float)KX022_Accel_Z_RawOUT / 16384; |
BlueShadow | 0:7f1c7e1755bc | 737 | |
BlueShadow | 0:7f1c7e1755bc | 738 | //Return Data through UART |
BlueShadow | 0:7f1c7e1755bc | 739 | // printf("KX022 Accelerometer Sensor Data: \r\n"); |
BlueShadow | 0:7f1c7e1755bc | 740 | // printf(" AccX= %0.2f g\r\n", KX022_Accel[0]); |
BlueShadow | 0:7f1c7e1755bc | 741 | // printf(" AccY= %0.2f g\r\n", KX022_Accel[1]); |
BlueShadow | 0:7f1c7e1755bc | 742 | // printf(" AccZ= %0.2f g\r\n", KX022_Accel[2]); |
BlueShadow | 0:7f1c7e1755bc | 743 | |
BlueShadow | 0:7f1c7e1755bc | 744 | } |
BlueShadow | 0:7f1c7e1755bc | 745 | #endif |
BlueShadow | 0:7f1c7e1755bc | 746 | |
BlueShadow | 0:7f1c7e1755bc | 747 | |
BlueShadow | 0:7f1c7e1755bc | 748 | #ifdef Pressure |
BlueShadow | 0:7f1c7e1755bc | 749 | void ReadPressure () |
BlueShadow | 0:7f1c7e1755bc | 750 | { |
BlueShadow | 0:7f1c7e1755bc | 751 | |
BlueShadow | 0:7f1c7e1755bc | 752 | i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart); |
BlueShadow | 0:7f1c7e1755bc | 753 | i2c.read(Press_addr_r, &Press_Content_ReadData[0], 6, NoRepStart); |
BlueShadow | 0:7f1c7e1755bc | 754 | |
BlueShadow | 0:7f1c7e1755bc | 755 | BM1383_Temp_Out = (Press_Content_ReadData[0]<<8) | (Press_Content_ReadData[1]); |
BlueShadow | 0:7f1c7e1755bc | 756 | BM1383[0] = (float)BM1383_Temp_Out/32; |
BlueShadow | 0:7f1c7e1755bc | 757 | |
BlueShadow | 0:7f1c7e1755bc | 758 | BM1383_Var = (Press_Content_ReadData[2]<<3) | (Press_Content_ReadData[3] >> 5); |
BlueShadow | 0:7f1c7e1755bc | 759 | BM1383_Deci = ((Press_Content_ReadData[3] & 0x1f) << 6 | ((Press_Content_ReadData[4] >> 2))); |
BlueShadow | 0:7f1c7e1755bc | 760 | BM1383_Deci = (float)BM1383_Deci* (float)0.00048828125; //0.00048828125 = 2^-11 |
BlueShadow | 0:7f1c7e1755bc | 761 | BM1383[1] = (BM1383_Var + BM1383_Deci); //question pending here... |
BlueShadow | 0:7f1c7e1755bc | 762 | |
BlueShadow | 0:7f1c7e1755bc | 763 | // printf("BM1383 Pressure Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 764 | // printf(" Temperature= %0.2f C\r\n", BM1383[0]); |
BlueShadow | 0:7f1c7e1755bc | 765 | // printf(" Pressure = %0.2f hPa\r\n", BM1383[1]); |
BlueShadow | 0:7f1c7e1755bc | 766 | |
BlueShadow | 0:7f1c7e1755bc | 767 | } |
BlueShadow | 0:7f1c7e1755bc | 768 | #endif |
BlueShadow | 0:7f1c7e1755bc | 769 | |
BlueShadow | 0:7f1c7e1755bc | 770 | |
BlueShadow | 0:7f1c7e1755bc | 771 | /************************************************************************************ |
BlueShadow | 0:7f1c7e1755bc | 772 | // reference only to remember what the names and fuctions are without finding them above. |
BlueShadow | 0:7f1c7e1755bc | 773 | ************************************************************************************ |
BlueShadow | 0:7f1c7e1755bc | 774 | (" Temp = %.2f C\r\n", BDE0600_output); |
BlueShadow | 0:7f1c7e1755bc | 775 | printf(" UV = %.1f mW/cm2\r\n", ML8511_output); |
BlueShadow | 0:7f1c7e1755bc | 776 | |
BlueShadow | 0:7f1c7e1755bc | 777 | printf("BH1745 COLOR Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 778 | printf(" Red = %d ADC Counts\r\n",BH1745[0]); |
BlueShadow | 0:7f1c7e1755bc | 779 | printf(" Green = %d ADC Counts\r\n",BH1745[1]); |
BlueShadow | 0:7f1c7e1755bc | 780 | printf(" Blue = %d ADC Counts\r\n",BH1745[2]); |
BlueShadow | 0:7f1c7e1755bc | 781 | |
BlueShadow | 0:7f1c7e1755bc | 782 | printf(" ALS = %0.2f lx\r\n", RPR0521_ALS[0]); |
BlueShadow | 0:7f1c7e1755bc | 783 | printf(" PROX= %u ADC Counts\r\n", RPR0521_ALS[1]); //defined as a float but is an unsigned, bad coding on my part. |
BlueShadow | 0:7f1c7e1755bc | 784 | |
BlueShadow | 0:7f1c7e1755bc | 785 | printf("KMX62 Accel+Mag Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 786 | printf(" AccX= %0.2f g\r\n", MEMS_Accel[0]); |
BlueShadow | 0:7f1c7e1755bc | 787 | printf(" AccY= %0.2f g\r\n", MEMS_Accel[1]); |
BlueShadow | 0:7f1c7e1755bc | 788 | printf(" AccZ= %0.2f g\r\n", MEMS_Accel[2]); |
BlueShadow | 0:7f1c7e1755bc | 789 | |
BlueShadow | 0:7f1c7e1755bc | 790 | printf(" MagX= %0.2f uT\r\n", MEMS_Mag[0]); |
BlueShadow | 0:7f1c7e1755bc | 791 | printf(" MagY= %0.2f uT\r\n", MEMS_Mag[1]); |
BlueShadow | 0:7f1c7e1755bc | 792 | printf(" MagZ= %0.2f uT\r\n", MEMS_Mag[2]); |
BlueShadow | 0:7f1c7e1755bc | 793 | |
BlueShadow | 0:7f1c7e1755bc | 794 | printf("KX022 Accelerometer Sensor Data: \r\n"); |
BlueShadow | 0:7f1c7e1755bc | 795 | printf(" AccX= %0.2f g\r\n", KX022_Accel[0]); |
BlueShadow | 0:7f1c7e1755bc | 796 | printf(" AccY= %0.2f g\r\n", KX022_Accel[1]); |
BlueShadow | 0:7f1c7e1755bc | 797 | printf(" AccZ= %0.2f g\r\n", KX022_Accel[2]); |
BlueShadow | 0:7f1c7e1755bc | 798 | |
BlueShadow | 0:7f1c7e1755bc | 799 | printf("BM1383 Pressure Sensor Data:\r\n"); |
BlueShadow | 0:7f1c7e1755bc | 800 | printf(" Temperature= %0.2f C\r\n", BM1383[0]); |
BlueShadow | 0:7f1c7e1755bc | 801 | printf(" Pressure = %0.2f hPa\r\n", BM1383[1]); |
BlueShadow | 0:7f1c7e1755bc | 802 | |
BlueShadow | 0:7f1c7e1755bc | 803 | **********************************************************************************/ |