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:
Sun Apr 02 03:03:57 2017 +0000
Revision:
1:797e84377dc1
Parent:
0:717d930be97d
Child:
2:74cba40b8b64
Change set_blocking timeout to 10s, easier to test with; Update NNN50_WIFI_API to revision 18 (it seems revision 20 have bug needs to be fixed)

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 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 1:797e84377dc1 27 client.set_blocking(false, 10000); // Timeout after 10s
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 1:797e84377dc1 33 if (n <= 0) break; //disconnect with client if no tcp packet is received within blocking period.
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