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:
mbedAustin
Date:
Mon May 04 22:47:02 2015 +0000
Revision:
7:a5ead1402704
Parent:
3:36fd3cfad85a
Child:
8:f837e0d255e8
updated example code to be more verbose

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:38cbb854d85f 1 #include "mbed.h"
emilmont 1:5cebe0e38cd2 2 #include "EthernetInterface.h"
emilmont 1:5cebe0e38cd2 3
emilmont 3:36fd3cfad85a 4 #define ECHO_SERVER_PORT 7
emilmont 3:36fd3cfad85a 5
emilmont 1:5cebe0e38cd2 6 int main (void) {
emilmont 1:5cebe0e38cd2 7 EthernetInterface eth;
emilmont 1:5cebe0e38cd2 8 eth.init(); //Use DHCP
emilmont 1:5cebe0e38cd2 9 eth.connect();
mbedAustin 7:a5ead1402704 10 printf("\nServer IP Address is %s\n", eth.getIPAddress());
emilmont 1:5cebe0e38cd2 11
emilmont 1:5cebe0e38cd2 12 TCPSocketServer server;
emilmont 3:36fd3cfad85a 13 server.bind(ECHO_SERVER_PORT);
emilmont 3:36fd3cfad85a 14 server.listen();
emilmont 1:5cebe0e38cd2 15
emilmont 1:5cebe0e38cd2 16 while (true) {
emilmont 1:5cebe0e38cd2 17 printf("\nWait for new connection...\n");
emilmont 1:5cebe0e38cd2 18 TCPSocketConnection client;
emilmont 1:5cebe0e38cd2 19 server.accept(client);
emilmont 3:36fd3cfad85a 20 client.set_blocking(false, 1500); // Timeout after (1.5)s
emilmont 1:5cebe0e38cd2 21
emilmont 1:5cebe0e38cd2 22 printf("Connection from: %s\n", client.get_address());
emilmont 1:5cebe0e38cd2 23 char buffer[256];
emilmont 1:5cebe0e38cd2 24 while (true) {
emilmont 3:36fd3cfad85a 25 int n = client.receive(buffer, sizeof(buffer));
emilmont 1:5cebe0e38cd2 26 if (n <= 0) break;
emilmont 1:5cebe0e38cd2 27
mbedAustin 7:a5ead1402704 28 // print received message to terminal
mbedAustin 7:a5ead1402704 29 buffer[n] = '\0';
mbedAustin 7:a5ead1402704 30 printf("Received message from Client :'%s'\n",buffer);
mbedAustin 7:a5ead1402704 31
mbedAustin 7:a5ead1402704 32 // reverse the message
mbedAustin 7:a5ead1402704 33 char temp;
mbedAustin 7:a5ead1402704 34 for(int f = 0, l = n-1; f<l; f++,l--){
mbedAustin 7:a5ead1402704 35 temp = buffer[f];
mbedAustin 7:a5ead1402704 36 buffer[f] = buffer[l];
mbedAustin 7:a5ead1402704 37 buffer[l] = temp;
mbedAustin 7:a5ead1402704 38 }
mbedAustin 7:a5ead1402704 39
mbedAustin 7:a5ead1402704 40 // print reversed message to terminal
mbedAustin 7:a5ead1402704 41 printf("Sending message to Client: '%s'\n",buffer);
mbedAustin 7:a5ead1402704 42
mbedAustin 7:a5ead1402704 43 // Echo received message back to client
emilmont 2:ec5ae99791da 44 client.send_all(buffer, n);
emilmont 1:5cebe0e38cd2 45 if (n <= 0) break;
emilmont 1:5cebe0e38cd2 46 }
emilmont 3:36fd3cfad85a 47
emilmont 1:5cebe0e38cd2 48 client.close();
emilmont 1:5cebe0e38cd2 49 }
emilmont 1:5cebe0e38cd2 50 }