Paul Staron
/
TCP-NTP-Server
TCP, NTP and server Ethernet and WI-FI example working on OS5.15
main.cpp@5:3ef245bbc6ef, 2020-06-13 (annotated)
- Committer:
- star297
- Date:
- Sat Jun 13 13:02:54 2020 +0000
- Revision:
- 5:3ef245bbc6ef
- Parent:
- 3:91523db40ebd
added additional web response code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:d2e1b817924d | 1 | #include "mbed.h" |
star297 | 0:d2e1b817924d | 2 | #include "NTPclient.h" |
star297 | 5:3ef245bbc6ef | 3 | #include "favicon_ico.h" |
star297 | 0:d2e1b817924d | 4 | |
star297 | 5:3ef245bbc6ef | 5 | // define static IP settings |
star297 | 0:d2e1b817924d | 6 | #define IP "192.168.1.180" |
star297 | 0:d2e1b817924d | 7 | #define GATEWAY "192.168.1.1" |
star297 | 0:d2e1b817924d | 8 | #define NETMASK "255.255.255.0" |
star297 | 3:91523db40ebd | 9 | #define PORT 80 |
star297 | 0:d2e1b817924d | 10 | |
star297 | 3:91523db40ebd | 11 | NetworkInterface *net = NetworkInterface::get_default_instance(); |
star297 | 3:91523db40ebd | 12 | NTPclient ntp(*net); |
star297 | 0:d2e1b817924d | 13 | |
star297 | 0:d2e1b817924d | 14 | DigitalOut led(LED1); |
star297 | 3:91523db40ebd | 15 | time_t seconds; |
star297 | 0:d2e1b817924d | 16 | |
star297 | 5:3ef245bbc6ef | 17 | char requestType[16]; |
star297 | 5:3ef245bbc6ef | 18 | char request[64]; |
star297 | 5:3ef245bbc6ef | 19 | |
star297 | 0:d2e1b817924d | 20 | char *sendbuffer = new char[512]; // create data send buffer space |
star297 | 5:3ef245bbc6ef | 21 | char recevbuffer[1024]; // create http receive buffer space |
star297 | 0:d2e1b817924d | 22 | |
star297 | 0:d2e1b817924d | 23 | int main() |
star297 | 3:91523db40ebd | 24 | { |
star297 | 0:d2e1b817924d | 25 | printf("\033[0m\033[2J\033[HTCP NTP client and server example\r\n\n\n"); // Tera Term clear screen |
star297 | 3:91523db40ebd | 26 | seconds = time(NULL); |
star297 | 3:91523db40ebd | 27 | printf("Initial RTC time: %s\r\n", ctime(&seconds)); |
star297 | 3:91523db40ebd | 28 | |
star297 | 3:91523db40ebd | 29 | // Include to set static IP |
star297 | 3:91523db40ebd | 30 | int net_static = net->set_network((SocketAddress)IP,(SocketAddress)NETMASK,(SocketAddress)GATEWAY); |
star297 | 0:d2e1b817924d | 31 | |
star297 | 3:91523db40ebd | 32 | printf("Connecting to network...\n\n"); |
star297 | 3:91523db40ebd | 33 | |
star297 | 3:91523db40ebd | 34 | if (!net) { |
star297 | 3:91523db40ebd | 35 | printf("No network interface found, select an interface in 'mbed_app.json'\n"); |
star297 | 3:91523db40ebd | 36 | return NULL; |
star297 | 3:91523db40ebd | 37 | } |
star297 | 3:91523db40ebd | 38 | nsapi_error_t connect_status = net->connect(); |
star297 | 3:91523db40ebd | 39 | if (connect_status != NSAPI_ERROR_OK) { |
star297 | 3:91523db40ebd | 40 | printf("Failed to connect to network (%d)\n", connect_status); |
star297 | 3:91523db40ebd | 41 | return NULL; |
star297 | 3:91523db40ebd | 42 | } |
star297 | 0:d2e1b817924d | 43 | |
star297 | 3:91523db40ebd | 44 | SocketAddress net_addr; |
star297 | 3:91523db40ebd | 45 | net->get_ip_address(&net_addr); |
star297 | 3:91523db40ebd | 46 | printf("IP address: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None"); |
star297 | 3:91523db40ebd | 47 | net->get_netmask(&net_addr); |
star297 | 3:91523db40ebd | 48 | printf("Netmask: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None"); |
star297 | 3:91523db40ebd | 49 | net->get_gateway(&net_addr); |
star297 | 3:91523db40ebd | 50 | printf("Gateway: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None"); |
star297 | 3:91523db40ebd | 51 | printf("MAC: %s\n", net->get_mac_address()); |
star297 | 3:91523db40ebd | 52 | |
star297 | 3:91523db40ebd | 53 | net->get_ip_address(&net_addr); |
star297 | 3:91523db40ebd | 54 | const char *ip = net_addr.get_ip_address(); |
star297 | 3:91523db40ebd | 55 | |
star297 | 0:d2e1b817924d | 56 | if(ip){ |
star297 | 3:91523db40ebd | 57 | printf("\nConnected\n\nGet NTP time...\n"); |
star297 | 5:3ef245bbc6ef | 58 | if(ntp.getNTP("0.pool.ntp.org",3600,1,1)){ |
star297 | 3:91523db40ebd | 59 | seconds = time(NULL); |
star297 | 3:91523db40ebd | 60 | printf("RTC time set by NTP: %s\n\n", ctime(&seconds)); |
star297 | 0:d2e1b817924d | 61 | } |
star297 | 3:91523db40ebd | 62 | else{printf("No NTP could not set RTC !!\n\n"); |
star297 | 3:91523db40ebd | 63 | } |
star297 | 0:d2e1b817924d | 64 | }else{ |
star297 | 0:d2e1b817924d | 65 | printf("No IP!!\n"); |
star297 | 0:d2e1b817924d | 66 | while(1); |
star297 | 0:d2e1b817924d | 67 | } |
star297 | 0:d2e1b817924d | 68 | |
star297 | 0:d2e1b817924d | 69 | TCPSocket srv; |
star297 | 0:d2e1b817924d | 70 | TCPSocket *client_sock; // srv.accept() will return pointer to socket |
star297 | 0:d2e1b817924d | 71 | SocketAddress client_addr; |
star297 | 5:3ef245bbc6ef | 72 | |
star297 | 3:91523db40ebd | 73 | // Open the server on ethernet stack |
star297 | 3:91523db40ebd | 74 | srv.open(net); |
star297 | 3:91523db40ebd | 75 | // Bind the HTTP port (TCP 80) to the server |
star297 | 3:91523db40ebd | 76 | srv.bind(80); |
star297 | 3:91523db40ebd | 77 | //Can handle x simultaneous connections |
star297 | 5:3ef245bbc6ef | 78 | srv.listen(5); |
star297 | 0:d2e1b817924d | 79 | |
star297 | 0:d2e1b817924d | 80 | printf("The Server IP address: '%s'\n", ip); |
star297 | 0:d2e1b817924d | 81 | printf("Waiting for connection....\r\n\n"); |
star297 | 5:3ef245bbc6ef | 82 | |
star297 | 5:3ef245bbc6ef | 83 | nsapi_size_or_error_t result; |
star297 | 3:91523db40ebd | 84 | |
star297 | 3:91523db40ebd | 85 | while(1){ |
star297 | 3:91523db40ebd | 86 | |
star297 | 3:91523db40ebd | 87 | client_sock = srv.accept(); //return pointer of a client socket |
star297 | 5:3ef245bbc6ef | 88 | led=1; |
star297 | 5:3ef245bbc6ef | 89 | //this will fill address of client to the SocketAddress object |
star297 | 5:3ef245bbc6ef | 90 | client_sock->getpeername(&client_addr); |
star297 | 5:3ef245bbc6ef | 91 | printf("\nConnected,Client: %s Port: %d\n", client_addr.get_ip_address(), client_addr.get_port()); |
star297 | 5:3ef245bbc6ef | 92 | //receive client data, (set large enough buffer) |
star297 | 5:3ef245bbc6ef | 93 | result = client_sock->recv(recevbuffer, 1024); |
star297 | 5:3ef245bbc6ef | 94 | if (result < 0) { |
star297 | 5:3ef245bbc6ef | 95 | printf("Error! socket.recv() returned: %d\n", result); |
star297 | 5:3ef245bbc6ef | 96 | } |
star297 | 5:3ef245bbc6ef | 97 | |
star297 | 5:3ef245bbc6ef | 98 | if(result){ |
star297 | 5:3ef245bbc6ef | 99 | //printf("Received Msg:\n%s\n\n", recevbuffer); // include to view complete receive buffer |
star297 | 5:3ef245bbc6ef | 100 | sscanf(recevbuffer,"%s %s",requestType,request); |
star297 | 5:3ef245bbc6ef | 101 | printf("request: %s Type: %s charcters:%d\n", requestType,request,result); |
star297 | 5:3ef245bbc6ef | 102 | |
star297 | 5:3ef245bbc6ef | 103 | if(strcmp(request,"/")==0){ |
star297 | 5:3ef245bbc6ef | 104 | //send current mcu rtc time to client |
star297 | 5:3ef245bbc6ef | 105 | time_t seconds = time(NULL); |
star297 | 5:3ef245bbc6ef | 106 | 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)); |
star297 | 5:3ef245bbc6ef | 107 | printf("Sending Message\n"); |
star297 | 5:3ef245bbc6ef | 108 | result=client_sock->send(sendbuffer, strlen(sendbuffer)); // send data in buffer to http port. |
star297 | 5:3ef245bbc6ef | 109 | printf("Sent Message, %d bytes\n",result); |
star297 | 5:3ef245bbc6ef | 110 | } |
star297 | 5:3ef245bbc6ef | 111 | else if(strcmp(request,"/favicon.ico")==0){ |
star297 | 5:3ef245bbc6ef | 112 | //send favicon if requested |
star297 | 5:3ef245bbc6ef | 113 | printf("Sending Favicon\n"); |
star297 | 5:3ef245bbc6ef | 114 | result=client_sock->send((char*)favicon_ico,sizeof(favicon_ico)); |
star297 | 5:3ef245bbc6ef | 115 | printf("Sent Favicon, %d bytes\n",result); |
star297 | 5:3ef245bbc6ef | 116 | } |
star297 | 5:3ef245bbc6ef | 117 | else{ |
star297 | 5:3ef245bbc6ef | 118 | // send page not found reply |
star297 | 5:3ef245bbc6ef | 119 | sprintf(sendbuffer,"HTTP/1.1 404 Not Found\nContent-type: text/plain\r\n\r\npage not found"); |
star297 | 5:3ef245bbc6ef | 120 | printf("Sending Not Found Message\n"); |
star297 | 5:3ef245bbc6ef | 121 | result=client_sock->send(sendbuffer, strlen(sendbuffer)); |
star297 | 5:3ef245bbc6ef | 122 | printf("Sent Not Found Message, %d bytes\n",result); |
star297 | 5:3ef245bbc6ef | 123 | } |
star297 | 5:3ef245bbc6ef | 124 | } |
star297 | 5:3ef245bbc6ef | 125 | else{ |
star297 | 5:3ef245bbc6ef | 126 | printf("Disconnected\n"); |
star297 | 5:3ef245bbc6ef | 127 | } |
star297 | 5:3ef245bbc6ef | 128 | //close socket |
star297 | 5:3ef245bbc6ef | 129 | client_sock->close(); |
star297 | 3:91523db40ebd | 130 | led=0; |
star297 | 0:d2e1b817924d | 131 | } |
star297 | 0:d2e1b817924d | 132 | } |