This is a quick example of a simple HTTP client program using the network-socket API that is provided as a part of mbed-os. The program brings up an underlying network interface, and uses it to perform an HTTP transaction over a TCPSocket.
README.md@56:02a6401ec508, 2018-08-15 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Aug 15 11:15:04 2018 +0100
- Revision:
- 56:02a6401ec508
- Parent:
- 53:0d44278430d3
- Child:
- 61:e61d4d810148
Improve error handling.
* socket.connect() was not causing program to stop, in case of error
* socket.send() was printing "Error! socket.connect()..." in case of error
* rename labels to upper case for readability
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-sockets
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 49:1923a727df5b | 1 | ## Getting started with the network-socket API |
mbed_official | 0:17bd84fc5087 | 2 | |
mbed_official | 0:17bd84fc5087 | 3 | This is a quick example of a simple HTTP client program using the |
mbed_official | 33:d470701728e6 | 4 | [network-socket API](https://os.mbed.com/docs/latest/reference/network-socket.html) that [Mbed OS](https://github.com/ARMmbed/mbed-os) provides. |
mbed_official | 0:17bd84fc5087 | 5 | |
mbed_official | 0:17bd84fc5087 | 6 | The program brings up an underlying network interface, and uses it to perform an HTTP |
mbed_official | 0:17bd84fc5087 | 7 | transaction over a TCPSocket. |
mbed_official | 0:17bd84fc5087 | 8 | |
mbed_official | 49:1923a727df5b | 9 | ### Selecting the network interface |
mbed_official | 49:1923a727df5b | 10 | |
mbed_official | 49:1923a727df5b | 11 | This application is able to use any network inteface it finds. Please see the Mbed OS documentationg for [selecting the default network interface](https://os.mbed.com/docs/v5.9/reference/configuration-connectivity.html#selecting-the-default-network-interface). |
mbed_official | 49:1923a727df5b | 12 | |
mbed_official | 49:1923a727df5b | 13 | For example, building on Ethernet enabled boards, you do not do any configuration. |
mbed_official | 49:1923a727df5b | 14 | |
mbed_official | 53:0d44278430d3 | 15 | Building for WiFi boards, you need to provide SSID, password and security settings in `mbed_app.json` as instructed in the documentation. For example, like this: |
mbed_official | 53:0d44278430d3 | 16 | |
mbed_official | 53:0d44278430d3 | 17 | ``` |
mbed_official | 53:0d44278430d3 | 18 | { |
mbed_official | 53:0d44278430d3 | 19 | "target_overrides": { |
mbed_official | 53:0d44278430d3 | 20 | "*": { |
mbed_official | 53:0d44278430d3 | 21 | "platform.stdio-convert-newlines": true, |
mbed_official | 53:0d44278430d3 | 22 | "target.network-default-interface-type": "WIFI", |
mbed_official | 53:0d44278430d3 | 23 | "nsapi.default-wifi-security": "WPA_WPA2", |
mbed_official | 53:0d44278430d3 | 24 | "nsapi.default-wifi-ssid": "\"ssid\"", |
mbed_official | 53:0d44278430d3 | 25 | "nsapi.default-wifi-password": "\"password\"" |
mbed_official | 53:0d44278430d3 | 26 | } |
mbed_official | 53:0d44278430d3 | 27 | } |
mbed_official | 53:0d44278430d3 | 28 | } |
mbed_official | 53:0d44278430d3 | 29 | ``` |
mbed_official | 49:1923a727df5b | 30 | |
mbed_official | 49:1923a727df5b | 31 | Building for boards that have more that one network interface, you might need to override `target.network-default-interface-type` variable. |
mbed_official | 0:17bd84fc5087 | 32 | |
mbed_official | 40:afef93b6d854 | 33 | ### Building |
mbed_official | 40:afef93b6d854 | 34 | |
mbed_official | 40:afef93b6d854 | 35 | ``` |
mbed_official | 40:afef93b6d854 | 36 | mbed compile -t <toolchain> -m <target> |
mbed_official | 40:afef93b6d854 | 37 | ``` |
mbed_official | 40:afef93b6d854 | 38 | |
mbed_official | 40:afef93b6d854 | 39 | For example, building for K64F using GCC: `mbed compile -t GCC_ARM -m K64F` |
mbed_official | 24:0449435e2ef2 | 40 | |
mbed_official | 13:ed9e4aa00044 | 41 | ### Expected output ### |
mbed_official | 13:ed9e4aa00044 | 42 | |
mbed_official | 40:afef93b6d854 | 43 | **Note:** The default serial port baud rate is 9600 bit/s. |
mbed_official | 40:afef93b6d854 | 44 | |
mbed_official | 13:ed9e4aa00044 | 45 | ``` |
mbed_official | 13:ed9e4aa00044 | 46 | IP address: 10.118.14.45 |
mbed_official | 13:ed9e4aa00044 | 47 | Netmask: 255.255.252.0 |
mbed_official | 13:ed9e4aa00044 | 48 | Gateway: 10.118.12.1 |
mbed_official | 13:ed9e4aa00044 | 49 | sent 39 [GET / HTTP/1.1] |
mbed_official | 13:ed9e4aa00044 | 50 | recv 173 [HTTP/1.1 200 OK] |
mbed_official | 13:ed9e4aa00044 | 51 | External IP address: 217.140.111.135 |
mbed_official | 13:ed9e4aa00044 | 52 | Done |
mbed_official | 13:ed9e4aa00044 | 53 | ``` |
mbed_official | 13:ed9e4aa00044 | 54 | |
mbed_official | 0:17bd84fc5087 | 55 | ### Documentation ### |
mbed_official | 0:17bd84fc5087 | 56 | |
mbed_official | 12:2e7466eba9a3 | 57 | More information on the network-socket API can be found in the [mbed handbook](https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/communication/network_sockets/). |
mbed_official | 33:d470701728e6 | 58 | |
mbed_official | 33:d470701728e6 | 59 | ## Troubleshooting |
mbed_official | 33:d470701728e6 | 60 | |
mbed_official | 33:d470701728e6 | 61 | If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it. |