LED control by TCP

Dependencies:   WIZnetInterface mbed

Fork of TCPEchoServer-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           
00044             // LED control if received message matches pre-defined command
00045             /*
00046             if ((buffer[0] == 'L') & (buffer[1] == '\0'))
00047               myled = 0; // LED ON in WIZwiki-W7500
00048             else
00049               myled = 1;
00050             */
00051             
00052             // LED control if received message matches pre-defined command
00053             char command_buf[256] = {'L', 'E', 'D', '_', 'O', 'N', '\0'};
00054 
00055             char string[256];
00056             strcpy (string, command_buf);
00057             printf("Received command : %s\n", string);
00058 
00059             flag = compare_strings(buffer, command_buf);
00060             
00061             if (flag == 0) {
00062               myled = 0; // LED ON in WIZwiki-W7500
00063               printf("LED is turned on!\r\n");
00064             }
00065             else {
00066               myled = 1;
00067               printf("LED is turned off!\r\n");
00068             }
00069 
00070 
00071             // LED blink one time                        
00072             //myled = 0; // LED ON in WIZwiki-W7500
00073             //wait(1.0);
00074             //myled = 1; // LED OFF in WIZwiki-W7500
00075             
00076             // reverse the message
00077             char temp;
00078             for(int f = 0, l = n-1; f<l; f++,l--){
00079                 temp = buffer[f];
00080                 buffer[f] = buffer[l];
00081                 buffer[l] = temp;
00082                 }
00083             
00084             // print reversed message to terminal
00085             printf("Sending message to Client: '%s'\r\n",buffer);
00086             
00087             // Echo received message back to client
00088             client.send_all(buffer, n);
00089             if (n <= 0) break;
00090         }
00091         
00092         client.close();
00093     }
00094     
00095 }
00096 
00097 int compare_strings(char a[], char b[])
00098 {
00099    int c = 0;
00100  
00101    while (a[c] == b[c]) {
00102       if (a[c] == '\0' || b[c] == '\0')
00103          break;
00104       c++;
00105    }
00106  
00107    if (a[c] == '\0' && b[c] == '\0')
00108       return 0;
00109    else
00110       return -1;
00111 }