TCP, NTP and server Ethernet and WI-FI example working on OS5.15

Dependencies:   NTPclient

Revision:
3:91523db40ebd
Parent:
2:06cbf3f53592
Child:
5:3ef245bbc6ef
--- a/main.cpp	Fri Feb 28 16:13:54 2020 +0000
+++ b/main.cpp	Sun Mar 08 14:32:56 2020 +0000
@@ -1,44 +1,62 @@
 #include "mbed.h"
-#include "EthernetInterface.h"
+#include "NetworkInterface.h"
 #include "NTPclient.h"
 
 #define IP         "192.168.1.180"
 #define GATEWAY    "192.168.1.1"
 #define NETMASK    "255.255.255.0" 
-#define PORT        80
+#define PORT       80
 
-// set required stdio buad rate in mbed_app.json. eg,"platform.stdio-baud-rate": 115200
- 
-EthernetInterface   net;
-NTPclient           ntp(net);
+NetworkInterface *net = NetworkInterface::get_default_instance();
+NTPclient         ntp(*net);
 
 DigitalOut led(LED1);
+time_t seconds;
 
 char    *sendbuffer = new char[512];    // create data send buffer space
+char    recevbuffer[1024];  // create http receive buffer space
 
 int main()
-{
-    led=0;
-    
+{   
     printf("\033[0m\033[2J\033[HTCP NTP client and server example\r\n\n\n"); // Tera Term clear screen
+    seconds = time(NULL);     
+    printf("Initial RTC time: %s\r\n", ctime(&seconds));    
+
+    // Include to set static IP    
+    int net_static = net->set_network((SocketAddress)IP,(SocketAddress)NETMASK,(SocketAddress)GATEWAY);
     
-    time_t seconds = time(NULL);     
-    printf("Initial RTC time: %s\r\n", ctime(&seconds));
-    
-    EthernetInterface eth;
+    printf("Connecting to network...\n\n");
+
+    if (!net) {
+        printf("No network interface found, select an interface in 'mbed_app.json'\n");
+        return NULL;
+    }
+    nsapi_error_t connect_status = net->connect();
+    if (connect_status != NSAPI_ERROR_OK) {
+        printf("Failed to connect to network (%d)\n", connect_status);
+        return NULL;
+    }   
     
-    eth.set_network (IP, NETMASK, GATEWAY);  // include to set network connection with static parameters.
-    
-    eth.connect();
-      
-    const char *ip = eth.get_ip_address();
+    SocketAddress net_addr;   
+    net->get_ip_address(&net_addr);
+    printf("IP address: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
+    net->get_netmask(&net_addr);
+    printf("Netmask:    %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
+    net->get_gateway(&net_addr);
+    printf("Gateway:    %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");    
+    printf("MAC:        %s\n", net->get_mac_address());  
+
+    net->get_ip_address(&net_addr);
+    const char *ip = net_addr.get_ip_address();    
+  
     if(ip){ 
-        printf("Connected\n\nGet NTP time...\n");
-        if(ntp.setTime("0.pool.ntp.org",123,3000)==0){
-            time_t seconds = time(NULL);
-            printf("System time set by NTP: %s\n\n", ctime(&seconds));
+        printf("\nConnected\n\nGet NTP time...\n");
+        if(ntp.getNTP("0.pool.ntp.org",0,1,1)){
+            seconds = time(NULL);
+            printf("RTC time set by NTP: %s\n\n", ctime(&seconds));
             }
-            else{printf("No NTP could not set RTC !!\n\n");}
+            else{printf("No NTP could not set RTC !!\n\n");
+            }  
     }else{
         printf("No IP!!\n");
         while(1);
@@ -47,32 +65,29 @@
     TCPSocket srv;
     TCPSocket *client_sock;  // srv.accept() will return pointer to socket
     SocketAddress client_addr;
-    
-        // Open the server on ethernet stack
-    srv.open(&eth);   
-        // Bind the HTTP port (TCP 80) to the server
-    srv.bind(eth.get_ip_address(), PORT);    
-        //Can handle x simultaneous connections
-    srv.listen(1);
+        
+    // Open the server on ethernet stack
+    srv.open(net);
+    // Bind the HTTP port (TCP 80) to the server    
+    srv.bind(80);       
+    //Can handle x simultaneous connections
+    srv.listen(5);
     
     printf("The Server IP address: '%s'\n", ip);
     printf("Waiting for connection....\r\n\n"); 
-       
-    
-    led=1;
-    
-    while(1){        
-        client_sock = srv.accept();  //return pointer of a client socket
-        char    *recevbuffer = new char[256];   // create 'clean' http receive buffer space   
-        client_sock->recv(recevbuffer, 256);     // set size of required receive data length
-        client_sock->getpeername(&client_addr);  //this will fill address of client to the SocketAddress object
+        
+    while(1){
+                           
+        client_sock = srv.accept();  //return pointer of a client socket 
+        led=1;   
+        client_sock->recv(recevbuffer, 1024);       // set size of required receive data length
+        client_sock->getpeername(&client_addr);     //this will fill address of client to the SocketAddress object
         printf("Accepted %s:%d\n\n", client_addr.get_ip_address(), client_addr.get_port());     
         printf("Received Msg:\n%s\n\n", recevbuffer); 
         time_t seconds = time(NULL);    //get current mcu rtc time
         sprintf(sendbuffer,"HTTP/1.1 200 OK\n Content-type: text/plain\r\n\r\n <h1> Hello !!</h1>\r\n\n <h1>Time is: %s</h1>\r\n", ctime(&seconds));
         client_sock->send(sendbuffer, strlen(sendbuffer));  // send data in buffer to http port.        
         client_sock->close();
-        delete[] recevbuffer;   // delete http buffer
+        led=0;
     }
-
 }