All sensors sending to server via http
Dependencies: C12832 CCS811 MMA7660 Sht31 TSL2561 mbed-http vl53l0x_api
Fork of HTTP-Python-Demo by
main.cpp
00001 //---------------------------------------------------------------------------- 00002 // The confidential and proprietary information contained in this file may 00003 // only be used by a person authorised under and to the extent permitted 00004 // by a subsisting licensing agreement from ARM Limited or its affiliates. 00005 // 00006 // (C) COPYRIGHT 2016 ARM Limited or its affiliates. 00007 // ALL RIGHTS RESERVED 00008 // 00009 // This entire notice must be reproduced on all copies of this file 00010 // and copies of this file may only be made by a person if such person is 00011 // permitted to do so under the terms of a subsisting license agreement 00012 // from ARM Limited or its affiliates. 00013 //---------------------------------------------------------------------------- 00014 #include <sstream> 00015 #include <string> 00016 #include <time.h> 00017 00018 #include "mbed.h" 00019 #include "OdinWiFiInterface.h" 00020 #include "http_request.h" 00021 00022 #include "C12832.h" 00023 #include "CCS811.h" 00024 #include "Sht31.h" 00025 #include "TSL2561.h" 00026 #include "MMA7660.h" 00027 00028 #include "vl53l0x_api.h" 00029 #include "vl53l0x_platform.h" 00030 #include "vl53l0x_i2c_platform.h" 00031 00032 #define USE_I2C_2V8 00033 00034 // GLOBAL VARIABLES HERE 00035 C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9); 00036 DigitalOut led(PB_6, 1); 00037 Sht31 temp_sensor(PF_0, PF_1); 00038 CCS811 air_sensor(PF_0, PF_1); 00039 TSL2561 light_sensor(PF_0, PF_1, TSL2561_ADDR_HIGH); 00040 MMA7660 accel(PF_0, PF_1); 00041 00042 OdinWiFiInterface wifi; 00043 00044 InterruptIn post_button(PF_2); 00045 InterruptIn get_put_button(PG_4); 00046 volatile bool post_clicked = false; 00047 volatile bool get_clicked = false; 00048 volatile bool put_clicked = false; 00049 00050 // FUNCTION DEFINITIONS HERE 00051 void lcd_print(const char* message) { 00052 lcd.cls(); 00053 lcd.locate(0, 3); 00054 lcd.printf(message); 00055 } 00056 00057 00058 float * read_temp() { 00059 float t = temp_sensor.readTemperature(); 00060 float h = temp_sensor.readHumidity(); 00061 //char val[32]; 00062 //sprintf(val, "TEMP: %3.2fC, HUM: %3.2f%%", t, h); 00063 //lcd_print(val); 00064 static float out[2]; 00065 out[0] = t; 00066 out[1] = h; 00067 return out; 00068 } 00069 00070 uint16_t * read_air() { 00071 air_sensor.init(); 00072 uint16_t eco2, tvoc; 00073 air_sensor.readData(&eco2, &tvoc); 00074 //char val[32]; 00075 //sprintf(val, "eCO2: %dppm, TVOC: %dppb", eco2, tvoc); 00076 //lcd_print(val); 00077 static uint16_t out[2]; 00078 out[0] = eco2; 00079 out[1] = tvoc; 00080 return out; 00081 } 00082 00083 int * read_light() { 00084 int vis = light_sensor.getLuminosity(TSL2561_VISIBLE); 00085 int infr = light_sensor.getLuminosity(TSL2561_INFRARED); 00086 //char val[32]; 00087 //sprintf(val, "VIS: %d, INFR: %d ", vis, infr); 00088 //lcd_print(val); 00089 static int out[2]; 00090 out[0] = vis; 00091 out[1] = infr; 00092 return out; 00093 } 00094 00095 00096 double * read_accel() { 00097 double x = accel.x(); 00098 double y = accel.y(); 00099 double z = accel.z(); 00100 //char val[32]; 00101 //sprintf(val, "x=%.2f y=%.2f z=%.2f", x, y, z); 00102 //lcd_print(val); 00103 static double out[3]; 00104 out[0] = x; 00105 out[1] = y; 00106 out[2] = z; 00107 return out; 00108 } 00109 00110 00111 00112 VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) { 00113 VL53L0X_Error Status = VL53L0X_ERROR_NONE; 00114 uint8_t NewDatReady=0; 00115 uint32_t LoopNb; 00116 00117 if (Status == VL53L0X_ERROR_NONE) { 00118 LoopNb = 0; 00119 do { 00120 Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady); 00121 if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) { 00122 break; 00123 } 00124 LoopNb = LoopNb + 1; 00125 VL53L0X_PollingDelay(Dev); 00126 } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP); 00127 00128 if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) { 00129 Status = VL53L0X_ERROR_TIME_OUT; 00130 } 00131 } 00132 00133 return Status; 00134 } 00135 00136 VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) { 00137 VL53L0X_Error Status = VL53L0X_ERROR_NONE; 00138 uint32_t StopCompleted=0; 00139 uint32_t LoopNb; 00140 00141 if (Status == VL53L0X_ERROR_NONE) { 00142 LoopNb = 0; 00143 do { 00144 Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted); 00145 if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) { 00146 break; 00147 } 00148 LoopNb = LoopNb + 1; 00149 VL53L0X_PollingDelay(Dev); 00150 } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP); 00151 00152 if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) { 00153 Status = VL53L0X_ERROR_TIME_OUT; 00154 } 00155 00156 } 00157 00158 return Status; 00159 } 00160 00161 void send_post() { 00162 post_clicked = true; 00163 } 00164 00165 void send_put() { 00166 put_clicked = true; 00167 } 00168 00169 int main() { 00170 00171 // Setup Laser 00172 VL53L0X_Dev_t MyDevice; 00173 VL53L0X_Dev_t *pMyDevice = &MyDevice; 00174 VL53L0X_RangingMeasurementData_t RangingMeasurementData; 00175 VL53L0X_RangingMeasurementData_t *pRangingMeasurementData = &RangingMeasurementData; 00176 00177 // Initialize Comms laster 00178 pMyDevice->I2cDevAddr = 0x52; 00179 pMyDevice->comms_type = 1; 00180 pMyDevice->comms_speed_khz = 400; 00181 00182 VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0); 00183 VL53L0X_DataInit(&MyDevice); 00184 uint32_t refSpadCount; 00185 uint8_t isApertureSpads; 00186 uint8_t VhvSettings; 00187 uint8_t PhaseCal; 00188 00189 VL53L0X_StaticInit(pMyDevice); 00190 VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization 00191 VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization 00192 VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode 00193 VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF 00194 VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF 00195 VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF 00196 VL53L0X_StartMeasurement(pMyDevice); 00197 00198 // Setup wifi 00199 lcd_print("Connecting..."); 00200 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00201 if (ret != 0) { 00202 lcd_print("Connection error."); 00203 return -1; 00204 } 00205 lcd_print("Successfully connected!"); 00206 00207 // Initialize reading variables 00208 int var=1, measure=0; 00209 int ave=0, sum=0; 00210 double * acc; 00211 int * light; 00212 uint16_t * air; 00213 float * temp; 00214 //auto time0 = chrono::steady_clock::now(); 00215 00216 int counter = 0; 00217 int buffer_size = 10; 00218 int buffer_time[buffer_size]; 00219 double buffer_acc[buffer_size][3]; 00220 int buffer_dist[buffer_size]; 00221 int buffer_light[buffer_size][2]; 00222 uint16_t buffer_air[buffer_size][2]; 00223 float buffer_temp[buffer_size][2]; 00224 00225 post_button.rise(&send_post); 00226 get_put_button.rise(&send_put); 00227 00228 Timer t; 00229 00230 t.start(); 00231 00232 while (true) { 00233 00234 // Get laser measurement 00235 while(var<=2){ 00236 WaitMeasurementDataReady(pMyDevice); 00237 VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData); 00238 measure=pRangingMeasurementData->RangeMilliMeter; 00239 sum=sum+measure; 00240 VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); 00241 VL53L0X_PollingDelay(pMyDevice); 00242 var++; 00243 } 00244 ave=sum/var; // This is the measurement value 00245 var=1; 00246 sum=0; 00247 00248 // TIMINGS 00249 int now_time = t.read_ms(); 00250 00251 std::ostringstream timestr; 00252 timestr << "Elapsed time: " << now_time/1000 << "\n Press SW1 to send data"; 00253 std::string tstr = timestr.str(); 00254 00255 //std::ostringstream timestr; 00256 //timestr << counter; 00257 //std:string tstr = timestr.str(); 00258 //lcd_print(tstr.c_str()); 00259 00260 //buffer is sent via http when it is full 00261 buffer_time[counter] = now_time; 00262 //buffer_time[counter] = 0; 00263 buffer_dist[counter] = ave; 00264 // Get accelerometer measurement 00265 acc = read_accel(); 00266 buffer_acc[counter][0] = acc[0]; 00267 buffer_acc[counter][1] = acc[1]; 00268 buffer_acc[counter][2] = acc[2]; 00269 light = read_light(); 00270 buffer_light[counter][0] = light[0]; 00271 buffer_light[counter][1] = light[1]; 00272 //temp = read_temp(); 00273 //buffer_temp[counter][0] = temp[0]; 00274 //buffer_temp[counter][0] = temp[1]; 00275 //air = read_air(); 00276 //buffer_air[counter][0] = air[0]; 00277 //buffer_air[counter][0] = air[1]; 00278 00279 counter += 1; 00280 00281 if (counter >= buffer_size) { 00282 counter = 0; 00283 lcd_print(tstr.c_str()); 00284 00285 if (post_clicked) { 00286 lcd_print("Sending Data... \nPress SW2 to stop."); 00287 //post_clicked = false; 00288 NetworkInterface* net = &wifi; 00289 HttpRequest* request = new HttpRequest(net, HTTP_POST, "http://10.25.1.118:5000"); 00290 request->set_header("Content-Type", "application/json"); 00291 std::ostringstream datastr; 00292 00293 datastr << "{ \"time\" : [ " << buffer_time[0]; 00294 for(int i=1; i<buffer_size; i++){ 00295 datastr << ", " << buffer_time[i]; 00296 } 00297 datastr << " ], "; 00298 00299 datastr << " \"dist\" : [ " << buffer_dist[0]; 00300 for(int i=1; i<buffer_size; i++){ 00301 datastr << ", " << buffer_dist[i]; 00302 } 00303 datastr << " ], "; 00304 00305 datastr << " \"acc\" : [ "; 00306 datastr << "{ \"x\" : " << buffer_acc[0][0] << ", \"y\" : " << buffer_acc[0][1] << ", \"z\": " << buffer_acc[0][2] << "}" ; 00307 for(int i=1; i<buffer_size; i++){ 00308 datastr << ", { \"x\" : " << buffer_acc[i][0] << ", \"y\" : " << buffer_acc[i][1] << ", \"z\": " << buffer_acc[i][2] << "}" ; 00309 } 00310 00311 // Light 00312 datastr << "], "; 00313 datastr << " \"visible\" : [ " << buffer_light[0][0]; 00314 for(int i=1; i<buffer_size; i++){ 00315 datastr << ", " << buffer_light[i][0]; 00316 } 00317 datastr << " ], "; 00318 datastr << " \"infared\" : [ " << buffer_light[0][1]; 00319 for(int i=1; i<buffer_size; i++){ 00320 datastr << ", " << buffer_light[i][1]; 00321 } 00322 datastr << " ] "; 00323 00324 /* 00325 // Temperature 00326 datastr << " ], "; 00327 datastr << " \"temp\" : [ "; 00328 datastr << "{ \"temperature\" : " << buffer_temp[0][0] << ", \"humidity\" : " << buffer_temp[0][1] << "}" ; 00329 for(int i=1; i<buffer_size; i++){ 00330 datastr << ", { \"visible\" : " << buffer_temp[i][0] << ", \"infared\" : " << buffer_temp[i][1] << "}" ; 00331 } 00332 00333 // Air 00334 datastr << " ], "; 00335 datastr << " \"air\" : [ "; 00336 datastr << "{ \"eco2\" : " << buffer_air[0][0] << ", \"tvoc\" : " << buffer_air[0][1] << "}" ; 00337 for(int i=1; i<buffer_size; i++){ 00338 datastr << ", { \"eco2\" : " << buffer_air[i][0] << ", \"tvoc\" : " << buffer_air[i][1] << "}" ; 00339 } 00340 */ 00341 00342 datastr << "}"; 00343 00344 std::string dstr = datastr.str(); 00345 HttpResponse* response = request->send(dstr.c_str(), strlen(dstr.c_str())); 00346 00347 //const char body[] = "{ \"key1\" : \"value1\" }"; 00348 //HttpResponse* response = request->send(body, strlen(body)); 00349 //lcd_print(response->get_body_as_string().c_str()); 00350 delete request; 00351 } 00352 } 00353 00354 if (put_clicked) { 00355 lcd_print("Stopped Sending Data..."); 00356 put_clicked = false; 00357 post_clicked = false; 00358 } 00359 00360 //wait_ms(10); 00361 00362 } 00363 00364 }
Generated on Wed Jul 27 2022 19:09:09 by 1.7.2