Plant Monitoring CS

Dependencies:   HTS221

Revision:
59:169324c8e604
Parent:
58:8d4bde75ebb9
Child:
61:700f3f204adc
--- a/main.cpp	Tue Feb 27 14:07:49 2018 +0100
+++ b/main.cpp	Wed Mar 25 15:46:56 2020 +0000
@@ -16,8 +16,15 @@
 
 #include "mbed.h"
 #include "TCPSocket.h"
+#include "HTS221Sensor.h"
+#include "DHT22.h"
+#include "TSL2591.h"
 
 #define WIFI_IDW0XX1    2
+#define UBIDOTS_TOKEN   "BBFF-xp89MM8kmzzbp6eJ074XjDpqz7qryh"
+#define UBIDOTS_DEVICE  "stmwifi"
+
+
 
 #if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH))
 #include "ISM43362Interface.h"
@@ -81,46 +88,104 @@
     TCPSocket socket;
     nsapi_error_t response;
 
-    printf("Sending HTTP request to www.arm.com...\n");
-
-    // Open a socket on the network interface, and create a TCP connection to www.arm.com
-    socket.open(net);
-    response = socket.connect("www.arm.com", 80);
-    if(0 != response) {
-        printf("Error connecting: %d\n", response);
-        socket.close();
-        return;
-    }
-
     // Send a simple http request
-    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
-    nsapi_size_t size = strlen(sbuffer);
-    response = 0;
-    while(size)
-    {
-        response = socket.send(sbuffer+response, size);
-        if (response < 0) {
-            printf("Error sending data: %d\n", response);
+    char sbuffer[256];
+    char message[64];
+    
+    /* Analog ressources */
+    AnalogIn adc_temp(ADC_TEMP);    // Internal Temp Sensor to ADC Channel
+    AnalogIn adc_vbat(ADC_VBAT);    // VBAT / 3 internal to ADC channel 
+    
+    static DevI2C devI2c(PB_11,PB_10);
+    static HTS221Sensor sen_hum_temp(&devI2c);
+    static DHT22 tempSensor(D2);
+    static I2C i2c1(I2C_SDA, I2C_SCL);
+    static TSL2591 sensor1(i2c1, TSL2591_ADDR);
+    static AnalogIn analog_value(A0);
+    
+    //sen_hum_temp.init(NULL);
+    //sen_hum_temp.enable();
+    
+    /* Global variables */
+    float temp;
+    float t;
+    float hum;
+    float h;
+    float lux;
+    float moist_r;
+    float moist_v;
+    
+    while(true){
+        // Open a socket on the network interface, and create a TCP connection
+        socket.open(net);
+        response = socket.connect("things.ubidots.com", 80);
+        if(0 != response) {
+            printf("Error connecting: %d\n", response);
             socket.close();
             return;
-        } else {
-            size -= response;
-            // Check if entire message was sent or not
-            printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
         }
+        printf("Connected to the Server\n");
+        
+        /* Sensor acquisition */
+        // ADC sensors
+        //temp = adc_temp.read() * 100; // Converted in C
+        //batt = adc_vbat.read() * 30000;   // Converted in mV 
+        
+        // HTS221 sensors
+        printf("Temperature and humidity acquisition\n");
+        tempSensor.sample();
+        printf("\n");
+        t = (float) tempSensor.getTemperature();
+        temp = t/10;
+        h = (float) tempSensor.getHumidity();
+        hum = h/10;
+        printf("Luminosity acquisition\n");
+        sensor1.getALS();
+        sensor1.calcLux();
+        lux = (float) sensor1.full;
+        printf("Soil moisture acquisition\n");
+        moist_r = analog_value.read();
+        printf("Soil moisture conversion\n");
+        moist_v = moist_r*100;
+        //sen_hum_temp.get_humidity(&h_val);
+        //sen_hum_temp.get_temperature(&t_val);
+        
+        /* Construct content of HTTP command */
+        sprintf(message, "{\"temperature\": %0.2f, \"humidity\": %0.2f, \"soilmoisture\": %f, \"luminosite\": %0.2f}", temp, hum, moist_v, lux);
+        printf("Content Length = %d\r\n", (int)strlen(message));
+        
+        /* Construct HTTP command to send */
+        sprintf(sbuffer, "POST /api/v1.6/devices/%s/?token=%s HTTP/1.1\r\nHost: things.ubidots.com\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", UBIDOTS_DEVICE, UBIDOTS_TOKEN, (int)strlen(message),message); 
+        printf("HTTP command %s\r\n", sbuffer);
+        wait(2.0);
+                     
+        /* Send http request to Ubidots */      
+        printf("Sending HTTP request to ubidots.com...\n");
+        nsapi_size_t size = strlen(sbuffer);
+        printf("send %d [%.*s]\r\n", size, strstr(sbuffer, "\r\n") - sbuffer, sbuffer);
+        response = 0;
+        while(size)
+        {
+            response = socket.send(sbuffer+response, size);
+            if (response < 0) {
+                printf("Error sending data: %d\n", response);
+                socket.close();
+                return;
+            } else {
+                size -= response;
+                // Check if entire message was sent or not
+                printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
+            }
+        }
+    
+        /* Receive a simple http response and print out the response line */
+        char respBuffer[64];
+        int rcount = socket.recv(respBuffer, sizeof respBuffer);
+        printf("recv %d [%.*s]\r\n", rcount, strstr(respBuffer, "\r\n") - respBuffer, respBuffer);
+        wait(10);
+        // Close the socket to return its memory and bring down the network interface
+        socket.close();
     }
-
-    // Recieve a simple http response and print out the response line
-    char rbuffer[64];
-    response = socket.recv(rbuffer, sizeof rbuffer);
-    if (response < 0) {
-        printf("Error receiving data: %d\n", response);
-    } else {
-        printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
-    }
-
-    // Close the socket to return its memory and bring down the network interface
-    socket.close();
 }
 
 int main()
@@ -129,11 +194,12 @@
 
     printf("WiFi example\n\n");
 
+    /*
     count = scan_demo(&wifi);
     if (count == 0) {
         printf("No WIFI APNs found - can't continue further.\n");
         return -1;
-    }
+    }*/
 
     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
     int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);