How to control relay through http string

Author: Published:2019-03-13 views:12

We have learned the communication protocol of the device KC868-H8. And we can control the relays of the device to open and close in LAN mode.


As mentioned earlier, there were some differences in the instruction parameters used in remote control, and today we will plug in the wings of remote control for the device, so that you can use the wings of remote control in any network environment.


It can control the device flexibly to open and close the relay switch, and read the input status. First of all, let's take a look at the communication principle, we used to control the device in the local area network, we set the network parameters of the device to "TCP server" mode, literally, the device is used as the server. The client connects with the device and communicates with each other in a point-to-point manner. But because the IP addresses we are assigned to by the ISP are dynamic when we access the public network, there is no way to make long-term connections between the two sides of the communication (unless the IP address of the connection you assign to the device is fixed). Therefore, we can build a bridge between the device and the client through the installation of cloud server, and communicate with each other remotely.


The client is connected to the cloud server through the network, and the hardware of the device is also connected to the cloud server. Therefore, the device and the client can communicate with each other through the cloud server.


Next, let's take a look at the differences between some of its network parameters and communication protocol instructions when the device is in remote control mode.


First, we modify the "work mode" in the network parameters of the device to "UDP mode" and set the "destination IP or domain name" column to our cloud server address "". "Destination port" is "5555", the device's "IP mode" recommendation can be set to "dynamic acquisition," that is, the router “DHCP” dynamically assigns the IP address, provided that the router is turning on the function, normally, most of the router is with the function on by default



After setting the network parameters and getting power on, the device will automatically connect to the cloud server, and when the connection is successful, the network light turns "green".



Then, we can use the commands to control the device.


Let's take the simplest example of sending a http command string through a web browser to turn on the first relay switch.


First, let's look at the command structure of sending http strings remotely.



Interface address:


Interface Parameter:

devicecode-KC868-H8/KC868-H32 the Serial number of the device

command-Communication Protocol commands to be delivered



HTTP Request:,1,1

Cloud server returns:

{"message": "command: relay-set-1,1,1 was sent to device:

56a890e6888793c97ed33f17 successfully.",

"result": "RELAY-SET-1,1,1,oku0000",

"status": "0"}


DeviceCode is the serial number of the device. There will be sticky QR code and serial number on the shell. If the serial number is not found, it can also be obtained by using the communication protocol to read the serial number.


What follow the Command is the protocol commands from the communication protocol we talked about earlier, including control relay switch, check relay status and read input status protocol content.


Note: when we are using the http protocol to remote the device, all the byte parameters including “package number” from the communication protocol will be self-assigned when the command is sending every time. The range is 1 to 255. If the parameter sent by the first command is “1”, the second will be “2”, the third will be “3” …... when the parameter is up to “255”, it will change back into “1” and loop again.


The below picture is a web browser to enter the command string, you can see the browser has the data return, while the lamp connects to the first relay of the device is lightening.



We make several examples for several common communication protocol commands, such as:


  1. Separatelycontrol one relay to open and close:

Send: RELAY-SET-x (1 byte pack_num), x (1 byte relay serial number), x (1 byte action 0 / 1) return: RELAY-SET-x (1 byte pack_num), X (1 byte relay serial number), x (1 byte action 0 / 1), ok/error


Remote control command string is:,1,1

It is indicating that the first relay is turned on. Note that the package number is “1”,1,0

It is indicating that the first relay is turned off.

Note: the package number is “2”, the self-increase has been carried out.


  1. Separately checkthe current switch status of a relay:

Send: RELAY-READ-x (1 byte pack_num), x (1 byte relay serial number)

Return: RELAY-READ-x (1 byte pack_num), x (1 byte relay serial number), X (1 byte status 0 / 1), OK/ERROR

Remote control command string is:,1

It is indicating that the first relay status is checking.

Note that the package number is “3”


  1. Checkingtrigger input status:

Send: RELAY-GET_INPUT-x (1 byte pack_num)

Return: RELAY-GET_INPUT-x (1 byte pack_num), x (1 byte status), OK/ERROR

Remote control command string is:

It is indicating that the status of the inputting of the device is checking

Note that the package number is “4”


  1. Controlling multiple relays once time:


Send: RELAY-SET_ALL-x (1byte pack_num), D0

Return: RELAY-SET_ALL-x (1byte pack_num), D0, OK/ERROR



Send: RELAY-SET_ALL-x(1byte pack_num),D3,D2,D1,D0

Return: RELAY-SET_ALL-x(1byte pack_num),D3,D2,D1,D0,OK/ERROR


Remote control command string is:,255

It is indicating that all the relays of the device are turned on.

Note: the package number is “5”


  1. Read the status of multiple relays once time:

Send: RELAY-STATE-x(1字节pack_num)


KC868-H32:RELAY-STATE-x(1byte pack_num),D0,OK/ERROR

KC868-H8:RELAY-STATE-x(1byte pack_num),D3,D2,D1,D0,OK/ERROR


Remote control command string is:

It is indicating that the current status of all the relays is checking now

Note: the package number is “6”


Till now, we have learned and mastered the methods of remote control and state inquiry of the device. We can realize remote control of remote devices by writing programs.

In future serial tutorials, We will give you an example of how to achieve intelligent linkage control by acquiring the state through the input.

This is also a more common application of smart home, but also the basic function of artificial intelligence.