![](/media/cache/profiles/starmcu-blue80_lsOO5jH.jpg.50x50_q85.jpg)
TCP, NTP and server Ethernet and WI-FI example working on OS5.15
Diff: main.cpp
- Revision:
- 5:3ef245bbc6ef
- Parent:
- 3:91523db40ebd
--- a/main.cpp Sun Mar 08 14:35:57 2020 +0000 +++ b/main.cpp Sat Jun 13 13:02:54 2020 +0000 @@ -1,7 +1,8 @@ #include "mbed.h" -#include "NetworkInterface.h" #include "NTPclient.h" +#include "favicon_ico.h" +// define static IP settings #define IP "192.168.1.180" #define GATEWAY "192.168.1.1" #define NETMASK "255.255.255.0" @@ -13,8 +14,11 @@ DigitalOut led(LED1); time_t seconds; +char requestType[16]; +char request[64]; + char *sendbuffer = new char[512]; // create data send buffer space -char recevbuffer[1024]; // create http receive buffer space +char recevbuffer[1024]; // create http receive buffer space int main() { @@ -51,7 +55,7 @@ if(ip){ printf("\nConnected\n\nGet NTP time...\n"); - if(ntp.getNTP("0.pool.ntp.org",0,1,1)){ + if(ntp.getNTP("0.pool.ntp.org",3600,1,1)){ seconds = time(NULL); printf("RTC time set by NTP: %s\n\n", ctime(&seconds)); } @@ -65,29 +69,64 @@ TCPSocket srv; TCPSocket *client_sock; // srv.accept() will return pointer to socket SocketAddress client_addr; - + // 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); + srv.listen(5); printf("The Server IP address: '%s'\n", ip); printf("Waiting for connection....\r\n\n"); + + nsapi_size_or_error_t result; 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(); + led=1; + //this will fill address of client to the SocketAddress object + client_sock->getpeername(&client_addr); + printf("\nConnected,Client: %s Port: %d\n", client_addr.get_ip_address(), client_addr.get_port()); + //receive client data, (set large enough buffer) + result = client_sock->recv(recevbuffer, 1024); + if (result < 0) { + printf("Error! socket.recv() returned: %d\n", result); + } + + if(result){ + //printf("Received Msg:\n%s\n\n", recevbuffer); // include to view complete receive buffer + sscanf(recevbuffer,"%s %s",requestType,request); + printf("request: %s Type: %s charcters:%d\n", requestType,request,result); + + if(strcmp(request,"/")==0){ + //send current mcu rtc time to client + time_t seconds = time(NULL); + 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)); + printf("Sending Message\n"); + result=client_sock->send(sendbuffer, strlen(sendbuffer)); // send data in buffer to http port. + printf("Sent Message, %d bytes\n",result); + } + else if(strcmp(request,"/favicon.ico")==0){ + //send favicon if requested + printf("Sending Favicon\n"); + result=client_sock->send((char*)favicon_ico,sizeof(favicon_ico)); + printf("Sent Favicon, %d bytes\n",result); + } + else{ + // send page not found reply + sprintf(sendbuffer,"HTTP/1.1 404 Not Found\nContent-type: text/plain\r\n\r\npage not found"); + printf("Sending Not Found Message\n"); + result=client_sock->send(sendbuffer, strlen(sendbuffer)); + printf("Sent Not Found Message, %d bytes\n",result); + } + } + else{ + printf("Disconnected\n"); + } + //close socket + client_sock->close(); led=0; } }