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
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
- WIZwiki-W7500 from WIZnet (Platform board with Ethernet I/F)
- USB cable
- LAN cable
- 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
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
Firmware
Command String
The following three command strings are implemented in this example project.
- LED_ON
- LED_OFF
- LED_BLINK
- LED blinks every 200 milliseconds.
- Implemented using mbed Ticker APIs
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 address | 192.168.0.21 |
| Subnet Mask | 255.255.255.0 |
| Gateway address | 192.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.
- Device's network settings and supported command strings.
- Default UART baudrate: 115200bps
5. Run the TCP client terminal program and Connect to your device.
6. Send command to your device and check status of LED and serial messages
Revision 14:3ecd0c9dfa6b, committed 2017-04-11
- Comitter:
- hkjung
- Date:
- Tue Apr 11 05:24:40 2017 +0000
- Parent:
- 13:31f4ecf83cca
- Child:
- 15:3e15e08ff603
- Commit message:
- Code modified / mbed and ethernet library updated
Changed in this revision
--- a/WIZnetInterface.lib Mon Jul 27 01:31:48 2015 +0000 +++ b/WIZnetInterface.lib Tue Apr 11 05:24:40 2017 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#24a9f2df2145 +http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#c91884bd2713
--- a/main.cpp Mon Jul 27 01:31:48 2015 +0000
+++ b/main.cpp Tue Apr 11 05:24:40 2017 +0000
@@ -1,23 +1,50 @@
#include "mbed.h"
#include "EthernetInterface.h"
-#define ECHO_SERVER_PORT 7
+#define MAC "\x00\x08\xDC\x55\x51\x52"
+#define IP "192.168.0.20"
+#define SUBNET "255.255.255.0"
+#define GATEWAY "192.168.0.1"
+
+#define ECHO_SERVER_PORT 5000
+
+//#define USE_DHCP
+
+#define FLAG_LED_ON 2
+#define FLAG_LED_OFF 1
+#define FLAG_INVALID_CMD 0
+
+#define LED_ON 0
+#define LED_OFF 1
DigitalOut myled(LED1);
-int compare_strings(char [], char []);
+ // LED control if received message matches pre-defined commands
+char cmd_ledon[] = {'L', 'E', 'D', '_', 'O', 'N', '\0'};
+char cmd_ledoff[] = {'L', 'E', 'D', '_', 'O', 'F', 'F', '\0'};
int main (void)
{
- myled = 1; // LED OFF in WIZwiki-W7500
- int flag=1;
+ myled = LED_OFF; // LED OFF in WIZwiki-W7500
+ int flag;
- printf("Wait a second...\r\n");
- uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x55, 0x51, 0x52};
+ printf("Wait a second...\r\n\r\n");
+
EthernetInterface eth;
- eth.init(mac_addr); //Use DHCP
+
+#ifdef USE_DHCP
+ eth.init(MAC); // Use DHCP
+#else
+ eth.init((uint8_t*)MAC, IP, SUBNET, GATEWAY); // Use Static IP address
+#endif
+
eth.connect();
- printf("Server IP Address is %s\r\n", eth.getIPAddress());
+
+ printf("Server IP Address is %s : %d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT);
+
+ printf("Command #1: %s\r\n", cmd_ledon);
+ printf("Command #2: %s\r\n", cmd_ledoff);
+ printf("\r\n");
TCPSocketServer server;
server.bind(ECHO_SERVER_PORT);
@@ -31,43 +58,56 @@
client.set_blocking(false, 15000); // Timeout after (1.5)s
printf("Connection from: %s\r\n", client.get_address());
+ printf("\r\n");
+
char buffer[256];
while (true) {
int n = client.receive(buffer, sizeof(buffer));
if (n <= 0) break;
- // print received message to terminal
+ // Print received message to terminal
buffer[n] = '\0';
- printf("Received message from Client :'%s'\r\n",buffer);
-
- // LED control if received message matches pre-defined command
- char command_buf[256] = {'L', 'E', 'D', '_', 'O', 'N', '\0'};
-
- char string[256];
- strcpy (string, command_buf);
- printf("Received command : %s\n", string);
-
- flag = compare_strings(buffer, command_buf);
-
- if (flag == 0) {
- myled = 0; // LED ON in WIZwiki-W7500
- printf("LED is turned on!\r\n");
- }
- else {
- myled = 1;
- printf("LED is turned off!\r\n");
+ printf("Received message from Client :'%s'\r\n",buffer);
+
+ // Compares the received string to predefined commands
+ if(strncmp(buffer, cmd_ledon, sizeof(buffer)) == 0) {
+ flag = FLAG_LED_ON;
+ } else if(strncmp(buffer, cmd_ledoff, sizeof(buffer)) == 0) {
+ flag = FLAG_LED_OFF;
+ } else {
+ flag = FLAG_INVALID_CMD;
}
- // reverse the message
+ // LED On/Off or Invalid command
+ switch(flag)
+ {
+ case FLAG_LED_ON:
+ myled = LED_ON; // LED ON in WIZwiki-W7500
+ printf("WIZwiki: LED is turned on!\r\n");
+ break;
+
+ case FLAG_LED_OFF:
+ myled = LED_OFF;
+ printf("WIZwiki: LED is turned off!\r\n");
+ break;
+
+ case FLAG_INVALID_CMD:
+ default:
+ printf("WIZwiki: Invalid command\r\n");
+ break;
+ }
+
+ // Reverse the message
char temp;
for(int f = 0, l = n-1; f<l; f++,l--){
temp = buffer[f];
buffer[f] = buffer[l];
buffer[l] = temp;
- }
+ }
- // print reversed message to terminal
- printf("Sending message to Client: '%s'\r\n",buffer);
+ // Print reversed message to terminal
+ printf("Sending reversed message to Client: '%s'\r\n", buffer);
+ printf("\r\n");
// Echo received message back to client
client.send_all(buffer, n);
@@ -75,22 +115,5 @@
}
client.close();
- }
-
-}
-
-int compare_strings(char a[], char b[])
-{
- int c = 0;
-
- while (a[c] == b[c]) {
- if (a[c] == '\0' || b[c] == '\0')
- break;
- c++;
- }
-
- if (a[c] == '\0' && b[c] == '\0')
- return 0;
- else
- return -1;
-}
+ }
+}
\ No newline at end of file
--- a/mbed.bld Mon Jul 27 01:31:48 2015 +0000 +++ b/mbed.bld Tue Apr 11 05:24:40 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file +https://mbed.org/users/mbed_official/code/mbed/builds/856d2700e60b \ No newline at end of file
