Skip to content

Commit 76594df

Browse files
committed
Add network examples
1 parent a48c881 commit 76594df

File tree

13 files changed

+479
-0
lines changed

13 files changed

+479
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# DNS_GetHostbyNameAsync
2+
3+
This example shows how to make asynchronous DNS host name resolution.
4+
5+
**Note:** The current example is limited to the ethernet interface on supported devices.
6+
To use the example with a different interface, you will need to modify main.cpp and
7+
replace the EthernetInterface class with the appropriate interface.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
#include "nsapi_types.h"
7+
#include "EthernetInterface.h"
8+
#include "SocketAddress.h"
9+
#include "Semaphore.h"
10+
11+
rtos::Semaphore callback_semaphore;
12+
SocketAddress address;
13+
nsapi_error_t result;
14+
15+
// Callback for asynchronous host name resolution
16+
void hostbyname_callback(nsapi_error_t res, SocketAddress *addr)
17+
{
18+
// Store result and release semaphore
19+
result = res;
20+
address = *addr;
21+
callback_semaphore.release();
22+
}
23+
24+
int main()
25+
{
26+
// Initialise network interface
27+
EthernetInterface eth;
28+
eth.connect();
29+
30+
// Initiate asynchronous DNS host name resolution
31+
eth.gethostbyname_async("www.mbed.com", hostbyname_callback);
32+
33+
// Wait for callback semaphore
34+
callback_semaphore.acquire();
35+
36+
// Print result
37+
printf("Result %s, Address %s\r\n", result == NSAPI_STATUS_GLOBAL_UP ? "OK" : "FAIL",
38+
result == NSAPI_STATUS_GLOBAL_UP ? address.get_ip_address() : "NONE");
39+
40+
// Disconnect network interface
41+
eth.disconnect();
42+
}

APIs_Network/TCPServer/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# TCPServer
2+
3+
This example shows how to accepts single telnet client connection and send buffer data.
4+
5+
**Note:** The current example is limited to the ethernet interface on supported
6+
devices. To use the example with a different interface, you will need to modify
7+
main.cpp and replace the EthernetInterface class with the appropriate interface.

APIs_Network/TCPServer/main.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
#include "EthernetInterface.h"
7+
#include "TCPServer.h"
8+
#include "TCPSocket.h"
9+
10+
int main()
11+
{
12+
printf("TCP server example\n");
13+
14+
EthernetInterface eth;
15+
eth.connect();
16+
17+
SocketAddress a;
18+
eth.get_ip_address(&a);
19+
a.set_port(23);
20+
21+
printf("The Server IP address is '%s'\n", a.get_ip_address());
22+
23+
TCPSocket srv;
24+
TCPSocket *client_sock;
25+
SocketAddress client_addr;
26+
char *buffer = new char[256];
27+
28+
/* Open the server on ethernet stack */
29+
srv.open(&eth);
30+
31+
/* Accepts a connection on a socket. */
32+
srv.bind(a);
33+
34+
/* Can handle x simultaneous connections */
35+
srv.listen(1);
36+
37+
nsapi_error_t error;
38+
client_sock = srv.accept(&error);
39+
40+
client_sock->getpeername(&a);
41+
42+
printf("Accepted %s:%d\n", a.get_ip_address(),
43+
a.get_port());
44+
strcpy(buffer, "Hello \n\r");
45+
client_sock->send(buffer, strlen(buffer));
46+
client_sock->recv(buffer, 256);
47+
48+
client_sock->close();
49+
srv.close();
50+
delete[] buffer;
51+
}

APIs_Network/TCPSocket/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# TCPSocket Example
2+
3+
This is a quick example of a simple HTTP client program using the
4+
network-socket API that is provided as a part of [mbed-os](github.com/armmbed/mbed-os).
5+
6+
The program brings up an underlying network interface, and uses it to perform an HTTP
7+
transaction over a TCPSocket.
8+
9+
**Note:** The current example is limited to the ethernet interface on supported devices.
10+
To use the example with a different interface, you will need to modify main.cpp and
11+
replace the EthernetInterface class with the appropriate interface.

