Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LPS22HB LSM6DSL HTS221 LIS3MDL VL53L0X
main.cpp
00001 /* WiFi Example 00002 * Copyright (c) 2018 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "TCPSocket.h" 00019 #include "ISM43362Interface.h" 00020 00021 #include "HTS221Sensor.h" 00022 #include "LPS22HBSensor.h" 00023 #include "LSM6DSLSensor.h" 00024 #include "lis3mdl_class.h" 00025 #include "VL53L0X.h" 00026 00027 00028 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); 00029 00030 #define IP "184.106.153.149" 00031 char* thingSpeakUrl = "http://api.thingspeak.com/update"; 00032 char* thingSpeakKey = "7A33Z8I8CVDB7GOW"; 00033 00034 /* Retrieve the composing elements of the expansion board */ 00035 /* Interface definition */ 00036 static DevI2C devI2c(PB_11,PB_10); 00037 /* Environmental sensors */ 00038 static LPS22HBSensor press_temp(&devI2c); 00039 static HTS221Sensor hum_temp(&devI2c); 00040 /* Motion sensors */ 00041 static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address 00042 static LIS3MDL magnetometer(&devI2c); 00043 /* Range sensor - B-L475E-IOT01A2 only */ 00044 static DigitalOut shutdown_pin(PC_6); 00045 static VL53L0X range(&devI2c, &shutdown_pin, PC_7); 00046 00047 float value1, value2, value3, value4; 00048 int32_t axes[3]; 00049 00050 const char *sec2str(nsapi_security_t sec) 00051 { 00052 switch (sec) { 00053 case NSAPI_SECURITY_NONE: 00054 return "None"; 00055 case NSAPI_SECURITY_WEP: 00056 return "WEP"; 00057 case NSAPI_SECURITY_WPA: 00058 return "WPA"; 00059 case NSAPI_SECURITY_WPA2: 00060 return "WPA2"; 00061 case NSAPI_SECURITY_WPA_WPA2: 00062 return "WPA/WPA2"; 00063 case NSAPI_SECURITY_UNKNOWN: 00064 default: 00065 return "Unknown"; 00066 } 00067 } 00068 00069 void http_demo(NetworkInterface *net) 00070 { 00071 TCPSocket socket; 00072 nsapi_error_t response; 00073 00074 char sbuffer[256]; 00075 char message[40]; 00076 00077 printf("Sending HTTP Data to thingspeak...\r\n"); 00078 00079 // Open a socket on the network interface, and create a TCP connection to www.arm.com 00080 socket.open(net); 00081 response = socket.connect(IP, 80); 00082 if(0 != response) { 00083 printf("Error connecting: %d\n", response); 00084 socket.close(); 00085 return; 00086 } 00087 printf("%.2f\n",value3); 00088 printf("%.2f\n",value4); 00089 // Send a simple http request 00090 sprintf(message,"field1=%.2f&field2=%.2f",value3,value4); 00091 printf("Message Length=%d\r\n",(int)strlen(message)); 00092 00093 // Send a simple http request 00094 // char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; 00095 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); 00096 nsapi_size_t size = strlen(sbuffer); 00097 response = 0; 00098 while(size) 00099 { 00100 response = socket.send(sbuffer+response, size); 00101 if (response < 0) { 00102 printf("Error sending data: %d\n", response); 00103 socket.close(); 00104 return; 00105 } else { 00106 size -= response; 00107 // Check if entire message was sent or not 00108 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); 00109 } 00110 } 00111 00112 // Recieve a simple http response and print out the response line 00113 /* char rbuffer[64]; 00114 response = socket.recv(rbuffer, sizeof rbuffer); 00115 if (response < 0) { 00116 printf("Error receiving data: %d\n", response); 00117 } else { 00118 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); 00119 } 00120 */ 00121 // Close the socket to return its memory and bring down the network interface 00122 socket.close(); 00123 } 00124 00125 void read_sensor(){ 00126 00127 //value1=value2=0; 00128 hum_temp.get_temperature(&value1); 00129 hum_temp.get_humidity(&value2); 00130 printf("HTS221: [temp] %.2f C, [hum] %.2f%%\r\n", value1, value2); 00131 00132 //value3=value4=0; 00133 press_temp.get_temperature(&value3); 00134 press_temp.get_pressure(&value4); 00135 printf("LPS22HB: [temp] %.2f C, [press] %.2f mbar\r\n", value3, value4); 00136 00137 printf("---\r\n"); 00138 00139 magnetometer.get_m_axes(axes); 00140 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); 00141 00142 acc_gyro.get_x_axes(axes); 00143 printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); 00144 00145 acc_gyro.get_g_axes(axes); 00146 printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); 00147 00148 uint32_t distance; 00149 int status = range.get_distance(&distance); 00150 if (status == VL53L0X_ERROR_NONE) { 00151 printf("VL53L0X [mm]: %6ld\r\n", distance); 00152 } else { 00153 printf("VL53L0X [mm]: --\r\n"); 00154 } 00155 00156 //printf("\033[8A"); 00157 wait(0.5); 00158 } 00159 00160 int main() 00161 { 00162 uint8_t id; 00163 // float value1, value2 value3, value4; 00164 // char buffer1[32], buffer2[32]; 00165 // int32_t axes[3]; 00166 00167 /* Init all sensors with default params */ 00168 press_temp.init(NULL); 00169 magnetometer.init(NULL); 00170 acc_gyro.init(NULL); 00171 00172 range.init_sensor(VL53L0X_DEFAULT_ADDRESS); 00173 00174 /* Enable all sensors */ 00175 hum_temp.enable(); 00176 press_temp.enable(); 00177 acc_gyro.enable_x(); 00178 acc_gyro.enable_g(); 00179 00180 printf("\033[2J\033[20A"); 00181 printf ("\r\n--- Starting new run ---\r\n\r\n"); 00182 00183 hum_temp.read_id(&id); 00184 printf("HTS221 humidity & temperature = 0x%X\r\n", id); 00185 press_temp.read_id(&id); 00186 printf("LPS22HB pressure & temperature = 0x%X\r\n", id); 00187 magnetometer.read_id(&id); 00188 printf("LIS3MDL magnetometer = 0x%X\r\n", id); 00189 acc_gyro.read_id(&id); 00190 printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); 00191 00192 printf("WiFi example\n\n"); 00193 00194 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); 00195 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00196 if (ret != 0) { 00197 printf("\nConnection error\n"); 00198 return -1; 00199 } 00200 00201 printf("Success\n\n"); 00202 printf("MAC: %s\n", wifi.get_mac_address()); 00203 printf("IP: %s\n", wifi.get_ip_address()); 00204 printf("Netmask: %s\n", wifi.get_netmask()); 00205 printf("Gateway: %s\n", wifi.get_gateway()); 00206 printf("RSSI: %d\n\n", wifi.get_rssi()); 00207 00208 for(int i=0; i<5; i++){ 00209 printf("\n\r--- Reading sensor values ---\n\r"); 00210 read_sensor(); 00211 printf("\n\r--- Sending data to thingspeak ---\n\r"); 00212 http_demo(&wifi); 00213 printf("Data no.%d\n",i); 00214 wait(15); 00215 } 00216 wifi.disconnect(); 00217 00218 printf("\nDone\n"); 00219 }
Generated on Fri Jul 15 2022 01:32:39 by
1.7.2