DISCO-STM32L475VG-IOT01A WiFi Sensor Thingspeak

Dependencies:   LPS22HB LSM6DSL HTS221 LIS3MDL VL53L0X

Revision:
59:df40038593a3
Parent:
58:8d4bde75ebb9
diff -r 8d4bde75ebb9 -r df40038593a3 main.cpp
--- a/main.cpp	Tue Feb 27 14:07:49 2018 +0100
+++ b/main.cpp	Wed Jul 22 16:45:13 2020 +0000
@@ -16,21 +16,36 @@
 
 #include "mbed.h"
 #include "TCPSocket.h"
-
-#define WIFI_IDW0XX1    2
+#include "ISM43362Interface.h"
 
-#if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH))
-#include "ISM43362Interface.h"
+#include "HTS221Sensor.h"
+#include "LPS22HBSensor.h"
+#include "LSM6DSLSensor.h"
+#include "lis3mdl_class.h"
+#include "VL53L0X.h"
+
+
 ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI, MBED_CONF_APP_WIFI_SPI_MISO, MBED_CONF_APP_WIFI_SPI_SCLK, MBED_CONF_APP_WIFI_SPI_NSS, MBED_CONF_APP_WIFI_RESET, MBED_CONF_APP_WIFI_DATAREADY, MBED_CONF_APP_WIFI_WAKEUP, false);
 
-#else // External WiFi modules
+#define IP "184.106.153.149" 
+char* thingSpeakUrl = "http://api.thingspeak.com/update";
+char* thingSpeakKey = "7A33Z8I8CVDB7GOW";
 
-#if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
-#include "SpwfSAInterface.h"
-SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
-#endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
+/* Retrieve the composing elements of the expansion board */
+/* Interface definition */
+static DevI2C devI2c(PB_11,PB_10);
+/* Environmental sensors */
+static LPS22HBSensor press_temp(&devI2c);
+static HTS221Sensor hum_temp(&devI2c);
+/* Motion sensors */
+static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address
+static LIS3MDL magnetometer(&devI2c);
+/* Range sensor - B-L475E-IOT01A2 only */
+static DigitalOut shutdown_pin(PC_6);
+static VL53L0X range(&devI2c, &shutdown_pin, PC_7);
 
-#endif
+float value1, value2, value3, value4;
+int32_t axes[3];
 
 const char *sec2str(nsapi_security_t sec)
 {
@@ -51,49 +66,33 @@
     }
 }
 
-int scan_demo(WiFiInterface *wifi)
-{
-    WiFiAccessPoint *ap;
-
-    printf("Scan:\n");
-
-    int count = wifi->scan(NULL,0);
-    printf("%d networks available.\n", count);
-
-    /* Limit number of network arbitrary to 15 */
-    count = count < 15 ? count : 15;
-
-    ap = new WiFiAccessPoint[count];
-    count = wifi->scan(ap, count);
-    for (int i = 0; i < count; i++)
-    {
-        printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
-               sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
-               ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
-    }
-
-    delete[] ap;
-    return count;
-}
-
 void http_demo(NetworkInterface *net)
 {
     TCPSocket socket;
     nsapi_error_t response;
 
-    printf("Sending HTTP request to www.arm.com...\n");
+    char sbuffer[256];
+    char message[40];
+    
+    printf("Sending HTTP Data to thingspeak...\r\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);
+    response = socket.connect(IP, 80);
     if(0 != response) {
         printf("Error connecting: %d\n", response);
         socket.close();
         return;
     }
-
+    printf("%.2f\n",value3);
+    printf("%.2f\n",value4);
     // Send a simple http request
-    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
+    sprintf(message,"field1=%.2f&field2=%.2f",value3,value4);
+    printf("Message Length=%d\r\n",(int)strlen(message));
+    
+    // Send a simple http request
+//    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
+    sprintf(sbuffer,"POST /update HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\nX-THINGSPEAKAPIKEY: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s",thingSpeakKey,(int)strlen(message),message);
     nsapi_size_t size = strlen(sbuffer);
     response = 0;
     while(size)
@@ -111,30 +110,87 @@
     }
 
     // Recieve a simple http response and print out the response line
-    char rbuffer[64];
+/*    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();
 }
 
+void read_sensor(){
+    
+    //value1=value2=0;
+    hum_temp.get_temperature(&value1);
+    hum_temp.get_humidity(&value2);
+    printf("HTS221:  [temp] %.2f C, [hum]   %.2f%%\r\n", value1, value2);
+
+    //value3=value4=0;    
+    press_temp.get_temperature(&value3);
+    press_temp.get_pressure(&value4);
+    printf("LPS22HB: [temp] %.2f C, [press] %.2f mbar\r\n", value3, value4);
+
+    printf("---\r\n");
+
+    magnetometer.get_m_axes(axes);
+    printf("LIS3MDL [mag/mgauss]:    %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
+
+    acc_gyro.get_x_axes(axes);
+    printf("LSM6DSL [acc/mg]:        %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
+
+    acc_gyro.get_g_axes(axes);
+    printf("LSM6DSL [gyro/mdps]:     %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
+
+    uint32_t distance;
+    int status = range.get_distance(&distance);
+    if (status == VL53L0X_ERROR_NONE) {
+        printf("VL53L0X [mm]:            %6ld\r\n", distance);
+    } else {
+        printf("VL53L0X [mm]:                --\r\n");
+    }
+
+    //printf("\033[8A");
+    wait(0.5);
+}
+
 int main()
 {
-    int count = 0;
+    uint8_t id;
+//    float value1, value2 value3, value4;
+  //  char buffer1[32], buffer2[32];
+  //  int32_t axes[3];
+    
+    /* Init all sensors with default params */
+    press_temp.init(NULL);
+    magnetometer.init(NULL);
+    acc_gyro.init(NULL);
+    
+    range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
+    
+    /* Enable all sensors */
+    hum_temp.enable();
+    press_temp.enable();
+    acc_gyro.enable_x();
+    acc_gyro.enable_g();
+    
+    printf("\033[2J\033[20A");
+    printf ("\r\n--- Starting new run ---\r\n\r\n");
+    
+    hum_temp.read_id(&id);
+    printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
+    press_temp.read_id(&id);
+    printf("LPS22HB pressure & temperature    = 0x%X\r\n", id);
+    magnetometer.read_id(&id);
+    printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
+    acc_gyro.read_id(&id);
+    printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
 
     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);
     if (ret != 0) {
@@ -149,8 +205,14 @@
     printf("Gateway: %s\n", wifi.get_gateway());
     printf("RSSI: %d\n\n", wifi.get_rssi());
 
-    http_demo(&wifi);
-
+    for(int i=0; i<5; i++){
+        printf("\n\r--- Reading sensor values ---\n\r");
+        read_sensor();
+        printf("\n\r--- Sending data to thingspeak ---\n\r");
+        http_demo(&wifi);
+        printf("Data no.%d\n",i);
+        wait(15);
+    }
     wifi.disconnect();
 
     printf("\nDone\n");