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 Paul Jaeger

Committer:
BlueShadow
Date:
Fri Apr 15 21:31:39 2016 +0000
Revision:
8:e78dcfad254b
Parent:
7:dd550a829ece
Child:
9:6475e1b83491
works from brandon, I changed the variable ALS to float vs an int timer.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:a44e71488e1f 1 /*************************************************************************
BlueShadow 8:e78dcfad254b 2 * Dragonfly Example program for 2016 Sprint Exosite Training
BlueShadow 6:7946b5c2376a 3 *
mfiore 0:a44e71488e1f 4 * The following hardware is required to successfully run this program:
mfiore 0:a44e71488e1f 5 * - MultiTech UDK2 (4" square white PCB with Arduino headers, antenna
mfiore 0:a44e71488e1f 6 * connector, micro USB ports, and 40-pin connector for Dragonfly)
mfiore 0:a44e71488e1f 7 * - MultiTech Dragonfly (1"x2" green PCB with Telit radio)
BlueShadow 8:e78dcfad254b 8 * - Rohm Electronics Sensor Board
BlueShadow 8:e78dcfad254b 9
BlueShadow 8:e78dcfad254b 10 * - Expansion board (LSM6DS0
mfiore 0:a44e71488e1f 11 * 3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis
mfiore 0:a44e71488e1f 12 * magnetometer, HTS221 humidity and temperature sensor and LPS25HB
mfiore 0:a44e71488e1f 13 * pressure sensor)
mfiore 0:a44e71488e1f 14 *
mfiore 0:a44e71488e1f 15 * What this program does:
BlueShadow 8:e78dcfad254b 16 * - reads data from all sensors on board
mfiore 0:a44e71488e1f 17 * - prints all sensor data to debug port on a periodic basis
BlueShadow 8:e78dcfad254b 18 * - optionally send data to Exosite
BlueShadow 8:e78dcfad254b 19 * - All data is sent to a specific location determined by the student login.
BlueShadow 8:e78dcfad254b 20 * - Exosite cloud platform (user must create own account and configure a device
BlueShadow 8:e78dcfad254b 21 * - you need to set the "VENDOR" and "MODEL"
BlueShadow 8:e78dcfad254b 22
mfiore 5:a946ef74a8c4 23 * - you need to set the "do_cloud_post" flag to true for this to
mfiore 5:a946ef74a8c4 24 * work
mfiore 0:a44e71488e1f 25 *
mfiore 0:a44e71488e1f 26 * Setup:
mfiore 0:a44e71488e1f 27 * - Correctly insert SIM card into Dragonfly
mfiore 0:a44e71488e1f 28 * - Seat the Dragonfly on the UDK2 board
mfiore 0:a44e71488e1f 29 * - Connect an antenna to the connector on the Dragonfly labled "M"
mfiore 0:a44e71488e1f 30 * - Stack the Base Shield on the UDK2 Arduino headers
mfiore 0:a44e71488e1f 31 * - Stack the MEMs board on top of the Base Shield
mfiore 0:a44e71488e1f 32 * - Plug in the power cable
mfiore 0:a44e71488e1f 33 * - Plug a micro USB cable into the port below and slightly to the
mfiore 0:a44e71488e1f 34 * left of the Dragonfly (NOT the port on the Dragonfly)
mfiore 0:a44e71488e1f 35 *
mfiore 0:a44e71488e1f 36 * Go have fun and make something cool!
mfiore 0:a44e71488e1f 37 *
mfiore 0:a44e71488e1f 38 ************************************************************************/
BlueShadow 6:7946b5c2376a 39 /*
BlueShadow 6:7946b5c2376a 40 Sample Program Description:
BlueShadow 6:7946b5c2376a 41 This Program will enable to Multi-Tech Dragonfly platform to utilize ROHM's Multi-sensor Shield Board.
BlueShadow 6:7946b5c2376a 42 This program will initialize all sensors on the shield and then read back the sensor data.
BlueShadow 6:7946b5c2376a 43 Data will then be output to the UART Debug Terminal every 1 second.
BlueShadow 6:7946b5c2376a 44
BlueShadow 6:7946b5c2376a 45 Sample Program Author:
BlueShadow 6:7946b5c2376a 46 ROHM USDC
BlueShadow 6:7946b5c2376a 47
BlueShadow 6:7946b5c2376a 48 Additional Resources:
BlueShadow 6:7946b5c2376a 49 ROHM Sensor Shield GitHub Repository: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield
BlueShadow 6:7946b5c2376a 50 */
BlueShadow 6:7946b5c2376a 51
BlueShadow 6:7946b5c2376a 52
mfiore 0:a44e71488e1f 53 #include "mbed.h"
mfiore 0:a44e71488e1f 54 #include "mtsas.h"
mfiore 0:a44e71488e1f 55 #include <string>
BlueShadow 8:e78dcfad254b 56 #include <sstream>
BlueShadow 8:e78dcfad254b 57
BlueShadow 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 8:e78dcfad254b 93
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 8:e78dcfad254b 261 template < typename T > std::string to_string( const T& n )
BlueShadow 8:e78dcfad254b 262 {
BlueShadow 8:e78dcfad254b 263 std::ostringstream stm ;
BlueShadow 8:e78dcfad254b 264 stm << n ;
BlueShadow 8:e78dcfad254b 265 return stm.str() ;
BlueShadow 8:e78dcfad254b 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 8:e78dcfad254b 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 8:e78dcfad254b 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 8:e78dcfad254b 294
BlueShadow 8:e78dcfad254b 295 // IHTTPDataOut object - contains data to be posted to server.
BlueShadow 8:e78dcfad254b 296 HTTPMap http_tx;
BlueShadow 8:e78dcfad254b 297
BlueShadow 8:e78dcfad254b 298
BlueShadow 8:e78dcfad254b 299 //****************************************************************************************************
BlueShadow 8:e78dcfad254b 300 // Initialize I2C Devices ************
BlueShadow 8:e78dcfad254b 301 //****************************************************************************************************/
BlueShadow 6:7946b5c2376a 302
BlueShadow 6:7946b5c2376a 303
BlueShadow 8:e78dcfad254b 304 //****************************************************************************************************/
BlueShadow 8:e78dcfad254b 305 //End I2C Initialization Section **********************************************************
BlueShadow 8:e78dcfad254b 306 //****************************************************************************************************/
mfiore 1:a049d113e250 307
BlueShadow 8:e78dcfad254b 308 Timer post_timer;
BlueShadow 8:e78dcfad254b 309 post_timer.start();
BlueShadow 8:e78dcfad254b 310 logInfo("Setup complete.");
BlueShadow 8:e78dcfad254b 311 logInfo("Waiting for %d ms to trigger connect...", post_interval_ms);
BlueShadow 6:7946b5c2376a 312
BlueShadow 8:e78dcfad254b 313 while (true) {
BlueShadow 8:e78dcfad254b 314 if (post_timer.read_ms() > post_interval_ms && do_cloud_post) {
BlueShadow 8:e78dcfad254b 315 logInfo("bringing up the link");
BlueShadow 8:e78dcfad254b 316 if (radio->connect()) {
BlueShadow 8:e78dcfad254b 317 float sensor_data = RPR0521_ALS_OUT;
BlueShadow 8:e78dcfad254b 318 logDebug("posting sensor data");
BlueShadow 8:e78dcfad254b 319 logDebug("%f",sensor_data);
BlueShadow 6:7946b5c2376a 320
BlueShadow 8:e78dcfad254b 321 http_tx.put("ALS", patch::to_string(sensor_data).c_str());
BlueShadow 8:e78dcfad254b 322
BlueShadow 8:e78dcfad254b 323 // Make HTTP POST request
BlueShadow 8:e78dcfad254b 324 result = http.post(EXOSITE_URL, http_tx, &http_rx);
BlueShadow 8:e78dcfad254b 325 if (result != HTTP_OK) {
BlueShadow 8:e78dcfad254b 326 logError("HTTP POST failed [%d][%s]", result, httpResToStr(result));
BlueShadow 8:e78dcfad254b 327 } else {
BlueShadow 8:e78dcfad254b 328 logInfo("HTTP POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf);
BlueShadow 8:e78dcfad254b 329 }
BlueShadow 6:7946b5c2376a 330
mfiore 4:730b61258422 331 radio->disconnect();
mfiore 4:730b61258422 332 } else {
mfiore 4:730b61258422 333 logError("establishing PPP link failed");
mfiore 4:730b61258422 334 }
BlueShadow 6:7946b5c2376a 335
mfiore 4:730b61258422 336 post_timer.reset();
BlueShadow 8:e78dcfad254b 337 logInfo("Waiting for %d ms to trigger connect...", post_interval_ms);
mfiore 4:730b61258422 338 }
mfiore 0:a44e71488e1f 339 }
mfiore 0:a44e71488e1f 340 }
mfiore 0:a44e71488e1f 341
BlueShadow 8:e78dcfad254b 342
mfiore 1:a049d113e250 343 // init functions
BlueShadow 6:7946b5c2376a 344 bool init_mtsas()
BlueShadow 6:7946b5c2376a 345 {
mfiore 1:a049d113e250 346 io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
mfiore 1:a049d113e250 347 if (! io)
mfiore 1:a049d113e250 348 return false;
BlueShadow 6:7946b5c2376a 349
mfiore 1:a049d113e250 350 io->baud(115200);
mfiore 1:a049d113e250 351 radio = CellularFactory::create(io);
mfiore 1:a049d113e250 352 if (! radio)
mfiore 1:a049d113e250 353 return false;
BlueShadow 6:7946b5c2376a 354
mfiore 1:a049d113e250 355 Code ret = radio->setApn(apn);
mfiore 1:a049d113e250 356 if (ret != MTS_SUCCESS)
mfiore 1:a049d113e250 357 return false;
BlueShadow 6:7946b5c2376a 358
mfiore 5:a946ef74a8c4 359 Transport::setTransport(radio);
BlueShadow 6:7946b5c2376a 360
mfiore 1:a049d113e250 361 return true;
mfiore 1:a049d113e250 362 }
mfiore 1:a049d113e250 363
BlueShadow 8:e78dcfad254b 364 char* httpResToStr(HTTPResult result) {
BlueShadow 8:e78dcfad254b 365 switch(result) {
BlueShadow 8:e78dcfad254b 366 case HTTP_PROCESSING:
BlueShadow 8:e78dcfad254b 367 return "HTTP_PROCESSING";
BlueShadow 8:e78dcfad254b 368 case HTTP_PARSE:
BlueShadow 8:e78dcfad254b 369 return "HTTP_PARSE";
BlueShadow 8:e78dcfad254b 370 case HTTP_DNS:
BlueShadow 8:e78dcfad254b 371 return "HTTP_DNS";
BlueShadow 8:e78dcfad254b 372 case HTTP_PRTCL:
BlueShadow 8:e78dcfad254b 373 return "HTTP_PRTCL";
BlueShadow 8:e78dcfad254b 374 case HTTP_NOTFOUND:
BlueShadow 8:e78dcfad254b 375 return "HTTP_NOTFOUND";
BlueShadow 8:e78dcfad254b 376 case HTTP_REFUSED:
BlueShadow 8:e78dcfad254b 377 return "HTTP_REFUSED";
BlueShadow 8:e78dcfad254b 378 case HTTP_ERROR:
BlueShadow 8:e78dcfad254b 379 return "HTTP_ERROR";
BlueShadow 8:e78dcfad254b 380 case HTTP_TIMEOUT:
BlueShadow 8:e78dcfad254b 381 return "HTTP_TIMEOUT";
BlueShadow 8:e78dcfad254b 382 case HTTP_CONN:
BlueShadow 8:e78dcfad254b 383 return "HTTP_CONN";
BlueShadow 8:e78dcfad254b 384 case HTTP_CLOSED:
BlueShadow 8:e78dcfad254b 385 return "HTTP_CLOSED";
BlueShadow 8:e78dcfad254b 386 case HTTP_REDIRECT:
BlueShadow 8:e78dcfad254b 387 return "HTTP_REDIRECT";
BlueShadow 8:e78dcfad254b 388 case HTTP_OK:
BlueShadow 8:e78dcfad254b 389 return "HTTP_OK";
BlueShadow 8:e78dcfad254b 390 default:
BlueShadow 8:e78dcfad254b 391 return "HTTP Result unknown";
BlueShadow 6:7946b5c2376a 392 }
mfiore 0:a44e71488e1f 393 }
mfiore 0:a44e71488e1f 394