This example shows usage of network-socket API.
The program brings up an underlying network interface and if it's Wifi also scans for access points.
It creates a TCPSocket and performs an HTTP transaction targeting the website in the mbed_app.json
config.
The example can be configured to use a TLSSocket. This works only on devices that support TRNG.
This application is able to use any network interface it finds.
The interface selections is done through weak functions that are overridden by your selected target or any additional component that provides a network interface.
If more than one interface is provided the target configuration target.network-default-interface-type
selects the type provided as the default one. This is usually the Ethernet so building on Ethernet enabled boards,
you do not need any further configuration.
By default the examples uses a TCP socket. To enable TLS edit the mbed_app.json to turn on the use-tls-socket
option:
"use-tls-socket": {
"value": true
}
It might be necessary to configure the mbedtls library with appropriate macros in mbed_app.json file. Some boards (like UBLOX_EVK_ODIN_W2) will work fine without any additional configuration and some of them might require some minimal adjustment. For example K64F requires at least the following macro added:
"K64F": {
"target.macros_add" : ["MBEDTLS_SHA1_C"]
}
See mbedtls configuration guidelines for more details.
Also see the API Documentation TLSSocket.
If you want to use WiFi you need to provide SSID, password and security settings in mbed_app.json
.
If your board doesn't provide WiFi as the default interface because it has multiple interfaces you need to specify that
you want WiFi in mbed_app.json
.
{
"target_overrides": {
"*": {
"target.network-default-interface-type": "WIFI",
}
}
}
For more information about Wi-Fi APIs, please visit the Mbed OS Wi-Fi documentation.
- All Mbed OS boards with build-in Wi-Fi module such as:
- ST DISCO IOT board with integrated ISM43362 WiFi Inventek module.
- ST DISCO_F413ZH board with integrated ISM43362 WiFi Inventek module.
- Boards with external WiFi shields such as:
- NUCLEO-F429ZI with ESP8266-01
- Clone it to your machine. Don't forget to use
--recursive
to clone the submodules:git clone --recursive https://github.com/mbed-ce/mbed-os-example-sockets
- Set up the GNU ARM toolchain (and other programs) on your machine using the toolchain setup guide.
- Set up the CMake project for editing. We have three ways to do this:
- On the command line
- Using the CLion IDE
- Using the VS Code IDE
- If your Mbed target has upload method support, you can build the
flash-mbed-os-example-sockets
target to automatically upload the code to a connected device. Otherwise, build thembed-os-example-sockets
target and manually load the resultant bin or hex file to your target.
(Assuming you are using a wifi interface, otherwise the scanning will be skipped)
Starting socket demo
2 networks available:
Network: Virgin Media secured: Unknown BSSID: 2A:35:D1:ba:c7:41 RSSI: -79 Ch: 6
Network: VM4392164 secured: WPA2 BSSID: 18:35:D1:ba:c7:41 RSSI: -79 Ch: 6
Connecting to the network...
IP address: 192.168.0.27
Netmask: 255.255.255.0
Gateway: 192.168.0.1
Resolve hostname ifconfig.io
ifconfig.io address is 104.24.122.146
sent 52 bytes:
GET / HTTP/1.1
Host: ifconfig.io
Connection: close
received 256 bytes:
HTTP/1.1 200 OK
Demo concluded successfully
The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license. Please see contributing.md for more info.
This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide