Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "NetworkInterface.h" 00003 #include "NTPclient.h" 00004 #include "favicon_ico.h" 00005 00006 // define static IP settings 00007 #define IP "192.168.1.180" 00008 #define GATEWAY "192.168.1.1" 00009 #define NETMASK "255.255.255.0" 00010 #define PORT 80 00011 00012 NetworkInterface *net = NetworkInterface::get_default_instance(); 00013 00014 TCPServer server; 00015 TCPSocket client; 00016 SocketAddress clientAddress; 00017 00018 NTPclient ntp(*net); 00019 00020 DigitalOut led(LED1); 00021 time_t seconds; 00022 00023 char requestType[16]; 00024 char request[64]; 00025 00026 char *sendbuffer = new char[512]; // create data send buffer space 00027 char recevbuffer[1024]; // create http receive buffer space 00028 00029 int main() 00030 { 00031 printf("\033[0m\033[2J\033[HTCP NTP client and server example\r\n\n\n"); // Tera Term clear screen 00032 seconds = time(NULL); 00033 printf("Initial RTC time: %s\r\n", ctime(&seconds)); 00034 00035 // Include to set static IP 00036 net->set_network (IP, NETMASK, GATEWAY); 00037 00038 printf("Connecting to network...\n\n"); 00039 00040 nsapi_size_or_error_t r = net->connect(); 00041 if (r != 0) { 00042 printf("Error! net.connect() returned: %d\n", r); 00043 while(1); // stop here and find the connection problem 00044 } 00045 00046 const char *ip = net->get_ip_address(); 00047 00048 if(ip){ 00049 printf("\nConnected\n\nGet NTP time...\n"); 00050 if(ntp.getNTP("0.pool.ntp.org",3600,1,1)){ 00051 seconds = time(NULL); 00052 printf("RTC time set by NTP: %s\n\n", ctime(&seconds)); 00053 } 00054 else{printf("No NTP could not set RTC !!\n\n"); 00055 } 00056 }else{ 00057 printf("No IP!!\n"); 00058 while(1); 00059 } 00060 00061 printf("Starting Server...\r\n\n"); 00062 00063 /* Open the server on ethernet stack */ 00064 server.open(net); 00065 /* Bind the HTTP port (TCP 80) to the server */ 00066 server.bind(ip, 80); 00067 /* Can handle 5 simultaneous connections */ 00068 server.listen(5); 00069 //listening for http GET request 00070 printf("Ready\r\n\n"); 00071 00072 printf("The Server IP address: '%s'\n", ip); 00073 printf("Waiting for connection....\r\n\n"); 00074 00075 nsapi_size_or_error_t result; 00076 00077 while(1){ 00078 00079 server.accept(&client, &clientAddress); 00080 00081 led=1; 00082 result=client.recv(recevbuffer, 1024); 00083 if (result < 0) { 00084 printf("Error! socket.recv() returned: %d\n", result); 00085 } 00086 00087 sscanf(recevbuffer,"%s %s",requestType,request); 00088 printf("\nClient IP: %s \n", clientAddress.get_ip_address()); 00089 //printf("Received Msg:\n%s\n\n", recevbuffer); // include to view complete receive buffer 00090 printf("request: %s Type: %s\n", requestType,request); 00091 00092 if(strcmp(request,"/")==0){ 00093 //send current mcu rtc time to client 00094 time_t seconds = time(NULL); 00095 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)); 00096 printf("Sending Message\n"); 00097 result=client.send(sendbuffer, strlen(sendbuffer)); // send data in buffer to http port. 00098 printf("Sent Message, %d bytes\n",result); 00099 } 00100 else if(strcmp(request,"/favicon.ico")==0){ 00101 //send favicon if requested 00102 printf("Sending Favicon\n"); 00103 result=client.send((char*)favicon_ico,sizeof(favicon_ico)); 00104 printf("Sent Favicon, %d bytes\n",result); 00105 } 00106 else{ 00107 // send page not found reply 00108 sprintf(sendbuffer,"HTTP/1.1 404 Not Found\nContent-type: text/plain\r\n\r\npage not found"); 00109 printf("Sending Not Found Message\n"); 00110 result=client.send(sendbuffer, strlen(sendbuffer)); 00111 printf("Sent Not Found Message, %d bytes\n",result); 00112 } 00113 //close socket 00114 client.close(); 00115 led=0; 00116 } 00117 }
Generated on Thu Jul 14 2022 22:47:04 by
1.7.2