yang hengcheng / Mbed 2 deprecated TCP_LED_Control-WIZwiki-W7500

Dependencies:   WIZnetInterface mbed-src mbed mbed

Fork of TCP_LED_Control-WIZwiki-W7500 by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 #define ECHO_SERVER_PORT   7
00005 
00006 DigitalOut myled(LED1);
00007 
00008 int compare_strings(char [], char []); 
00009 
00010 int main (void) 
00011 {
00012     myled = 1; // LED OFF in WIZwiki-W7500
00013     int flag=1;
00014 
00015     printf("Wait a second...\r\n");
00016     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x55, 0x51, 0x52}; 
00017     EthernetInterface eth;
00018     eth.init(mac_addr); //Use DHCP
00019     eth.connect();
00020     printf("Server IP Address is %s\r\n", eth.getIPAddress());
00021     
00022     TCPSocketServer server;
00023     server.bind(ECHO_SERVER_PORT);
00024     server.listen();
00025     
00026     while (true) 
00027     {
00028         printf("Wait for new connection...\r\n");
00029         TCPSocketConnection client;
00030         server.accept(client);
00031         client.set_blocking(false, 15000); // Timeout after (1.5)s
00032         
00033         printf("Connection from: %s\r\n", client.get_address());
00034         char buffer[256];
00035         while (true) {
00036             int n = client.receive(buffer, sizeof(buffer));
00037             if (n <= 0) break;
00038             
00039             // print received message to terminal
00040             buffer[n] = '\0';
00041             printf("Received message from Client :'%s'\r\n",buffer);
00042     
00043             // LED control if received message matches pre-defined command
00044             char command_buf[256] = {'L', 'E', 'D', '_', 'O', 'N', '\0'};
00045 
00046             char string[256];
00047             strcpy (string, command_buf);
00048             printf("Received command : %s\n", string);
00049 
00050             flag = compare_strings(buffer, command_buf);
00051             
00052             if (flag == 0) {
00053               myled = 0; // LED ON in WIZwiki-W7500
00054               printf("LED is turned on!\r\n");
00055             }
00056             else {
00057               myled = 1;
00058               printf("LED is turned off!\r\n");
00059             }
00060             
00061             // reverse the message
00062             char temp;
00063             for(int f = 0, l = n-1; f<l; f++,l--){
00064                 temp = buffer[f];
00065                 buffer[f] = buffer[l];
00066                 buffer[l] = temp;
00067                 }
00068             
00069             // print reversed message to terminal
00070             printf("Sending message to Client: '%s'\r\n",buffer);
00071             
00072             // Echo received message back to client
00073             client.send_all(buffer, n);
00074             if (n <= 0) break;
00075         }
00076         
00077         client.close();
00078     }
00079     
00080 }
00081 
00082 int compare_strings(char a[], char b[])
00083 {
00084    int c = 0;
00085  
00086    while (a[c] == b[c]) {
00087       if (a[c] == '\0' || b[c] == '\0')
00088          break;
00089       c++;
00090    }
00091  
00092    if (a[c] == '\0' && b[c] == '\0')
00093       return 0;
00094    else
00095       return -1;
00096 }