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

Dependencies:   NTPclient

Committer:
star297
Date:
Sun Mar 08 14:32:56 2020 +0000
Revision:
3:91523db40ebd
Parent:
2:06cbf3f53592
Child:
5:3ef245bbc6ef
Updated stuff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:d2e1b817924d 1 #include "mbed.h"
star297 3:91523db40ebd 2 #include "NetworkInterface.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 3:91523db40ebd 8 #define PORT 80
star297 0:d2e1b817924d 9
star297 3:91523db40ebd 10 NetworkInterface *net = NetworkInterface::get_default_instance();
star297 3:91523db40ebd 11 NTPclient ntp(*net);
star297 0:d2e1b817924d 12
star297 0:d2e1b817924d 13 DigitalOut led(LED1);
star297 3:91523db40ebd 14 time_t seconds;
star297 0:d2e1b817924d 15
star297 0:d2e1b817924d 16 char *sendbuffer = new char[512]; // create data send buffer space
star297 3:91523db40ebd 17 char recevbuffer[1024]; // create http receive buffer space
star297 0:d2e1b817924d 18
star297 0:d2e1b817924d 19 int main()
star297 3:91523db40ebd 20 {
star297 0:d2e1b817924d 21 printf("\033[0m\033[2J\033[HTCP NTP client and server example\r\n\n\n"); // Tera Term clear screen
star297 3:91523db40ebd 22 seconds = time(NULL);
star297 3:91523db40ebd 23 printf("Initial RTC time: %s\r\n", ctime(&seconds));
star297 3:91523db40ebd 24
star297 3:91523db40ebd 25 // Include to set static IP
star297 3:91523db40ebd 26 int net_static = net->set_network((SocketAddress)IP,(SocketAddress)NETMASK,(SocketAddress)GATEWAY);
star297 0:d2e1b817924d 27
star297 3:91523db40ebd 28 printf("Connecting to network...\n\n");
star297 3:91523db40ebd 29
star297 3:91523db40ebd 30 if (!net) {
star297 3:91523db40ebd 31 printf("No network interface found, select an interface in 'mbed_app.json'\n");
star297 3:91523db40ebd 32 return NULL;
star297 3:91523db40ebd 33 }
star297 3:91523db40ebd 34 nsapi_error_t connect_status = net->connect();
star297 3:91523db40ebd 35 if (connect_status != NSAPI_ERROR_OK) {
star297 3:91523db40ebd 36 printf("Failed to connect to network (%d)\n", connect_status);
star297 3:91523db40ebd 37 return NULL;
star297 3:91523db40ebd 38 }
star297 0:d2e1b817924d 39
star297 3:91523db40ebd 40 SocketAddress net_addr;
star297 3:91523db40ebd 41 net->get_ip_address(&net_addr);
star297 3:91523db40ebd 42 printf("IP address: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
star297 3:91523db40ebd 43 net->get_netmask(&net_addr);
star297 3:91523db40ebd 44 printf("Netmask: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
star297 3:91523db40ebd 45 net->get_gateway(&net_addr);
star297 3:91523db40ebd 46 printf("Gateway: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
star297 3:91523db40ebd 47 printf("MAC: %s\n", net->get_mac_address());
star297 3:91523db40ebd 48
star297 3:91523db40ebd 49 net->get_ip_address(&net_addr);
star297 3:91523db40ebd 50 const char *ip = net_addr.get_ip_address();
star297 3:91523db40ebd 51
star297 0:d2e1b817924d 52 if(ip){
star297 3:91523db40ebd 53 printf("\nConnected\n\nGet NTP time...\n");
star297 3:91523db40ebd 54 if(ntp.getNTP("0.pool.ntp.org",0,1,1)){
star297 3:91523db40ebd 55 seconds = time(NULL);
star297 3:91523db40ebd 56 printf("RTC time set by NTP: %s\n\n", ctime(&seconds));
star297 0:d2e1b817924d 57 }
star297 3:91523db40ebd 58 else{printf("No NTP could not set RTC !!\n\n");
star297 3:91523db40ebd 59 }
star297 0:d2e1b817924d 60 }else{
star297 0:d2e1b817924d 61 printf("No IP!!\n");
star297 0:d2e1b817924d 62 while(1);
star297 0:d2e1b817924d 63 }
star297 0:d2e1b817924d 64
star297 0:d2e1b817924d 65 TCPSocket srv;
star297 0:d2e1b817924d 66 TCPSocket *client_sock; // srv.accept() will return pointer to socket
star297 0:d2e1b817924d 67 SocketAddress client_addr;
star297 3:91523db40ebd 68
star297 3:91523db40ebd 69 // Open the server on ethernet stack
star297 3:91523db40ebd 70 srv.open(net);
star297 3:91523db40ebd 71 // Bind the HTTP port (TCP 80) to the server
star297 3:91523db40ebd 72 srv.bind(80);
star297 3:91523db40ebd 73 //Can handle x simultaneous connections
star297 3:91523db40ebd 74 srv.listen(5);
star297 0:d2e1b817924d 75
star297 0:d2e1b817924d 76 printf("The Server IP address: '%s'\n", ip);
star297 0:d2e1b817924d 77 printf("Waiting for connection....\r\n\n");
star297 3:91523db40ebd 78
star297 3:91523db40ebd 79 while(1){
star297 3:91523db40ebd 80
star297 3:91523db40ebd 81 client_sock = srv.accept(); //return pointer of a client socket
star297 3:91523db40ebd 82 led=1;
star297 3:91523db40ebd 83 client_sock->recv(recevbuffer, 1024); // set size of required receive data length
star297 3:91523db40ebd 84 client_sock->getpeername(&client_addr); //this will fill address of client to the SocketAddress object
star297 0:d2e1b817924d 85 printf("Accepted %s:%d\n\n", client_addr.get_ip_address(), client_addr.get_port());
star297 0:d2e1b817924d 86 printf("Received Msg:\n%s\n\n", recevbuffer);
star297 0:d2e1b817924d 87 time_t seconds = time(NULL); //get current mcu rtc time
star297 0:d2e1b817924d 88 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 89 client_sock->send(sendbuffer, strlen(sendbuffer)); // send data in buffer to http port.
star297 0:d2e1b817924d 90 client_sock->close();
star297 3:91523db40ebd 91 led=0;
star297 0:d2e1b817924d 92 }
star297 0:d2e1b817924d 93 }