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

Dependencies:   NTPclient

Revision:
0:d2e1b817924d
Child:
2:06cbf3f53592
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Feb 28 16:10:25 2020 +0000
@@ -0,0 +1,142 @@
+#include "mbed.h"
+#include "EthernetInterface.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
+
+// set required stdio buad rate in mbed_app.json. eg,"platform.stdio-baud-rate": 115200
+ 
+EthernetInterface   net;
+NTPclient           ntp(net);
+
+DigitalOut led(LED1);
+
+char    *sendbuffer = new char[512];    // create data send 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
+    
+    time_t seconds = time(NULL);     
+    printf("Initial RTC time: %s\r\n", ctime(&seconds));
+    
+    EthernetInterface eth;
+    
+    eth.set_network (IP, NETMASK, GATEWAY);  // include to set network connection with static parameters.
+    
+    eth.connect();
+      
+    const char *ip = eth.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));
+            }
+            else{printf("No NTP could not set RTC !!\n\n");}
+    }else{
+        printf("No IP!!\n");
+        while(1);
+    }         
+ 
+    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);
+    
+    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
+        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
+    }
+
+}
+
+
+
+/*
+
+
+int main(void) {  
+
+    printf("\033[0m\033[2J\033[HInitialise!\r\n\n\n"); // Tera Term clear screen
+              
+    time_t seconds = time(NULL);     
+    printf("Starting time: %s\r\n", ctime(&seconds));
+        
+    printf("EthernetInterface connecting...\n\n");
+    
+    net.set_network (IP, NETMASK, GATEWAY);  // include to set network connection with static parameters.
+    net.connect();
+
+    const char *ip = net.get_ip_address();
+    if(ip){ 
+        printf("Connected\nIP: %s\nGet NTP...\n", ip);
+        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));
+            }
+            else{printf("No NTP could not set RTC !!\n\n");}
+    }else{
+        printf("No IP!!\n");
+        while(1);
+    }         
+       
+    // Show the network address    
+    printf("Server IP: %s:\n", net.get_ip_address());
+    printf("Netmask:   %s\n", net.get_netmask());
+    printf("Gateway:   %s\n\n", net.get_gateway());  
+    
+    printf("Starting Server...\r\n\n");
+
+    // Open the server on ethernet stack
+    server.open(&net); 
+    // Bind the HTTP port to the server
+    server.bind(ip, PORT); 
+    // Can handle 5 simultaneous connections
+    server.listen(5);   
+    //listening for http GET request
+    
+    printf("Ready \r\nWaiting for connection....\r\n\n");
+    
+    led=1;
+    while(1){         
+        led=1;            
+        server.accept(&client, &clientAddress);
+        led=0;
+        client.recv(receiveBuf, 1024);             
+        //printf("\033[0m\033[2J\033[HClient IP: %s\r\n\n%s\r\n",clientAddress.get_ip_address(),receiveBuf); 
+        printf("Client IP: %s\r\n\n%s\r\n",clientAddress.get_ip_address(),receiveBuf);        
+        time_t seconds = time(NULL);
+        sprintf(buffer,"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.send(buffer, strlen(buffer));
+        client.close();        
+    }
+}
+
+*/
\ No newline at end of file