All sensors sending to server via http
Dependencies: C12832 CCS811 MMA7660 Sht31 TSL2561 mbed-http vl53l0x_api
Fork of HTTP-Python-Demo by
Revision 4:061755016e24, committed 2017-11-26
- Comitter:
- lwc24
- Date:
- Sun Nov 26 02:23:40 2017 +0000
- Parent:
- 3:409108394e75
- Commit message:
- Working send all data with time
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat Nov 25 20:53:09 2017 +0000 +++ b/main.cpp Sun Nov 26 02:23:40 2017 +0000 @@ -13,6 +13,7 @@ //---------------------------------------------------------------------------- #include <sstream> #include <string> +#include <time.h> #include "mbed.h" #include "OdinWiFiInterface.h" @@ -30,12 +31,6 @@ #define USE_I2C_2V8 -struct vector { - public: - double x, y, z; - vector (double xval, double yval, double zval) : x(xval), y(yval), z(zval) {}; -}; - // GLOBAL VARIABLES HERE C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9); DigitalOut led(PB_6, 1); @@ -173,14 +168,6 @@ int main() { - // Initialize reading variables - int var=1, measure=0; - int ave=0, sum=0; - double * acc; - int * light; - uint16_t * air; - float * temp; - // Setup Laser VL53L0X_Dev_t MyDevice; VL53L0X_Dev_t *pMyDevice = &MyDevice; @@ -192,7 +179,6 @@ pMyDevice->comms_type = 1; pMyDevice->comms_speed_khz = 400; - VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0); VL53L0X_DataInit(&MyDevice); uint32_t refSpadCount; @@ -217,14 +203,36 @@ return -1; } lcd_print("Successfully connected!"); - //char ipaddress[] = "http://10.25.1.118:5000"; + + // Initialize reading variables + int var=1, measure=0; + int ave=0, sum=0; + double * acc; + int * light; + uint16_t * air; + float * temp; + //auto time0 = chrono::steady_clock::now(); + + int counter = 0; + int buffer_size = 10; + int buffer_time[buffer_size]; + double buffer_acc[buffer_size][3]; + int buffer_dist[buffer_size]; + int buffer_light[buffer_size][2]; + uint16_t buffer_air[buffer_size][2]; + float buffer_temp[buffer_size][2]; post_button.rise(&send_post); get_put_button.rise(&send_put); + + Timer t; + + t.start(); + while (true) { // Get laser measurement - while(var<=4){ + while(var<=2){ WaitMeasurementDataReady(pMyDevice); VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData); measure=pRangingMeasurementData->RangeMilliMeter; @@ -236,49 +244,117 @@ ave=sum/var; // This is the measurement value var=1; sum=0; + + // TIMINGS + int now_time = t.read_ms(); + std::ostringstream timestr; + timestr << "Elapsed time: " << now_time/1000 << "\n Press SW1 to send data"; + std::string tstr = timestr.str(); + + //std::ostringstream timestr; + //timestr << counter; + //std:string tstr = timestr.str(); + //lcd_print(tstr.c_str()); + + //buffer is sent via http when it is full + buffer_time[counter] = now_time; + //buffer_time[counter] = 0; + buffer_dist[counter] = ave; // Get accelerometer measurement acc = read_accel(); - + buffer_acc[counter][0] = acc[0]; + buffer_acc[counter][1] = acc[1]; + buffer_acc[counter][2] = acc[2]; light = read_light(); - + buffer_light[counter][0] = light[0]; + buffer_light[counter][1] = light[1]; //temp = read_temp(); - temp[0] = 0; - temp[1] = 1; + //buffer_temp[counter][0] = temp[0]; + //buffer_temp[counter][0] = temp[1]; + //air = read_air(); + //buffer_air[counter][0] = air[0]; + //buffer_air[counter][0] = air[1]; - //air = read_air(); - air[0] = 0; - air[1] = 0; + counter += 1; + + if (counter >= buffer_size) { + counter = 0; + lcd_print(tstr.c_str()); - if (post_clicked) { - //post_clicked = false; - NetworkInterface* net = &wifi; - HttpRequest* request = new HttpRequest(net, HTTP_POST, "http://10.25.1.118:5000"); - request->set_header("Content-Type", "application/json"); - //const char body[] = "{\"post\":\"request\" , \"message\" : \"hello\" }"; - std::ostringstream datastr; - datastr << "{ \"dist\" : " << ave << - ", \"acceleration\" : { \"x\" : " << acc[0] << ", \"y\" : " << acc[1] << ", \"z\": " << acc[2] << "}" << \ - ", \"co2\" : " << air[0] << ", \"voc\" : " << air[1] << \ - ", \"visible\" : " << light[0] << ", \"infared\" : " << light[1] << \ - ", \"temperature\" : " << temp[0] << ", \"humidity\" : " << temp[1] << " }"; - std::string dstr = datastr.str(); - //const char body[] = dstr.c_str(); - HttpResponse* response = request->send(dstr.c_str(), strlen(dstr.c_str())); - lcd_print(response->get_body_as_string().c_str()); - delete request; + if (post_clicked) { + lcd_print("Sending Data... \nPress SW2 to stop."); + //post_clicked = false; + NetworkInterface* net = &wifi; + HttpRequest* request = new HttpRequest(net, HTTP_POST, "http://10.25.1.118:5000"); + request->set_header("Content-Type", "application/json"); + std::ostringstream datastr; + + datastr << "{ \"time\" : [ " << buffer_time[0]; + for(int i=1; i<buffer_size; i++){ + datastr << ", " << buffer_time[i]; + } + datastr << " ], "; + + datastr << " \"dist\" : [ " << buffer_dist[0]; + for(int i=1; i<buffer_size; i++){ + datastr << ", " << buffer_dist[i]; + } + datastr << " ], "; + + datastr << " \"acc\" : [ "; + datastr << "{ \"x\" : " << buffer_acc[0][0] << ", \"y\" : " << buffer_acc[0][1] << ", \"z\": " << buffer_acc[0][2] << "}" ; + for(int i=1; i<buffer_size; i++){ + datastr << ", { \"x\" : " << buffer_acc[i][0] << ", \"y\" : " << buffer_acc[i][1] << ", \"z\": " << buffer_acc[i][2] << "}" ; + } + + // Light + datastr << "], "; + datastr << " \"visible\" : [ " << buffer_light[0][0]; + for(int i=1; i<buffer_size; i++){ + datastr << ", " << buffer_light[i][0]; + } + datastr << " ], "; + datastr << " \"infared\" : [ " << buffer_light[0][1]; + for(int i=1; i<buffer_size; i++){ + datastr << ", " << buffer_light[i][1]; + } + datastr << " ] "; + + /* + // Temperature + datastr << " ], "; + datastr << " \"temp\" : [ "; + datastr << "{ \"temperature\" : " << buffer_temp[0][0] << ", \"humidity\" : " << buffer_temp[0][1] << "}" ; + for(int i=1; i<buffer_size; i++){ + datastr << ", { \"visible\" : " << buffer_temp[i][0] << ", \"infared\" : " << buffer_temp[i][1] << "}" ; + } + + // Air + datastr << " ], "; + datastr << " \"air\" : [ "; + datastr << "{ \"eco2\" : " << buffer_air[0][0] << ", \"tvoc\" : " << buffer_air[0][1] << "}" ; + for(int i=1; i<buffer_size; i++){ + datastr << ", { \"eco2\" : " << buffer_air[i][0] << ", \"tvoc\" : " << buffer_air[i][1] << "}" ; + } + */ + + datastr << "}"; + + std::string dstr = datastr.str(); + HttpResponse* response = request->send(dstr.c_str(), strlen(dstr.c_str())); + + //const char body[] = "{ \"key1\" : \"value1\" }"; + //HttpResponse* response = request->send(body, strlen(body)); + //lcd_print(response->get_body_as_string().c_str()); + delete request; + } } if (put_clicked) { + lcd_print("Stopped Sending Data..."); put_clicked = false; post_clicked = false; - NetworkInterface* net = &wifi; - HttpRequest* request = new HttpRequest(net, HTTP_PUT, "http://IP_ADDRESS_HERE:8080"); - request->set_header("Content-Type", "application/json"); - const char body[] = "{\"put\":\"request\"}"; - HttpResponse* response = request->send(body, strlen(body)); - lcd_print(response->get_body_as_string().c_str()); - delete request; } //wait_ms(10);