DISCO-STM32L475VG-IOT01A WiFi Sensor Thingspeak

Dependencies:   LPS22HB LSM6DSL HTS221 LIS3MDL VL53L0X

Committer:
teamiotfkekk
Date:
Wed Jul 22 16:45:13 2020 +0000
Revision:
59:df40038593a3
Parent:
58:8d4bde75ebb9
DISCO-STM32L475VG-IOT01A + WiFi + Sensor + Thingspeak

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:857719181846 1 /* WiFi Example
adustm 57:c8c960b825f0 2 * Copyright (c) 2018 ARM Limited
mbed_official 0:857719181846 3 *
mbed_official 0:857719181846 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 0:857719181846 5 * you may not use this file except in compliance with the License.
mbed_official 0:857719181846 6 * You may obtain a copy of the License at
mbed_official 0:857719181846 7 *
mbed_official 0:857719181846 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:857719181846 9 *
mbed_official 0:857719181846 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:857719181846 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 0:857719181846 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:857719181846 13 * See the License for the specific language governing permissions and
mbed_official 0:857719181846 14 * limitations under the License.
mbed_official 0:857719181846 15 */
mbed_official 0:857719181846 16
mbed_official 0:857719181846 17 #include "mbed.h"
mbed_official 0:857719181846 18 #include "TCPSocket.h"
teamiotfkekk 59:df40038593a3 19 #include "ISM43362Interface.h"
mbed_official 32:bca3f5f442b3 20
teamiotfkekk 59:df40038593a3 21 #include "HTS221Sensor.h"
teamiotfkekk 59:df40038593a3 22 #include "LPS22HBSensor.h"
teamiotfkekk 59:df40038593a3 23 #include "LSM6DSLSensor.h"
teamiotfkekk 59:df40038593a3 24 #include "lis3mdl_class.h"
teamiotfkekk 59:df40038593a3 25 #include "VL53L0X.h"
teamiotfkekk 59:df40038593a3 26
teamiotfkekk 59:df40038593a3 27
adustm adustm@gmail.com 55:23a0d1e8270a 28 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);
adustm adustm@gmail.com 55:23a0d1e8270a 29
teamiotfkekk 59:df40038593a3 30 #define IP "184.106.153.149"
teamiotfkekk 59:df40038593a3 31 char* thingSpeakUrl = "http://api.thingspeak.com/update";
teamiotfkekk 59:df40038593a3 32 char* thingSpeakKey = "7A33Z8I8CVDB7GOW";
mbed_official 21:b2f2f6a840b4 33
teamiotfkekk 59:df40038593a3 34 /* Retrieve the composing elements of the expansion board */
teamiotfkekk 59:df40038593a3 35 /* Interface definition */
teamiotfkekk 59:df40038593a3 36 static DevI2C devI2c(PB_11,PB_10);
teamiotfkekk 59:df40038593a3 37 /* Environmental sensors */
teamiotfkekk 59:df40038593a3 38 static LPS22HBSensor press_temp(&devI2c);
teamiotfkekk 59:df40038593a3 39 static HTS221Sensor hum_temp(&devI2c);
teamiotfkekk 59:df40038593a3 40 /* Motion sensors */
teamiotfkekk 59:df40038593a3 41 static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address
teamiotfkekk 59:df40038593a3 42 static LIS3MDL magnetometer(&devI2c);
teamiotfkekk 59:df40038593a3 43 /* Range sensor - B-L475E-IOT01A2 only */
teamiotfkekk 59:df40038593a3 44 static DigitalOut shutdown_pin(PC_6);
teamiotfkekk 59:df40038593a3 45 static VL53L0X range(&devI2c, &shutdown_pin, PC_7);
mbed_official 10:5b5beb106156 46
teamiotfkekk 59:df40038593a3 47 float value1, value2, value3, value4;
teamiotfkekk 59:df40038593a3 48 int32_t axes[3];
mbed_official 0:857719181846 49
mbed_official 0:857719181846 50 const char *sec2str(nsapi_security_t sec)
mbed_official 0:857719181846 51 {
mbed_official 0:857719181846 52 switch (sec) {
mbed_official 0:857719181846 53 case NSAPI_SECURITY_NONE:
mbed_official 0:857719181846 54 return "None";
mbed_official 0:857719181846 55 case NSAPI_SECURITY_WEP:
mbed_official 0:857719181846 56 return "WEP";
mbed_official 0:857719181846 57 case NSAPI_SECURITY_WPA:
mbed_official 0:857719181846 58 return "WPA";
mbed_official 0:857719181846 59 case NSAPI_SECURITY_WPA2:
mbed_official 0:857719181846 60 return "WPA2";
mbed_official 0:857719181846 61 case NSAPI_SECURITY_WPA_WPA2:
mbed_official 0:857719181846 62 return "WPA/WPA2";
mbed_official 0:857719181846 63 case NSAPI_SECURITY_UNKNOWN:
mbed_official 0:857719181846 64 default:
mbed_official 0:857719181846 65 return "Unknown";
mbed_official 0:857719181846 66 }
mbed_official 0:857719181846 67 }
mbed_official 0:857719181846 68
mbed_official 0:857719181846 69 void http_demo(NetworkInterface *net)
mbed_official 0:857719181846 70 {
mbed_official 0:857719181846 71 TCPSocket socket;
mbed_official 24:323569a565ec 72 nsapi_error_t response;
mbed_official 0:857719181846 73
teamiotfkekk 59:df40038593a3 74 char sbuffer[256];
teamiotfkekk 59:df40038593a3 75 char message[40];
teamiotfkekk 59:df40038593a3 76
teamiotfkekk 59:df40038593a3 77 printf("Sending HTTP Data to thingspeak...\r\n");
mbed_official 0:857719181846 78
mbed_official 0:857719181846 79 // Open a socket on the network interface, and create a TCP connection to www.arm.com
mbed_official 0:857719181846 80 socket.open(net);
teamiotfkekk 59:df40038593a3 81 response = socket.connect(IP, 80);
mbed_official 24:323569a565ec 82 if(0 != response) {
mbed_official 35:052c1ba06ce7 83 printf("Error connecting: %d\n", response);
mbed_official 24:323569a565ec 84 socket.close();
mbed_official 24:323569a565ec 85 return;
mbed_official 24:323569a565ec 86 }
teamiotfkekk 59:df40038593a3 87 printf("%.2f\n",value3);
teamiotfkekk 59:df40038593a3 88 printf("%.2f\n",value4);
mbed_official 0:857719181846 89 // Send a simple http request
teamiotfkekk 59:df40038593a3 90 sprintf(message,"field1=%.2f&field2=%.2f",value3,value4);
teamiotfkekk 59:df40038593a3 91 printf("Message Length=%d\r\n",(int)strlen(message));
teamiotfkekk 59:df40038593a3 92
teamiotfkekk 59:df40038593a3 93 // Send a simple http request
teamiotfkekk 59:df40038593a3 94 // char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
teamiotfkekk 59:df40038593a3 95 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);
mbed_official 33:12f0df4d51d7 96 nsapi_size_t size = strlen(sbuffer);
mbed_official 24:323569a565ec 97 response = 0;
mbed_official 24:323569a565ec 98 while(size)
mbed_official 24:323569a565ec 99 {
mbed_official 24:323569a565ec 100 response = socket.send(sbuffer+response, size);
mbed_official 24:323569a565ec 101 if (response < 0) {
mbed_official 35:052c1ba06ce7 102 printf("Error sending data: %d\n", response);
mbed_official 24:323569a565ec 103 socket.close();
mbed_official 24:323569a565ec 104 return;
mbed_official 24:323569a565ec 105 } else {
mbed_official 24:323569a565ec 106 size -= response;
mbed_official 24:323569a565ec 107 // Check if entire message was sent or not
mbed_official 35:052c1ba06ce7 108 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 24:323569a565ec 109 }
mbed_official 24:323569a565ec 110 }
mbed_official 0:857719181846 111
mbed_official 0:857719181846 112 // Recieve a simple http response and print out the response line
teamiotfkekk 59:df40038593a3 113 /* char rbuffer[64];
mbed_official 24:323569a565ec 114 response = socket.recv(rbuffer, sizeof rbuffer);
mbed_official 24:323569a565ec 115 if (response < 0) {
mbed_official 35:052c1ba06ce7 116 printf("Error receiving data: %d\n", response);
mbed_official 24:323569a565ec 117 } else {
mbed_official 35:052c1ba06ce7 118 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
mbed_official 24:323569a565ec 119 }
teamiotfkekk 59:df40038593a3 120 */
mbed_official 0:857719181846 121 // Close the socket to return its memory and bring down the network interface
mbed_official 0:857719181846 122 socket.close();
mbed_official 0:857719181846 123 }
mbed_official 0:857719181846 124
teamiotfkekk 59:df40038593a3 125 void read_sensor(){
teamiotfkekk 59:df40038593a3 126
teamiotfkekk 59:df40038593a3 127 //value1=value2=0;
teamiotfkekk 59:df40038593a3 128 hum_temp.get_temperature(&value1);
teamiotfkekk 59:df40038593a3 129 hum_temp.get_humidity(&value2);
teamiotfkekk 59:df40038593a3 130 printf("HTS221: [temp] %.2f C, [hum] %.2f%%\r\n", value1, value2);
teamiotfkekk 59:df40038593a3 131
teamiotfkekk 59:df40038593a3 132 //value3=value4=0;
teamiotfkekk 59:df40038593a3 133 press_temp.get_temperature(&value3);
teamiotfkekk 59:df40038593a3 134 press_temp.get_pressure(&value4);
teamiotfkekk 59:df40038593a3 135 printf("LPS22HB: [temp] %.2f C, [press] %.2f mbar\r\n", value3, value4);
teamiotfkekk 59:df40038593a3 136
teamiotfkekk 59:df40038593a3 137 printf("---\r\n");
teamiotfkekk 59:df40038593a3 138
teamiotfkekk 59:df40038593a3 139 magnetometer.get_m_axes(axes);
teamiotfkekk 59:df40038593a3 140 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
teamiotfkekk 59:df40038593a3 141
teamiotfkekk 59:df40038593a3 142 acc_gyro.get_x_axes(axes);
teamiotfkekk 59:df40038593a3 143 printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
teamiotfkekk 59:df40038593a3 144
teamiotfkekk 59:df40038593a3 145 acc_gyro.get_g_axes(axes);
teamiotfkekk 59:df40038593a3 146 printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
teamiotfkekk 59:df40038593a3 147
teamiotfkekk 59:df40038593a3 148 uint32_t distance;
teamiotfkekk 59:df40038593a3 149 int status = range.get_distance(&distance);
teamiotfkekk 59:df40038593a3 150 if (status == VL53L0X_ERROR_NONE) {
teamiotfkekk 59:df40038593a3 151 printf("VL53L0X [mm]: %6ld\r\n", distance);
teamiotfkekk 59:df40038593a3 152 } else {
teamiotfkekk 59:df40038593a3 153 printf("VL53L0X [mm]: --\r\n");
teamiotfkekk 59:df40038593a3 154 }
teamiotfkekk 59:df40038593a3 155
teamiotfkekk 59:df40038593a3 156 //printf("\033[8A");
teamiotfkekk 59:df40038593a3 157 wait(0.5);
teamiotfkekk 59:df40038593a3 158 }
teamiotfkekk 59:df40038593a3 159
mbed_official 0:857719181846 160 int main()
mbed_official 0:857719181846 161 {
teamiotfkekk 59:df40038593a3 162 uint8_t id;
teamiotfkekk 59:df40038593a3 163 // float value1, value2 value3, value4;
teamiotfkekk 59:df40038593a3 164 // char buffer1[32], buffer2[32];
teamiotfkekk 59:df40038593a3 165 // int32_t axes[3];
teamiotfkekk 59:df40038593a3 166
teamiotfkekk 59:df40038593a3 167 /* Init all sensors with default params */
teamiotfkekk 59:df40038593a3 168 press_temp.init(NULL);
teamiotfkekk 59:df40038593a3 169 magnetometer.init(NULL);
teamiotfkekk 59:df40038593a3 170 acc_gyro.init(NULL);
teamiotfkekk 59:df40038593a3 171
teamiotfkekk 59:df40038593a3 172 range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
teamiotfkekk 59:df40038593a3 173
teamiotfkekk 59:df40038593a3 174 /* Enable all sensors */
teamiotfkekk 59:df40038593a3 175 hum_temp.enable();
teamiotfkekk 59:df40038593a3 176 press_temp.enable();
teamiotfkekk 59:df40038593a3 177 acc_gyro.enable_x();
teamiotfkekk 59:df40038593a3 178 acc_gyro.enable_g();
teamiotfkekk 59:df40038593a3 179
teamiotfkekk 59:df40038593a3 180 printf("\033[2J\033[20A");
teamiotfkekk 59:df40038593a3 181 printf ("\r\n--- Starting new run ---\r\n\r\n");
teamiotfkekk 59:df40038593a3 182
teamiotfkekk 59:df40038593a3 183 hum_temp.read_id(&id);
teamiotfkekk 59:df40038593a3 184 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
teamiotfkekk 59:df40038593a3 185 press_temp.read_id(&id);
teamiotfkekk 59:df40038593a3 186 printf("LPS22HB pressure & temperature = 0x%X\r\n", id);
teamiotfkekk 59:df40038593a3 187 magnetometer.read_id(&id);
teamiotfkekk 59:df40038593a3 188 printf("LIS3MDL magnetometer = 0x%X\r\n", id);
teamiotfkekk 59:df40038593a3 189 acc_gyro.read_id(&id);
teamiotfkekk 59:df40038593a3 190 printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
mbed_official 37:3a31525e2971 191
mbed_official 35:052c1ba06ce7 192 printf("WiFi example\n\n");
mbed_official 0:857719181846 193
mbed_official 37:3a31525e2971 194 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
mbed_official 0:857719181846 195 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
mbed_official 0:857719181846 196 if (ret != 0) {
mbed_official 35:052c1ba06ce7 197 printf("\nConnection error\n");
mbed_official 0:857719181846 198 return -1;
mbed_official 0:857719181846 199 }
mbed_official 0:857719181846 200
mbed_official 35:052c1ba06ce7 201 printf("Success\n\n");
mbed_official 35:052c1ba06ce7 202 printf("MAC: %s\n", wifi.get_mac_address());
mbed_official 35:052c1ba06ce7 203 printf("IP: %s\n", wifi.get_ip_address());
mbed_official 35:052c1ba06ce7 204 printf("Netmask: %s\n", wifi.get_netmask());
mbed_official 35:052c1ba06ce7 205 printf("Gateway: %s\n", wifi.get_gateway());
mbed_official 35:052c1ba06ce7 206 printf("RSSI: %d\n\n", wifi.get_rssi());
mbed_official 0:857719181846 207
teamiotfkekk 59:df40038593a3 208 for(int i=0; i<5; i++){
teamiotfkekk 59:df40038593a3 209 printf("\n\r--- Reading sensor values ---\n\r");
teamiotfkekk 59:df40038593a3 210 read_sensor();
teamiotfkekk 59:df40038593a3 211 printf("\n\r--- Sending data to thingspeak ---\n\r");
teamiotfkekk 59:df40038593a3 212 http_demo(&wifi);
teamiotfkekk 59:df40038593a3 213 printf("Data no.%d\n",i);
teamiotfkekk 59:df40038593a3 214 wait(15);
teamiotfkekk 59:df40038593a3 215 }
mbed_official 0:857719181846 216 wifi.disconnect();
mbed_official 0:857719181846 217
mbed_official 35:052c1ba06ce7 218 printf("\nDone\n");
mbed_official 0:857719181846 219 }