Delta / Mbed 2 deprecated NNN40_WiFi_SoftAP

Dependencies:   WIFI_API_32kRAM mbed

Fork of NNN40_WiFi by Delta

Revision:
14:1f8b31c4dcc1
Parent:
13:6570897e31f8
Child:
15:16c3060424e3
--- a/main.cpp	Sat Aug 29 13:22:33 2015 +0000
+++ b/main.cpp	Mon Aug 31 07:19:46 2015 +0000
@@ -1,66 +1,62 @@
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
-20
-21
-22
-23
-24
-25
-26
-27
-28
-29
-30
-31
-32
-33
-34
 #include "mbed.h"
 #include "WIFIDevice.h"
 #include "EthernetInterface.h"
  
 WIFIDevice wifi;
- 
-const char* PC_SERVER_ADDRESS = "192.168.1.234";
-uint16_t PC_PORT = 5222;
-DigitalOut LED01(LED1);
-DigitalOut LED02(LED2);
+
+uint16_t ECHO_SERVER_PORT = 5222;
  
 int main(void)
 {   
+    printf("NNN40 init...\n");
+    wifi.setAccessPoint("NNN40", "0123456789", SECURITY_WPA2_TKIP_PSK, 2);      
     EthernetInterface eth;
-    eth.init(); //Use DHCP
+    eth.init();
     wait(1);
-    // set given SSID and PW as the highest priority  
-    wifi.setNetwork("HTC820", "11111111", 0);
-    wait(1);
-    LED01 = 1;
+   
     eth.connect(40000);
- 
-    LED02 = 1;
-    TCPSocketConnection socket;     
-    socket.connect(PC_SERVER_ADDRESS,PC_PORT); 
-    char msg[] = "Hello World";
-    socket.send(msg, sizeof(msg));
- 
-    socket.close();
-    eth.disconnect();
-    wifi.sleep();
-}
\ No newline at end of file
+
+    printf("IP Address: %s\n",eth.getIPAddress());       
+    printf("MAC Address: %s\n",eth.getMACAddress());  
+    printf("Gateway Address: %s\n",eth.getGateway());       
+    printf("Network Mask: %s\n",eth.getNetworkMask()); 
+
+    TCPSocketServer server;
+    server.bind(ECHO_SERVER_PORT);
+    server.listen();
+    
+    while (true) {
+        printf("\nWait for new connection...\n");
+        TCPSocketConnection client;
+        server.accept(client);
+        client.set_blocking(false, 5000); // Timeout after (5)s
+        
+        printf("Connection from: %s\n", client.get_address());
+        char buffer[256];
+        while (true) {
+            int n = client.receive(buffer, sizeof(buffer));
+            if (n <= 0) break;
+            
+            // print received message to terminal
+            buffer[n] = '\0';
+            printf("Received message from Client :'%s'\n",buffer);
+            
+            // reverse the message
+            char temp;
+            for(int f = 0, l = n-1; f<l; f++,l--){
+                temp = buffer[f];
+                buffer[f] = buffer[l];
+                buffer[l] = temp;
+                }
+            
+            // print reversed message to terminal
+            printf("Sending message to Client: '%s'\n",buffer);
+            
+            // Echo received message back to client
+            client.send_all(buffer, n);
+            if (n <= 0) break;
+        }
+        
+        client.close();
+    }
+}