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