TCP Socket example using WiFi interface

Committer:
sarahmarshy
Date:
Fri Jun 23 16:38:56 2017 -0500
Revision:
1:ed8f6162cecf
Parent:
0:6a4e57edc2b2
"Update mbed-os"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mab5449 0:6a4e57edc2b2 1 /* WiFi Example
mab5449 0:6a4e57edc2b2 2 * Copyright (c) 2016 ARM Limited
mab5449 0:6a4e57edc2b2 3 *
mab5449 0:6a4e57edc2b2 4 * Licensed under the Apache License, Version 2.0 (the "License");
mab5449 0:6a4e57edc2b2 5 * you may not use this file except in compliance with the License.
mab5449 0:6a4e57edc2b2 6 * You may obtain a copy of the License at
mab5449 0:6a4e57edc2b2 7 *
mab5449 0:6a4e57edc2b2 8 * http://www.apache.org/licenses/LICENSE-2.0
mab5449 0:6a4e57edc2b2 9 *
mab5449 0:6a4e57edc2b2 10 * Unless required by applicable law or agreed to in writing, software
mab5449 0:6a4e57edc2b2 11 * distributed under the License is distributed on an "AS IS" BASIS,
mab5449 0:6a4e57edc2b2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mab5449 0:6a4e57edc2b2 13 * See the License for the specific language governing permissions and
mab5449 0:6a4e57edc2b2 14 * limitations under the License.
mab5449 0:6a4e57edc2b2 15 */
mab5449 0:6a4e57edc2b2 16
mab5449 0:6a4e57edc2b2 17 #include "mbed.h"
mab5449 0:6a4e57edc2b2 18 #include "TCPSocket.h"
mab5449 0:6a4e57edc2b2 19
mab5449 0:6a4e57edc2b2 20 #if TARGET_UBLOX_EVK_ODIN_W2
mab5449 0:6a4e57edc2b2 21 #include "OdinWiFiInterface.h"
mab5449 0:6a4e57edc2b2 22 OdinWiFiInterface wifi;
mab5449 0:6a4e57edc2b2 23 #else
mab5449 0:6a4e57edc2b2 24 #if !TARGET_FF_ARDUINO
mab5449 0:6a4e57edc2b2 25 #error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
mab5449 0:6a4e57edc2b2 26 #endif
mab5449 0:6a4e57edc2b2 27 #include "ESP8266Interface.h"
mab5449 0:6a4e57edc2b2 28 ESP8266Interface wifi(D1, D0);
mab5449 0:6a4e57edc2b2 29 #endif
mab5449 0:6a4e57edc2b2 30
mab5449 0:6a4e57edc2b2 31 const char *sec2str(nsapi_security_t sec)
mab5449 0:6a4e57edc2b2 32 {
mab5449 0:6a4e57edc2b2 33 switch (sec) {
mab5449 0:6a4e57edc2b2 34 case NSAPI_SECURITY_NONE:
mab5449 0:6a4e57edc2b2 35 return "None";
mab5449 0:6a4e57edc2b2 36 case NSAPI_SECURITY_WEP:
mab5449 0:6a4e57edc2b2 37 return "WEP";
mab5449 0:6a4e57edc2b2 38 case NSAPI_SECURITY_WPA:
mab5449 0:6a4e57edc2b2 39 return "WPA";
mab5449 0:6a4e57edc2b2 40 case NSAPI_SECURITY_WPA2:
mab5449 0:6a4e57edc2b2 41 return "WPA2";
mab5449 0:6a4e57edc2b2 42 case NSAPI_SECURITY_WPA_WPA2:
mab5449 0:6a4e57edc2b2 43 return "WPA/WPA2";
mab5449 0:6a4e57edc2b2 44 case NSAPI_SECURITY_UNKNOWN:
mab5449 0:6a4e57edc2b2 45 default:
mab5449 0:6a4e57edc2b2 46 return "Unknown";
mab5449 0:6a4e57edc2b2 47 }
mab5449 0:6a4e57edc2b2 48 }
mab5449 0:6a4e57edc2b2 49
mab5449 0:6a4e57edc2b2 50 void scan_demo(WiFiInterface *wifi)
mab5449 0:6a4e57edc2b2 51 {
mab5449 0:6a4e57edc2b2 52 WiFiAccessPoint *ap;
mab5449 0:6a4e57edc2b2 53
mab5449 0:6a4e57edc2b2 54 printf("Scan:\r\n");
mab5449 0:6a4e57edc2b2 55
mab5449 0:6a4e57edc2b2 56 int count = wifi->scan(NULL,0);
mab5449 0:6a4e57edc2b2 57
mab5449 0:6a4e57edc2b2 58 /* Limit number of network arbitrary to 15 */
mab5449 0:6a4e57edc2b2 59 count = count < 15 ? count : 15;
mab5449 0:6a4e57edc2b2 60
mab5449 0:6a4e57edc2b2 61 ap = new WiFiAccessPoint[count];
mab5449 0:6a4e57edc2b2 62 count = wifi->scan(ap, count);
mab5449 0:6a4e57edc2b2 63 for (int i = 0; i < count; i++)
mab5449 0:6a4e57edc2b2 64 {
mab5449 0:6a4e57edc2b2 65 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
mab5449 0:6a4e57edc2b2 66 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
mab5449 0:6a4e57edc2b2 67 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
mab5449 0:6a4e57edc2b2 68 }
mab5449 0:6a4e57edc2b2 69 printf("%d networks available.\r\n", count);
mab5449 0:6a4e57edc2b2 70
mab5449 0:6a4e57edc2b2 71 delete[] ap;
mab5449 0:6a4e57edc2b2 72 }
mab5449 0:6a4e57edc2b2 73
mab5449 0:6a4e57edc2b2 74 void http_demo(NetworkInterface *net)
mab5449 0:6a4e57edc2b2 75 {
mab5449 0:6a4e57edc2b2 76 TCPSocket socket;
mab5449 0:6a4e57edc2b2 77
mab5449 0:6a4e57edc2b2 78 printf("Sending HTTP request to www.arm.com...\r\n");
mab5449 0:6a4e57edc2b2 79
mab5449 0:6a4e57edc2b2 80 // Open a socket on the network interface, and create a TCP connection to www.arm.com
mab5449 0:6a4e57edc2b2 81 socket.open(net);
mab5449 0:6a4e57edc2b2 82 socket.connect("www.arm.com", 80);
mab5449 0:6a4e57edc2b2 83
mab5449 0:6a4e57edc2b2 84 // Send a simple http request
mab5449 0:6a4e57edc2b2 85 char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
mab5449 0:6a4e57edc2b2 86 int scount = socket.send(sbuffer, sizeof sbuffer);
mab5449 0:6a4e57edc2b2 87 printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mab5449 0:6a4e57edc2b2 88
mab5449 0:6a4e57edc2b2 89 // Recieve a simple http response and print out the response line
mab5449 0:6a4e57edc2b2 90 char rbuffer[64];
mab5449 0:6a4e57edc2b2 91 int rcount = socket.recv(rbuffer, sizeof rbuffer);
mab5449 0:6a4e57edc2b2 92 printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
mab5449 0:6a4e57edc2b2 93
mab5449 0:6a4e57edc2b2 94 // Close the socket to return its memory and bring down the network interface
mab5449 0:6a4e57edc2b2 95 socket.close();
mab5449 0:6a4e57edc2b2 96 }
mab5449 0:6a4e57edc2b2 97
mab5449 0:6a4e57edc2b2 98 int main()
mab5449 0:6a4e57edc2b2 99 {
mab5449 0:6a4e57edc2b2 100 printf("WiFi example\r\n\r\n");
mab5449 0:6a4e57edc2b2 101
mab5449 0:6a4e57edc2b2 102 scan_demo(&wifi);
mab5449 0:6a4e57edc2b2 103
mab5449 0:6a4e57edc2b2 104 printf("\r\nConnecting...\r\n");
mab5449 0:6a4e57edc2b2 105 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
mab5449 0:6a4e57edc2b2 106 if (ret != 0) {
mab5449 0:6a4e57edc2b2 107 printf("\r\nConnection error\r\n");
mab5449 0:6a4e57edc2b2 108 return -1;
mab5449 0:6a4e57edc2b2 109 }
mab5449 0:6a4e57edc2b2 110
mab5449 0:6a4e57edc2b2 111 printf("Success\r\n\r\n");
mab5449 0:6a4e57edc2b2 112 printf("MAC: %s\r\n", wifi.get_mac_address());
mab5449 0:6a4e57edc2b2 113 printf("IP: %s\r\n", wifi.get_ip_address());
mab5449 0:6a4e57edc2b2 114 printf("Netmask: %s\r\n", wifi.get_netmask());
mab5449 0:6a4e57edc2b2 115 printf("Gateway: %s\r\n", wifi.get_gateway());
mab5449 0:6a4e57edc2b2 116 printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
mab5449 0:6a4e57edc2b2 117
mab5449 0:6a4e57edc2b2 118 http_demo(&wifi);
mab5449 0:6a4e57edc2b2 119
mab5449 0:6a4e57edc2b2 120 wifi.disconnect();
mab5449 0:6a4e57edc2b2 121
mab5449 0:6a4e57edc2b2 122 printf("\r\nDone\r\n");
mab5449 0:6a4e57edc2b2 123 }