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:
hkjung
Date:
Thu Apr 13 23:16:22 2017 +0000
Revision:
16:c335303f3c69
Parent:
15:3e15e08ff603
Added UART configuration code for debug message print out

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
hkjung 14:3ecd0c9dfa6b 4 #define MAC "\x00\x08\xDC\x55\x51\x52"
hkjung 14:3ecd0c9dfa6b 5 #define IP "192.168.0.20"
hkjung 14:3ecd0c9dfa6b 6 #define SUBNET "255.255.255.0"
hkjung 14:3ecd0c9dfa6b 7 #define GATEWAY "192.168.0.1"
hkjung 14:3ecd0c9dfa6b 8
hkjung 14:3ecd0c9dfa6b 9 #define ECHO_SERVER_PORT 5000
hkjung 14:3ecd0c9dfa6b 10
hkjung 14:3ecd0c9dfa6b 11 //#define USE_DHCP
hkjung 14:3ecd0c9dfa6b 12
hkjung 15:3e15e08ff603 13 #define FLAG_LED_BLINK 3
hkjung 14:3ecd0c9dfa6b 14 #define FLAG_LED_ON 2
hkjung 14:3ecd0c9dfa6b 15 #define FLAG_LED_OFF 1
hkjung 14:3ecd0c9dfa6b 16 #define FLAG_INVALID_CMD 0
hkjung 14:3ecd0c9dfa6b 17
hkjung 14:3ecd0c9dfa6b 18 #define LED_ON 0
hkjung 14:3ecd0c9dfa6b 19 #define LED_OFF 1
hjjeon 9:a63ff95c354b 20
hkjung 15:3e15e08ff603 21 void led_switch(void);
hkjung 15:3e15e08ff603 22
hkjung 15:3e15e08ff603 23 Ticker time_up;
hkjung 16:c335303f3c69 24
hkjung 16:c335303f3c69 25 /* LED Pin Configuration */
hkjung 16:c335303f3c69 26 DigitalOut myled(LED1, LED_OFF);
hkjung 16:c335303f3c69 27
hkjung 16:c335303f3c69 28 /* UART Pin Configuration */
hkjung 16:c335303f3c69 29 Serial pc(USBTX, USBRX);
kzl108 12:aee11a1d7f14 30
hkjung 14:3ecd0c9dfa6b 31 // LED control if received message matches pre-defined commands
hkjung 14:3ecd0c9dfa6b 32 char cmd_ledon[] = {'L', 'E', 'D', '_', 'O', 'N', '\0'};
hkjung 14:3ecd0c9dfa6b 33 char cmd_ledoff[] = {'L', 'E', 'D', '_', 'O', 'F', 'F', '\0'};
hkjung 15:3e15e08ff603 34 char cmd_ledblink[] = {'L', 'E', 'D', '_', 'B', 'L', 'I', 'N', 'K', '\0'};
hkjung 15:3e15e08ff603 35
hkjung 15:3e15e08ff603 36 int flag; // LED status flag
hkjung 15:3e15e08ff603 37
hkjung 15:3e15e08ff603 38 void led_switch(void)
hkjung 15:3e15e08ff603 39 {
hkjung 15:3e15e08ff603 40 if(flag == FLAG_LED_BLINK) {
hkjung 15:3e15e08ff603 41 myled=!myled;
hkjung 15:3e15e08ff603 42 }
hkjung 15:3e15e08ff603 43 }
kzl108 12:aee11a1d7f14 44
hjjeon 11:0da8667a9201 45 int main (void)
justinkim 8:f837e0d255e8 46 {
hkjung 16:c335303f3c69 47 /* Serial baudrate configuration */
hkjung 16:c335303f3c69 48 pc.baud(115200);
hkjung 15:3e15e08ff603 49
hkjung 15:3e15e08ff603 50 // mbed Ticker
hkjung 15:3e15e08ff603 51 time_up.attach(&led_switch, 0.2);
kzl108 12:aee11a1d7f14 52
hkjung 16:c335303f3c69 53 pc.printf("Wait a second...\r\n\r\n");
hkjung 15:3e15e08ff603 54
hkjung 15:3e15e08ff603 55 // Ethernet
hkjung 15:3e15e08ff603 56 EthernetInterface eth;
hkjung 14:3ecd0c9dfa6b 57 #ifdef USE_DHCP
hkjung 14:3ecd0c9dfa6b 58 eth.init(MAC); // Use DHCP
hkjung 14:3ecd0c9dfa6b 59 #else
hkjung 14:3ecd0c9dfa6b 60 eth.init((uint8_t*)MAC, IP, SUBNET, GATEWAY); // Use Static IP address
hkjung 15:3e15e08ff603 61 #endif
justinkim 8:f837e0d255e8 62 eth.connect();
hkjung 14:3ecd0c9dfa6b 63
hkjung 16:c335303f3c69 64 pc.printf("Server IP Address is %s : %d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT);
hkjung 14:3ecd0c9dfa6b 65
hkjung 16:c335303f3c69 66 pc.printf("Command #1: %s\r\n", cmd_ledon);
hkjung 16:c335303f3c69 67 pc.printf("Command #2: %s\r\n", cmd_ledoff);
hkjung 16:c335303f3c69 68 pc.printf("Command #3: %s\r\n", cmd_ledblink);
hkjung 16:c335303f3c69 69 pc.printf("\r\n");
hjjeon 11:0da8667a9201 70
hjjeon 11:0da8667a9201 71 TCPSocketServer server;
hjjeon 11:0da8667a9201 72 server.bind(ECHO_SERVER_PORT);
hjjeon 11:0da8667a9201 73 server.listen();
hjjeon 11:0da8667a9201 74
hjjeon 11:0da8667a9201 75 while (true)
hjjeon 11:0da8667a9201 76 {
hkjung 16:c335303f3c69 77 pc.printf("Wait for new connection...\r\n");
hjjeon 11:0da8667a9201 78 TCPSocketConnection client;
hjjeon 11:0da8667a9201 79 server.accept(client);
hjjeon 11:0da8667a9201 80 client.set_blocking(false, 15000); // Timeout after (1.5)s
hjjeon 11:0da8667a9201 81
hkjung 16:c335303f3c69 82 pc.printf("Connection from: %s\r\n", client.get_address());
hkjung 14:3ecd0c9dfa6b 83 printf("\r\n");
hkjung 14:3ecd0c9dfa6b 84
hjjeon 11:0da8667a9201 85 char buffer[256];
hjjeon 11:0da8667a9201 86 while (true) {
hjjeon 11:0da8667a9201 87 int n = client.receive(buffer, sizeof(buffer));
hjjeon 11:0da8667a9201 88 if (n <= 0) break;
hjjeon 11:0da8667a9201 89
hkjung 14:3ecd0c9dfa6b 90 // Print received message to terminal
hjjeon 11:0da8667a9201 91 buffer[n] = '\0';
hkjung 16:c335303f3c69 92 pc.printf("Received message from Client :'%s'\r\n",buffer);
hkjung 14:3ecd0c9dfa6b 93
hkjung 14:3ecd0c9dfa6b 94 // Compares the received string to predefined commands
hkjung 14:3ecd0c9dfa6b 95 if(strncmp(buffer, cmd_ledon, sizeof(buffer)) == 0) {
hkjung 14:3ecd0c9dfa6b 96 flag = FLAG_LED_ON;
hkjung 14:3ecd0c9dfa6b 97 } else if(strncmp(buffer, cmd_ledoff, sizeof(buffer)) == 0) {
hkjung 14:3ecd0c9dfa6b 98 flag = FLAG_LED_OFF;
hkjung 15:3e15e08ff603 99 } else if(strncmp(buffer, cmd_ledblink, sizeof(buffer)) == 0) {
hkjung 15:3e15e08ff603 100 flag = FLAG_LED_BLINK;
hkjung 14:3ecd0c9dfa6b 101 } else {
hkjung 14:3ecd0c9dfa6b 102 flag = FLAG_INVALID_CMD;
kzl108 12:aee11a1d7f14 103 }
hjjeon 11:0da8667a9201 104
hkjung 14:3ecd0c9dfa6b 105 // LED On/Off or Invalid command
hkjung 14:3ecd0c9dfa6b 106 switch(flag)
hkjung 14:3ecd0c9dfa6b 107 {
hkjung 14:3ecd0c9dfa6b 108 case FLAG_LED_ON:
hkjung 14:3ecd0c9dfa6b 109 myled = LED_ON; // LED ON in WIZwiki-W7500
hkjung 16:c335303f3c69 110 pc.printf("WIZwiki: LED is turned on!\r\n");
hkjung 14:3ecd0c9dfa6b 111 break;
hkjung 14:3ecd0c9dfa6b 112
hkjung 14:3ecd0c9dfa6b 113 case FLAG_LED_OFF:
hkjung 15:3e15e08ff603 114 myled = LED_OFF; // LED OFF in WIZwiki-W7500
hkjung 16:c335303f3c69 115 pc.printf("WIZwiki: LED is turned off!\r\n");
hkjung 14:3ecd0c9dfa6b 116 break;
hkjung 15:3e15e08ff603 117
hkjung 15:3e15e08ff603 118 case FLAG_LED_BLINK:
hkjung 16:c335303f3c69 119 pc.printf("WIZwiki: LED starts blinking!\r\n");
hkjung 15:3e15e08ff603 120 break;
hkjung 14:3ecd0c9dfa6b 121
hkjung 14:3ecd0c9dfa6b 122 case FLAG_INVALID_CMD:
hkjung 14:3ecd0c9dfa6b 123 default:
hkjung 16:c335303f3c69 124 pc.printf("WIZwiki: Invalid command\r\n");
hkjung 14:3ecd0c9dfa6b 125 break;
hkjung 14:3ecd0c9dfa6b 126 }
hkjung 14:3ecd0c9dfa6b 127
hkjung 14:3ecd0c9dfa6b 128 // Reverse the message
hjjeon 11:0da8667a9201 129 char temp;
hjjeon 11:0da8667a9201 130 for(int f = 0, l = n-1; f<l; f++,l--){
hjjeon 11:0da8667a9201 131 temp = buffer[f];
hjjeon 11:0da8667a9201 132 buffer[f] = buffer[l];
hjjeon 11:0da8667a9201 133 buffer[l] = temp;
hkjung 14:3ecd0c9dfa6b 134 }
hjjeon 11:0da8667a9201 135
hkjung 14:3ecd0c9dfa6b 136 // Print reversed message to terminal
hkjung 16:c335303f3c69 137 pc.printf("Sending reversed message to Client: '%s'\r\n", buffer);
hkjung 16:c335303f3c69 138 pc.printf("\r\n");
hjjeon 11:0da8667a9201 139
hjjeon 11:0da8667a9201 140 // Echo received message back to client
hjjeon 11:0da8667a9201 141 client.send_all(buffer, n);
hjjeon 11:0da8667a9201 142 if (n <= 0) break;
hjjeon 11:0da8667a9201 143 }
hjjeon 11:0da8667a9201 144
hjjeon 11:0da8667a9201 145 client.close();
hkjung 14:3ecd0c9dfa6b 146 }
hkjung 14:3ecd0c9dfa6b 147 }