WiFi AP example for Odin-W2
mbed-os-example-odinw2-wifi-ap
Wi-Fi access point example for Mbed OS
Getting started with the Wi-Fi API
This is an example of a Wi-Fi access point application using the odinWiFiInterface and network socket APIs that [Mbed OS](https://github.com/ARMmbed/mbed-os) provides.
The program brings up the Wi-Fi access point and the underlying network interface and brings up a TCP echo server on access point.
Supported hardware
- [u-blox ODIN-W2](https://os.mbed.com/platforms/ublox-EVK-ODIN-W2/)
Getting started
1. Import the example.
mbed import mbed-os-example-odinw2-wifi-ap cd mbed-os-example-odinw2-wifi-ap
2. Configure the Wi-Fi to enable and use access point.
Edit mbed_app.json to include the correct Wi-Fi configurations, SSID and password:
"macros": ["DEVICE_WIFI_AP=1"], "config": { "wifi-ssid": { "help": "AP SSID", "value": "\"odin-w2\"" }, "ap-ip": { "help": "AP IP", "value": "\"ip value\"" }, "ap-netmask": { "help": "Netmask", "value": "\"netmask value\"" }, "ap-gateway": { "help": "Network gateway", "value": "\"gateway value\"" }, "ap-channel": { "help": "AP channel", "value": "\"some channel\"" }, "wifi-password": { "help": "WiFi Password", "value": "\"some pass\"" }
3. Compile and generate binary.
For example, for 'GCC':
mbed compile -t GCC_ARM -m UBLOX_EVK_ODIN_W2 ```
4. Open a serial console session with the target platform using the following parameters:
- Baud rate: 9600
- Data bits: 8
- Stop bits: 1
- Parity: None
5. Copy or drag the application 'mbed-os-example-odinw2-wifi-ap' in the folder 'mbed-os-example-odinw2-wifi-ap/BUILD/<TARGET NAME>/<PLATFORM NAME>' onto the target board.
6. The serial console should display a similar output to below, indicating a successful Wi-Fi connection:
Starting AP
AP started successfully
TCP: connected with 10.0.0.1 server
7. After the server message is displayed, connect a TCP client, upton sending a message to server, message will be echoed to client.
Limitations
1. DHCP mode is not supported.
main.cpp@1:b7bd774d83cb, 2018-09-24 (annotated)
- Committer:
- AmmadRehmatUbx
- Date:
- Mon Sep 24 14:51:51 2018 +0500
- Revision:
- 1:b7bd774d83cb
- Parent:
- 0:ebe81a0cdefc
mbed-os lib added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AmmadRehmatUbx | 0:ebe81a0cdefc | 1 | /* WiFi AP Example |
AmmadRehmatUbx | 0:ebe81a0cdefc | 2 | * Copyright (c) 2018 ARM Limited |
AmmadRehmatUbx | 0:ebe81a0cdefc | 3 | * |
AmmadRehmatUbx | 0:ebe81a0cdefc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 5 | * you may not use this file except in compliance with the License. |
AmmadRehmatUbx | 0:ebe81a0cdefc | 6 | * You may obtain a copy of the License at |
AmmadRehmatUbx | 0:ebe81a0cdefc | 7 | * |
AmmadRehmatUbx | 0:ebe81a0cdefc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AmmadRehmatUbx | 0:ebe81a0cdefc | 9 | * |
AmmadRehmatUbx | 0:ebe81a0cdefc | 10 | * Unless required by applicable law or agreed to in writing, software |
AmmadRehmatUbx | 0:ebe81a0cdefc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AmmadRehmatUbx | 0:ebe81a0cdefc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AmmadRehmatUbx | 0:ebe81a0cdefc | 13 | * See the License for the specific language governing permissions and |
AmmadRehmatUbx | 0:ebe81a0cdefc | 14 | * limitations under the License. |
AmmadRehmatUbx | 0:ebe81a0cdefc | 15 | */ |
AmmadRehmatUbx | 0:ebe81a0cdefc | 16 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 17 | #include "mbed.h" |
AmmadRehmatUbx | 0:ebe81a0cdefc | 18 | #include "OdinWiFiInterface.h" |
AmmadRehmatUbx | 0:ebe81a0cdefc | 19 | #include "TCPSocket.h" |
AmmadRehmatUbx | 0:ebe81a0cdefc | 20 | #include <string> |
AmmadRehmatUbx | 0:ebe81a0cdefc | 21 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 22 | #ifdef DEVICE_WIFI_AP |
AmmadRehmatUbx | 0:ebe81a0cdefc | 23 | static const char *wifi_ssid = MBED_CONF_APP_WIFI_SSID; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 24 | static const char *wifi_password = MBED_CONF_APP_WIFI_PASSWORD; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 25 | static const char *ap_ip = MBED_CONF_APP_AP_IP; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 26 | static const char *ap_netmask = MBED_CONF_APP_AP_NETMASK; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 27 | static const char *ap_gateway = MBED_CONF_APP_AP_GATEWAY; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 28 | #endif |
AmmadRehmatUbx | 0:ebe81a0cdefc | 29 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 30 | #define ECHO_SERVER_PORT 5050 |
AmmadRehmatUbx | 0:ebe81a0cdefc | 31 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 32 | OdinWiFiInterface *_wifi; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 33 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 34 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 35 | static void start_ap(nsapi_security_t security = NSAPI_SECURITY_WPA_WPA2) |
AmmadRehmatUbx | 0:ebe81a0cdefc | 36 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 37 | nsapi_error_t error_code; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 38 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 39 | printf("\nStarting AP\n"); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 40 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 41 | // AP Configure and start |
AmmadRehmatUbx | 0:ebe81a0cdefc | 42 | error_code = _wifi->set_ap_network(ap_ip, ap_netmask, ap_gateway); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 43 | MBED_ASSERT(error_code == NSAPI_ERROR_OK); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 44 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 45 | //DHCP not available |
AmmadRehmatUbx | 0:ebe81a0cdefc | 46 | error_code = _wifi->set_ap_dhcp(false); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 47 | MBED_ASSERT(error_code == NSAPI_ERROR_OK); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 48 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 49 | //Set beacon interval to default value |
AmmadRehmatUbx | 0:ebe81a0cdefc | 50 | _wifi->set_ap_beacon_interval(100); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 51 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 52 | //Set ap ssid, password and channel |
AmmadRehmatUbx | 0:ebe81a0cdefc | 53 | error_code = _wifi->ap_start(wifi_ssid, wifi_password, security, cbWLAN_CHANNEL_01); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 54 | MBED_ASSERT(error_code == NSAPI_ERROR_OK); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 55 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 56 | printf("\nAP started successfully\n"); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 57 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 58 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 59 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 60 | static void stop_ap() |
AmmadRehmatUbx | 0:ebe81a0cdefc | 61 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 62 | nsapi_error_t error_code; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 63 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 64 | error_code = _wifi->ap_stop(); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 65 | MBED_ASSERT(error_code == NSAPI_ERROR_OK); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 66 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 67 | printf("\nAP stopped\n"); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 68 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 69 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 70 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 71 | int main() |
AmmadRehmatUbx | 0:ebe81a0cdefc | 72 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 73 | nsapi_size_or_error_t errcode; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 74 | nsapi_error_t *err; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 75 | TCPSocket sock, *sock_data; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 76 | int n = 0; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 77 | char recv_buf[1024]; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 78 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 79 | /*Start AP*/ |
AmmadRehmatUbx | 0:ebe81a0cdefc | 80 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 81 | _wifi = new OdinWiFiInterface(true); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 82 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 83 | start_ap(); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 84 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 85 | /*Socket initialization*/ |
AmmadRehmatUbx | 0:ebe81a0cdefc | 86 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 87 | errcode = sock.open(_wifi); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 88 | if (errcode != NSAPI_ERROR_OK) { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 89 | printf("TCPSocket.open() fails, code: %d\n", errcode); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 90 | return -1; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 91 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 92 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 93 | errcode = sock.bind("10.0.0.1", ECHO_SERVER_PORT); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 94 | if (errcode < 0) { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 95 | printf("TCPSocket.connect() fails, code: %d\n", errcode); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 96 | return -1; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 97 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 98 | else { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 99 | printf("TCP: connected with %s server\n", "10.0.0.1"); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 100 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 101 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 102 | /*Echo server*/ |
AmmadRehmatUbx | 0:ebe81a0cdefc | 103 | if (sock.listen() == 0) |
AmmadRehmatUbx | 0:ebe81a0cdefc | 104 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 105 | sock_data = sock.accept(err = NULL); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 106 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 107 | if (sock_data != NULL) |
AmmadRehmatUbx | 0:ebe81a0cdefc | 108 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 109 | while (true) |
AmmadRehmatUbx | 0:ebe81a0cdefc | 110 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 111 | n = sock_data->recv((void*) recv_buf, sizeof(recv_buf)); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 112 | if (n > 0) |
AmmadRehmatUbx | 0:ebe81a0cdefc | 113 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 114 | printf("\n Received from client %d bytes: %s \n", n, recv_buf); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 115 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 116 | errcode = sock_data->send((void*) recv_buf, n); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 117 | if (errcode < 0) |
AmmadRehmatUbx | 0:ebe81a0cdefc | 118 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 119 | printf("\n TCPSocket.send() fails, code: %d\n", errcode); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 120 | return -1; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 121 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 122 | else |
AmmadRehmatUbx | 0:ebe81a0cdefc | 123 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 124 | printf("\n TCP: Sent %d Bytes to client\n", n); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 125 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 126 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 127 | else |
AmmadRehmatUbx | 0:ebe81a0cdefc | 128 | { |
AmmadRehmatUbx | 0:ebe81a0cdefc | 129 | printf("\n TCPSocket.recv() failed"); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 130 | return -1; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 131 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 132 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 133 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 134 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 135 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 136 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 137 | } |
AmmadRehmatUbx | 0:ebe81a0cdefc | 138 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 139 | sock_data->close(); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 140 | sock.close(); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 141 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 142 | stop_ap(); |
AmmadRehmatUbx | 0:ebe81a0cdefc | 143 | |
AmmadRehmatUbx | 0:ebe81a0cdefc | 144 | return 0; |
AmmadRehmatUbx | 0:ebe81a0cdefc | 145 | } |