Paul Staron
/
TCP-NTP-Server
TCP, NTP and server Ethernet and WI-FI example working on OS5.15
main.cpp@2:06cbf3f53592, 2020-02-28 (annotated)
- Committer:
- star297
- Date:
- Fri Feb 28 16:13:54 2020 +0000
- Revision:
- 2:06cbf3f53592
- Parent:
- 0:d2e1b817924d
- Child:
- 3:91523db40ebd
tidy
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 "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(ð); |
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 | } |