LED can be controlled by remote TCP client. This code was tested with WIZwiki-W7500 platform board.

Dependencies:   WIZnetInterface mbed

Fork of TCP_LED_Control-WIZwiki-W7500 by Kiyong Lee

WIZwiki-W7500 TCP LED Control Example

Overview

The WIZwiki board series has an Ethernet interface with an RJ-45 connector, so you can quickly implement various network applications. This example controls the on-board LED by sending the specified command string to the board with Ethernet - TCP data.

Prerequisite

The following items are required to implement this example project.

  • Mandatory
  • Optional
    • Ethernet Hub or Router (supports DHCP IP allocation)

Information

If there is no Ethernet hub or router, you can connect PC and device directly with LAN cable.


Hardware Configuration

WIZwiki-W7500 Pinout

Pinout

On-board LED

The on-board LED (red) used to verify the transmission of TCP data is connected to the PC_08 pin and is declared as LED1 in the mbed library.

Network Configuration

/media/uploads/hkjung/tcp_loopback_system_config.jpg


Firmware

Command String

The following three command strings are implemented in this example project.

  • LED_ON
  • LED_OFF
  • LED_BLINK

Network Settings

MAC address

For Ethernet networking, each network device must have its own unique hardware address. that hardware address is called MAC address, and typically the MAC address is represented by six bytes hex separated by a colon. e.g., 00:08:DC:XX:XX:XX

The MAC address is defined in the code as follows.

main.cpp

#define MAC     "\x00\x08\xDC\x55\x51\x52"

IP address allocation method

Use Static IP address

The 'Static IP address' setting is the default value, and the device is configured with the network information defined in the code. It can be used when connecting the device directly to the PC or through an Ethernet hub. The IP address of the PC should be set to the same ip address range as the device.

The network information is defined in the code as follows.

main.cpp

#define IP      "192.168.0.20"   // IP address
#define SUBNET  "255.255.255.0"  // Subnet Mask
#define GATEWAY "192.168.0.1"    // Gateway address

The following is an example of the PC's network settings that can connect with the device's settings.

PC's network setting example
IP address192.168.0.21
Subnet Mask255.255.255.0
Gateway address192.168.0.1
Use DHCP

Routers that support DHCP automatically assign a leased range of IP addresses to each connected devices. If your PC is connected to a router, uncomment the following define and the device will be automatically assigned network information.

main.cpp

#define USE_DHCP

The network information assigned by the router can be checked through the debug messages output to the serial terminal.


Building and Running an Example

Steps

1. Import this project into your mbed compiler.

2. View the default program source code and modify what you need.

  • Network settings and so on.

3. Compile and Download the Program.

  • To compile the program, click the Compile button in the toolbar. This will compile all the program source code files within the program folder to create a binary program.
  • After a successful compile, you will get a "Success!" message in the compiler output and the download dialog will pop up. Save it to the location of the mbed Microcontroller drive, and then hit reset on the microcontroller to start it running!

4. Run the serial terminal program and check the messages.

/media/uploads/hkjung/serial_debug_wizwiki-w7500.png

  • Device's network settings and supported command strings.
  • Default UART baudrate: 115200bps

5. Run the TCP client terminal program and Connect to your device.

/media/uploads/hkjung/tcp_client_connect_wizwiki-w7500.png

6. Send command to your device and check status of LED and serial messages

/media/uploads/hkjung/tcp_client_wizwiki-w7500.png

/media/uploads/hkjung/serial_debug_cmd_wizwiki-w7500.png

