Hello World program for NNN50 SoftAP mode

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

Committer:
tsungta
Date:
Mon Apr 17 15:58:54 2017 +0000
Revision:
2:74cba40b8b64
Parent:
1:797e84377dc1
This revision is based on mbed-os (mbedOS 5) with an application bug (panic when remote endpoint close the socket) correction ; NNN50_WIFI_API need to update to 22:5b38592 at least; Add mbed_app.json for correct setting; Add.mbedignore (may not shown)

Who changed what in which revision?

UserRevisionLine numberNew 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 WIFIDevice wifi;
tsungta 0:717d930be97d 6 EthernetInterface eth;
tsungta 0:717d930be97d 7
tsungta 0:717d930be97d 8 uint16_t ECHO_SERVER_PORT = 1030;
tsungta 0:717d930be97d 9
tsungta 0:717d930be97d 10 int main(void)
tsungta 0:717d930be97d 11 {
tsungta 0:717d930be97d 12 eth.init();
tsungta 0:717d930be97d 13 //Default AP mode setting: ip = 192.168.1.1 / security = WEP / channel no. = 1, refer to WIFIDevice.h for detail
tsungta 0:717d930be97d 14 if(wifi.enableAccessPoint("NNN50_AP", "0123456789") != 0)
tsungta 2:74cba40b8b64 15 printf("enableAccessPoint return error!...\n");
tsungta 0:717d930be97d 16
tsungta 0:717d930be97d 17 TCPSocketServer server;
tsungta 0:717d930be97d 18
tsungta 0:717d930be97d 19 while (true) {
tsungta 2:74cba40b8b64 20 printf("Wait for new connection...\n");
tsungta 0:717d930be97d 21 TCPSocketConnection client;
tsungta 2:74cba40b8b64 22 server.bind(ECHO_SERVER_PORT);
tsungta 2:74cba40b8b64 23 server.listen();
tsungta 0:717d930be97d 24 server.accept(client);
tsungta 1:797e84377dc1 25 client.set_blocking(false, 10000); // Timeout after 10s
tsungta 0:717d930be97d 26
tsungta 2:74cba40b8b64 27 printf("Connection from: %s\n", client.get_address());
tsungta 0:717d930be97d 28 char buffer[256];
tsungta 0:717d930be97d 29 while (true) {
tsungta 0:717d930be97d 30 int n = client.receive(buffer, sizeof(buffer));
tsungta 2:74cba40b8b64 31 if (n <= 0) {//disconnect with client if no tcp packet is received within blocking period or remote endpoint close the socket.
tsungta 2:74cba40b8b64 32 server.close();
tsungta 2:74cba40b8b64 33 break;
tsungta 2:74cba40b8b64 34 }
tsungta 0:717d930be97d 35 // print received message to terminal
tsungta 0:717d930be97d 36 buffer[n] = '\0';
tsungta 2:74cba40b8b64 37 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 2:74cba40b8b64 48 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