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

Dependencies:   NTPclient

Committer:
star297
Date:
Fri Feb 28 16:10:25 2020 +0000
Revision:
0:d2e1b817924d
Child:
2:06cbf3f53592
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:d2e1b817924d 1 #include "mbed.h"
star297 0:d2e1b817924d 2 #include "EthernetInterface.h"
star297 0:d2e1b817924d 3 #include "NTPclient.h"
star297 0:d2e1b817924d 4
star297 0:d2e1b817924d 5 #define IP "192.168.1.180"
star297 0:d2e1b817924d 6 #define GATEWAY "192.168.1.1"
star297 0:d2e1b817924d 7 #define NETMASK "255.255.255.0"
star297 0:d2e1b817924d 8 #define PORT 80
star297 0:d2e1b817924d 9
star297 0:d2e1b817924d 10 // set required stdio buad rate in mbed_app.json. eg,"platform.stdio-baud-rate": 115200
star297 0:d2e1b817924d 11
star297 0:d2e1b817924d 12 EthernetInterface net;
star297 0:d2e1b817924d 13 NTPclient ntp(net);
star297 0:d2e1b817924d 14
star297 0:d2e1b817924d 15 DigitalOut led(LED1);
star297 0:d2e1b817924d 16
star297 0:d2e1b817924d 17 char *sendbuffer = new char[512]; // create data send buffer space
star297 0:d2e1b817924d 18
star297 0:d2e1b817924d 19 int main()
star297 0:d2e1b817924d 20 {
star297 0:d2e1b817924d 21 led=0;
star297 0:d2e1b817924d 22
star297 0:d2e1b817924d 23 printf("\033[0m\033[2J\033[HTCP NTP client and server example\r\n\n\n"); // Tera Term clear screen
star297 0:d2e1b817924d 24
star297 0:d2e1b817924d 25 time_t seconds = time(NULL);
star297 0:d2e1b817924d 26 printf("Initial RTC time: %s\r\n", ctime(&seconds));
star297 0:d2e1b817924d 27
star297 0:d2e1b817924d 28 EthernetInterface eth;
star297 0:d2e1b817924d 29
star297 0:d2e1b817924d 30 eth.set_network (IP, NETMASK, GATEWAY); // include to set network connection with static parameters.
star297 0:d2e1b817924d 31
star297 0:d2e1b817924d 32 eth.connect();
star297 0:d2e1b817924d 33
star297 0:d2e1b817924d 34 const char *ip = eth.get_ip_address();
star297 0:d2e1b817924d 35 if(ip){
star297 0:d2e1b817924d 36 printf("Connected\n\nGet NTP time...\n");
star297 0:d2e1b817924d 37 if(ntp.setTime("0.pool.ntp.org",123,3000)==0){
star297 0:d2e1b817924d 38 time_t seconds = time(NULL);
star297 0:d2e1b817924d 39 printf("System time set by NTP: %s\n\n", ctime(&seconds));
star297 0:d2e1b817924d 40 }
star297 0:d2e1b817924d 41 else{printf("No NTP could not set RTC !!\n\n");}
star297 0:d2e1b817924d 42 }else{
star297 0:d2e1b817924d 43 printf("No IP!!\n");
star297 0:d2e1b817924d 44 while(1);
star297 0:d2e1b817924d 45 }
star297 0:d2e1b817924d 46
star297 0:d2e1b817924d 47 TCPSocket srv;
star297 0:d2e1b817924d 48 TCPSocket *client_sock; // srv.accept() will return pointer to socket
star297 0:d2e1b817924d 49 SocketAddress client_addr;
star297 0:d2e1b817924d 50
star297 0:d2e1b817924d 51 // Open the server on ethernet stack
star297 0:d2e1b817924d 52 srv.open(&eth);
star297 0:d2e1b817924d 53 // Bind the HTTP port (TCP 80) to the server
star297 0:d2e1b817924d 54 srv.bind(eth.get_ip_address(), PORT);
star297 0:d2e1b817924d 55 //Can handle x simultaneous connections
star297 0:d2e1b817924d 56 srv.listen(1);
star297 0:d2e1b817924d 57
star297 0:d2e1b817924d 58 printf("The Server IP address: '%s'\n", ip);
star297 0:d2e1b817924d 59 printf("Waiting for connection....\r\n\n");
star297 0:d2e1b817924d 60
star297 0:d2e1b817924d 61
star297 0:d2e1b817924d 62 led=1;
star297 0:d2e1b817924d 63
star297 0:d2e1b817924d 64 while(1){
star297 0:d2e1b817924d 65 client_sock = srv.accept(); //return pointer of a client socket
star297 0:d2e1b817924d 66 char *recevbuffer = new char[256]; // create 'clean' http receive buffer space
star297 0:d2e1b817924d 67 client_sock->recv(recevbuffer, 256); // set size of required receive data length
star297 0:d2e1b817924d 68 client_sock->getpeername(&client_addr); //this will fill address of client to the SocketAddress object
star297 0:d2e1b817924d 69 printf("Accepted %s:%d\n\n", client_addr.get_ip_address(), client_addr.get_port());
star297 0:d2e1b817924d 70 printf("Received Msg:\n%s\n\n", recevbuffer);
star297 0:d2e1b817924d 71 time_t seconds = time(NULL); //get current mcu rtc time
star297 0:d2e1b817924d 72 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 0:d2e1b817924d 73 client_sock->send(sendbuffer, strlen(sendbuffer)); // send data in buffer to http port.
star297 0:d2e1b817924d 74 client_sock->close();
star297 0:d2e1b817924d 75 delete[] recevbuffer; // delete http buffer
star297 0:d2e1b817924d 76 }
star297 0:d2e1b817924d 77
star297 0:d2e1b817924d 78 }
star297 0:d2e1b817924d 79
star297 0:d2e1b817924d 80
star297 0:d2e1b817924d 81
star297 0:d2e1b817924d 82 /*
star297 0:d2e1b817924d 83
star297 0:d2e1b817924d 84
star297 0:d2e1b817924d 85 int main(void) {
star297 0:d2e1b817924d 86
star297 0:d2e1b817924d 87 printf("\033[0m\033[2J\033[HInitialise!\r\n\n\n"); // Tera Term clear screen
star297 0:d2e1b817924d 88
star297 0:d2e1b817924d 89 time_t seconds = time(NULL);
star297 0:d2e1b817924d 90 printf("Starting time: %s\r\n", ctime(&seconds));
star297 0:d2e1b817924d 91
star297 0:d2e1b817924d 92 printf("EthernetInterface connecting...\n\n");
star297 0:d2e1b817924d 93
star297 0:d2e1b817924d 94 net.set_network (IP, NETMASK, GATEWAY); // include to set network connection with static parameters.
star297 0:d2e1b817924d 95 net.connect();
star297 0:d2e1b817924d 96
star297 0:d2e1b817924d 97 const char *ip = net.get_ip_address();
star297 0:d2e1b817924d 98 if(ip){
star297 0:d2e1b817924d 99 printf("Connected\nIP: %s\nGet NTP...\n", ip);
star297 0:d2e1b817924d 100 if(ntp.setTime("0.pool.ntp.org",123,3000)==0){
star297 0:d2e1b817924d 101 time_t seconds = time(NULL);
star297 0:d2e1b817924d 102 printf("System time set by NTP: %s\n\n", ctime(&seconds));
star297 0:d2e1b817924d 103 }
star297 0:d2e1b817924d 104 else{printf("No NTP could not set RTC !!\n\n");}
star297 0:d2e1b817924d 105 }else{
star297 0:d2e1b817924d 106 printf("No IP!!\n");
star297 0:d2e1b817924d 107 while(1);
star297 0:d2e1b817924d 108 }
star297 0:d2e1b817924d 109
star297 0:d2e1b817924d 110 // Show the network address
star297 0:d2e1b817924d 111 printf("Server IP: %s:\n", net.get_ip_address());
star297 0:d2e1b817924d 112 printf("Netmask: %s\n", net.get_netmask());
star297 0:d2e1b817924d 113 printf("Gateway: %s\n\n", net.get_gateway());
star297 0:d2e1b817924d 114
star297 0:d2e1b817924d 115 printf("Starting Server...\r\n\n");
star297 0:d2e1b817924d 116
star297 0:d2e1b817924d 117 // Open the server on ethernet stack
star297 0:d2e1b817924d 118 server.open(&net);
star297 0:d2e1b817924d 119 // Bind the HTTP port to the server
star297 0:d2e1b817924d 120 server.bind(ip, PORT);
star297 0:d2e1b817924d 121 // Can handle 5 simultaneous connections
star297 0:d2e1b817924d 122 server.listen(5);
star297 0:d2e1b817924d 123 //listening for http GET request
star297 0:d2e1b817924d 124
star297 0:d2e1b817924d 125 printf("Ready \r\nWaiting for connection....\r\n\n");
star297 0:d2e1b817924d 126
star297 0:d2e1b817924d 127 led=1;
star297 0:d2e1b817924d 128 while(1){
star297 0:d2e1b817924d 129 led=1;
star297 0:d2e1b817924d 130 server.accept(&client, &clientAddress);
star297 0:d2e1b817924d 131 led=0;
star297 0:d2e1b817924d 132 client.recv(receiveBuf, 1024);
star297 0:d2e1b817924d 133 //printf("\033[0m\033[2J\033[HClient IP: %s\r\n\n%s\r\n",clientAddress.get_ip_address(),receiveBuf);
star297 0:d2e1b817924d 134 printf("Client IP: %s\r\n\n%s\r\n",clientAddress.get_ip_address(),receiveBuf);
star297 0:d2e1b817924d 135 time_t seconds = time(NULL);
star297 0:d2e1b817924d 136 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));
star297 0:d2e1b817924d 137 client.send(buffer, strlen(buffer));
star297 0:d2e1b817924d 138 client.close();
star297 0:d2e1b817924d 139 }
star297 0:d2e1b817924d 140 }
star297 0:d2e1b817924d 141
star297 0:d2e1b817924d 142 */