Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NNN50_WIFI_API
This is a SoftAP example for Delta DFCM-NNN50 platform. In order to test with this example, user first need to open TCP Client port (use port 1030 in this example) on a PC or mobile with Socket test tool (download RealTerm' for PC, and Socket X for iOS or anymore socket test tool program available) When this example is running, the module will run as SoftAP with TCP Server. A PC or mobile can then connect to the SoftAP and connect to Server port. Once Server port is connected, user can send out the test message and expected to receive the reversed test message reply by Delta DFCM-NNN50. For more information on the usage of regular TCP and UDP Sockets, the mbed handbook can be found here
main.cpp@0:717d930be97d, 2016-12-03 (annotated)
- Committer:
- tsungta
- Date:
- Sat Dec 03 15:52:33 2016 +0000
- Revision:
- 0:717d930be97d
- Child:
- 1:797e84377dc1
First commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tsungta | 0:717d930be97d | 1 | #include "mbed.h" |
| tsungta | 0:717d930be97d | 2 | #include "EthernetInterface.h" |
| tsungta | 0:717d930be97d | 3 | #include "WIFIDevice.h" |
| tsungta | 0:717d930be97d | 4 | |
| tsungta | 0:717d930be97d | 5 | Serial uart(p17, p16);//for NNN50 |
| tsungta | 0:717d930be97d | 6 | |
| tsungta | 0:717d930be97d | 7 | WIFIDevice wifi; |
| tsungta | 0:717d930be97d | 8 | EthernetInterface eth; |
| tsungta | 0:717d930be97d | 9 | |
| tsungta | 0:717d930be97d | 10 | uint16_t ECHO_SERVER_PORT = 1030; |
| tsungta | 0:717d930be97d | 11 | |
| tsungta | 0:717d930be97d | 12 | int main(void) |
| tsungta | 0:717d930be97d | 13 | { |
| tsungta | 0:717d930be97d | 14 | eth.init(); |
| tsungta | 0:717d930be97d | 15 | //Default AP mode setting: ip = 192.168.1.1 / security = WEP / channel no. = 1, refer to WIFIDevice.h for detail |
| tsungta | 0:717d930be97d | 16 | if(wifi.enableAccessPoint("NNN50_AP", "0123456789") != 0) |
| tsungta | 0:717d930be97d | 17 | uart.printf("enableAccessPoint return error!...\n"); |
| tsungta | 0:717d930be97d | 18 | |
| tsungta | 0:717d930be97d | 19 | TCPSocketServer server; |
| tsungta | 0:717d930be97d | 20 | server.bind(ECHO_SERVER_PORT); |
| tsungta | 0:717d930be97d | 21 | server.listen(); |
| tsungta | 0:717d930be97d | 22 | |
| tsungta | 0:717d930be97d | 23 | while (true) { |
| tsungta | 0:717d930be97d | 24 | uart.printf("Wait for new connection...\n"); |
| tsungta | 0:717d930be97d | 25 | TCPSocketConnection client; |
| tsungta | 0:717d930be97d | 26 | server.accept(client); |
| tsungta | 0:717d930be97d | 27 | client.set_blocking(false, 5000); // Timeout after (5)s |
| tsungta | 0:717d930be97d | 28 | |
| tsungta | 0:717d930be97d | 29 | uart.printf("Connection from: %s\n", client.get_address()); |
| tsungta | 0:717d930be97d | 30 | char buffer[256]; |
| tsungta | 0:717d930be97d | 31 | while (true) { |
| tsungta | 0:717d930be97d | 32 | int n = client.receive(buffer, sizeof(buffer)); |
| tsungta | 0:717d930be97d | 33 | if (n <= 0) break; |
| tsungta | 0:717d930be97d | 34 | |
| tsungta | 0:717d930be97d | 35 | // print received message to terminal |
| tsungta | 0:717d930be97d | 36 | buffer[n] = '\0'; |
| tsungta | 0:717d930be97d | 37 | uart.printf("Received message from Client :'%s'\n",buffer); |
| tsungta | 0:717d930be97d | 38 | |
| tsungta | 0:717d930be97d | 39 | // reverse the message |
| tsungta | 0:717d930be97d | 40 | char temp; |
| tsungta | 0:717d930be97d | 41 | for(int f = 0, l = n-1; f<l; f++,l--){ |
| tsungta | 0:717d930be97d | 42 | temp = buffer[f]; |
| tsungta | 0:717d930be97d | 43 | buffer[f] = buffer[l]; |
| tsungta | 0:717d930be97d | 44 | buffer[l] = temp; |
| tsungta | 0:717d930be97d | 45 | } |
| tsungta | 0:717d930be97d | 46 | |
| tsungta | 0:717d930be97d | 47 | // print reversed message to terminal |
| tsungta | 0:717d930be97d | 48 | uart.printf("Sending message to Client: '%s'\n",buffer); |
| tsungta | 0:717d930be97d | 49 | |
| tsungta | 0:717d930be97d | 50 | // Echo received message back to client |
| tsungta | 0:717d930be97d | 51 | client.send_all(buffer, n); |
| tsungta | 0:717d930be97d | 52 | if (n <= 0) break; |
| tsungta | 0:717d930be97d | 53 | } |
| tsungta | 0:717d930be97d | 54 | |
| tsungta | 0:717d930be97d | 55 | client.close(); |
| tsungta | 0:717d930be97d | 56 | } |
| tsungta | 0:717d930be97d | 57 | } |
| tsungta | 0:717d930be97d | 58 |