Committer:
kzl108
Date:
Mon Jul 20 07:54:18 2015 +0000
Revision:
12:aee11a1d7f14
Parent:
11:0da8667a9201
Child:
13:31f4ecf83cca
LED Control by TCP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 8:f837e0d255e8 1 #include "mbed.h"
justinkim 8:f837e0d255e8 2 #include "EthernetInterface.h"
hjjeon 9:a63ff95c354b 3
hjjeon 11:0da8667a9201 4 #define ECHO_SERVER_PORT 7
hjjeon 9:a63ff95c354b 5
kzl108 12:aee11a1d7f14 6 DigitalOut myled(LED1);
kzl108 12:aee11a1d7f14 7
kzl108 12:aee11a1d7f14 8 int compare_strings(char [], char []);
kzl108 12:aee11a1d7f14 9
hjjeon 11:0da8667a9201 10 int main (void)
justinkim 8:f837e0d255e8 11 {
kzl108 12:aee11a1d7f14 12 myled = 1; // LED OFF in WIZwiki-W7500
kzl108 12:aee11a1d7f14 13 int flag=1;
kzl108 12:aee11a1d7f14 14
justinkim 8:f837e0d255e8 15 printf("Wait a second...\r\n");
kzl108 12:aee11a1d7f14 16 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x55, 0x51, 0x52};
hjjeon 11:0da8667a9201 17 EthernetInterface eth;
hjjeon 11:0da8667a9201 18 eth.init(mac_addr); //Use DHCP
justinkim 8:f837e0d255e8 19 eth.connect();
justinkim 8:f837e0d255e8 20 printf("Server IP Address is %s\r\n", eth.getIPAddress());
hjjeon 11:0da8667a9201 21
hjjeon 11:0da8667a9201 22 TCPSocketServer server;
hjjeon 11:0da8667a9201 23 server.bind(ECHO_SERVER_PORT);
hjjeon 11:0da8667a9201 24 server.listen();
hjjeon 11:0da8667a9201 25
hjjeon 11:0da8667a9201 26 while (true)
hjjeon 11:0da8667a9201 27 {
hjjeon 11:0da8667a9201 28 printf("Wait for new connection...\r\n");
hjjeon 11:0da8667a9201 29 TCPSocketConnection client;
hjjeon 11:0da8667a9201 30 server.accept(client);
hjjeon 11:0da8667a9201 31 client.set_blocking(false, 15000); // Timeout after (1.5)s
hjjeon 11:0da8667a9201 32
hjjeon 11:0da8667a9201 33 printf("Connection from: %s\r\n", client.get_address());
hjjeon 11:0da8667a9201 34 char buffer[256];
hjjeon 11:0da8667a9201 35 while (true) {
hjjeon 11:0da8667a9201 36 int n = client.receive(buffer, sizeof(buffer));
hjjeon 11:0da8667a9201 37 if (n <= 0) break;
hjjeon 11:0da8667a9201 38
hjjeon 11:0da8667a9201 39 // print received message to terminal
hjjeon 11:0da8667a9201 40 buffer[n] = '\0';
hjjeon 11:0da8667a9201 41 printf("Received message from Client :'%s'\r\n",buffer);
kzl108 12:aee11a1d7f14 42
kzl108 12:aee11a1d7f14 43
kzl108 12:aee11a1d7f14 44 // LED control if received message matches pre-defined command
kzl108 12:aee11a1d7f14 45 /*
kzl108 12:aee11a1d7f14 46 if ((buffer[0] == 'L') & (buffer[1] == '\0'))
kzl108 12:aee11a1d7f14 47 myled = 0; // LED ON in WIZwiki-W7500
kzl108 12:aee11a1d7f14 48 else
kzl108 12:aee11a1d7f14 49 myled = 1;
kzl108 12:aee11a1d7f14 50 */
kzl108 12:aee11a1d7f14 51
kzl108 12:aee11a1d7f14 52 // LED control if received message matches pre-defined command
kzl108 12:aee11a1d7f14 53 char command_buf[256] = {'L', 'E', 'D', '_', 'O', 'N', '\0'};
kzl108 12:aee11a1d7f14 54
kzl108 12:aee11a1d7f14 55 char string[256];
kzl108 12:aee11a1d7f14 56 strcpy (string, command_buf);
kzl108 12:aee11a1d7f14 57 printf("Received command : %s\n", string);
kzl108 12:aee11a1d7f14 58
kzl108 12:aee11a1d7f14 59 flag = compare_strings(buffer, command_buf);
kzl108 12:aee11a1d7f14 60
kzl108 12:aee11a1d7f14 61 if (flag == 0) {
kzl108 12:aee11a1d7f14 62 myled = 0; // LED ON in WIZwiki-W7500
kzl108 12:aee11a1d7f14 63 printf("LED is turned on!\r\n");
kzl108 12:aee11a1d7f14 64 }
kzl108 12:aee11a1d7f14 65 else {
kzl108 12:aee11a1d7f14 66 myled = 1;
kzl108 12:aee11a1d7f14 67 printf("LED is turned off!\r\n");
kzl108 12:aee11a1d7f14 68 }
kzl108 12:aee11a1d7f14 69
kzl108 12:aee11a1d7f14 70
kzl108 12:aee11a1d7f14 71 // LED blink one time
kzl108 12:aee11a1d7f14 72 //myled = 0; // LED ON in WIZwiki-W7500
kzl108 12:aee11a1d7f14 73 //wait(1.0);
kzl108 12:aee11a1d7f14 74 //myled = 1; // LED OFF in WIZwiki-W7500
hjjeon 11:0da8667a9201 75
hjjeon 11:0da8667a9201 76 // reverse the message
hjjeon 11:0da8667a9201 77 char temp;
hjjeon 11:0da8667a9201 78 for(int f = 0, l = n-1; f<l; f++,l--){
hjjeon 11:0da8667a9201 79 temp = buffer[f];
hjjeon 11:0da8667a9201 80 buffer[f] = buffer[l];
hjjeon 11:0da8667a9201 81 buffer[l] = temp;
hjjeon 11:0da8667a9201 82 }
hjjeon 11:0da8667a9201 83
hjjeon 11:0da8667a9201 84 // print reversed message to terminal
hjjeon 11:0da8667a9201 85 printf("Sending message to Client: '%s'\r\n",buffer);
hjjeon 11:0da8667a9201 86
hjjeon 11:0da8667a9201 87 // Echo received message back to client
hjjeon 11:0da8667a9201 88 client.send_all(buffer, n);
hjjeon 11:0da8667a9201 89 if (n <= 0) break;
hjjeon 11:0da8667a9201 90 }
hjjeon 11:0da8667a9201 91
hjjeon 11:0da8667a9201 92 client.close();
hjjeon 9:a63ff95c354b 93 }
justinkim 8:f837e0d255e8 94
justinkim 8:f837e0d255e8 95 }
justinkim 8:f837e0d255e8 96
kzl108 12:aee11a1d7f14 97 int compare_strings(char a[], char b[])
kzl108 12:aee11a1d7f14 98 {
kzl108 12:aee11a1d7f14 99 int c = 0;
kzl108 12:aee11a1d7f14 100
kzl108 12:aee11a1d7f14 101 while (a[c] == b[c]) {
kzl108 12:aee11a1d7f14 102 if (a[c] == '\0' || b[c] == '\0')
kzl108 12:aee11a1d7f14 103 break;
kzl108 12:aee11a1d7f14 104 c++;
kzl108 12:aee11a1d7f14 105 }
kzl108 12:aee11a1d7f14 106
kzl108 12:aee11a1d7f14 107 if (a[c] == '\0' && b[c] == '\0')
kzl108 12:aee11a1d7f14 108 return 0;
kzl108 12:aee11a1d7f14 109 else
kzl108 12:aee11a1d7f14 110 return -1;
kzl108 12:aee11a1d7f14 111 }