DISCO-STM32L475VG-IOT01A WiFi Sensor Thingspeak

Dependencies:   LPS22HB LSM6DSL HTS221 LIS3MDL VL53L0X

Committer:
mbed_official
Date:
Fri Jul 07 10:00:03 2017 +0100
Revision:
24:323569a565ec
Parent:
21:b2f2f6a840b4
Child:
32:bca3f5f442b3
Made sure entire buffer is transmitted

.
Commit copied from https://github.com/ARMmbed/mbed-os-example-wifi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:857719181846 1 /* WiFi Example
mbed_official 0:857719181846 2 * Copyright (c) 2016 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"
mbed_official 0:857719181846 19
mbed_official 0:857719181846 20 #if TARGET_UBLOX_EVK_ODIN_W2
mbed_official 1:aea78e21a7da 21 #include "OdinWiFiInterface.h"
mbed_official 1:aea78e21a7da 22 OdinWiFiInterface wifi;
mbed_official 10:5b5beb106156 23
mbed_official 21:b2f2f6a840b4 24 #elif TARGET_REALTEK_RTL8195AM
mbed_official 21:b2f2f6a840b4 25 #include "RTWInterface.h"
mbed_official 21:b2f2f6a840b4 26 RTWInterface wifi;
mbed_official 21:b2f2f6a840b4 27
mbed_official 0:857719181846 28 #else
mbed_official 1:aea78e21a7da 29 #if !TARGET_FF_ARDUINO
mbed_official 1:aea78e21a7da 30 #error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
mbed_official 1:aea78e21a7da 31 #endif
mbed_official 21:b2f2f6a840b4 32
mbed_official 1:aea78e21a7da 33 #include "ESP8266Interface.h"
mbed_official 10:5b5beb106156 34 ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
mbed_official 10:5b5beb106156 35
mbed_official 0:857719181846 36 #endif
mbed_official 0:857719181846 37
mbed_official 0:857719181846 38 const char *sec2str(nsapi_security_t sec)
mbed_official 0:857719181846 39 {
mbed_official 0:857719181846 40 switch (sec) {
mbed_official 0:857719181846 41 case NSAPI_SECURITY_NONE:
mbed_official 0:857719181846 42 return "None";
mbed_official 0:857719181846 43 case NSAPI_SECURITY_WEP:
mbed_official 0:857719181846 44 return "WEP";
mbed_official 0:857719181846 45 case NSAPI_SECURITY_WPA:
mbed_official 0:857719181846 46 return "WPA";
mbed_official 0:857719181846 47 case NSAPI_SECURITY_WPA2:
mbed_official 0:857719181846 48 return "WPA2";
mbed_official 0:857719181846 49 case NSAPI_SECURITY_WPA_WPA2:
mbed_official 0:857719181846 50 return "WPA/WPA2";
mbed_official 0:857719181846 51 case NSAPI_SECURITY_UNKNOWN:
mbed_official 0:857719181846 52 default:
mbed_official 0:857719181846 53 return "Unknown";
mbed_official 0:857719181846 54 }
mbed_official 0:857719181846 55 }
mbed_official 0:857719181846 56
mbed_official 0:857719181846 57 void scan_demo(WiFiInterface *wifi)
mbed_official 0:857719181846 58 {
mbed_official 0:857719181846 59 WiFiAccessPoint *ap;
mbed_official 0:857719181846 60
mbed_official 0:857719181846 61 printf("Scan:\r\n");
mbed_official 0:857719181846 62
mbed_official 0:857719181846 63 int count = wifi->scan(NULL,0);
mbed_official 0:857719181846 64
mbed_official 0:857719181846 65 /* Limit number of network arbitrary to 15 */
mbed_official 0:857719181846 66 count = count < 15 ? count : 15;
mbed_official 0:857719181846 67
mbed_official 0:857719181846 68 ap = new WiFiAccessPoint[count];
mbed_official 0:857719181846 69 count = wifi->scan(ap, count);
mbed_official 0:857719181846 70 for (int i = 0; i < count; i++)
mbed_official 0:857719181846 71 {
mbed_official 0:857719181846 72 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
mbed_official 0:857719181846 73 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
mbed_official 0:857719181846 74 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
mbed_official 0:857719181846 75 }
mbed_official 0:857719181846 76 printf("%d networks available.\r\n", count);
mbed_official 0:857719181846 77
mbed_official 0:857719181846 78 delete[] ap;
mbed_official 0:857719181846 79 }
mbed_official 0:857719181846 80
mbed_official 0:857719181846 81 void http_demo(NetworkInterface *net)
mbed_official 0:857719181846 82 {
mbed_official 0:857719181846 83 TCPSocket socket;
mbed_official 24:323569a565ec 84 nsapi_error_t response;
mbed_official 0:857719181846 85
mbed_official 0:857719181846 86 printf("Sending HTTP request to www.arm.com...\r\n");
mbed_official 0:857719181846 87
mbed_official 0:857719181846 88 // Open a socket on the network interface, and create a TCP connection to www.arm.com
mbed_official 0:857719181846 89 socket.open(net);
mbed_official 24:323569a565ec 90 response = socket.connect("www.arm.com", 80);
mbed_official 24:323569a565ec 91 if(0 != response) {
mbed_official 24:323569a565ec 92 printf("Error connecting: %d\r\n", response);
mbed_official 24:323569a565ec 93 socket.close();
mbed_official 24:323569a565ec 94 return;
mbed_official 24:323569a565ec 95 }
mbed_official 0:857719181846 96
mbed_official 0:857719181846 97 // Send a simple http request
mbed_official 0:857719181846 98 char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
mbed_official 24:323569a565ec 99 nsapi_size_t size = sizeof sbuffer;
mbed_official 24:323569a565ec 100 response = 0;
mbed_official 24:323569a565ec 101 while(size)
mbed_official 24:323569a565ec 102 {
mbed_official 24:323569a565ec 103 response = socket.send(sbuffer+response, size);
mbed_official 24:323569a565ec 104 if (response < 0) {
mbed_official 24:323569a565ec 105 printf("Error sending data: %d\r\n", response);
mbed_official 24:323569a565ec 106 socket.close();
mbed_official 24:323569a565ec 107 return;
mbed_official 24:323569a565ec 108 } else {
mbed_official 24:323569a565ec 109 size -= response;
mbed_official 24:323569a565ec 110 // Check if entire message was sent or not
mbed_official 24:323569a565ec 111 printf("sent %d [%.*s]\r\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 24:323569a565ec 112 }
mbed_official 24:323569a565ec 113 }
mbed_official 0:857719181846 114
mbed_official 0:857719181846 115 // Recieve a simple http response and print out the response line
mbed_official 0:857719181846 116 char rbuffer[64];
mbed_official 24:323569a565ec 117 response = socket.recv(rbuffer, sizeof rbuffer);
mbed_official 24:323569a565ec 118 if (response < 0) {
mbed_official 24:323569a565ec 119 printf("Error receiving data: %d\r\n", response);
mbed_official 24:323569a565ec 120 } else {
mbed_official 24:323569a565ec 121 printf("recv %d [%.*s]\r\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
mbed_official 24:323569a565ec 122 }
mbed_official 0:857719181846 123
mbed_official 0:857719181846 124 // Close the socket to return its memory and bring down the network interface
mbed_official 0:857719181846 125 socket.close();
mbed_official 0:857719181846 126 }
mbed_official 0:857719181846 127
mbed_official 0:857719181846 128 int main()
mbed_official 0:857719181846 129 {
mbed_official 0:857719181846 130 printf("WiFi example\r\n\r\n");
mbed_official 0:857719181846 131
mbed_official 0:857719181846 132 scan_demo(&wifi);
mbed_official 0:857719181846 133
mbed_official 0:857719181846 134 printf("\r\nConnecting...\r\n");
mbed_official 0:857719181846 135 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
mbed_official 0:857719181846 136 if (ret != 0) {
mbed_official 0:857719181846 137 printf("\r\nConnection error\r\n");
mbed_official 0:857719181846 138 return -1;
mbed_official 0:857719181846 139 }
mbed_official 0:857719181846 140
mbed_official 0:857719181846 141 printf("Success\r\n\r\n");
mbed_official 0:857719181846 142 printf("MAC: %s\r\n", wifi.get_mac_address());
mbed_official 0:857719181846 143 printf("IP: %s\r\n", wifi.get_ip_address());
mbed_official 0:857719181846 144 printf("Netmask: %s\r\n", wifi.get_netmask());
mbed_official 0:857719181846 145 printf("Gateway: %s\r\n", wifi.get_gateway());
mbed_official 0:857719181846 146 printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
mbed_official 0:857719181846 147
mbed_official 0:857719181846 148 http_demo(&wifi);
mbed_official 0:857719181846 149
mbed_official 0:857719181846 150 wifi.disconnect();
mbed_official 0:857719181846 151
mbed_official 0:857719181846 152 printf("\r\nDone\r\n");
mbed_official 0:857719181846 153 }