All sensors sending to server via http

Dependencies:   C12832 CCS811 MMA7660 Sht31 TSL2561 mbed-http vl53l0x_api

Fork of HTTP-Python-Demo by Cambridge Hackathon

Files at this revision

API Documentation at this revision

Comitter:
lwc24
Date:
Sat Nov 25 20:53:09 2017 +0000
Parent:
2:b91140c3c3f6
Child:
4:061755016e24
Commit message:
Working sensor readings sending to server

Changed in this revision

CCS811.lib Show annotated file Show diff for this revision Revisions of this file
MMA7660.lib Show annotated file Show diff for this revision Revisions of this file
Sht31.lib Show annotated file Show diff for this revision Revisions of this file
TSL2561.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
vl53l0x_api.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CCS811.lib	Sat Nov 25 20:53:09 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/andcor02/code/CCS811/#64a96d555ef0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA7660.lib	Sat Nov 25 20:53:09 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/Sissors/code/MMA7660/#36a163511e34
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sht31.lib	Sat Nov 25 20:53:09 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/andcor02/code/Sht31/#c84a60326ecf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TSL2561.lib	Sat Nov 25 20:53:09 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/andcor02/code/TSL2561/#8146a28a20cc
--- a/main.cpp	Fri Nov 17 16:04:17 2017 -0600
+++ b/main.cpp	Sat Nov 25 20:53:09 2017 +0000
@@ -11,20 +11,278 @@
 // permitted to do so under the terms of a subsisting license agreement
 // from ARM Limited or its affiliates.
 //----------------------------------------------------------------------------
+#include <sstream>
+#include <string>
+
 #include "mbed.h"
-#include "C12832.h"
 #include "OdinWiFiInterface.h"
 #include "http_request.h"
 
+#include "C12832.h"
+#include "CCS811.h"
+#include "Sht31.h"
+#include "TSL2561.h"
+#include "MMA7660.h"
+
+#include "vl53l0x_api.h"
+#include "vl53l0x_platform.h"
+#include "vl53l0x_i2c_platform.h"
+ 
+#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);
+Sht31   temp_sensor(PF_0, PF_1);
+CCS811  air_sensor(PF_0, PF_1);
+TSL2561 light_sensor(PF_0, PF_1, TSL2561_ADDR_HIGH);
+MMA7660 accel(PF_0, PF_1);
+
+OdinWiFiInterface wifi;
+
+InterruptIn post_button(PF_2);
+InterruptIn get_put_button(PG_4);
+volatile bool post_clicked = false;
+volatile bool get_clicked = false;
+volatile bool put_clicked = false;
+
+// FUNCTION DEFINITIONS HERE
+void lcd_print(const char* message) {
+    lcd.cls();
+    lcd.locate(0, 3);
+    lcd.printf(message);
+}
 
 
-// FUNCTION DEFINITIONS HERE
+float * read_temp() {
+    float t = temp_sensor.readTemperature();
+    float h = temp_sensor.readHumidity();
+    //char val[32];
+    //sprintf(val, "TEMP: %3.2fC, HUM: %3.2f%%", t, h);
+    //lcd_print(val);
+    static float out[2];
+    out[0] = t;
+    out[1] = h;
+    return out;
+}
+
+uint16_t * read_air() {
+    air_sensor.init();
+    uint16_t eco2, tvoc;
+    air_sensor.readData(&eco2, &tvoc);
+    //char val[32];
+    //sprintf(val, "eCO2: %dppm, TVOC: %dppb", eco2, tvoc);
+    //lcd_print(val);
+    static uint16_t out[2];
+    out[0] = eco2;
+    out[1] = tvoc;
+    return out;
+}
+
+int * read_light() {
+    int vis = light_sensor.getLuminosity(TSL2561_VISIBLE);
+    int infr = light_sensor.getLuminosity(TSL2561_INFRARED);
+    //char val[32];
+    //sprintf(val, "VIS: %d, INFR: %d ", vis, infr);
+    //lcd_print(val);
+    static int out[2];
+    out[0] = vis;
+    out[1] = infr;
+    return out;
+}
+
+
+double * read_accel() {
+    double x = accel.x();
+    double y = accel.y();
+    double z = accel.z();
+    //char val[32];
+    //sprintf(val, "x=%.2f y=%.2f z=%.2f", x, y, z);
+    //lcd_print(val);
+    static double out[3];
+    out[0] = x;
+    out[1] = y;
+    out[2] = z;
+    return out;
+}
+
+
 
+VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
+    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
+    uint8_t NewDatReady=0;
+    uint32_t LoopNb;
+    
+    if (Status == VL53L0X_ERROR_NONE) {
+        LoopNb = 0;
+        do {
+            Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
+            if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
+                break;
+            }
+            LoopNb = LoopNb + 1;
+            VL53L0X_PollingDelay(Dev);
+        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
+ 
+        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
+            Status = VL53L0X_ERROR_TIME_OUT;
+        }
+    }
+ 
+    return Status;
+}
+ 
+VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) {
+    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
+    uint32_t StopCompleted=0;
+    uint32_t LoopNb;
+ 
+    if (Status == VL53L0X_ERROR_NONE) {
+        LoopNb = 0;
+        do {
+            Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted);
+            if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) {
+                break;
+            }
+            LoopNb = LoopNb + 1;
+            VL53L0X_PollingDelay(Dev);
+        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
+ 
+        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
+            Status = VL53L0X_ERROR_TIME_OUT;
+        }
+ 
+    }
+ 
+    return Status;
+}
+
+void send_post() {
+    post_clicked = true;
+}
+ 
+void send_put() {
+    put_clicked = true;
+}
 
 int main() {
 
-    // MAIN CODE HERE
+    // 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;
+    VL53L0X_RangingMeasurementData_t    RangingMeasurementData;
+    VL53L0X_RangingMeasurementData_t   *pRangingMeasurementData    = &RangingMeasurementData;
+    
+    // Initialize Comms laster
+    pMyDevice->I2cDevAddr      = 0x52;
+    pMyDevice->comms_type      =  1;
+    pMyDevice->comms_speed_khz =  400;
+    
+    
+    VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0);
+    VL53L0X_DataInit(&MyDevice); 
+    uint32_t refSpadCount;
+    uint8_t isApertureSpads;
+    uint8_t VhvSettings;
+    uint8_t PhaseCal;
+
+    VL53L0X_StaticInit(pMyDevice); 
+    VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization
+    VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization
+    VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
+    VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF
+    VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF
+    VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF
+    VL53L0X_StartMeasurement(pMyDevice);
 
+    // Setup wifi
+    lcd_print("Connecting...");
+    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
+    if (ret != 0) {
+        lcd_print("Connection error.");
+        return -1;
+    }
+    lcd_print("Successfully connected!");
+    //char ipaddress[] = "http://10.25.1.118:5000";
+    
+    post_button.rise(&send_post);
+    get_put_button.rise(&send_put);
+    while (true) {
+        
+        // Get laser measurement
+           while(var<=4){
+                WaitMeasurementDataReady(pMyDevice);
+                VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
+                measure=pRangingMeasurementData->RangeMilliMeter;
+                sum=sum+measure;
+                VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
+                VL53L0X_PollingDelay(pMyDevice);
+                var++;
+                }
+        ave=sum/var; // This is the measurement value
+        var=1;
+        sum=0; 
+        
+        // Get accelerometer measurement
+        acc = read_accel();
+        
+        light = read_light();
+        
+        //temp = read_temp();
+        temp[0] = 0;
+        temp[1] = 1;
+        
+        //air = read_air();
+        air[0] = 0;
+        air[1] = 0;
+        
+        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 (put_clicked) {
+            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);
+     
+    }
 
 }
--- a/mbed_app.json	Fri Nov 17 16:04:17 2017 -0600
+++ b/mbed_app.json	Sat Nov 25 20:53:09 2017 +0000
@@ -2,11 +2,11 @@
     "config": {
         "wifi-ssid": {
             "help": "WiFi SSID",
-            "value": "\"SSID\""
+            "value": "\"ht\""
         },
         "wifi-password": {
             "help": "WiFi Password",
-            "value": "\"PASSWORD\""
+            "value": "\"mb3dcl0ud\""
         }
     },
     "target_overrides": {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vl53l0x_api.lib	Sat Nov 25 20:53:09 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/andcor02/code/vl53l0x_api/#5f79559961c7