Linux Relay Controller using Netcat for Ethernet and WiFi

Linux users can easily control NCD Ethernet and WiFi relay boards using Netcat.  Netcat provides a convenient method for Linux users to send and receive TCP data over a network port, ideal for sensor monitoring and relay control applications.  Netcat is available for free download, users may read more about it here.  This tutorial will demonstrate the steps necessary to use Netcat for relay control applications.

Step 1: Finding the IP Address

First you need to know the controller’s IP address. The controller sends out a UDP broadcast on port 13000. You can listen for that broadcast through netcat by entering:
nc -u -l 13000
Once you see the UDP broadcast, you can see the IP address of the controller and it’s mac address.   In our sample, we will use 192.168.1.42 and port 2101 as our default port for communications to the ethernet relay board.  Note that your IP address may be different, but the port (2101) is likely to be the same.

Step 2: Sending Relay Control Commands

If you have seen our other tutorials, you have likely seen the tables we use are in decimal format, as shown below:

TX: Function: (Decimal Version) RX:
170 3 254 108 1 24 Turn On Relay 1 in Bank 1 170 1 85 0
170 3 254 109 1 25 Turn On Relay 2 in Bank 1 170 1 85 0
170 3 254 110 1 26 Turn On Relay 3 in Bank 1 170 1 85 0
170 3 254 111 1 27 Turn On Relay 4 in Bank 1 170 1 85 0
170 3 254 100 1 16 Turn Off Relay 1 in Bank 1 170 1 85 0
170 3 254 101 1 17 Turn Off Relay 2 in Bank 1 170 1 85 0
170 3 254 102 1 18 Turn Off Relay 3 in Bank 1 170 1 85 0
170 3 254 103 1 19 Turn Off Relay 4 in Bank 1 170 1 85 0

Netcat prefers working with Hex values, so we need to convert the commands from decimal to hex.  Below is a Hex version of the same table:

TX: Function: (Hex Version) RX:
AA 03 FE 6C 01 18 Turn On Relay 1 in Bank 1 AA 01 55 00
AA 03 FE 6D 01 19 Turn On Relay 2 in Bank 1 AA 01 55 00
AA 03 FE 6E 01 1A Turn On Relay 3 in Bank 1 AA 01 55 00
AA 03 FE 6F 01 1B Turn On Relay 4 in Bank 1 AA 01 55 00
AA 03 FE 64 01 10 Turn Off Relay 1 in Bank 1 AA 01 55 00
AA 03 FE 65 01 11 Turn Off Relay 2 in Bank 1 AA 01 55 00
AA 03 FE 66 01 12 Turn Off Relay 3 in Bank 1 AA 01 55 00
AA 03 FE 67 01 13 Turn Off Relay 4 in Bank 1 AA 01 55 00

The commands found in the table above are now converted to Netcat commands:

echo -e ‘\xAA\x03\xFE\x6C\x01\x18’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x6D\x01\x19’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x6E\x01\x1A’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x6F\x01\x1B’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x64\x01\x10’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x65\x01\x11’ | nc 192.168.1.42 2101
echo -e ‘\xAA\x03\xFE\x66\x01\x12’ | nc 192.168.1.42 2101
​echo -e ‘\xAA\x03\xFE\x67\x01\x13’ | nc 192.168.1.42 2101

If you have problems try changing `-e` to `-en`, netcat differs slightly on different systems.