Axel Hwang / Mbed OS ProjetIOT
Committer:
axelhwang
Date:
Wed Oct 28 07:42:46 2020 +0000
Revision:
0:5738799d7a56
Push

Who changed what in which revision?

UserRevisionLine numberNew contents of line
axelhwang 0:5738799d7a56 1 /* WiFi Example
axelhwang 0:5738799d7a56 2 * Copyright (c) 2018 ARM Limited
axelhwang 0:5738799d7a56 3 *
axelhwang 0:5738799d7a56 4 * Licensed under the Apache License, Version 2.0 (the "License");
axelhwang 0:5738799d7a56 5 * you may not use this file except in compliance with the License.
axelhwang 0:5738799d7a56 6 * You may obtain a copy of the License at
axelhwang 0:5738799d7a56 7 *
axelhwang 0:5738799d7a56 8 * http://www.apache.org/licenses/LICENSE-2.0
axelhwang 0:5738799d7a56 9 *
axelhwang 0:5738799d7a56 10 * Unless required by applicable law or agreed to in writing, software
axelhwang 0:5738799d7a56 11 * distributed under the License is distributed on an "AS IS" BASIS,
axelhwang 0:5738799d7a56 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
axelhwang 0:5738799d7a56 13 * See the License for the specific language governing permissions and
axelhwang 0:5738799d7a56 14 * limitations under the License.
axelhwang 0:5738799d7a56 15 */
axelhwang 0:5738799d7a56 16
axelhwang 0:5738799d7a56 17 #include "mbed.h"
axelhwang 0:5738799d7a56 18 #include "TCPSocket.h"
axelhwang 0:5738799d7a56 19 #include <DigitalIn.h>
axelhwang 0:5738799d7a56 20
axelhwang 0:5738799d7a56 21 #define WIFI_IDW0XX1 2
axelhwang 0:5738799d7a56 22
axelhwang 0:5738799d7a56 23 #define TS_DEVICE "StmWifi" //mettre le nom de votre canal TS
axelhwang 0:5738799d7a56 24 #define thingspeak_APIkey_write "SOSW9VB0YR0D0GF8" //mettre la clé d'écriture de votre canal TS
axelhwang 0:5738799d7a56 25 #define thingspeak_APIkey_read "YEBSWYV532L13T23" //mettre la clé lecture de votre canal TS
axelhwang 0:5738799d7a56 26
axelhwang 0:5738799d7a56 27
axelhwang 0:5738799d7a56 28 #if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH))
axelhwang 0:5738799d7a56 29 #include "ISM43362Interface.h"
axelhwang 0:5738799d7a56 30
axelhwang 0:5738799d7a56 31 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);
axelhwang 0:5738799d7a56 32
axelhwang 0:5738799d7a56 33 #else // External WiFi modules
axelhwang 0:5738799d7a56 34
axelhwang 0:5738799d7a56 35 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
axelhwang 0:5738799d7a56 36 #include "SpwfSAInterface.h"
axelhwang 0:5738799d7a56 37 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
axelhwang 0:5738799d7a56 38 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
axelhwang 0:5738799d7a56 39
axelhwang 0:5738799d7a56 40 #endif
axelhwang 0:5738799d7a56 41 char hostName[] = "8406e1ca8b3e.ngrok.io";
axelhwang 0:5738799d7a56 42 char cardId[] = "carte_9";
axelhwang 0:5738799d7a56 43 char sensorId[256];
axelhwang 0:5738799d7a56 44 DigitalIn sensorIn(D8);
axelhwang 0:5738799d7a56 45
axelhwang 0:5738799d7a56 46 int sensorState = 0;
axelhwang 0:5738799d7a56 47 const char *sec2str(nsapi_security_t sec)
axelhwang 0:5738799d7a56 48 {
axelhwang 0:5738799d7a56 49 switch (sec) {
axelhwang 0:5738799d7a56 50 case NSAPI_SECURITY_NONE:
axelhwang 0:5738799d7a56 51 return "None";
axelhwang 0:5738799d7a56 52 case NSAPI_SECURITY_WEP:
axelhwang 0:5738799d7a56 53 return "WEP";
axelhwang 0:5738799d7a56 54 case NSAPI_SECURITY_WPA:
axelhwang 0:5738799d7a56 55 return "WPA";
axelhwang 0:5738799d7a56 56 case NSAPI_SECURITY_WPA2:
axelhwang 0:5738799d7a56 57 return "WPA2";
axelhwang 0:5738799d7a56 58 case NSAPI_SECURITY_WPA_WPA2:
axelhwang 0:5738799d7a56 59 return "WPA/WPA2";
axelhwang 0:5738799d7a56 60 case NSAPI_SECURITY_UNKNOWN:
axelhwang 0:5738799d7a56 61 default:
axelhwang 0:5738799d7a56 62 return "Unknown";
axelhwang 0:5738799d7a56 63 }
axelhwang 0:5738799d7a56 64 }
axelhwang 0:5738799d7a56 65
axelhwang 0:5738799d7a56 66 int scan_wifi(WiFiInterface *wifi)
axelhwang 0:5738799d7a56 67 {
axelhwang 0:5738799d7a56 68 WiFiAccessPoint *ap;
axelhwang 0:5738799d7a56 69
axelhwang 0:5738799d7a56 70 printf("Scan:\n");
axelhwang 0:5738799d7a56 71
axelhwang 0:5738799d7a56 72 int count = wifi->scan(NULL,0);
axelhwang 0:5738799d7a56 73 printf("%d networks available.\n", count);
axelhwang 0:5738799d7a56 74
axelhwang 0:5738799d7a56 75 /* Limit number of network arbitrary to 15 */
axelhwang 0:5738799d7a56 76 count = count < 15 ? count : 15;
axelhwang 0:5738799d7a56 77
axelhwang 0:5738799d7a56 78 ap = new WiFiAccessPoint[count];
axelhwang 0:5738799d7a56 79 count = wifi->scan(ap, count);
axelhwang 0:5738799d7a56 80 for (int i = 0; i < count; i++)
axelhwang 0:5738799d7a56 81 {
axelhwang 0:5738799d7a56 82 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
axelhwang 0:5738799d7a56 83 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
axelhwang 0:5738799d7a56 84 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
axelhwang 0:5738799d7a56 85 }
axelhwang 0:5738799d7a56 86
axelhwang 0:5738799d7a56 87 delete[] ap;
axelhwang 0:5738799d7a56 88 return count;
axelhwang 0:5738799d7a56 89 }
axelhwang 0:5738799d7a56 90 void http_demo(NetworkInterface *net)
axelhwang 0:5738799d7a56 91 {
axelhwang 0:5738799d7a56 92 TCPSocket socket;
axelhwang 0:5738799d7a56 93 nsapi_error_t response;
axelhwang 0:5738799d7a56 94
axelhwang 0:5738799d7a56 95 printf("Sending HTTP request to ngrok.io...\n");
axelhwang 0:5738799d7a56 96
axelhwang 0:5738799d7a56 97 // Open a socket on the network interface, and create a TCP connection to www.arm.com
axelhwang 0:5738799d7a56 98 socket.open(net);
axelhwang 0:5738799d7a56 99 response = socket.connect(hostName, 80);
axelhwang 0:5738799d7a56 100 if(0 != response) {
axelhwang 0:5738799d7a56 101 printf("Error connecting: %d\n", response);
axelhwang 0:5738799d7a56 102 socket.close();
axelhwang 0:5738799d7a56 103 return;
axelhwang 0:5738799d7a56 104 }
axelhwang 0:5738799d7a56 105
axelhwang 0:5738799d7a56 106 // Send a simple http request
axelhwang 0:5738799d7a56 107 char sbuffer[256];
axelhwang 0:5738799d7a56 108 sprintf(sbuffer, "GET /api/v1/sensor/identify HTTP/1.1\r\nHost: %s\r\n\r\n", hostName);
axelhwang 0:5738799d7a56 109 nsapi_size_t size = strlen(sbuffer);
axelhwang 0:5738799d7a56 110 response = 0;
axelhwang 0:5738799d7a56 111 while(size)
axelhwang 0:5738799d7a56 112 {
axelhwang 0:5738799d7a56 113 response = socket.send(sbuffer+response, size);
axelhwang 0:5738799d7a56 114 if (response < 0) {
axelhwang 0:5738799d7a56 115 printf("Error sending data: %d\n", response);
axelhwang 0:5738799d7a56 116 socket.close();
axelhwang 0:5738799d7a56 117 return;
axelhwang 0:5738799d7a56 118 } else {
axelhwang 0:5738799d7a56 119 size -= response;
axelhwang 0:5738799d7a56 120 // Check if entire message was sent or not
axelhwang 0:5738799d7a56 121 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
axelhwang 0:5738799d7a56 122 }
axelhwang 0:5738799d7a56 123 }
axelhwang 0:5738799d7a56 124
axelhwang 0:5738799d7a56 125 // Recieve a simple http response and print out the response line
axelhwang 0:5738799d7a56 126 char rbuffer[64];
axelhwang 0:5738799d7a56 127 response = socket.recv(rbuffer, sizeof rbuffer);
axelhwang 0:5738799d7a56 128 if (response < 0) {
axelhwang 0:5738799d7a56 129 printf("Error receiving data: %d\n", response);
axelhwang 0:5738799d7a56 130 } else {
axelhwang 0:5738799d7a56 131 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
axelhwang 0:5738799d7a56 132 }
axelhwang 0:5738799d7a56 133
axelhwang 0:5738799d7a56 134 // Close the socket to return its memory and bring down the network interface
axelhwang 0:5738799d7a56 135 socket.close();
axelhwang 0:5738799d7a56 136 }
axelhwang 0:5738799d7a56 137 void authentificate(NetworkInterface *net){
axelhwang 0:5738799d7a56 138 TCPSocket socket;
axelhwang 0:5738799d7a56 139 nsapi_error_t response;
axelhwang 0:5738799d7a56 140
axelhwang 0:5738799d7a56 141 printf("Sending HTTP request to host...\n");
axelhwang 0:5738799d7a56 142
axelhwang 0:5738799d7a56 143 // Open a socket on the network interface, and create a TCP connection to www.arm.com
axelhwang 0:5738799d7a56 144 socket.open(net);
axelhwang 0:5738799d7a56 145 response = socket.connect(hostName, 80);
axelhwang 0:5738799d7a56 146 if(0 != response) {
axelhwang 0:5738799d7a56 147 printf("Error connecting: %d\n", response);
axelhwang 0:5738799d7a56 148 socket.close();
axelhwang 0:5738799d7a56 149 return;
axelhwang 0:5738799d7a56 150 }
axelhwang 0:5738799d7a56 151
axelhwang 0:5738799d7a56 152 // Send a simple http request
axelhwang 0:5738799d7a56 153 char sbuffer[256];
axelhwang 0:5738799d7a56 154 sprintf(sbuffer, "GET /api/v1/sensor/identify?sensorId=%s HTTP/1.1\r\nHost: %s\r\n\r\n", cardId,hostName);
axelhwang 0:5738799d7a56 155 nsapi_size_t size = strlen(sbuffer);
axelhwang 0:5738799d7a56 156 response = 0;
axelhwang 0:5738799d7a56 157 while(size)
axelhwang 0:5738799d7a56 158 {
axelhwang 0:5738799d7a56 159 response = socket.send(sbuffer+response, size);
axelhwang 0:5738799d7a56 160 if (response < 0) {
axelhwang 0:5738799d7a56 161 printf("Error sending data: %d\n", response);
axelhwang 0:5738799d7a56 162 socket.close();
axelhwang 0:5738799d7a56 163 return;
axelhwang 0:5738799d7a56 164 } else {
axelhwang 0:5738799d7a56 165 size -= response;
axelhwang 0:5738799d7a56 166 // Check if entire message was sent or not
axelhwang 0:5738799d7a56 167 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
axelhwang 0:5738799d7a56 168 }
axelhwang 0:5738799d7a56 169 }
axelhwang 0:5738799d7a56 170
axelhwang 0:5738799d7a56 171 // Recieve a simple http response and print out the response line
axelhwang 0:5738799d7a56 172 char rbuffer[64];
axelhwang 0:5738799d7a56 173 response = socket.recv(rbuffer, sizeof rbuffer);
axelhwang 0:5738799d7a56 174 if (response < 0) {
axelhwang 0:5738799d7a56 175 printf("Error receiving data: %d\n", response);
axelhwang 0:5738799d7a56 176 } else {
axelhwang 0:5738799d7a56 177 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
axelhwang 0:5738799d7a56 178 }
axelhwang 0:5738799d7a56 179
axelhwang 0:5738799d7a56 180 // Close the socket to return its memory and bring down the network interface
axelhwang 0:5738799d7a56 181 socket.close();
axelhwang 0:5738799d7a56 182 }
axelhwang 0:5738799d7a56 183 void updateStatus(NetworkInterface *net){
axelhwang 0:5738799d7a56 184 TCPSocket socket;
axelhwang 0:5738799d7a56 185 nsapi_error_t response;
axelhwang 0:5738799d7a56 186
axelhwang 0:5738799d7a56 187 printf("Sending HTTP request to ngrok.io...\n");
axelhwang 0:5738799d7a56 188
axelhwang 0:5738799d7a56 189 // Open a socket on the network interface, and create a TCP connection to www.arm.com
axelhwang 0:5738799d7a56 190 socket.open(net);
axelhwang 0:5738799d7a56 191 response = socket.connect(hostName, 80);
axelhwang 0:5738799d7a56 192 if(0 != response) {
axelhwang 0:5738799d7a56 193 printf("Error connecting: %d\n", response);
axelhwang 0:5738799d7a56 194 socket.close();
axelhwang 0:5738799d7a56 195 return;
axelhwang 0:5738799d7a56 196 }
axelhwang 0:5738799d7a56 197
axelhwang 0:5738799d7a56 198 // Send a simple http request
axelhwang 0:5738799d7a56 199 char sbuffer[256];
axelhwang 0:5738799d7a56 200 if(!sensorState){
axelhwang 0:5738799d7a56 201 sprintf(sbuffer, "GET /api/v1/sensor/update?sensorId=%s&state=1 HTTP/1.1\r\nHost: %s\r\n\r\n", cardId, hostName);
axelhwang 0:5738799d7a56 202 } else {
axelhwang 0:5738799d7a56 203 sprintf(sbuffer, "GET /api/v1/sensor/update?sensorId=%s&state= HTTP/1.1\r\nHost: %s\r\n\r\n", cardId , hostName);
axelhwang 0:5738799d7a56 204
axelhwang 0:5738799d7a56 205 }
axelhwang 0:5738799d7a56 206 // sprintf(sbuffer, "GET /api/v1/sensor/update?sensorId=%s&state=%d HTTP/1.1\r\nHost: %s\r\n\r\n", cardId, sensorState, hostName);
axelhwang 0:5738799d7a56 207 nsapi_size_t size = strlen(sbuffer);
axelhwang 0:5738799d7a56 208 response = 0;
axelhwang 0:5738799d7a56 209 while(size)
axelhwang 0:5738799d7a56 210 {
axelhwang 0:5738799d7a56 211 response = socket.send(sbuffer+response, size);
axelhwang 0:5738799d7a56 212 if (response < 0) {
axelhwang 0:5738799d7a56 213 printf("Error sending data: %d\n", response);
axelhwang 0:5738799d7a56 214 socket.close();
axelhwang 0:5738799d7a56 215 return;
axelhwang 0:5738799d7a56 216 } else {
axelhwang 0:5738799d7a56 217 size -= response;
axelhwang 0:5738799d7a56 218 // Check if entire message was sent or not
axelhwang 0:5738799d7a56 219 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
axelhwang 0:5738799d7a56 220 }
axelhwang 0:5738799d7a56 221 }
axelhwang 0:5738799d7a56 222
axelhwang 0:5738799d7a56 223 // Recieve a simple http response and print out the response line
axelhwang 0:5738799d7a56 224 char rbuffer[64];
axelhwang 0:5738799d7a56 225 response = socket.recv(rbuffer, sizeof rbuffer);
axelhwang 0:5738799d7a56 226 if (response < 0) {
axelhwang 0:5738799d7a56 227 printf("Error receiving data: %d\n", response);
axelhwang 0:5738799d7a56 228 } else {
axelhwang 0:5738799d7a56 229 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
axelhwang 0:5738799d7a56 230 }
axelhwang 0:5738799d7a56 231
axelhwang 0:5738799d7a56 232 // Close the socket to return its memory and bring down the network interface
axelhwang 0:5738799d7a56 233 socket.close();
axelhwang 0:5738799d7a56 234 }
axelhwang 0:5738799d7a56 235
axelhwang 0:5738799d7a56 236 int main()
axelhwang 0:5738799d7a56 237 {
axelhwang 0:5738799d7a56 238 int previousState;
axelhwang 0:5738799d7a56 239 int count = 0;
axelhwang 0:5738799d7a56 240
axelhwang 0:5738799d7a56 241 printf("WiFi connect attempt\n\n");
axelhwang 0:5738799d7a56 242 count = scan_wifi(&wifi);
axelhwang 0:5738799d7a56 243 if (count == 0) {
axelhwang 0:5738799d7a56 244 printf("No WIFI APNs found - can't continue further.\n");
axelhwang 0:5738799d7a56 245 //return -1;
axelhwang 0:5738799d7a56 246 }
axelhwang 0:5738799d7a56 247
axelhwang 0:5738799d7a56 248
axelhwang 0:5738799d7a56 249 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
axelhwang 0:5738799d7a56 250 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
axelhwang 0:5738799d7a56 251 if (ret != 0) {
axelhwang 0:5738799d7a56 252 printf("\nConnection error\n");
axelhwang 0:5738799d7a56 253 return -1;
axelhwang 0:5738799d7a56 254 }
axelhwang 0:5738799d7a56 255 printf("Success\n\n");
axelhwang 0:5738799d7a56 256 printf("MAC: %s\n", wifi.get_mac_address());
axelhwang 0:5738799d7a56 257 printf("IP: %s\n", wifi.get_ip_address());
axelhwang 0:5738799d7a56 258 printf("Netmask: %s\n", wifi.get_netmask());
axelhwang 0:5738799d7a56 259 printf("Gateway: %s\n", wifi.get_gateway());
axelhwang 0:5738799d7a56 260 printf("RSSI: %d\n\n", wifi.get_rssi());
axelhwang 0:5738799d7a56 261
axelhwang 0:5738799d7a56 262 http_demo(&wifi);
axelhwang 0:5738799d7a56 263 authentificate(&wifi);
axelhwang 0:5738799d7a56 264 sensorState = sensorIn.read();
axelhwang 0:5738799d7a56 265 updateStatus(&wifi);
axelhwang 0:5738799d7a56 266 while(true){
axelhwang 0:5738799d7a56 267 previousState = sensorState;
axelhwang 0:5738799d7a56 268 sensorState = sensorIn.read();
axelhwang 0:5738799d7a56 269 if(previousState != sensorState){
axelhwang 0:5738799d7a56 270 printf("Sensor = %d\n",sensorState);
axelhwang 0:5738799d7a56 271 updateStatus(&wifi);
axelhwang 0:5738799d7a56 272 }
axelhwang 0:5738799d7a56 273 // wait(1.0);
axelhwang 0:5738799d7a56 274 //printf("Sensor %s", digitalRead(9));
axelhwang 0:5738799d7a56 275 }
axelhwang 0:5738799d7a56 276 wifi.disconnect();
axelhwang 0:5738799d7a56 277 }