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.
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 "HTS221Sensor.h" 00020 #include "DHT22.h" 00021 #include "TSL2561.h" 00022 00023 #define WIFI_IDW0XX1 2 00024 #define UBIDOTS_TOKEN "BBFF-xp89MM8kmzzbp6eJ074XjDpqz7qryh" 00025 #define UBIDOTS_DEVICE "stmwifi" 00026 00027 00028 00029 #if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH)) 00030 #include "ISM43362Interface.h" 00031 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); 00032 00033 #else // External WiFi modules 00034 00035 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 00036 #include "SpwfSAInterface.h" 00037 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); 00038 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 00039 00040 #endif 00041 00042 const char *sec2str(nsapi_security_t sec) 00043 { 00044 switch (sec) { 00045 case NSAPI_SECURITY_NONE: 00046 return "None"; 00047 case NSAPI_SECURITY_WEP: 00048 return "WEP"; 00049 case NSAPI_SECURITY_WPA: 00050 return "WPA"; 00051 case NSAPI_SECURITY_WPA2: 00052 return "WPA2"; 00053 case NSAPI_SECURITY_WPA_WPA2: 00054 return "WPA/WPA2"; 00055 case NSAPI_SECURITY_UNKNOWN: 00056 default: 00057 return "Unknown"; 00058 } 00059 } 00060 00061 int scan_demo(WiFiInterface *wifi) 00062 { 00063 WiFiAccessPoint *ap; 00064 00065 printf("Scan:\n"); 00066 00067 int count = wifi->scan(NULL,0); 00068 printf("%d networks available.\n", count); 00069 00070 /* Limit number of network arbitrary to 15 */ 00071 count = count < 15 ? count : 15; 00072 00073 ap = new WiFiAccessPoint[count]; 00074 count = wifi->scan(ap, count); 00075 for (int i = 0; i < count; i++) 00076 { 00077 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), 00078 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], 00079 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); 00080 } 00081 00082 delete[] ap; 00083 return count; 00084 } 00085 00086 void http_demo(NetworkInterface *net) 00087 { 00088 TCPSocket socket; 00089 nsapi_error_t response; 00090 00091 // Send a simple http request 00092 char sbuffer[256]; 00093 char message[64]; 00094 00095 /* Analog ressources */ 00096 AnalogIn adc_temp(ADC_TEMP); // Internal Temp Sensor to ADC Channel 00097 AnalogIn adc_vbat(ADC_VBAT); // VBAT / 3 internal to ADC channel 00098 00099 // static DevI2C devI2c(PB_11,PB_10); 00100 // static DevI2C devI2c(D14,D15); 00101 //static HTS221Sensor sen_hum_temp(&devI2c); 00102 static DHT22 tempSensor(D2); // Vérifier que le capteur DHT22 est relié au PIN D2 00103 static TSL2561 LUM(PB_11, PB_10); // Vérifier que le capteur TSL2591 est bien relié aux ports SDA et SCL 00104 static AnalogIn analog_value(A0); // Vérifier que le capteur d'humidité du sol est bien relié au port analogique A0 00105 00106 //sen_hum_temp.init(NULL); 00107 //sen_hum_temp.enable(); 00108 00109 /* Global variables */ 00110 float temp; 00111 float t; 00112 float hum; 00113 float h; 00114 float lux; 00115 float moist_r; 00116 float moist_v; 00117 00118 while(true){ 00119 // Open a socket on the network interface, and create a TCP connection 00120 socket.open(net); 00121 response = socket.connect("things.ubidots.com", 80); 00122 if(0 != response) { 00123 printf("Error connecting: %d\n", response); 00124 socket.close(); 00125 return; 00126 } 00127 printf("Connected to the Server\n"); 00128 00129 /* Sensor acquisition */ 00130 // ADC sensors 00131 printf("Temperature and humidity acquisition\n"); 00132 tempSensor.sample(); 00133 printf("\n"); 00134 t = (float) tempSensor.getTemperature(); 00135 temp = t/10; 00136 h = (float) tempSensor.getHumidity(); 00137 hum = h/10; 00138 printf("Luminosity acquisition\n"); 00139 lux = (float) LUM.lux(); 00140 printf("Soil moisture acquisition\n"); 00141 moist_r = analog_value.read(); 00142 printf("Soil moisture conversion\n"); 00143 moist_v = moist_r*100; 00144 00145 /* Construct content of HTTP command */ 00146 sprintf(message, "{\"temperature\": %0.2f, \"humidity\": %0.2f, \"soilmoisture\": %f, \"luminosite\": %0.2f}", temp, hum, moist_v, lux); // Vérifier que les API labels sont bien renseignés 00147 printf("Content Length = %d\r\n", (int)strlen(message)); 00148 00149 /* Construct HTTP command to send */ 00150 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); 00151 printf("HTTP command %s\r\n", sbuffer); 00152 wait(2.0); 00153 00154 /* Send http request to Ubidots */ 00155 printf("Sending HTTP request to ubidots.com...\n"); 00156 nsapi_size_t size = strlen(sbuffer); 00157 printf("send %d [%.*s]\r\n", size, strstr(sbuffer, "\r\n") - sbuffer, sbuffer); 00158 response = 0; 00159 while(size) 00160 { 00161 response = socket.send(sbuffer+response, size); 00162 if (response < 0) { 00163 printf("Error sending data: %d\n", response); 00164 socket.close(); 00165 return; 00166 } else { 00167 size -= response; 00168 // Check if entire message was sent or not 00169 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); 00170 } 00171 } 00172 00173 /* Receive a simple http response and print out the response line */ 00174 char respBuffer[64]; 00175 int rcount = socket.recv(respBuffer, sizeof respBuffer); 00176 printf("recv %d [%.*s]\r\n", rcount, strstr(respBuffer, "\r\n") - respBuffer, respBuffer); 00177 wait(10); 00178 // Close the socket to return its memory and bring down the network interface 00179 socket.close(); 00180 } 00181 } 00182 00183 int main() 00184 { 00185 int count = 0; 00186 00187 printf("WiFi example\n\n"); 00188 00189 /* 00190 count = scan_demo(&wifi); 00191 if (count == 0) { 00192 printf("No WIFI APNs found - can't continue further.\n"); 00193 return -1; 00194 }*/ 00195 00196 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); 00197 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00198 if (ret != 0) { 00199 printf("\nConnection error\n"); 00200 return -1; 00201 } 00202 00203 printf("Success\n\n"); 00204 printf("MAC: %s\n", wifi.get_mac_address()); 00205 printf("IP: %s\n", wifi.get_ip_address()); 00206 printf("Netmask: %s\n", wifi.get_netmask()); 00207 printf("Gateway: %s\n", wifi.get_gateway()); 00208 printf("RSSI: %d\n\n", wifi.get_rssi()); 00209 00210 http_demo(&wifi); 00211 00212 wifi.disconnect(); 00213 00214 printf("\nDone\n"); 00215 }
Generated on Sun Aug 7 2022 04:19:43 by
1.7.2