APIs_Network/TCPSocket/main.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
#include "EthernetInterface.h"
7+
8+
// Network interface
9+
EthernetInterface net;
10+
11+
// Socket demo
12+
int main()
13+
{
14+
// Bring up the ethernet interface
15+
printf("Ethernet socket example\n");
16+
net.connect();
17+
18+
// Show the network address
19+
SocketAddress a;
20+
net.get_ip_address(&a);
21+
printf("IP address: %s\n", a.get_ip_address() ? a.get_ip_address() : "None");
22+
23+
// Open a socket on the network interface, and create a TCP connection to mbed.org
24+
TCPSocket socket;
25+
socket.open(&net);
26+
27+
net.gethostbyname("ifconfig.io", &a);
28+
a.set_port(80);
29+
socket.connect(a);
30+
// Send a simple http request
31+
char sbuffer[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\n\r\n";
32+
int scount = socket.send(sbuffer, sizeof sbuffer);
33+
printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n") - sbuffer, sbuffer);
34+
35+
// Recieve a simple http response and print out the response line
36+
char rbuffer[64];
37+
int rcount = socket.recv(rbuffer, sizeof rbuffer);
38+
printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n") - rbuffer, rbuffer);
39+
40+
// Close the socket to return its memory and bring down the network interface
41+
socket.close();
42+
43+
// Bring down the ethernet interface
44+
net.disconnect();
45+
printf("Done\n");
46+
}

APIs_Network/TCPSocketWiFi/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# mbed-os-example-wifi
2+
3+
WiFi example for mbed OS
4+
5+
# Getting started with the WiFi API
6+
7+
This is a quick example of a simple WiFi application using the WiFi and network-socket APIs that is provided as a part of [mbed-os](github.com/armmbed/mbed-os).
8+
9+
The program brings up the WiFi and the underlying network interface, and uses it to scans available networks, connects to a network, prints interface and connection details and performs simple HTTP operation.
10+
11+
# Supported hardware
12+
13+
* [UBLOX Odin board](https://developer.mbed.org/platforms/ublox-EVK-ODIN-W2/) (`UBLOX_EVK_ODIN_W2` target when using mbed CLI)
14+
* Other mbed target with ESP2866 module (Board it's connected to shouldn't have other network interface eg. Ethernet)
15+
16+
ESP2866 is a fallback option and will be used if the build is for unsupported platform.
17+
18+
# Connecting the ESP2866
19+
20+
ESP module needs to be connected to RX and TX UART pins (+ power and ground) on your target board. That can be achieved using Grove shield or connected directly using jumper wires, please note that not all Arduino form factor boards have UART compatible with the Grove shiled.
21+
22+
For Grove shield TX has to be connected to D0 and RX to D1.
23+
24+
Make sure that UART module you're connecting ESP to is different than the debug UART connected to your USB port.
25+
26+
# Getting started
27+
28+
1. Import the example
29+
30+
```
31+
mbed import mbed-os-example-wifi
32+
cd mbed-os-example-wifi
33+
```
34+
2. Configure the WiFi credentials
35+
36+
Edit ```mbed_app.json``` to include correct SSID and Password:
37+
38+
```
39+
"config": {
40+
"wifi-ssid": {
41+
"help": "WiFi SSID",
42+
"value": "\"SSID\""
43+
},
44+
"wifi-password": {
45+
"help": "WiFi Password",
46+
"value": "\"Password\""
47+
}
48+
},
49+
```
50+
51+
3. Compile, generate binary and flash target
52+
53+
For example, for `GCC`:
54+
55+
```
56+
mbed compile -t GCC_ARM -m UBLOX_EVK_ODIN_W2 -f
57+
```

APIs_Network/TCPSocketWiFi/main.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
#include "TCPSocket.h"
7+
8+
#if TARGET_UBLOX_EVK_ODIN_W2
9+
#include "OdinWiFiInterface.h"
10+
OdinWiFiInterface wifi;
11+
#else
12+
#if !TARGET_FF_ARDUINO
13+
#error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
14+
#endif
15+
#include "ESP8266Interface.h"
16+
ESP8266Interface wifi(D1, D0);
17+
#endif
18+
19+
const char *sec2str(nsapi_security_t sec)
20+
{
21+
switch (sec) {
22+
case NSAPI_SECURITY_NONE:
23+
return "None";
24+
case NSAPI_SECURITY_WEP:
25+
return "WEP";
26+
case NSAPI_SECURITY_WPA:
27+
return "WPA";
28+
case NSAPI_SECURITY_WPA2:
29+
return "WPA2";
30+
case NSAPI_SECURITY_WPA_WPA2:
31+
return "WPA/WPA2";
32+
case NSAPI_SECURITY_UNKNOWN:
33+
default:
34+
return "Unknown";
35+
}
36+
}
37+
38+
void scan_demo(WiFiInterface *wifi)
39+
{
40+
WiFiAccessPoint *ap;
41+
42+
printf("Scan:\r\n");
43+
44+
int count = wifi->scan(NULL, 0);
45+
46+
/* Limit number of network arbitrary to 15 */
47+
count = count < 15 ? count : 15;
48+
49+
ap = new WiFiAccessPoint[count];
50+
51+
count = wifi->scan(ap, count);
52+
for (int i = 0; i < count; i++) {
53+
printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
54+
sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
55+
ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
56+
}
57+
printf("%d networks available.\r\n", count);
58+
59+
delete[] ap;
60+
}
61+
62+
void http_demo(NetworkInterface *net)
63+
{
64+
// Open a socket on the network interface, and create a TCP connection to mbed.org
65+
TCPSocket socket;
66+
socket.open(net);
67+
68+
SocketAddress a;
69+
net->gethostbyname("ifconfig.io", &a);
70+
a.set_port(80);
71+
socket.connect(a);
72+
// Send a simple http request
73+
char sbuffer[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\n\r\n";
74+
int scount = socket.send(sbuffer, sizeof sbuffer);
75+
printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n") - sbuffer, sbuffer);
76+
77+
// Recieve a simple http response and print out the response line
78+
char rbuffer[64];
79+
int rcount = socket.recv(rbuffer, sizeof rbuffer);
80+
printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n") - rbuffer, rbuffer);
81+
82+
// Close the socket to return its memory and bring down the network interface
83+
socket.close();
84+
}
85+
86+
int main()
87+
{
88+
SocketAddress a;
89+
90+
printf("WiFi example\r\n\r\n");
91+
92+
scan_demo(&wifi);
93+
94+
printf("\r\nConnecting...\r\n");
95+
int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
96+
if (ret != 0) {
97+
printf("\r\nConnection error\r\n");
98+
return -1;
99+
}
100+
101+
printf("Success\r\n\r\n");
102+
printf("MAC: %s\r\n", wifi.get_mac_address());
103+
wifi.get_ip_address(&a);
104+
printf("IP: %s\r\n", a.get_ip_address());
105+
wifi.get_netmask(&a);
106+
printf("Netmask: %s\r\n", a.get_ip_address());
107+
wifi.get_gateway(&a);
108+
printf("Gateway: %s\r\n", a.get_ip_address());
109+
printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
110+
111+
http_demo(&wifi);
112+
113+
wifi.disconnect();
114+
115+
printf("\r\nDone\r\n");
116+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# TCPSocket Connection State Example
2+
3+
This example shows how to register a network connection state change callback. Possible state changes are listed on the example.
4+
5+
**Note:** The current example is limited to the ethernet interface on supported devices.
6+
To use the example with a different interface, you will need to modify main.cpp and
7+
replace the EthernetInterface class with the appropriate interface.

0 commit comments

Comments
 (0)