<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[Smart Home Automation Forum - KC868-ATC / Tuya adapter V2]]></title>
		<link>https://www.kincony.com/forum/</link>
		<description><![CDATA[Smart Home Automation Forum - https://www.kincony.com/forum]]></description>
		<pubDate>Tue, 21 Apr 2026 15:37:58 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[KinCony ESP32 Tuya IoT adapter]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=9200</link>
			<pubDate>Thu, 02 Apr 2026 15:22:11 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=79">athxp</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=9200</guid>
			<description><![CDATA[Hi, I have two H32L along with two KinCony ESP32 Tuya IoT adapters. I use everything on Home Assistant, and it works well via "local tuya" integration (and a Tuya developer project). However, if I reboot the system, the two KinCony ESP32 Tuya IoT adapters don't connect and must be rebooted AFTER the router to connect. I'd like to port everything to "tuya local" integration, which is 100% local. Is that possible? Thanks as always]]></description>
			<content:encoded><![CDATA[Hi, I have two H32L along with two KinCony ESP32 Tuya IoT adapters. I use everything on Home Assistant, and it works well via "local tuya" integration (and a Tuya developer project). However, if I reboot the system, the two KinCony ESP32 Tuya IoT adapters don't connect and must be rebooted AFTER the router to connect. I'd like to port everything to "tuya local" integration, which is 100% local. Is that possible? Thanks as always]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[adaptor V2 and KC868 h32bs]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=8840</link>
			<pubDate>Tue, 23 Dec 2025 05:58:49 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=10096">Gunnuck1306</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=8840</guid>
			<description><![CDATA[hello<br />
<br />
can you send me step by step, how to setup tuya adaptor V2 and KC868 h32bs.<br />
<br />
Thank you]]></description>
			<content:encoded><![CDATA[hello<br />
<br />
can you send me step by step, how to setup tuya adaptor V2 and KC868 h32bs.<br />
<br />
Thank you]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[KC868-ATC v1.4 connect to H32 rev 2.3]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=7408</link>
			<pubDate>Sun, 19 Jan 2025 00:23:07 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=7347">SrgX13</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=7408</guid>
			<description><![CDATA[Hello,<br />
I have been trying for a few days to set up the KC868-ATC v1.4 with Tuya module to connect to KC868-H32 rev 2.3 and control the relays. I managed to set the KC868-ATC  Tuya module and connect using Tuya mobile App (Android), however the button/switches are not responding. <br />
The weird part is that I can connect to  KC868-H32 using Vircom and the IP seems to be set to 192.168.2.160 (we are using 192.168.2.x ranges). From what I have seen in the referred videos is that I should also be able to connect using the browser however this is failing.  KC868-H32 is connected to the router through Ethernet cable<br />
<br />
I have even tried to set  KC868-ATC v1.4 to use RS232 instead of wifi. This had the same negative results.<br />
<br />
If you require more information please let me know.<br />
Please help me with some ideas how to set this system. I assume that I might have to change the firmware on KC868-H32 as in Tuya it was showing H32B PRO<br />
Thank you very much and I wish you a great day]]></description>
			<content:encoded><![CDATA[Hello,<br />
I have been trying for a few days to set up the KC868-ATC v1.4 with Tuya module to connect to KC868-H32 rev 2.3 and control the relays. I managed to set the KC868-ATC  Tuya module and connect using Tuya mobile App (Android), however the button/switches are not responding. <br />
The weird part is that I can connect to  KC868-H32 using Vircom and the IP seems to be set to 192.168.2.160 (we are using 192.168.2.x ranges). From what I have seen in the referred videos is that I should also be able to connect using the browser however this is failing.  KC868-H32 is connected to the router through Ethernet cable<br />
<br />
I have even tried to set  KC868-ATC v1.4 to use RS232 instead of wifi. This had the same negative results.<br />
<br />
If you require more information please let me know.<br />
Please help me with some ideas how to set this system. I assume that I might have to change the firmware on KC868-H32 as in Tuya it was showing H32B PRO<br />
Thank you very much and I wish you a great day]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[KC868-ATC Tuya HRC Zehner rs232 integration]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=6984</link>
			<pubDate>Sun, 03 Nov 2024 20:47:40 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=6447">PHNijborg</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=6984</guid>
			<description><![CDATA[Hello,<br />
<br />
I have purchased the KC868-ATC V1.4 and want to use the rs232 functionality to control a Heat recovery system.<br />
I use the tuya app to control my house, however i am not sure how to convert this app and KC868-ATC to the full protocol functionallity in the pdf...<br />
<br />
Anyone any ideas?<br />
<br />
I have a tuya developer account and Arduino IDE, i have created a IDE code, but doesn't work, the connection to tuya is missing i think...<br /><!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/pdf.png" title="Adobe Acrobat PDF" border="0" alt=".pdf" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=6346" target="_blank" title="">Protocol_Eng.pdf</a> (Size: 1.34 MB / Downloads: 1694)
<!-- end: postbit_attachments_attachment -->]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
I have purchased the KC868-ATC V1.4 and want to use the rs232 functionality to control a Heat recovery system.<br />
I use the tuya app to control my house, however i am not sure how to convert this app and KC868-ATC to the full protocol functionallity in the pdf...<br />
<br />
Anyone any ideas?<br />
<br />
I have a tuya developer account and Arduino IDE, i have created a IDE code, but doesn't work, the connection to tuya is missing i think...<br /><!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/pdf.png" title="Adobe Acrobat PDF" border="0" alt=".pdf" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=6346" target="_blank" title="">Protocol_Eng.pdf</a> (Size: 1.34 MB / Downloads: 1694)
<!-- end: postbit_attachments_attachment -->]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Tuya adapter V2 arduino source code - control KinCony KC868-Hx series relay board]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=5844</link>
			<pubDate>Mon, 03 Jun 2024 08:28:32 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=1">admin</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=5844</guid>
			<description><![CDATA[these BIN file firmware and arduino source code work for KinCony KC868-Hx series relay board:<br />
KC868-H32, KC868-H32L, KC868-H32W, KC868-H32BS, KC868-H32B<br />
KC868-H16, KC868-H16W, KC868-H16B<br />
KC868-H8, KC868-H8W, KC868-H8B<br />
<br />
you not need connect any cable between Tuya adapter v2 and relay board. Because of it use by TCP connection, it use network. <br />
fimrware download:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5336" target="_blank" title="">H8x_0x0.zip</a> (Size: 478.35 KB / Downloads: 622)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5337" target="_blank" title="">H16x_0x0.zip</a> (Size: 478.44 KB / Downloads: 586)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5338" target="_blank" title="">H32x_0x0.zip</a> (Size: 478.7 KB / Downloads: 604)
<!-- end: postbit_attachments_attachment --><br />
download file and unzip, then download BIN file by ESP DOWNLOAD TOOL, write code at 0x0 address.<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5329" target="_blank" title="">download-firmware.png</a> (Size: 41.47 KB / Downloads: 1002)
<!-- end: postbit_attachments_attachment --><br />
ESP DOWNLOAD TOOL download link:<br />
<a href="https://www.kincony.com/wp-content/uploads/2022/08/flash_download_tool_3.9.2.zip" target="_blank" rel="noopener" class="mycode_url">https://www.kincony.com/wp-content/uploa..._3.9.2.zip</a><br />
if you want modify arduino source code, which relay board model to use, just uncomment the model command line.<br />
first time run code, you need to set relay board's IP address, port, your router's ssid and password for Tuya adapter v2 by usb-serial port.<br />
just send command as this: SSID:KinCony,PSW:a12345678,192.168.3.141,4196<br />
serial port baud rate: 115200bps<br />
serial port debug tool download:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5335" target="_blank" title="">UartAssist.zip</a> (Size: 468.4 KB / Downloads: 652)
<!-- end: postbit_attachments_attachment --><br />
here is arduino source code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>// USB set command     SSID:XXXXX,PSW:XXXXX,X.X.X.X,YYYY<br />
// X.X.X.X is relay board IP address,  YYYY is relay board port.  SSID and PSW is your router's ssid and password<br />
// for example         SSID:KinCony,PSW:a12345678,192.168.3.141,4196<br />
// make sure your H8,H16,H32 relay board work with "TCP Server" mode.<br />
#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include &lt;Preferences.h&gt;<br />
<br />
#include "HardwareSerial.h"<br />
<br />
///ADD TCP CLIENT FOR TUYA ADAPT<br />
//#include &lt;WiFiUdp.h&gt; <br />
#include &lt;WiFi.h&gt;<br />
#include &lt;WiFiClient.h&gt;<br />
<br />
//uncomment the relay board model you are using<br />
//#define KINCONY_H8X    ////  <br />
//#define KINCONY_H16X    ////<br />
#define KINCONY_H32X    ////<br />
<br />
#if defined(KINCONY_H8X)<br />
#define RLY_NUMS  8<br />
#elif defined(KINCONY_H16X)<br />
#define RLY_NUMS  16<br />
#elif defined(KINCONY_H32X)<br />
#define RLY_NUMS  32<br />
#endif<br />
<br />
//---------RS485 setting<br />
#define DEBUG   1<br />
<br />
#define SERIAL_BAUD   9600<br />
#define RX_PIN    32<br />
#define TX_PIN    33<br />
#define ADAPT485_NUM  2<br />
#define SLAVE_ADDR  1<br />
#define READ_ADDR  0 <br />
#define READ_NUM   32  <br />
<br />
////SERIAL COMMAND IS<br />
////SSID,<br />
char ssid_buf[30] = {0};<br />
char password_buf[20] = {0};<br />
char server_ip[20] = {0};<br />
uint16_t server_port = 4096;<br />
uint8_t str[]={"client_me"};<br />
//uint8_t Rece[256]={0};<br />
uint8_t Send[128]={0};<br />
Preferences prefs;<br />
<br />
//IPAddress staticIP(192, 168, 10, 12);<br />
//IPAddress gateway(192, 168, 10, 1);<br />
//IPAddress subnet(255, 255, 255, 0);<br />
//IPAddress dns(192, 168, 10,1);<br />
<br />
WiFiClient client;<br />
typedef enum{INIT_STATUS=0,RDY_STATUS,CONNECT_STATUS,TCP_COMM,STOP_STATUS,}STATUS_MACHINE;<br />
<br />
static int statusMachine = INIT_STATUS;<br />
 <br />
static void InitWifi(void)<br />
{<br />
  //WiFi.mode(WIFI_STA);     <br />
  //WiFi.setSleep(false); // disable STA mode wifi sleep mode<br />
  /*if(WiFi.config(staticIP, gateway, subnet, dns, dns)==false)<br />
  {<br />
    Serial1.println("Configuration failed.");<br />
  }*/<br />
  /*IPAddress ip = WiFi.localIP(); <br />
  Serial1.print("IP address: ");      <br />
  Serial1.println(WiFi.localIP()); */  <br />
<br />
  Serial1.print("rcv SSID is: ");  <br />
  Serial1.println(ssid_buf);<br />
  Serial1.print("rcv PSW is: ");  <br />
  Serial1.println(password_buf);    <br />
  Serial1.print("rcv server ip is: ");  <br />
  Serial1.println(server_ip);<br />
  Serial1.print("rcv server port is: ");  <br />
  Serial1.println(server_port);<br />
    <br />
  WiFi.begin(ssid_buf, password_buf);//link wifi hotspot <br />
  Serial1.println("WiFi try to connect~");<br />
  statusMachine = RDY_STATUS;<br />
}<br />
<br />
static bool bFlagConnWifi = false;<br />
static bool bFlagConnTcp = false;<br />
static String serial_cmd_rcv = "";<br />
static String tcp_cmd_rcv = "";<br />
static char command_str_arr[30]={0};<br />
bool rt_coils_status[RLY_NUMS] = {0}; <br />
void dp_update_all(void);<br />
<br />
////online check for wifi&amp;tcp connections for 500ms<br />
static void SetRlyByChannel(uint8_t ch,bool value)<br />
{<br />
  memset(command_str_arr,0,sizeof(command_str_arr));<br />
  sprintf(command_str_arr,"RELAY-SET-255,%d,%d",ch+1,value?1:0);<br />
  return;<br />
}<br />
static void ReadAllRlyStatus()<br />
{<br />
  memset(command_str_arr,0,sizeof(command_str_arr));<br />
  memcpy(command_str_arr,(char*)"RELAY-STATE-255",15); <br />
  return;<br />
}<br />
<br />
static void WifiConnect(void)<br />
{<br />
  if (WiFi.status() != WL_CONNECTED) //wait wifi connection success<br />
  { <br />
    Serial1.print(".");<br />
    //WiFi.begin(ssid_buf, password_buf);//link wifi hotspot<br />
    //Serial1.println("WiFi try to connect~");<br />
    bFlagConnWifi = bFlagConnTcp = false;<br />
    return ;<br />
  }<br />
  bFlagConnWifi = true;<br />
  statusMachine = CONNECT_STATUS;<br />
  Serial1.println("WiFi connected");<br />
  <br />
   <br />
}<br />
<br />
static void TcpServerConn(void)<br />
{<br />
  if(WiFi.status() != WL_CONNECTED)<br />
  {<br />
    if(bFlagConnTcp) client.stop();<br />
    bFlagConnWifi = bFlagConnTcp = false;<br />
    statusMachine = RDY_STATUS;<br />
    Serial1.println("WiFi exception 0");<br />
    return;<br />
  }<br />
 <br />
  if (!bFlagConnTcp)//begin connect to tcp server///if (client.connected() != 0)<br />
  {    <br />
      if (!client.connect(server_ip, server_port,2000))<br />
      {<br />
          Serial1.println("try to connect host....");<br />
          bFlagConnTcp = false;<br />
          //delay(5);<br />
          <br />
      }<br />
      else <br />
      {<br />
        bFlagConnTcp = true;<br />
        statusMachine = TCP_COMM;<br />
        Serial1.println("TCP connected Succeed"); <br />
      }<br />
  } <br />
   <br />
}<br />
<br />
static void SendPeriodCheckCommand(void)<br />
{<br />
  if(WiFi.status() != WL_CONNECTED)<br />
  {<br />
    if(bFlagConnTcp) client.stop();<br />
    bFlagConnWifi = bFlagConnTcp = false;<br />
    statusMachine = RDY_STATUS;<br />
    Serial1.println("WiFi exception 1");<br />
    return;<br />
  }<br />
  if(client.connected())<br />
  Serial1.println("send period check cmd~");<br />
  statusMachine = TCP_COMM;<br />
  ReadAllRlyStatus();<br />
  //SetRlyByChannel();<br />
  client.write(command_str_arr,strlen(command_str_arr));<br />
}<br />
<br />
static void GetCoilStatus(String data)<br />
{///ACK STANDARD FORMAT IS "RELAY-STATE-255,D1,D0,OK" ///16x<br />
////ACK STANDARD FORMAT IS "RELAY-STATE-255,D0,OK"  ///8x<br />
////ACK STANDARD FORMAT IS "RELAY-STATE-255,D3,D2,D1,D0,OK" ///32x<br />
  <br />
   String rcv_data = data;<br />
   if(rcv_data.substring(15,16) != "," ) return;<br />
   <br />
   uint8_t rlys_arr[4] = {0};<br />
   uint32_t rly_status = 0;<br />
   rcv_data = rcv_data.substring(16);<br />
   //Serial1.println("Remain strs1:" + rcv_data);<br />
   int index_find = rcv_data.indexOf(',');<br />
   rlys_arr[0] = atoi(rcv_data.substring(0,index_find).c_str());<br />
    <br />
   #if defined(KINCONY_H16X) || defined(KINCONY_H32X)<br />
   rcv_data = rcv_data.substring(index_find+1);<br />
   //Serial1.println("Remain strs2:" + rcv_data);<br />
   index_find = rcv_data.indexOf(',');<br />
   rlys_arr[1] = atoi(rcv_data.substring(0,index_find).c_str());<br />
   #endif<br />
   #if defined(KINCONY_H32X)<br />
   rcv_data = rcv_data.substring(index_find+1);<br />
   index_find = rcv_data.indexOf(','); <br />
   rlys_arr[2] = atoi(rcv_data.substring(0,index_find).c_str());<br />
   <br />
   rcv_data = rcv_data.substring(index_find+1);<br />
   index_find = rcv_data.indexOf(','); <br />
   rlys_arr[3] = atoi(rcv_data.substring(0,index_find).c_str());<br />
   #endif <br />
   <br />
   #if defined(KINCONY_H8X)<br />
  rly_status = rlys_arr[0];<br />
   #elif defined(KINCONY_H16X)<br />
  rly_status = (rlys_arr[0]&lt;&lt;8 ) | rlys_arr[1];<br />
   #else<br />
  rly_status = (rlys_arr[3]&lt;&lt;24 ) | (rlys_arr[2]&lt;&lt;16 )| (rlys_arr[1]&lt;&lt;8 )| (rlys_arr[0] );<br />
   #endif<br />
   <br />
<br />
   Serial1.print("rly_status = 0x");<br />
   Serial1.println(rly_status,HEX);<br />
   <br />
   for(int i=0;i&lt;RLY_NUMS;i++)<br />
   { <br />
     rt_coils_status[i] = (rly_status&amp;(1&lt;&lt;i))?true:false;<br />
   }<br />
}<br />
<br />
static char rece_tcp_buf[100] = {0};<br />
<br />
static bool CheckClientRcv(void)<br />
{<br />
  uint8_t i=0;<br />
  if(client.available() == 0) return false;<br />
  memset(rece_tcp_buf,0,sizeof(rece_tcp_buf));<br />
  <br />
  tcp_cmd_rcv = "";<br />
   while (client.available()&gt;0)  //whether received data from server<br />
   {<br />
       rece_tcp_buf[i++] = client.read();//receive data from server<br />
    //tcp_cmd_rcv += client.read();//store data from server<br />
    delay(1);<br />
   }<br />
   <br />
   tcp_cmd_rcv = String(rece_tcp_buf);<br />
      <br />
   Serial1.print("Ack:");<br />
   Serial1.println(tcp_cmd_rcv.c_str());<br />
   if (tcp_cmd_rcv.length() &lt; 22 || <br />
      tcp_cmd_rcv.substring(0,15) != "RELAY-STATE-255" )  {tcp_cmd_rcv="";return false;}<br />
  //Serial1.println("OKAY COILS VALUE");  <br />
  GetCoilStatus(tcp_cmd_rcv);<br />
  tcp_cmd_rcv="";<br />
  return true;<br />
}<br />
<br />
 <br />
static bool ProtocolDeal(const char* buf)<br />
{<br />
  char deal_str[100] = {0};<br />
  char tmp[30];<br />
  char *pos,*pos1,*pos2,*pos3;<br />
<br />
  memcpy(deal_str,buf,strlen(buf)); <br />
<br />
  Serial1.println("check1");<br />
  <br />
  if ((pos = strchr(deal_str,','))!=0)    //find','<br />
  {<br />
    *pos = '&#92;0';    //","replace with ‘&#92;0’<br />
    pos++;<br />
    pos1 = pos;<br />
    memset(tmp,0,30);<br />
    //memcpy(tmp,buf,strlen(buf));<br />
    strcpy(tmp,deal_str);<br />
    if(tmp[0]=='S' &amp;&amp; tmp[1]=='S' &amp;&amp; tmp[2]=='I' &amp;&amp; tmp[3]=='D' &amp;&amp; <br />
      tmp[4]==':')  <br />
    {<br />
      memset(ssid_buf,0,sizeof(ssid_buf));<br />
      memcpy(ssid_buf,&amp;tmp[5],strlen(tmp)-5);<br />
      Serial1.println("rcv ssid ok.");<br />
    }     <br />
  }<br />
  else <br />
    {<br />
      Serial1.println("end1"); <br />
      return  false; <br />
      <br />
    }<br />
<br />
  Serial1.println("check2");<br />
  if ((pos = strchr(pos1,','))!=0)    //find','<br />
  {<br />
    *pos = '&#92;0';    //","replace with‘&#92;0’<br />
    pos++;<br />
    pos2 = pos;<br />
    memset(tmp,0,30);<br />
    //memcpy(tmp,pos1,strlen(pos1));<br />
    strcpy(tmp,pos1);<br />
    if(tmp[0]=='P' &amp;&amp; tmp[1]=='S' &amp;&amp; tmp[2]=='W' &amp;&amp; tmp[3]==':' )  <br />
    {<br />
      memset(password_buf,0,sizeof(password_buf));<br />
      memcpy(password_buf,&amp;tmp[4],strlen(tmp)-4);<br />
      Serial1.println("rcv psw ok.");<br />
    }  <br />
  }<br />
  else{<br />
    return false; <br />
    Serial1.println("end2");}<br />
    <br />
  Serial1.println("check3");<br />
  if ((pos = strchr(pos2,','))!=0)    //find','  IP:XXXX,PORT:XXXX<br />
  {<br />
    *pos = '&#92;0';    //","replace with‘&#92;0’<br />
    pos++;<br />
    pos3 = pos;<br />
    memset(server_ip,0,sizeof(server_ip));<br />
    memcpy(server_ip,pos2,strlen(pos2));<br />
  <br />
    server_port = atoi(pos3);<br />
<br />
    Serial1.print("rcv SSID is: ");  <br />
    Serial1.println(ssid_buf);<br />
    Serial1.print("rcv PSW is: ");  <br />
    Serial1.println(password_buf);     <br />
    Serial1.print("rcv server ip is: ");  <br />
    Serial1.println(server_ip);<br />
    Serial1.print("rcv server port is: ");  <br />
    Serial1.println(server_port);<br />
    <br />
    /*bFlagConnWifi = bFlagConnTcp = false;<br />
    statusMachine = INIT_STATUS; <br />
     <br />
    if(client.connected())<br />
    {<br />
      client.stop(); <br />
    }<br />
    <br />
    if (WiFi.status() == WL_CONNECTED) //等待WiFi热点连接成功<br />
    { <br />
      Serial1.print("stop wifi second.");<br />
      WiFi.disconnect();<br />
      delay(3000);<br />
    }*/<br />
<br />
    ///Preferences param here...<br />
    prefs.begin("myns_netcfg");<br />
    prefs.putString("ns_ssid",ssid_buf);<br />
    prefs.putString("ns_psw",password_buf);<br />
    prefs.putString("ns_ip",server_ip);<br />
    prefs.putUShort("ns_port",server_port);<br />
   <br />
    prefs.end();<br />
    ESP.restart();<br />
  }<br />
  else<br />
    {<br />
    return false; <br />
    Serial1.println("end3");<br />
    }<br />
<br />
  <br />
}<br />
<br />
static void GetSerialCmd() <br />
{ ////format: SSID:REALTIME2SENS-2.4G,PSW:12345678,IP:192.168.1.100,PORT:4196<br />
  if(Serial1.available() == 0) return;<br />
  serial_cmd_rcv = "";<br />
  while (Serial1.available() &gt; 0)<br />
  {<br />
    serial_cmd_rcv += char(Serial1.read());<br />
    delay(1);  <br />
  } <br />
  Serial1.println(serial_cmd_rcv);<br />
  if (serial_cmd_rcv.length() &lt; 36 || <br />
      serial_cmd_rcv.substring(0,4) != "SSID" )  {serial_cmd_rcv="";return;}<br />
 <br />
  ProtocolDeal(serial_cmd_rcv.c_str()); <br />
  <br />
  serial_cmd_rcv = ""; <br />
}<br />
<br />
/* Current device DP values */<br />
<br />
<br />
HardwareSerial My485Serial(ADAPT485_NUM);<br />
 <br />
<br />
//---RS485 setting end----<br />
<br />
TuyaWifi my_device;<br />
<br />
<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
<br />
/* Data point define */<br />
typedef enum<br />
{<br />
  DPID_SWITCH01 = 1,<br />
  DPID_SWITCH02,DPID_SWITCH03,<br />
  DPID_SWITCH04,DPID_SWITCH05,DPID_SWITCH06,<br />
  <br />
  DPID_SWITCH07 = 101,<br />
  DPID_SWITCH08,DPID_SWITCH09,DPID_SWITCH10,<br />
  DPID_SWITCH11,DPID_SWITCH12,DPID_SWITCH13,<br />
  DPID_SWITCH14,DPID_SWITCH15,DPID_SWITCH16,<br />
   <br />
  DPID_SWITCH17,DPID_SWITCH18,DPID_SWITCH19,<br />
  DPID_SWITCH20,DPID_SWITCH21,DPID_SWITCH22,<br />
  DPID_SWITCH23,DPID_SWITCH24,DPID_SWITCH25,<br />
  <br />
  DPID_SWITCH26,DPID_SWITCH27,DPID_SWITCH28,<br />
  DPID_SWITCH29,DPID_SWITCH30,DPID_SWITCH31,<br />
  DPID_SWITCH32,<br />
<br />
}HXBS_DPID_DEF; <br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_String, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
#if defined(KINCONY_H16X)<br />
unsigned char dp_array[][2] =<br />
{ <br />
  {DPID_SWITCH01, DP_TYPE_BOOL},{DPID_SWITCH02, DP_TYPE_BOOL},<br />
  {DPID_SWITCH03, DP_TYPE_BOOL},{DPID_SWITCH04, DP_TYPE_BOOL},<br />
  {DPID_SWITCH05, DP_TYPE_BOOL},{DPID_SWITCH06, DP_TYPE_BOOL},<br />
  {DPID_SWITCH07, DP_TYPE_BOOL},{DPID_SWITCH08, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH09, DP_TYPE_BOOL},{DPID_SWITCH10, DP_TYPE_BOOL},<br />
  {DPID_SWITCH11, DP_TYPE_BOOL},{DPID_SWITCH12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH13, DP_TYPE_BOOL},{DPID_SWITCH14, DP_TYPE_BOOL},<br />
  {DPID_SWITCH15, DP_TYPE_BOOL},{DPID_SWITCH16, DP_TYPE_BOOL}, <br />
};<br />
<br />
unsigned char pid[] = {"7en1hzfnjtvprt35"}; ////change pid here....<br />
 <br />
#elif defined(KINCONY_H8X)<br />
unsigned char dp_array[][2] =<br />
{ <br />
  {DPID_SWITCH01, DP_TYPE_BOOL},{DPID_SWITCH02, DP_TYPE_BOOL},<br />
  {DPID_SWITCH03, DP_TYPE_BOOL},{DPID_SWITCH04, DP_TYPE_BOOL},<br />
  {DPID_SWITCH05, DP_TYPE_BOOL},{DPID_SWITCH06, DP_TYPE_BOOL},<br />
  {DPID_SWITCH07, DP_TYPE_BOOL},{DPID_SWITCH08, DP_TYPE_BOOL},  <br />
};<br />
<br />
unsigned char pid[] = {"zyauvyer5insa2ne"}; ////change pid here....<br />
 <br />
#elif defined(KINCONY_H32X)<br />
unsigned char dp_array[][2] =<br />
{ <br />
  {DPID_SWITCH01, DP_TYPE_BOOL},{DPID_SWITCH02, DP_TYPE_BOOL},<br />
  {DPID_SWITCH03, DP_TYPE_BOOL},{DPID_SWITCH04, DP_TYPE_BOOL},<br />
  {DPID_SWITCH05, DP_TYPE_BOOL},{DPID_SWITCH06, DP_TYPE_BOOL},<br />
  {DPID_SWITCH07, DP_TYPE_BOOL},{DPID_SWITCH08, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH09, DP_TYPE_BOOL},{DPID_SWITCH10, DP_TYPE_BOOL},<br />
  {DPID_SWITCH11, DP_TYPE_BOOL},{DPID_SWITCH12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH13, DP_TYPE_BOOL},{DPID_SWITCH14, DP_TYPE_BOOL},<br />
  {DPID_SWITCH15, DP_TYPE_BOOL},{DPID_SWITCH16, DP_TYPE_BOOL}, <br />
<br />
  {DPID_SWITCH17, DP_TYPE_BOOL},{DPID_SWITCH18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH19, DP_TYPE_BOOL},{DPID_SWITCH20, DP_TYPE_BOOL},<br />
  {DPID_SWITCH21, DP_TYPE_BOOL},{DPID_SWITCH22, DP_TYPE_BOOL},<br />
  {DPID_SWITCH23, DP_TYPE_BOOL},{DPID_SWITCH24, DP_TYPE_BOOL}, <br />
<br />
  {DPID_SWITCH25, DP_TYPE_BOOL},{DPID_SWITCH26, DP_TYPE_BOOL},<br />
  {DPID_SWITCH27, DP_TYPE_BOOL},{DPID_SWITCH28, DP_TYPE_BOOL},<br />
  {DPID_SWITCH29, DP_TYPE_BOOL},{DPID_SWITCH30, DP_TYPE_BOOL},<br />
  {DPID_SWITCH31, DP_TYPE_BOOL},{DPID_SWITCH32, DP_TYPE_BOOL}, <br />
};<br />
<br />
unsigned char pid[] = {"ytgvxhy4zoaxdqtp"}; ////change pid here....<br />
<br />
#endif<br />
<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
  <br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  int16_t index_out_coil = -1;<br />
  unsigned char get_down_cmd = false;<br />
  <br />
  for(int16_t i=0;i&lt;RLY_NUMS;i++)<br />
  {<br />
    if(dpid == dp_array[i][0]) <br />
    {<br />
      index_out_coil = i;<br />
      break;<br />
    }<br />
  }<br />
<br />
    #if DEBUG<br />
    Serial1.print("Rcv Command:&lt;dpid,");<br />
    Serial1.print(dpid,DEC); <br />
    Serial1.print(" value,");<br />
    Serial1.print(value[0],DEC);<br />
    Serial1.print(" len,");<br />
    Serial1.print(length,DEC);<br />
    Serial1.println("&gt; ");<br />
  #endif<br />
  <br />
  if(index_out_coil &lt; 0) return TY_ERROR;<br />
<br />
<br />
    <br />
  get_down_cmd = my_device.mcu_get_dp_download_data(dpid,value,length);<br />
<br />
  #if DEBUG<br />
    Serial1.print("get_down_cmd value is:&lt;");<br />
    Serial1.print(get_down_cmd,DEC);  <br />
    Serial1.println("&gt; ");<br />
  #endif<br />
  <br />
  SetRlyByChannel(index_out_coil,get_down_cmd?true:false);<br />
  Serial1.print("Send board control command:");<br />
  Serial1.println(command_str_arr);<br />
  uint16_t result = client.write(command_str_arr,strlen(command_str_arr));  <br />
  #if DEBUG<br />
    Serial1.print("client WriteCoilAddr&lt;");<br />
    Serial1.print(index_out_coil,DEC);<br />
    Serial1.print("&gt; result:&lt;");<br />
    Serial1.print(result,DEC);  <br />
    Serial1.println("&gt; ");<br />
  #endif<br />
  <br />
  my_device.mcu_dp_update(dpid, value, length);<br />
  return TY_SUCCESS;<br />
}<br />
<br />
void setup() <br />
{<br />
<br />
  My485Serial.begin(SERIAL_BAUD, SERIAL_8N1, RX_PIN, TX_PIN); //RS485 serial port<br />
   <br />
  Serial.begin(9600,SERIAL_8N1,22,19);  //Tuya module serial port<br />
  Serial1.begin(115200,SERIAL_8N1,3,1);   //USB serial port<br />
<br />
  <br />
  // Serial with tuyawifi<br />
  <br />
  <br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 2);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
  <br />
  //delay(300);<br />
  last_time = millis();<br />
<br />
<br />
  ///Preferences param here...<br />
  prefs.begin("myns_netcfg");<br />
  String ns_ssid = prefs.getString("ns_ssid","123");<br />
  String ns_psw = prefs.getString("ns_psw","123");<br />
  String ns_ip = prefs.getString("ns_ip","127.0.0.1");<br />
  server_port = prefs.getUShort("ns_port",4096);<br />
<br />
  memcpy(ssid_buf,ns_ssid.c_str(),ns_ssid.length());<br />
  memcpy(password_buf,ns_psw.c_str(),ns_psw.length());<br />
  memcpy(server_ip,ns_ip.c_str(),ns_ip.length()); <br />
  prefs.end();<br />
<br />
  WiFi.mode(WIFI_STA);     //set to STA mode<br />
}<br />
<br />
static void FuncStatusMachine(int status)<br />
{<br />
  switch(status)<br />
  {<br />
    case INIT_STATUS:<br />
    InitWifi();break;<br />
    case RDY_STATUS:<br />
    WifiConnect();break;<br />
    case CONNECT_STATUS:<br />
    TcpServerConn();break;<br />
    case TCP_COMM:<br />
    SendPeriodCheckCommand();break;<br />
    case STOP_STATUS:<br />
    break;<br />
    default:break;<br />
  }<br />
}<br />
<br />
void loop() <br />
{<br />
  FuncStatusMachine(statusMachine);  // read RS485 sensor<br />
  delay(100);<br />
  CheckClientRcv();<br />
  <br />
  my_device.uart_service();<br />
  delay(100);<br />
  GetSerialCmd();<br />
  <br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  /* report the temperature and humidity */<br />
  if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || <br />
      (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)) {<br />
    dp_update_all();<br />
  }<br />
<br />
  delay(800);<br />
}<br />
 <br />
static bool coils_old_status[RLY_NUMS] = {0};<br />
<br />
static void CompareAndReport(void)<br />
{<br />
  for(int16_t i=0;i&lt;RLY_NUMS;i++)<br />
  {<br />
    if(coils_old_status[i] != rt_coils_status[i])<br />
    {<br />
      coils_old_status[i] = rt_coils_status[i];<br />
      my_device.mcu_dp_update(dp_array[i][0],rt_coils_status[i], 1);<br />
      break;<br />
    }<br />
  } <br />
}<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{ <br />
  CompareAndReport();<br />
}</code></div></div>here is arduino source code download link:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5330" target="_blank" title="">Hx.zip</a> (Size: 5.12 KB / Downloads: 577)
<!-- end: postbit_attachments_attachment -->]]></description>
			<content:encoded><![CDATA[these BIN file firmware and arduino source code work for KinCony KC868-Hx series relay board:<br />
KC868-H32, KC868-H32L, KC868-H32W, KC868-H32BS, KC868-H32B<br />
KC868-H16, KC868-H16W, KC868-H16B<br />
KC868-H8, KC868-H8W, KC868-H8B<br />
<br />
you not need connect any cable between Tuya adapter v2 and relay board. Because of it use by TCP connection, it use network. <br />
fimrware download:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5336" target="_blank" title="">H8x_0x0.zip</a> (Size: 478.35 KB / Downloads: 622)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5337" target="_blank" title="">H16x_0x0.zip</a> (Size: 478.44 KB / Downloads: 586)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5338" target="_blank" title="">H32x_0x0.zip</a> (Size: 478.7 KB / Downloads: 604)
<!-- end: postbit_attachments_attachment --><br />
download file and unzip, then download BIN file by ESP DOWNLOAD TOOL, write code at 0x0 address.<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5329" target="_blank" title="">download-firmware.png</a> (Size: 41.47 KB / Downloads: 1002)
<!-- end: postbit_attachments_attachment --><br />
ESP DOWNLOAD TOOL download link:<br />
<a href="https://www.kincony.com/wp-content/uploads/2022/08/flash_download_tool_3.9.2.zip" target="_blank" rel="noopener" class="mycode_url">https://www.kincony.com/wp-content/uploa..._3.9.2.zip</a><br />
if you want modify arduino source code, which relay board model to use, just uncomment the model command line.<br />
first time run code, you need to set relay board's IP address, port, your router's ssid and password for Tuya adapter v2 by usb-serial port.<br />
just send command as this: SSID:KinCony,PSW:a12345678,192.168.3.141,4196<br />
serial port baud rate: 115200bps<br />
serial port debug tool download:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5335" target="_blank" title="">UartAssist.zip</a> (Size: 468.4 KB / Downloads: 652)
<!-- end: postbit_attachments_attachment --><br />
here is arduino source code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>// USB set command     SSID:XXXXX,PSW:XXXXX,X.X.X.X,YYYY<br />
// X.X.X.X is relay board IP address,  YYYY is relay board port.  SSID and PSW is your router's ssid and password<br />
// for example         SSID:KinCony,PSW:a12345678,192.168.3.141,4196<br />
// make sure your H8,H16,H32 relay board work with "TCP Server" mode.<br />
#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include &lt;Preferences.h&gt;<br />
<br />
#include "HardwareSerial.h"<br />
<br />
///ADD TCP CLIENT FOR TUYA ADAPT<br />
//#include &lt;WiFiUdp.h&gt; <br />
#include &lt;WiFi.h&gt;<br />
#include &lt;WiFiClient.h&gt;<br />
<br />
//uncomment the relay board model you are using<br />
//#define KINCONY_H8X    ////  <br />
//#define KINCONY_H16X    ////<br />
#define KINCONY_H32X    ////<br />
<br />
#if defined(KINCONY_H8X)<br />
#define RLY_NUMS  8<br />
#elif defined(KINCONY_H16X)<br />
#define RLY_NUMS  16<br />
#elif defined(KINCONY_H32X)<br />
#define RLY_NUMS  32<br />
#endif<br />
<br />
//---------RS485 setting<br />
#define DEBUG   1<br />
<br />
#define SERIAL_BAUD   9600<br />
#define RX_PIN    32<br />
#define TX_PIN    33<br />
#define ADAPT485_NUM  2<br />
#define SLAVE_ADDR  1<br />
#define READ_ADDR  0 <br />
#define READ_NUM   32  <br />
<br />
////SERIAL COMMAND IS<br />
////SSID,<br />
char ssid_buf[30] = {0};<br />
char password_buf[20] = {0};<br />
char server_ip[20] = {0};<br />
uint16_t server_port = 4096;<br />
uint8_t str[]={"client_me"};<br />
//uint8_t Rece[256]={0};<br />
uint8_t Send[128]={0};<br />
Preferences prefs;<br />
<br />
//IPAddress staticIP(192, 168, 10, 12);<br />
//IPAddress gateway(192, 168, 10, 1);<br />
//IPAddress subnet(255, 255, 255, 0);<br />
//IPAddress dns(192, 168, 10,1);<br />
<br />
WiFiClient client;<br />
typedef enum{INIT_STATUS=0,RDY_STATUS,CONNECT_STATUS,TCP_COMM,STOP_STATUS,}STATUS_MACHINE;<br />
<br />
static int statusMachine = INIT_STATUS;<br />
 <br />
static void InitWifi(void)<br />
{<br />
  //WiFi.mode(WIFI_STA);     <br />
  //WiFi.setSleep(false); // disable STA mode wifi sleep mode<br />
  /*if(WiFi.config(staticIP, gateway, subnet, dns, dns)==false)<br />
  {<br />
    Serial1.println("Configuration failed.");<br />
  }*/<br />
  /*IPAddress ip = WiFi.localIP(); <br />
  Serial1.print("IP address: ");      <br />
  Serial1.println(WiFi.localIP()); */  <br />
<br />
  Serial1.print("rcv SSID is: ");  <br />
  Serial1.println(ssid_buf);<br />
  Serial1.print("rcv PSW is: ");  <br />
  Serial1.println(password_buf);    <br />
  Serial1.print("rcv server ip is: ");  <br />
  Serial1.println(server_ip);<br />
  Serial1.print("rcv server port is: ");  <br />
  Serial1.println(server_port);<br />
    <br />
  WiFi.begin(ssid_buf, password_buf);//link wifi hotspot <br />
  Serial1.println("WiFi try to connect~");<br />
  statusMachine = RDY_STATUS;<br />
}<br />
<br />
static bool bFlagConnWifi = false;<br />
static bool bFlagConnTcp = false;<br />
static String serial_cmd_rcv = "";<br />
static String tcp_cmd_rcv = "";<br />
static char command_str_arr[30]={0};<br />
bool rt_coils_status[RLY_NUMS] = {0}; <br />
void dp_update_all(void);<br />
<br />
////online check for wifi&amp;tcp connections for 500ms<br />
static void SetRlyByChannel(uint8_t ch,bool value)<br />
{<br />
  memset(command_str_arr,0,sizeof(command_str_arr));<br />
  sprintf(command_str_arr,"RELAY-SET-255,%d,%d",ch+1,value?1:0);<br />
  return;<br />
}<br />
static void ReadAllRlyStatus()<br />
{<br />
  memset(command_str_arr,0,sizeof(command_str_arr));<br />
  memcpy(command_str_arr,(char*)"RELAY-STATE-255",15); <br />
  return;<br />
}<br />
<br />
static void WifiConnect(void)<br />
{<br />
  if (WiFi.status() != WL_CONNECTED) //wait wifi connection success<br />
  { <br />
    Serial1.print(".");<br />
    //WiFi.begin(ssid_buf, password_buf);//link wifi hotspot<br />
    //Serial1.println("WiFi try to connect~");<br />
    bFlagConnWifi = bFlagConnTcp = false;<br />
    return ;<br />
  }<br />
  bFlagConnWifi = true;<br />
  statusMachine = CONNECT_STATUS;<br />
  Serial1.println("WiFi connected");<br />
  <br />
   <br />
}<br />
<br />
static void TcpServerConn(void)<br />
{<br />
  if(WiFi.status() != WL_CONNECTED)<br />
  {<br />
    if(bFlagConnTcp) client.stop();<br />
    bFlagConnWifi = bFlagConnTcp = false;<br />
    statusMachine = RDY_STATUS;<br />
    Serial1.println("WiFi exception 0");<br />
    return;<br />
  }<br />
 <br />
  if (!bFlagConnTcp)//begin connect to tcp server///if (client.connected() != 0)<br />
  {    <br />
      if (!client.connect(server_ip, server_port,2000))<br />
      {<br />
          Serial1.println("try to connect host....");<br />
          bFlagConnTcp = false;<br />
          //delay(5);<br />
          <br />
      }<br />
      else <br />
      {<br />
        bFlagConnTcp = true;<br />
        statusMachine = TCP_COMM;<br />
        Serial1.println("TCP connected Succeed"); <br />
      }<br />
  } <br />
   <br />
}<br />
<br />
static void SendPeriodCheckCommand(void)<br />
{<br />
  if(WiFi.status() != WL_CONNECTED)<br />
  {<br />
    if(bFlagConnTcp) client.stop();<br />
    bFlagConnWifi = bFlagConnTcp = false;<br />
    statusMachine = RDY_STATUS;<br />
    Serial1.println("WiFi exception 1");<br />
    return;<br />
  }<br />
  if(client.connected())<br />
  Serial1.println("send period check cmd~");<br />
  statusMachine = TCP_COMM;<br />
  ReadAllRlyStatus();<br />
  //SetRlyByChannel();<br />
  client.write(command_str_arr,strlen(command_str_arr));<br />
}<br />
<br />
static void GetCoilStatus(String data)<br />
{///ACK STANDARD FORMAT IS "RELAY-STATE-255,D1,D0,OK" ///16x<br />
////ACK STANDARD FORMAT IS "RELAY-STATE-255,D0,OK"  ///8x<br />
////ACK STANDARD FORMAT IS "RELAY-STATE-255,D3,D2,D1,D0,OK" ///32x<br />
  <br />
   String rcv_data = data;<br />
   if(rcv_data.substring(15,16) != "," ) return;<br />
   <br />
   uint8_t rlys_arr[4] = {0};<br />
   uint32_t rly_status = 0;<br />
   rcv_data = rcv_data.substring(16);<br />
   //Serial1.println("Remain strs1:" + rcv_data);<br />
   int index_find = rcv_data.indexOf(',');<br />
   rlys_arr[0] = atoi(rcv_data.substring(0,index_find).c_str());<br />
    <br />
   #if defined(KINCONY_H16X) || defined(KINCONY_H32X)<br />
   rcv_data = rcv_data.substring(index_find+1);<br />
   //Serial1.println("Remain strs2:" + rcv_data);<br />
   index_find = rcv_data.indexOf(',');<br />
   rlys_arr[1] = atoi(rcv_data.substring(0,index_find).c_str());<br />
   #endif<br />
   #if defined(KINCONY_H32X)<br />
   rcv_data = rcv_data.substring(index_find+1);<br />
   index_find = rcv_data.indexOf(','); <br />
   rlys_arr[2] = atoi(rcv_data.substring(0,index_find).c_str());<br />
   <br />
   rcv_data = rcv_data.substring(index_find+1);<br />
   index_find = rcv_data.indexOf(','); <br />
   rlys_arr[3] = atoi(rcv_data.substring(0,index_find).c_str());<br />
   #endif <br />
   <br />
   #if defined(KINCONY_H8X)<br />
  rly_status = rlys_arr[0];<br />
   #elif defined(KINCONY_H16X)<br />
  rly_status = (rlys_arr[0]&lt;&lt;8 ) | rlys_arr[1];<br />
   #else<br />
  rly_status = (rlys_arr[3]&lt;&lt;24 ) | (rlys_arr[2]&lt;&lt;16 )| (rlys_arr[1]&lt;&lt;8 )| (rlys_arr[0] );<br />
   #endif<br />
   <br />
<br />
   Serial1.print("rly_status = 0x");<br />
   Serial1.println(rly_status,HEX);<br />
   <br />
   for(int i=0;i&lt;RLY_NUMS;i++)<br />
   { <br />
     rt_coils_status[i] = (rly_status&amp;(1&lt;&lt;i))?true:false;<br />
   }<br />
}<br />
<br />
static char rece_tcp_buf[100] = {0};<br />
<br />
static bool CheckClientRcv(void)<br />
{<br />
  uint8_t i=0;<br />
  if(client.available() == 0) return false;<br />
  memset(rece_tcp_buf,0,sizeof(rece_tcp_buf));<br />
  <br />
  tcp_cmd_rcv = "";<br />
   while (client.available()&gt;0)  //whether received data from server<br />
   {<br />
       rece_tcp_buf[i++] = client.read();//receive data from server<br />
    //tcp_cmd_rcv += client.read();//store data from server<br />
    delay(1);<br />
   }<br />
   <br />
   tcp_cmd_rcv = String(rece_tcp_buf);<br />
      <br />
   Serial1.print("Ack:");<br />
   Serial1.println(tcp_cmd_rcv.c_str());<br />
   if (tcp_cmd_rcv.length() &lt; 22 || <br />
      tcp_cmd_rcv.substring(0,15) != "RELAY-STATE-255" )  {tcp_cmd_rcv="";return false;}<br />
  //Serial1.println("OKAY COILS VALUE");  <br />
  GetCoilStatus(tcp_cmd_rcv);<br />
  tcp_cmd_rcv="";<br />
  return true;<br />
}<br />
<br />
 <br />
static bool ProtocolDeal(const char* buf)<br />
{<br />
  char deal_str[100] = {0};<br />
  char tmp[30];<br />
  char *pos,*pos1,*pos2,*pos3;<br />
<br />
  memcpy(deal_str,buf,strlen(buf)); <br />
<br />
  Serial1.println("check1");<br />
  <br />
  if ((pos = strchr(deal_str,','))!=0)    //find','<br />
  {<br />
    *pos = '&#92;0';    //","replace with ‘&#92;0’<br />
    pos++;<br />
    pos1 = pos;<br />
    memset(tmp,0,30);<br />
    //memcpy(tmp,buf,strlen(buf));<br />
    strcpy(tmp,deal_str);<br />
    if(tmp[0]=='S' &amp;&amp; tmp[1]=='S' &amp;&amp; tmp[2]=='I' &amp;&amp; tmp[3]=='D' &amp;&amp; <br />
      tmp[4]==':')  <br />
    {<br />
      memset(ssid_buf,0,sizeof(ssid_buf));<br />
      memcpy(ssid_buf,&amp;tmp[5],strlen(tmp)-5);<br />
      Serial1.println("rcv ssid ok.");<br />
    }     <br />
  }<br />
  else <br />
    {<br />
      Serial1.println("end1"); <br />
      return  false; <br />
      <br />
    }<br />
<br />
  Serial1.println("check2");<br />
  if ((pos = strchr(pos1,','))!=0)    //find','<br />
  {<br />
    *pos = '&#92;0';    //","replace with‘&#92;0’<br />
    pos++;<br />
    pos2 = pos;<br />
    memset(tmp,0,30);<br />
    //memcpy(tmp,pos1,strlen(pos1));<br />
    strcpy(tmp,pos1);<br />
    if(tmp[0]=='P' &amp;&amp; tmp[1]=='S' &amp;&amp; tmp[2]=='W' &amp;&amp; tmp[3]==':' )  <br />
    {<br />
      memset(password_buf,0,sizeof(password_buf));<br />
      memcpy(password_buf,&amp;tmp[4],strlen(tmp)-4);<br />
      Serial1.println("rcv psw ok.");<br />
    }  <br />
  }<br />
  else{<br />
    return false; <br />
    Serial1.println("end2");}<br />
    <br />
  Serial1.println("check3");<br />
  if ((pos = strchr(pos2,','))!=0)    //find','  IP:XXXX,PORT:XXXX<br />
  {<br />
    *pos = '&#92;0';    //","replace with‘&#92;0’<br />
    pos++;<br />
    pos3 = pos;<br />
    memset(server_ip,0,sizeof(server_ip));<br />
    memcpy(server_ip,pos2,strlen(pos2));<br />
  <br />
    server_port = atoi(pos3);<br />
<br />
    Serial1.print("rcv SSID is: ");  <br />
    Serial1.println(ssid_buf);<br />
    Serial1.print("rcv PSW is: ");  <br />
    Serial1.println(password_buf);     <br />
    Serial1.print("rcv server ip is: ");  <br />
    Serial1.println(server_ip);<br />
    Serial1.print("rcv server port is: ");  <br />
    Serial1.println(server_port);<br />
    <br />
    /*bFlagConnWifi = bFlagConnTcp = false;<br />
    statusMachine = INIT_STATUS; <br />
     <br />
    if(client.connected())<br />
    {<br />
      client.stop(); <br />
    }<br />
    <br />
    if (WiFi.status() == WL_CONNECTED) //等待WiFi热点连接成功<br />
    { <br />
      Serial1.print("stop wifi second.");<br />
      WiFi.disconnect();<br />
      delay(3000);<br />
    }*/<br />
<br />
    ///Preferences param here...<br />
    prefs.begin("myns_netcfg");<br />
    prefs.putString("ns_ssid",ssid_buf);<br />
    prefs.putString("ns_psw",password_buf);<br />
    prefs.putString("ns_ip",server_ip);<br />
    prefs.putUShort("ns_port",server_port);<br />
   <br />
    prefs.end();<br />
    ESP.restart();<br />
  }<br />
  else<br />
    {<br />
    return false; <br />
    Serial1.println("end3");<br />
    }<br />
<br />
  <br />
}<br />
<br />
static void GetSerialCmd() <br />
{ ////format: SSID:REALTIME2SENS-2.4G,PSW:12345678,IP:192.168.1.100,PORT:4196<br />
  if(Serial1.available() == 0) return;<br />
  serial_cmd_rcv = "";<br />
  while (Serial1.available() &gt; 0)<br />
  {<br />
    serial_cmd_rcv += char(Serial1.read());<br />
    delay(1);  <br />
  } <br />
  Serial1.println(serial_cmd_rcv);<br />
  if (serial_cmd_rcv.length() &lt; 36 || <br />
      serial_cmd_rcv.substring(0,4) != "SSID" )  {serial_cmd_rcv="";return;}<br />
 <br />
  ProtocolDeal(serial_cmd_rcv.c_str()); <br />
  <br />
  serial_cmd_rcv = ""; <br />
}<br />
<br />
/* Current device DP values */<br />
<br />
<br />
HardwareSerial My485Serial(ADAPT485_NUM);<br />
 <br />
<br />
//---RS485 setting end----<br />
<br />
TuyaWifi my_device;<br />
<br />
<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
<br />
/* Data point define */<br />
typedef enum<br />
{<br />
  DPID_SWITCH01 = 1,<br />
  DPID_SWITCH02,DPID_SWITCH03,<br />
  DPID_SWITCH04,DPID_SWITCH05,DPID_SWITCH06,<br />
  <br />
  DPID_SWITCH07 = 101,<br />
  DPID_SWITCH08,DPID_SWITCH09,DPID_SWITCH10,<br />
  DPID_SWITCH11,DPID_SWITCH12,DPID_SWITCH13,<br />
  DPID_SWITCH14,DPID_SWITCH15,DPID_SWITCH16,<br />
   <br />
  DPID_SWITCH17,DPID_SWITCH18,DPID_SWITCH19,<br />
  DPID_SWITCH20,DPID_SWITCH21,DPID_SWITCH22,<br />
  DPID_SWITCH23,DPID_SWITCH24,DPID_SWITCH25,<br />
  <br />
  DPID_SWITCH26,DPID_SWITCH27,DPID_SWITCH28,<br />
  DPID_SWITCH29,DPID_SWITCH30,DPID_SWITCH31,<br />
  DPID_SWITCH32,<br />
<br />
}HXBS_DPID_DEF; <br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_String, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
#if defined(KINCONY_H16X)<br />
unsigned char dp_array[][2] =<br />
{ <br />
  {DPID_SWITCH01, DP_TYPE_BOOL},{DPID_SWITCH02, DP_TYPE_BOOL},<br />
  {DPID_SWITCH03, DP_TYPE_BOOL},{DPID_SWITCH04, DP_TYPE_BOOL},<br />
  {DPID_SWITCH05, DP_TYPE_BOOL},{DPID_SWITCH06, DP_TYPE_BOOL},<br />
  {DPID_SWITCH07, DP_TYPE_BOOL},{DPID_SWITCH08, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH09, DP_TYPE_BOOL},{DPID_SWITCH10, DP_TYPE_BOOL},<br />
  {DPID_SWITCH11, DP_TYPE_BOOL},{DPID_SWITCH12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH13, DP_TYPE_BOOL},{DPID_SWITCH14, DP_TYPE_BOOL},<br />
  {DPID_SWITCH15, DP_TYPE_BOOL},{DPID_SWITCH16, DP_TYPE_BOOL}, <br />
};<br />
<br />
unsigned char pid[] = {"7en1hzfnjtvprt35"}; ////change pid here....<br />
 <br />
#elif defined(KINCONY_H8X)<br />
unsigned char dp_array[][2] =<br />
{ <br />
  {DPID_SWITCH01, DP_TYPE_BOOL},{DPID_SWITCH02, DP_TYPE_BOOL},<br />
  {DPID_SWITCH03, DP_TYPE_BOOL},{DPID_SWITCH04, DP_TYPE_BOOL},<br />
  {DPID_SWITCH05, DP_TYPE_BOOL},{DPID_SWITCH06, DP_TYPE_BOOL},<br />
  {DPID_SWITCH07, DP_TYPE_BOOL},{DPID_SWITCH08, DP_TYPE_BOOL},  <br />
};<br />
<br />
unsigned char pid[] = {"zyauvyer5insa2ne"}; ////change pid here....<br />
 <br />
#elif defined(KINCONY_H32X)<br />
unsigned char dp_array[][2] =<br />
{ <br />
  {DPID_SWITCH01, DP_TYPE_BOOL},{DPID_SWITCH02, DP_TYPE_BOOL},<br />
  {DPID_SWITCH03, DP_TYPE_BOOL},{DPID_SWITCH04, DP_TYPE_BOOL},<br />
  {DPID_SWITCH05, DP_TYPE_BOOL},{DPID_SWITCH06, DP_TYPE_BOOL},<br />
  {DPID_SWITCH07, DP_TYPE_BOOL},{DPID_SWITCH08, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH09, DP_TYPE_BOOL},{DPID_SWITCH10, DP_TYPE_BOOL},<br />
  {DPID_SWITCH11, DP_TYPE_BOOL},{DPID_SWITCH12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH13, DP_TYPE_BOOL},{DPID_SWITCH14, DP_TYPE_BOOL},<br />
  {DPID_SWITCH15, DP_TYPE_BOOL},{DPID_SWITCH16, DP_TYPE_BOOL}, <br />
<br />
  {DPID_SWITCH17, DP_TYPE_BOOL},{DPID_SWITCH18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH19, DP_TYPE_BOOL},{DPID_SWITCH20, DP_TYPE_BOOL},<br />
  {DPID_SWITCH21, DP_TYPE_BOOL},{DPID_SWITCH22, DP_TYPE_BOOL},<br />
  {DPID_SWITCH23, DP_TYPE_BOOL},{DPID_SWITCH24, DP_TYPE_BOOL}, <br />
<br />
  {DPID_SWITCH25, DP_TYPE_BOOL},{DPID_SWITCH26, DP_TYPE_BOOL},<br />
  {DPID_SWITCH27, DP_TYPE_BOOL},{DPID_SWITCH28, DP_TYPE_BOOL},<br />
  {DPID_SWITCH29, DP_TYPE_BOOL},{DPID_SWITCH30, DP_TYPE_BOOL},<br />
  {DPID_SWITCH31, DP_TYPE_BOOL},{DPID_SWITCH32, DP_TYPE_BOOL}, <br />
};<br />
<br />
unsigned char pid[] = {"ytgvxhy4zoaxdqtp"}; ////change pid here....<br />
<br />
#endif<br />
<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
  <br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  int16_t index_out_coil = -1;<br />
  unsigned char get_down_cmd = false;<br />
  <br />
  for(int16_t i=0;i&lt;RLY_NUMS;i++)<br />
  {<br />
    if(dpid == dp_array[i][0]) <br />
    {<br />
      index_out_coil = i;<br />
      break;<br />
    }<br />
  }<br />
<br />
    #if DEBUG<br />
    Serial1.print("Rcv Command:&lt;dpid,");<br />
    Serial1.print(dpid,DEC); <br />
    Serial1.print(" value,");<br />
    Serial1.print(value[0],DEC);<br />
    Serial1.print(" len,");<br />
    Serial1.print(length,DEC);<br />
    Serial1.println("&gt; ");<br />
  #endif<br />
  <br />
  if(index_out_coil &lt; 0) return TY_ERROR;<br />
<br />
<br />
    <br />
  get_down_cmd = my_device.mcu_get_dp_download_data(dpid,value,length);<br />
<br />
  #if DEBUG<br />
    Serial1.print("get_down_cmd value is:&lt;");<br />
    Serial1.print(get_down_cmd,DEC);  <br />
    Serial1.println("&gt; ");<br />
  #endif<br />
  <br />
  SetRlyByChannel(index_out_coil,get_down_cmd?true:false);<br />
  Serial1.print("Send board control command:");<br />
  Serial1.println(command_str_arr);<br />
  uint16_t result = client.write(command_str_arr,strlen(command_str_arr));  <br />
  #if DEBUG<br />
    Serial1.print("client WriteCoilAddr&lt;");<br />
    Serial1.print(index_out_coil,DEC);<br />
    Serial1.print("&gt; result:&lt;");<br />
    Serial1.print(result,DEC);  <br />
    Serial1.println("&gt; ");<br />
  #endif<br />
  <br />
  my_device.mcu_dp_update(dpid, value, length);<br />
  return TY_SUCCESS;<br />
}<br />
<br />
void setup() <br />
{<br />
<br />
  My485Serial.begin(SERIAL_BAUD, SERIAL_8N1, RX_PIN, TX_PIN); //RS485 serial port<br />
   <br />
  Serial.begin(9600,SERIAL_8N1,22,19);  //Tuya module serial port<br />
  Serial1.begin(115200,SERIAL_8N1,3,1);   //USB serial port<br />
<br />
  <br />
  // Serial with tuyawifi<br />
  <br />
  <br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 2);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
  <br />
  //delay(300);<br />
  last_time = millis();<br />
<br />
<br />
  ///Preferences param here...<br />
  prefs.begin("myns_netcfg");<br />
  String ns_ssid = prefs.getString("ns_ssid","123");<br />
  String ns_psw = prefs.getString("ns_psw","123");<br />
  String ns_ip = prefs.getString("ns_ip","127.0.0.1");<br />
  server_port = prefs.getUShort("ns_port",4096);<br />
<br />
  memcpy(ssid_buf,ns_ssid.c_str(),ns_ssid.length());<br />
  memcpy(password_buf,ns_psw.c_str(),ns_psw.length());<br />
  memcpy(server_ip,ns_ip.c_str(),ns_ip.length()); <br />
  prefs.end();<br />
<br />
  WiFi.mode(WIFI_STA);     //set to STA mode<br />
}<br />
<br />
static void FuncStatusMachine(int status)<br />
{<br />
  switch(status)<br />
  {<br />
    case INIT_STATUS:<br />
    InitWifi();break;<br />
    case RDY_STATUS:<br />
    WifiConnect();break;<br />
    case CONNECT_STATUS:<br />
    TcpServerConn();break;<br />
    case TCP_COMM:<br />
    SendPeriodCheckCommand();break;<br />
    case STOP_STATUS:<br />
    break;<br />
    default:break;<br />
  }<br />
}<br />
<br />
void loop() <br />
{<br />
  FuncStatusMachine(statusMachine);  // read RS485 sensor<br />
  delay(100);<br />
  CheckClientRcv();<br />
  <br />
  my_device.uart_service();<br />
  delay(100);<br />
  GetSerialCmd();<br />
  <br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  /* report the temperature and humidity */<br />
  if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || <br />
      (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)) {<br />
    dp_update_all();<br />
  }<br />
<br />
  delay(800);<br />
}<br />
 <br />
static bool coils_old_status[RLY_NUMS] = {0};<br />
<br />
static void CompareAndReport(void)<br />
{<br />
  for(int16_t i=0;i&lt;RLY_NUMS;i++)<br />
  {<br />
    if(coils_old_status[i] != rt_coils_status[i])<br />
    {<br />
      coils_old_status[i] = rt_coils_status[i];<br />
      my_device.mcu_dp_update(dp_array[i][0],rt_coils_status[i], 1);<br />
      break;<br />
    }<br />
  } <br />
}<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{ <br />
  CompareAndReport();<br />
}</code></div></div>here is arduino source code download link:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5330" target="_blank" title="">Hx.zip</a> (Size: 5.12 KB / Downloads: 577)
<!-- end: postbit_attachments_attachment -->]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[tuya + h32l pulse]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=5771</link>
			<pubDate>Sat, 18 May 2024 22:06:18 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=4821">leonardo reis</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=5771</guid>
			<description><![CDATA[hello everything is fine??<br />
I need to clear up a doubt.<br />
I bought the Tuya adapter to put it next to my project, it worked fine, but I can't transform some channels into pulses, for example, curtain<br />
can you help me]]></description>
			<content:encoded><![CDATA[hello everything is fine??<br />
I need to clear up a doubt.<br />
I bought the Tuya adapter to put it next to my project, it worked fine, but I can't transform some channels into pulses, for example, curtain<br />
can you help me]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Tuya adapter V2 arduino source code - RS485 relay controller KC868-H32BS]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=5732</link>
			<pubDate>Tue, 14 May 2024 10:18:04 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=1">admin</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=5732</guid>
			<description><![CDATA[This is sample demo let RS485 digital output device (KinCony KC868-H32BS controller) can work with Tuya app by internet.  you can let your own RS485 relay board add to Tuya app.<br />
added Tuya adapter v2, relay board will support alexa or Google home speaker for voice control directly.<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5248" target="_blank" title="">tuya-rs485-relay-1.jpg</a> (Size: 170.63 KB / Downloads: 1068)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5249" target="_blank" title="">tuya-rs485-relay-app.jpg</a> (Size: 47.93 KB / Downloads: 1104)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5250" target="_blank" title="">arduino-debug.png</a> (Size: 118.87 KB / Downloads: 1082)
<!-- end: postbit_attachments_attachment --><br />
arduino source code download: <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5251" target="_blank" title="">Tuya_AdapterV2_RS485_Relay.zip</a> (Size: 2.63 KB / Downloads: 587)
<!-- end: postbit_attachments_attachment --><br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;ModbusMaster.h&gt;<br />
#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include "HardwareSerial.h"<br />
//---------RS485 setting<br />
#define DEBUG   1<br />
<br />
#define SERIAL_BAUD   9600<br />
#define RX_PIN    32<br />
#define TX_PIN    33<br />
#define ADAPT485_NUM  2<br />
#define SLAVE_ADDR  1<br />
#define READ_ADDR  0 <br />
#define READ_NUM   32  <br />
<br />
/* Current device DP values */<br />
bool rt_coils_status[32] = {0}; <br />
<br />
HardwareSerial My485Serial(ADAPT485_NUM);<br />
ModbusMaster node;<br />
uint16_t rcv_buf[READ_NUM&gt;&gt;4] = {0};<br />
   <br />
static void ReadEmeterRegs(void)<br />
{<br />
  uint32_t read_tmp = 0;<br />
   <br />
  uint8_t result = node.readCoils(READ_ADDR, READ_NUM);<br />
  if (result == node.ku8MBSuccess) {//if communicate successful then<br />
        <br />
    for (uint8_t i = 0; i &lt; 2; i++)<br />
    {<br />
      rcv_buf[i] = node.getResponseBuffer(i);<br />
      for(uint8_t j = 0; j &lt; 16; j++)<br />
      {<br />
        if(j&lt;8) rt_coils_status[i*16 + j+8] = (rcv_buf[1-i]&amp;(1&lt;&lt;j))?true:false;<br />
        else rt_coils_status[i*16 + j - 8] = (rcv_buf[1-i]&amp;(1&lt;&lt;j))?true:false;<br />
      }<br />
    } <br />
    <br />
    #if DEBUG<br />
    Serial1.print("Get switch[0..31] value is");<br />
    Serial1.print(":&lt;0x");<br />
    Serial1.print(rcv_buf[0],HEX);<br />
    Serial1.print(rcv_buf[1],HEX);<br />
    Serial1.println("&gt; ");<br />
    #endif<br />
     <br />
  }<br />
  else <br />
  {<br />
    node.clearResponseBuffer();<br />
  }<br />
<br />
  delay(100);<br />
}<br />
<br />
//---RS485 setting end----<br />
<br />
TuyaWifi my_device;<br />
<br />
<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
<br />
/* Data point define */<br />
typedef enum<br />
{<br />
  DPID_SWITCH01 = 1,<br />
  DPID_SWITCH02,DPID_SWITCH03,<br />
  DPID_SWITCH04,DPID_SWITCH05,DPID_SWITCH06,<br />
  DPID_SWITCH07 = 113,<br />
  DPID_SWITCH08,DPID_SWITCH09,DPID_SWITCH10,<br />
  DPID_SWITCH11,DPID_SWITCH12,DPID_SWITCH13,<br />
  DPID_SWITCH14,DPID_SWITCH15,DPID_SWITCH16,<br />
  DPID_SWITCH17,DPID_SWITCH18,DPID_SWITCH19,<br />
  DPID_SWITCH20,DPID_SWITCH21,DPID_SWITCH22,<br />
  DPID_SWITCH23,DPID_SWITCH24,DPID_SWITCH25,<br />
  DPID_SWITCH26,DPID_SWITCH27,DPID_SWITCH28,<br />
  DPID_SWITCH29,DPID_SWITCH30,DPID_SWITCH31,<br />
  DPID_SWITCH32,<br />
}H32BS_DPID_DEF;<br />
 <br />
/*const unsigned char dpid_arr[32] = {<br />
  1,2,3,4,5,6,<br />
  113,114,115,116,117,118,119,120,<br />
  121,122,123,124,125,126,127,128,<br />
  129,130,131,132,133,134,135,136,<br />
  137,138,<br />
};*/<br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{ <br />
  {DPID_SWITCH01, DP_TYPE_BOOL},{DPID_SWITCH02, DP_TYPE_BOOL},<br />
  {DPID_SWITCH03, DP_TYPE_BOOL},{DPID_SWITCH04, DP_TYPE_BOOL},<br />
  {DPID_SWITCH05, DP_TYPE_BOOL},{DPID_SWITCH06, DP_TYPE_BOOL},<br />
  {DPID_SWITCH07, DP_TYPE_BOOL},{DPID_SWITCH08, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH09, DP_TYPE_BOOL},{DPID_SWITCH10, DP_TYPE_BOOL},<br />
  {DPID_SWITCH11, DP_TYPE_BOOL},{DPID_SWITCH12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH13, DP_TYPE_BOOL},{DPID_SWITCH14, DP_TYPE_BOOL},<br />
  {DPID_SWITCH15, DP_TYPE_BOOL},{DPID_SWITCH16, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH17, DP_TYPE_BOOL},{DPID_SWITCH18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH19, DP_TYPE_BOOL},{DPID_SWITCH20, DP_TYPE_BOOL},<br />
  {DPID_SWITCH21, DP_TYPE_BOOL},{DPID_SWITCH22, DP_TYPE_BOOL},<br />
  {DPID_SWITCH23, DP_TYPE_BOOL},{DPID_SWITCH24, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH25, DP_TYPE_BOOL},{DPID_SWITCH26, DP_TYPE_BOOL},<br />
  {DPID_SWITCH27, DP_TYPE_BOOL},{DPID_SWITCH28, DP_TYPE_BOOL},<br />
  {DPID_SWITCH29, DP_TYPE_BOOL},{DPID_SWITCH30, DP_TYPE_BOOL},<br />
  {DPID_SWITCH31, DP_TYPE_BOOL},{DPID_SWITCH32, DP_TYPE_BOOL},<br />
};<br />
<br />
unsigned char pid[] = {"3ocztklfu3q32jab"}; ////change pid here....<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  int16_t index_out_coil = -1;<br />
  unsigned char get_down_cmd = false;<br />
  <br />
  for(int16_t i=0;i&lt;32;i++)<br />
  {<br />
    if(dpid == dp_array[i][0]) <br />
    {<br />
      index_out_coil = i;<br />
      break;<br />
    }<br />
  }<br />
  if(index_out_coil &lt; 0) return TY_ERROR;<br />
<br />
  get_down_cmd = my_device.mcu_get_dp_download_data(dpid,value,length);<br />
<br />
  Serial1.print("Rcv Command:&lt;dpid,");<br />
  Serial1.print(dpid,DEC);<br />
  Serial1.print(" value,");<br />
  Serial1.print(value[0],HEX);<br />
  Serial1.print(" len,");<br />
  Serial1.println(length,HEX);<br />
  Serial1.println("&gt; ");<br />
    <br />
  <br />
<br />
  #if DEBUG<br />
    Serial1.print("get_down_cmd value is:&lt;");<br />
    Serial1.print(get_down_cmd,DEC);  <br />
    Serial1.println("&gt; ");<br />
    Serial1.print("Node WriteCoilAddr&lt;");<br />
    Serial1.print(index_out_coil,DEC);<br />
    Serial1.print("&gt; ");<br />
  #endif<br />
<br />
  delay(100);<br />
  uint8_t result = node.writeSingleCoil(index_out_coil, value[0]);<br />
  #if DEBUG<br />
    Serial1.print(" result:&lt;");<br />
    Serial1.print(result,DEC);  <br />
    Serial1.println("&gt; ");<br />
  #endif<br />
<br />
  delay(100);<br />
  my_device.mcu_dp_update(dpid, value, length);<br />
  return TY_SUCCESS;<br />
}<br />
<br />
void setup() <br />
{<br />
<br />
  My485Serial.begin(SERIAL_BAUD, SERIAL_8N1, RX_PIN, TX_PIN); //RS485 serial port<br />
  node.begin(SLAVE_ADDR,My485Serial);<br />
  Serial.begin(9600,SERIAL_8N1,22,19);  //Tuya module serial port<br />
  Serial1.begin(115200,SERIAL_8N1,3,1);   //USB serial port<br />
<br />
  <br />
  // Serial with tuyawifi<br />
  <br />
  <br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 2);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
  <br />
  //delay(300);<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  ReadEmeterRegs();  // read RS485 sensor<br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  /* report the temperature and humidity */<br />
  if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || <br />
      (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)) {<br />
    dp_update_all();<br />
  }<br />
<br />
  delay(500);<br />
}<br />
 <br />
static bool coils_old_status[32] = {false};<br />
<br />
unsigned char tmp_cmd = 0;<br />
static void CompareAndReport(void)<br />
{<br />
  for(int16_t i=0;i&lt;32;i++)<br />
  {<br />
    if(coils_old_status[i] != rt_coils_status[i])<br />
    {<br />
      coils_old_status[i] = rt_coils_status[i];<br />
      tmp_cmd = rt_coils_status[i]?1:0;<br />
      #if 0<br />
      Serial1.print("Changed happed:&lt;dpid,");<br />
      Serial1.print(dp_array[i][0],DEC);<br />
      Serial1.println("&gt; ");<br />
      #endif<br />
      my_device.mcu_dp_update(dp_array[i][0],tmp_cmd, 1);<br />
      delay(10); <br />
      //break;<br />
    }<br />
  } <br />
}<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{ <br />
  CompareAndReport();<br />
  <br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[This is sample demo let RS485 digital output device (KinCony KC868-H32BS controller) can work with Tuya app by internet.  you can let your own RS485 relay board add to Tuya app.<br />
added Tuya adapter v2, relay board will support alexa or Google home speaker for voice control directly.<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5248" target="_blank" title="">tuya-rs485-relay-1.jpg</a> (Size: 170.63 KB / Downloads: 1068)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5249" target="_blank" title="">tuya-rs485-relay-app.jpg</a> (Size: 47.93 KB / Downloads: 1104)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5250" target="_blank" title="">arduino-debug.png</a> (Size: 118.87 KB / Downloads: 1082)
<!-- end: postbit_attachments_attachment --><br />
arduino source code download: <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5251" target="_blank" title="">Tuya_AdapterV2_RS485_Relay.zip</a> (Size: 2.63 KB / Downloads: 587)
<!-- end: postbit_attachments_attachment --><br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;ModbusMaster.h&gt;<br />
#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include "HardwareSerial.h"<br />
//---------RS485 setting<br />
#define DEBUG   1<br />
<br />
#define SERIAL_BAUD   9600<br />
#define RX_PIN    32<br />
#define TX_PIN    33<br />
#define ADAPT485_NUM  2<br />
#define SLAVE_ADDR  1<br />
#define READ_ADDR  0 <br />
#define READ_NUM   32  <br />
<br />
/* Current device DP values */<br />
bool rt_coils_status[32] = {0}; <br />
<br />
HardwareSerial My485Serial(ADAPT485_NUM);<br />
ModbusMaster node;<br />
uint16_t rcv_buf[READ_NUM&gt;&gt;4] = {0};<br />
   <br />
static void ReadEmeterRegs(void)<br />
{<br />
  uint32_t read_tmp = 0;<br />
   <br />
  uint8_t result = node.readCoils(READ_ADDR, READ_NUM);<br />
  if (result == node.ku8MBSuccess) {//if communicate successful then<br />
        <br />
    for (uint8_t i = 0; i &lt; 2; i++)<br />
    {<br />
      rcv_buf[i] = node.getResponseBuffer(i);<br />
      for(uint8_t j = 0; j &lt; 16; j++)<br />
      {<br />
        if(j&lt;8) rt_coils_status[i*16 + j+8] = (rcv_buf[1-i]&amp;(1&lt;&lt;j))?true:false;<br />
        else rt_coils_status[i*16 + j - 8] = (rcv_buf[1-i]&amp;(1&lt;&lt;j))?true:false;<br />
      }<br />
    } <br />
    <br />
    #if DEBUG<br />
    Serial1.print("Get switch[0..31] value is");<br />
    Serial1.print(":&lt;0x");<br />
    Serial1.print(rcv_buf[0],HEX);<br />
    Serial1.print(rcv_buf[1],HEX);<br />
    Serial1.println("&gt; ");<br />
    #endif<br />
     <br />
  }<br />
  else <br />
  {<br />
    node.clearResponseBuffer();<br />
  }<br />
<br />
  delay(100);<br />
}<br />
<br />
//---RS485 setting end----<br />
<br />
TuyaWifi my_device;<br />
<br />
<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
<br />
/* Data point define */<br />
typedef enum<br />
{<br />
  DPID_SWITCH01 = 1,<br />
  DPID_SWITCH02,DPID_SWITCH03,<br />
  DPID_SWITCH04,DPID_SWITCH05,DPID_SWITCH06,<br />
  DPID_SWITCH07 = 113,<br />
  DPID_SWITCH08,DPID_SWITCH09,DPID_SWITCH10,<br />
  DPID_SWITCH11,DPID_SWITCH12,DPID_SWITCH13,<br />
  DPID_SWITCH14,DPID_SWITCH15,DPID_SWITCH16,<br />
  DPID_SWITCH17,DPID_SWITCH18,DPID_SWITCH19,<br />
  DPID_SWITCH20,DPID_SWITCH21,DPID_SWITCH22,<br />
  DPID_SWITCH23,DPID_SWITCH24,DPID_SWITCH25,<br />
  DPID_SWITCH26,DPID_SWITCH27,DPID_SWITCH28,<br />
  DPID_SWITCH29,DPID_SWITCH30,DPID_SWITCH31,<br />
  DPID_SWITCH32,<br />
}H32BS_DPID_DEF;<br />
 <br />
/*const unsigned char dpid_arr[32] = {<br />
  1,2,3,4,5,6,<br />
  113,114,115,116,117,118,119,120,<br />
  121,122,123,124,125,126,127,128,<br />
  129,130,131,132,133,134,135,136,<br />
  137,138,<br />
};*/<br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{ <br />
  {DPID_SWITCH01, DP_TYPE_BOOL},{DPID_SWITCH02, DP_TYPE_BOOL},<br />
  {DPID_SWITCH03, DP_TYPE_BOOL},{DPID_SWITCH04, DP_TYPE_BOOL},<br />
  {DPID_SWITCH05, DP_TYPE_BOOL},{DPID_SWITCH06, DP_TYPE_BOOL},<br />
  {DPID_SWITCH07, DP_TYPE_BOOL},{DPID_SWITCH08, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH09, DP_TYPE_BOOL},{DPID_SWITCH10, DP_TYPE_BOOL},<br />
  {DPID_SWITCH11, DP_TYPE_BOOL},{DPID_SWITCH12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH13, DP_TYPE_BOOL},{DPID_SWITCH14, DP_TYPE_BOOL},<br />
  {DPID_SWITCH15, DP_TYPE_BOOL},{DPID_SWITCH16, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH17, DP_TYPE_BOOL},{DPID_SWITCH18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH19, DP_TYPE_BOOL},{DPID_SWITCH20, DP_TYPE_BOOL},<br />
  {DPID_SWITCH21, DP_TYPE_BOOL},{DPID_SWITCH22, DP_TYPE_BOOL},<br />
  {DPID_SWITCH23, DP_TYPE_BOOL},{DPID_SWITCH24, DP_TYPE_BOOL},<br />
<br />
  {DPID_SWITCH25, DP_TYPE_BOOL},{DPID_SWITCH26, DP_TYPE_BOOL},<br />
  {DPID_SWITCH27, DP_TYPE_BOOL},{DPID_SWITCH28, DP_TYPE_BOOL},<br />
  {DPID_SWITCH29, DP_TYPE_BOOL},{DPID_SWITCH30, DP_TYPE_BOOL},<br />
  {DPID_SWITCH31, DP_TYPE_BOOL},{DPID_SWITCH32, DP_TYPE_BOOL},<br />
};<br />
<br />
unsigned char pid[] = {"3ocztklfu3q32jab"}; ////change pid here....<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  int16_t index_out_coil = -1;<br />
  unsigned char get_down_cmd = false;<br />
  <br />
  for(int16_t i=0;i&lt;32;i++)<br />
  {<br />
    if(dpid == dp_array[i][0]) <br />
    {<br />
      index_out_coil = i;<br />
      break;<br />
    }<br />
  }<br />
  if(index_out_coil &lt; 0) return TY_ERROR;<br />
<br />
  get_down_cmd = my_device.mcu_get_dp_download_data(dpid,value,length);<br />
<br />
  Serial1.print("Rcv Command:&lt;dpid,");<br />
  Serial1.print(dpid,DEC);<br />
  Serial1.print(" value,");<br />
  Serial1.print(value[0],HEX);<br />
  Serial1.print(" len,");<br />
  Serial1.println(length,HEX);<br />
  Serial1.println("&gt; ");<br />
    <br />
  <br />
<br />
  #if DEBUG<br />
    Serial1.print("get_down_cmd value is:&lt;");<br />
    Serial1.print(get_down_cmd,DEC);  <br />
    Serial1.println("&gt; ");<br />
    Serial1.print("Node WriteCoilAddr&lt;");<br />
    Serial1.print(index_out_coil,DEC);<br />
    Serial1.print("&gt; ");<br />
  #endif<br />
<br />
  delay(100);<br />
  uint8_t result = node.writeSingleCoil(index_out_coil, value[0]);<br />
  #if DEBUG<br />
    Serial1.print(" result:&lt;");<br />
    Serial1.print(result,DEC);  <br />
    Serial1.println("&gt; ");<br />
  #endif<br />
<br />
  delay(100);<br />
  my_device.mcu_dp_update(dpid, value, length);<br />
  return TY_SUCCESS;<br />
}<br />
<br />
void setup() <br />
{<br />
<br />
  My485Serial.begin(SERIAL_BAUD, SERIAL_8N1, RX_PIN, TX_PIN); //RS485 serial port<br />
  node.begin(SLAVE_ADDR,My485Serial);<br />
  Serial.begin(9600,SERIAL_8N1,22,19);  //Tuya module serial port<br />
  Serial1.begin(115200,SERIAL_8N1,3,1);   //USB serial port<br />
<br />
  <br />
  // Serial with tuyawifi<br />
  <br />
  <br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 2);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
  <br />
  //delay(300);<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  ReadEmeterRegs();  // read RS485 sensor<br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  /* report the temperature and humidity */<br />
  if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || <br />
      (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)) {<br />
    dp_update_all();<br />
  }<br />
<br />
  delay(500);<br />
}<br />
 <br />
static bool coils_old_status[32] = {false};<br />
<br />
unsigned char tmp_cmd = 0;<br />
static void CompareAndReport(void)<br />
{<br />
  for(int16_t i=0;i&lt;32;i++)<br />
  {<br />
    if(coils_old_status[i] != rt_coils_status[i])<br />
    {<br />
      coils_old_status[i] = rt_coils_status[i];<br />
      tmp_cmd = rt_coils_status[i]?1:0;<br />
      #if 0<br />
      Serial1.print("Changed happed:&lt;dpid,");<br />
      Serial1.print(dp_array[i][0],DEC);<br />
      Serial1.println("&gt; ");<br />
      #endif<br />
      my_device.mcu_dp_update(dp_array[i][0],tmp_cmd, 1);<br />
      delay(10); <br />
      //break;<br />
    }<br />
  } <br />
}<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{ <br />
  CompareAndReport();<br />
  <br />
}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Tuya adapter V2 arduino source code - RS485 1 phase AC Energy Meter]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=5705</link>
			<pubDate>Wed, 08 May 2024 15:27:39 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=1">admin</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=5705</guid>
			<description><![CDATA[let your own RS485 AC energy meter work with Tuya app, so that can remote monitor meter data by Tuya app via internet.<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5204" target="_blank" title="">tuya-meter-9.jpg</a> (Size: 155.47 KB / Downloads: 1108)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5206" target="_blank" title="">tuya-meter-app1.jpg</a> (Size: 36.72 KB / Downloads: 1197)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5207" target="_blank" title="">tuya-meter-app2.jpg</a> (Size: 35.44 KB / Downloads: 1187)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5194" target="_blank" title="">arduino-code.png</a> (Size: 128.54 KB / Downloads: 1162)
<!-- end: postbit_attachments_attachment --><br />
arduino source code donwload:  <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5195" target="_blank" title="">Tuya_AdapterV2_RS485_Energy_Meter.zip</a> (Size: 2.41 KB / Downloads: 604)
<!-- end: postbit_attachments_attachment --><br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;ModbusMaster.h&gt;<br />
#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include "HardwareSerial.h"<br />
//---------RS485 setting<br />
#define DEBUG &nbsp;&nbsp;1<br />
<br />
#define SERIAL_BAUD &nbsp;&nbsp;9600<br />
#define RX_PIN    32<br />
#define TX_PIN    33<br />
#define SERIAL_NUM  2<br />
#define SLAVE_ADDR  225<br />
//#define READ_ADDR  0 <br />
#define READ_NUM &nbsp;&nbsp;2  <br />
<br />
uint8_t raw_vcp_data[8] = {0};<br />
uint16_t total_energy = 0;<br />
<br />
uint16_t READ_ADDR = 0;<br />
const static uint16_t addr_start_arr[6] = {<br />
  0,100,106,118,142,144,<br />
};<br />
const static String emeter_name_arr[6] = {<br />
  "Total active energy",<br />
  "Voltage",<br />
  "Current",<br />
  "Active power",<br />
  "Power factor",<br />
  "Grid frequency",<br />
};<br />
<br />
typedef struct<br />
{<br />
  float total_active_energy;<br />
  float voltage;<br />
  float current;<br />
  float active_power;<br />
  float power_factor;<br />
  float grid_freq;<br />
}EMETER_STR;<br />
typedef union<br />
{<br />
  EMETER_STR emeter_str;<br />
  float emeter_arr[6];<br />
}EMETER_UNION;<br />
<br />
/* Current device DP values */<br />
EMETER_UNION emeter_union = {0};<br />
<br />
HardwareSerial My485Serial(SERIAL_NUM);<br />
ModbusMaster node;<br />
uint16_t rcv_buf[READ_NUM] = {0};<br />
uint16_t conv_buf[READ_NUM] = {0};<br />
<br />
static int16_t cnt_read = 0;<br />
<br />
static void ReadEmeterRegs(void)<br />
{<br />
  uint32_t read_tmp = 0;<br />
  <br />
  READ_ADDR = addr_start_arr[cnt_read]; <br />
  uint8_t result = node.readHoldingRegisters(READ_ADDR, READ_NUM);<br />
  if (result == node.ku8MBSuccess) {//if communicate successful then<br />
        <br />
    for (uint8_t j = 0; j &lt; READ_NUM; j++)<br />
    {<br />
      rcv_buf[j] = node.getResponseBuffer(j);      <br />
    }<br />
    conv_buf[1] = rcv_buf[0];<br />
    conv_buf[0] = rcv_buf[1];<br />
    emeter_union.emeter_arr[cnt_read] = *(float*)&amp;conv_buf[0];<br />
    switch(cnt_read)<br />
    {<br />
      case 0:total_energy = emeter_union.emeter_arr[cnt_read] * 10;break;<br />
      case 1:<br />
        read_tmp = emeter_union.emeter_arr[cnt_read] * 10;<br />
        raw_vcp_data[0] = (read_tmp &amp;0xff00)&gt;&gt;8;<br />
        raw_vcp_data[1] = (read_tmp &amp;0x0ff);<br />
        break;<br />
      case 2:<br />
        read_tmp = emeter_union.emeter_arr[cnt_read] * 1000;<br />
        raw_vcp_data[2] = (read_tmp &amp;0x0ff0000)&gt;&gt;16;<br />
        raw_vcp_data[3] = (read_tmp &amp;0x0ff00)&gt;&gt;8;<br />
        raw_vcp_data[4] = read_tmp &amp;0x0ff;<br />
        break;<br />
      case 3:<br />
        read_tmp = emeter_union.emeter_arr[cnt_read] * 1000;<br />
        raw_vcp_data[5] = (read_tmp &amp;0x0ff0000)&gt;&gt;16;<br />
        raw_vcp_data[6] = (read_tmp &amp;0x0ff00)&gt;&gt;8;<br />
        raw_vcp_data[7] = read_tmp &amp;0x0ff;<br />
        break;<br />
      default:break;<br />
    }<br />
    #if DEBUG<br />
    Serial1.print(emeter_name_arr[cnt_read]);<br />
    Serial1.print(":&lt;HEX 0x");<br />
    Serial1.print(rcv_buf[0],HEX);<br />
    Serial1.print(" ");<br />
    Serial1.print(rcv_buf[1],HEX);<br />
    Serial1.print(" FVALUE ");<br />
    Serial1.print(emeter_union.emeter_arr[cnt_read]);<br />
    Serial1.println("&gt; ");<br />
    #endif<br />
    cnt_read++;<br />
    if(cnt_read&gt;=4) cnt_read = 0; <br />
  }<br />
  else <br />
  {<br />
    node.clearResponseBuffer();<br />
  }<br />
}<br />
<br />
//---RS485 setting end----<br />
<br />
TuyaWifi my_device;<br />
<br />
<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
<br />
/* Data point define */<br />
typedef enum<br />
{<br />
  DPID_TOTAL_ENERGY = 1,<br />
  //DPID_VOLTAGE,<br />
  //DPID_CURRENT,<br />
  //DPID_ACTIVE_POWER,<br />
  DPID_VCP = 6,<br />
  //DPID_POWER_FACTOR,<br />
  //DPID_GRID_FREQ,<br />
}EMETER_DPID_DEF;<br />
 <br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                   &nbsp;&nbsp;dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_TOTAL_ENERGY, DP_TYPE_VALUE},<br />
  {DPID_VCP, DP_TYPE_RAW},<br />
};<br />
<br />
unsigned char pid[] = {"gl3gvyksva54unam"}; ////change pid here....<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
void setup() <br />
{<br />
<br />
  My485Serial.begin(SERIAL_BAUD, SERIAL_8E1, RX_PIN, TX_PIN); //RS485 serial port<br />
  node.begin(SLAVE_ADDR,My485Serial);<br />
  Serial.begin(9600,SERIAL_8N1,22,19);  //Tuya module serial port<br />
  Serial1.begin(115200,SERIAL_8N1,3,1); &nbsp;&nbsp;//USB serial port<br />
<br />
  <br />
  // Serial with tuyawifi<br />
  <br />
  <br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 2);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
  <br />
  //delay(300);<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  ReadEmeterRegs();  // read RS485 sensor<br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  /* report the temperature and humidity */<br />
  if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || <br />
      (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)) {<br />
    dp_update_all();<br />
  }<br />
<br />
  delay(1000);<br />
}<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  /* all DP only report */<br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{ <br />
  my_device.mcu_dp_update((unsigned char)DPID_TOTAL_ENERGY, total_energy, 1);<br />
  my_device.mcu_dp_update((unsigned char)DPID_VCP, raw_vcp_data, 8);<br />
}</code></div></div><!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5196" target="_blank" title="">tuya-meter-1.jpg</a> (Size: 144.8 KB / Downloads: 1116)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5197" target="_blank" title="">tuya-meter-2.jpg</a> (Size: 59.07 KB / Downloads: 1162)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5198" target="_blank" title="">tuya-meter-3.jpg</a> (Size: 57.7 KB / Downloads: 1170)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5199" target="_blank" title="">tuya-meter-4.jpg</a> (Size: 59.57 KB / Downloads: 1140)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5200" target="_blank" title="">tuya-meter-5.jpg</a> (Size: 59.85 KB / Downloads: 1169)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5201" target="_blank" title="">tuya-meter-6.jpg</a> (Size: 56.67 KB / Downloads: 1180)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5202" target="_blank" title="">tuya-meter-7.jpg</a> (Size: 57.04 KB / Downloads: 1176)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5203" target="_blank" title="">tuya-meter-8.jpg</a> (Size: 57.88 KB / Downloads: 1174)
<!-- end: postbit_attachments_attachment --><br /><!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5205" target="_blank" title="">tuya-meter-10.jpg</a> (Size: 62.79 KB / Downloads: 968)
<!-- end: postbit_attachments_attachment -->]]></description>
			<content:encoded><![CDATA[let your own RS485 AC energy meter work with Tuya app, so that can remote monitor meter data by Tuya app via internet.<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5204" target="_blank" title="">tuya-meter-9.jpg</a> (Size: 155.47 KB / Downloads: 1108)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5206" target="_blank" title="">tuya-meter-app1.jpg</a> (Size: 36.72 KB / Downloads: 1197)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5207" target="_blank" title="">tuya-meter-app2.jpg</a> (Size: 35.44 KB / Downloads: 1187)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5194" target="_blank" title="">arduino-code.png</a> (Size: 128.54 KB / Downloads: 1162)
<!-- end: postbit_attachments_attachment --><br />
arduino source code donwload:  <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5195" target="_blank" title="">Tuya_AdapterV2_RS485_Energy_Meter.zip</a> (Size: 2.41 KB / Downloads: 604)
<!-- end: postbit_attachments_attachment --><br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;ModbusMaster.h&gt;<br />
#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include "HardwareSerial.h"<br />
//---------RS485 setting<br />
#define DEBUG &nbsp;&nbsp;1<br />
<br />
#define SERIAL_BAUD &nbsp;&nbsp;9600<br />
#define RX_PIN    32<br />
#define TX_PIN    33<br />
#define SERIAL_NUM  2<br />
#define SLAVE_ADDR  225<br />
//#define READ_ADDR  0 <br />
#define READ_NUM &nbsp;&nbsp;2  <br />
<br />
uint8_t raw_vcp_data[8] = {0};<br />
uint16_t total_energy = 0;<br />
<br />
uint16_t READ_ADDR = 0;<br />
const static uint16_t addr_start_arr[6] = {<br />
  0,100,106,118,142,144,<br />
};<br />
const static String emeter_name_arr[6] = {<br />
  "Total active energy",<br />
  "Voltage",<br />
  "Current",<br />
  "Active power",<br />
  "Power factor",<br />
  "Grid frequency",<br />
};<br />
<br />
typedef struct<br />
{<br />
  float total_active_energy;<br />
  float voltage;<br />
  float current;<br />
  float active_power;<br />
  float power_factor;<br />
  float grid_freq;<br />
}EMETER_STR;<br />
typedef union<br />
{<br />
  EMETER_STR emeter_str;<br />
  float emeter_arr[6];<br />
}EMETER_UNION;<br />
<br />
/* Current device DP values */<br />
EMETER_UNION emeter_union = {0};<br />
<br />
HardwareSerial My485Serial(SERIAL_NUM);<br />
ModbusMaster node;<br />
uint16_t rcv_buf[READ_NUM] = {0};<br />
uint16_t conv_buf[READ_NUM] = {0};<br />
<br />
static int16_t cnt_read = 0;<br />
<br />
static void ReadEmeterRegs(void)<br />
{<br />
  uint32_t read_tmp = 0;<br />
  <br />
  READ_ADDR = addr_start_arr[cnt_read]; <br />
  uint8_t result = node.readHoldingRegisters(READ_ADDR, READ_NUM);<br />
  if (result == node.ku8MBSuccess) {//if communicate successful then<br />
        <br />
    for (uint8_t j = 0; j &lt; READ_NUM; j++)<br />
    {<br />
      rcv_buf[j] = node.getResponseBuffer(j);      <br />
    }<br />
    conv_buf[1] = rcv_buf[0];<br />
    conv_buf[0] = rcv_buf[1];<br />
    emeter_union.emeter_arr[cnt_read] = *(float*)&amp;conv_buf[0];<br />
    switch(cnt_read)<br />
    {<br />
      case 0:total_energy = emeter_union.emeter_arr[cnt_read] * 10;break;<br />
      case 1:<br />
        read_tmp = emeter_union.emeter_arr[cnt_read] * 10;<br />
        raw_vcp_data[0] = (read_tmp &amp;0xff00)&gt;&gt;8;<br />
        raw_vcp_data[1] = (read_tmp &amp;0x0ff);<br />
        break;<br />
      case 2:<br />
        read_tmp = emeter_union.emeter_arr[cnt_read] * 1000;<br />
        raw_vcp_data[2] = (read_tmp &amp;0x0ff0000)&gt;&gt;16;<br />
        raw_vcp_data[3] = (read_tmp &amp;0x0ff00)&gt;&gt;8;<br />
        raw_vcp_data[4] = read_tmp &amp;0x0ff;<br />
        break;<br />
      case 3:<br />
        read_tmp = emeter_union.emeter_arr[cnt_read] * 1000;<br />
        raw_vcp_data[5] = (read_tmp &amp;0x0ff0000)&gt;&gt;16;<br />
        raw_vcp_data[6] = (read_tmp &amp;0x0ff00)&gt;&gt;8;<br />
        raw_vcp_data[7] = read_tmp &amp;0x0ff;<br />
        break;<br />
      default:break;<br />
    }<br />
    #if DEBUG<br />
    Serial1.print(emeter_name_arr[cnt_read]);<br />
    Serial1.print(":&lt;HEX 0x");<br />
    Serial1.print(rcv_buf[0],HEX);<br />
    Serial1.print(" ");<br />
    Serial1.print(rcv_buf[1],HEX);<br />
    Serial1.print(" FVALUE ");<br />
    Serial1.print(emeter_union.emeter_arr[cnt_read]);<br />
    Serial1.println("&gt; ");<br />
    #endif<br />
    cnt_read++;<br />
    if(cnt_read&gt;=4) cnt_read = 0; <br />
  }<br />
  else <br />
  {<br />
    node.clearResponseBuffer();<br />
  }<br />
}<br />
<br />
//---RS485 setting end----<br />
<br />
TuyaWifi my_device;<br />
<br />
<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
<br />
/* Data point define */<br />
typedef enum<br />
{<br />
  DPID_TOTAL_ENERGY = 1,<br />
  //DPID_VOLTAGE,<br />
  //DPID_CURRENT,<br />
  //DPID_ACTIVE_POWER,<br />
  DPID_VCP = 6,<br />
  //DPID_POWER_FACTOR,<br />
  //DPID_GRID_FREQ,<br />
}EMETER_DPID_DEF;<br />
 <br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                   &nbsp;&nbsp;dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_TOTAL_ENERGY, DP_TYPE_VALUE},<br />
  {DPID_VCP, DP_TYPE_RAW},<br />
};<br />
<br />
unsigned char pid[] = {"gl3gvyksva54unam"}; ////change pid here....<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
void setup() <br />
{<br />
<br />
  My485Serial.begin(SERIAL_BAUD, SERIAL_8E1, RX_PIN, TX_PIN); //RS485 serial port<br />
  node.begin(SLAVE_ADDR,My485Serial);<br />
  Serial.begin(9600,SERIAL_8N1,22,19);  //Tuya module serial port<br />
  Serial1.begin(115200,SERIAL_8N1,3,1); &nbsp;&nbsp;//USB serial port<br />
<br />
  <br />
  // Serial with tuyawifi<br />
  <br />
  <br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 2);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
  <br />
  //delay(300);<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  ReadEmeterRegs();  // read RS485 sensor<br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  /* report the temperature and humidity */<br />
  if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || <br />
      (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)) {<br />
    dp_update_all();<br />
  }<br />
<br />
  delay(1000);<br />
}<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  /* all DP only report */<br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{ <br />
  my_device.mcu_dp_update((unsigned char)DPID_TOTAL_ENERGY, total_energy, 1);<br />
  my_device.mcu_dp_update((unsigned char)DPID_VCP, raw_vcp_data, 8);<br />
}</code></div></div><!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5196" target="_blank" title="">tuya-meter-1.jpg</a> (Size: 144.8 KB / Downloads: 1116)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5197" target="_blank" title="">tuya-meter-2.jpg</a> (Size: 59.07 KB / Downloads: 1162)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5198" target="_blank" title="">tuya-meter-3.jpg</a> (Size: 57.7 KB / Downloads: 1170)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5199" target="_blank" title="">tuya-meter-4.jpg</a> (Size: 59.57 KB / Downloads: 1140)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5200" target="_blank" title="">tuya-meter-5.jpg</a> (Size: 59.85 KB / Downloads: 1169)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5201" target="_blank" title="">tuya-meter-6.jpg</a> (Size: 56.67 KB / Downloads: 1180)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5202" target="_blank" title="">tuya-meter-7.jpg</a> (Size: 57.04 KB / Downloads: 1176)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5203" target="_blank" title="">tuya-meter-8.jpg</a> (Size: 57.88 KB / Downloads: 1174)
<!-- end: postbit_attachments_attachment --><br /><!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5205" target="_blank" title="">tuya-meter-10.jpg</a> (Size: 62.79 KB / Downloads: 968)
<!-- end: postbit_attachments_attachment -->]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Tuya adapter V2 arduino source code - RS485 temperature humidity sensor]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=5702</link>
			<pubDate>Mon, 06 May 2024 09:35:47 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=1">admin</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=5702</guid>
			<description><![CDATA[let your own RS485 temperature and humidity sensor can work with Tuya app, so that can remote monitor sensor data and make AUTOMATION by Tuya app via internet.<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5186" target="_blank" title="">tuya-adapter-temperature.jpg</a> (Size: 380.63 KB / Downloads: 1223)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5189" target="_blank" title="">tuya-app-1.jpg</a> (Size: 36.05 KB / Downloads: 1162)
<!-- end: postbit_attachments_attachment --> <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5190" target="_blank" title="">tuya-app-2.jpg</a> (Size: 37.5 KB / Downloads: 1197)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5187" target="_blank" title="">Tuya adatperV2-RS485-Temperature.png</a> (Size: 113.4 KB / Downloads: 1167)
<!-- end: postbit_attachments_attachment --><br />
arduino source code donwload: <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5188" target="_blank" title="">Tuya_AdapterV2_RS485_Temper_Hum.zip</a> (Size: 1.98 KB / Downloads: 638)
<!-- end: postbit_attachments_attachment --><br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;ModbusMaster.h&gt;<br />
#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include "HardwareSerial.h"<br />
//---------RS485 setting<br />
#define DEBUG &nbsp;&nbsp;1<br />
<br />
#define SERIAL_BAUD &nbsp;&nbsp;9600<br />
#define RX_PIN    32<br />
#define TX_PIN    33<br />
#define SERIAL_NUM  2<br />
#define SLAVE_ADDR  1<br />
#define READ_ADDR  0 <br />
#define READ_NUM &nbsp;&nbsp;2  <br />
<br />
HardwareSerial My485Serial(SERIAL_NUM);<br />
ModbusMaster node;<br />
uint16_t rcv_buf[READ_NUM] = {0};<br />
float get_value[2] = {0.0};<br />
<br />
/* Current device DP values */<br />
int temperature = 0;<br />
int humidity = 0;<br />
<br />
static void ReadInputRegs(void)<br />
{<br />
  uint8_t result = node.readInputRegisters(READ_ADDR, READ_NUM);<br />
  if (result == node.ku8MBSuccess) {//if communicate successful then<br />
    #if DEBUG<br />
    Serial1.print("Result:");  <br />
    #endif<br />
    <br />
    for (uint8_t j = 0; j &lt; READ_NUM; j++)<br />
    {<br />
      rcv_buf[j] = node.getResponseBuffer(j);<br />
      #if 0 ///just for debug.<br />
      Serial1.print(" 0x");<br />
      Serial1.print( rcv_buf[j], HEX );//display data in hex.<br />
      #endif <br />
    }<br />
    get_value[0] = rcv_buf[0]/10;///huminity <br />
    get_value[1] = rcv_buf[1]/10;///temperature <br />
<br />
    temperature = rcv_buf[0];<br />
    humidity = get_value[1];<br />
    <br />
    #if DEBUG<br />
    Serial1.print("humi.&lt;");  <br />
    Serial1.print(get_value[1]);<br />
    Serial1.print("%&gt;"); <br />
    Serial1.print(" temp.&lt;");  <br />
    Serial1.print(get_value[0]);<br />
    Serial1.println("°c&gt;");<br />
    #endif<br />
<br />
    //delay(300); <br />
  }<br />
  else <br />
  {<br />
    node.clearResponseBuffer();<br />
  }<br />
}<br />
<br />
//---RS485 setting end----<br />
<br />
TuyaWifi my_device;<br />
<br />
<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
/* Data point define */<br />
#define DPID_TEMP_CURRENT   &nbsp;&nbsp;1<br />
#define DPID_HUMIDITY_CURRENT 2<br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                   &nbsp;&nbsp;dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_TEMP_CURRENT, DP_TYPE_VALUE},<br />
  {DPID_HUMIDITY_CURRENT, DP_TYPE_VALUE},<br />
};<br />
<br />
unsigned char pid[] = {"dpyditdgaadmqw8m"};<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
void setup() <br />
{<br />
<br />
  My485Serial.begin(SERIAL_BAUD, SERIAL_8N1, RX_PIN, TX_PIN); //RS485 serial port<br />
  node.begin(SLAVE_ADDR,My485Serial);<br />
  Serial.begin(9600,SERIAL_8N1,22,19);  //Tuya module serial port<br />
  Serial1.begin(115200,SERIAL_8N1,3,1); &nbsp;&nbsp;//USB serial port<br />
<br />
  <br />
  // Serial with tuyawifi<br />
  <br />
  <br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 2);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
  <br />
  //delay(300);<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  ReadInputRegs();  // read RS485 sensor<br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  /* report the temperature and humidity */<br />
  if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)) {<br />
    my_device.mcu_dp_update(DPID_TEMP_CURRENT, temperature, 1);<br />
    my_device.mcu_dp_update(DPID_HUMIDITY_CURRENT, humidity, 1);<br />
  }<br />
<br />
  delay(1000);<br />
}<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  /* all DP only report */<br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{<br />
  my_device.mcu_dp_update(DPID_TEMP_CURRENT, temperature, 1);<br />
  my_device.mcu_dp_update(DPID_HUMIDITY_CURRENT, humidity, 1);<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[let your own RS485 temperature and humidity sensor can work with Tuya app, so that can remote monitor sensor data and make AUTOMATION by Tuya app via internet.<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5186" target="_blank" title="">tuya-adapter-temperature.jpg</a> (Size: 380.63 KB / Downloads: 1223)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5189" target="_blank" title="">tuya-app-1.jpg</a> (Size: 36.05 KB / Downloads: 1162)
<!-- end: postbit_attachments_attachment --> <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5190" target="_blank" title="">tuya-app-2.jpg</a> (Size: 37.5 KB / Downloads: 1197)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5187" target="_blank" title="">Tuya adatperV2-RS485-Temperature.png</a> (Size: 113.4 KB / Downloads: 1167)
<!-- end: postbit_attachments_attachment --><br />
arduino source code donwload: <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=5188" target="_blank" title="">Tuya_AdapterV2_RS485_Temper_Hum.zip</a> (Size: 1.98 KB / Downloads: 638)
<!-- end: postbit_attachments_attachment --><br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;ModbusMaster.h&gt;<br />
#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include "HardwareSerial.h"<br />
//---------RS485 setting<br />
#define DEBUG &nbsp;&nbsp;1<br />
<br />
#define SERIAL_BAUD &nbsp;&nbsp;9600<br />
#define RX_PIN    32<br />
#define TX_PIN    33<br />
#define SERIAL_NUM  2<br />
#define SLAVE_ADDR  1<br />
#define READ_ADDR  0 <br />
#define READ_NUM &nbsp;&nbsp;2  <br />
<br />
HardwareSerial My485Serial(SERIAL_NUM);<br />
ModbusMaster node;<br />
uint16_t rcv_buf[READ_NUM] = {0};<br />
float get_value[2] = {0.0};<br />
<br />
/* Current device DP values */<br />
int temperature = 0;<br />
int humidity = 0;<br />
<br />
static void ReadInputRegs(void)<br />
{<br />
  uint8_t result = node.readInputRegisters(READ_ADDR, READ_NUM);<br />
  if (result == node.ku8MBSuccess) {//if communicate successful then<br />
    #if DEBUG<br />
    Serial1.print("Result:");  <br />
    #endif<br />
    <br />
    for (uint8_t j = 0; j &lt; READ_NUM; j++)<br />
    {<br />
      rcv_buf[j] = node.getResponseBuffer(j);<br />
      #if 0 ///just for debug.<br />
      Serial1.print(" 0x");<br />
      Serial1.print( rcv_buf[j], HEX );//display data in hex.<br />
      #endif <br />
    }<br />
    get_value[0] = rcv_buf[0]/10;///huminity <br />
    get_value[1] = rcv_buf[1]/10;///temperature <br />
<br />
    temperature = rcv_buf[0];<br />
    humidity = get_value[1];<br />
    <br />
    #if DEBUG<br />
    Serial1.print("humi.&lt;");  <br />
    Serial1.print(get_value[1]);<br />
    Serial1.print("%&gt;"); <br />
    Serial1.print(" temp.&lt;");  <br />
    Serial1.print(get_value[0]);<br />
    Serial1.println("°c&gt;");<br />
    #endif<br />
<br />
    //delay(300); <br />
  }<br />
  else <br />
  {<br />
    node.clearResponseBuffer();<br />
  }<br />
}<br />
<br />
//---RS485 setting end----<br />
<br />
TuyaWifi my_device;<br />
<br />
<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
/* Data point define */<br />
#define DPID_TEMP_CURRENT   &nbsp;&nbsp;1<br />
#define DPID_HUMIDITY_CURRENT 2<br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                   &nbsp;&nbsp;dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_TEMP_CURRENT, DP_TYPE_VALUE},<br />
  {DPID_HUMIDITY_CURRENT, DP_TYPE_VALUE},<br />
};<br />
<br />
unsigned char pid[] = {"dpyditdgaadmqw8m"};<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
void setup() <br />
{<br />
<br />
  My485Serial.begin(SERIAL_BAUD, SERIAL_8N1, RX_PIN, TX_PIN); //RS485 serial port<br />
  node.begin(SLAVE_ADDR,My485Serial);<br />
  Serial.begin(9600,SERIAL_8N1,22,19);  //Tuya module serial port<br />
  Serial1.begin(115200,SERIAL_8N1,3,1); &nbsp;&nbsp;//USB serial port<br />
<br />
  <br />
  // Serial with tuyawifi<br />
  <br />
  <br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 2);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
  <br />
  //delay(300);<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  ReadInputRegs();  // read RS485 sensor<br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  /* report the temperature and humidity */<br />
  if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)) {<br />
    my_device.mcu_dp_update(DPID_TEMP_CURRENT, temperature, 1);<br />
    my_device.mcu_dp_update(DPID_HUMIDITY_CURRENT, humidity, 1);<br />
  }<br />
<br />
  delay(1000);<br />
}<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  /* all DP only report */<br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{<br />
  my_device.mcu_dp_update(DPID_TEMP_CURRENT, temperature, 1);<br />
  my_device.mcu_dp_update(DPID_HUMIDITY_CURRENT, humidity, 1);<br />
}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[KC868-ATC V1 dont work on Tuya]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=5587</link>
			<pubDate>Sat, 20 Apr 2024 10:52:03 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=4602">FelipeAlmanca</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=5587</guid>
			<description><![CDATA[I received the board yesterday and start trying. The wifi state led blinks when its looking for conection, but after connect the led turn off, the switchs on Tuya app does not respond. After clean wifi config and connect again the app shows the switchs at the last change I did, and the wifi led star on and turn off after couple seconds. At the Tuya app shows all 32 switchs and the digital the inputs correct.]]></description>
			<content:encoded><![CDATA[I received the board yesterday and start trying. The wifi state led blinks when its looking for conection, but after connect the led turn off, the switchs on Tuya app does not respond. After clean wifi config and connect again the app shows the switchs at the last change I did, and the wifi led star on and turn off after couple seconds. At the Tuya app shows all 32 switchs and the digital the inputs correct.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Tuya adapter V2 arduino source code - 32CH virtual switch]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=5280</link>
			<pubDate>Tue, 19 Mar 2024 10:03:39 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=1">admin</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=5280</guid>
			<description><![CDATA[<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include "HardwareSerial.h"<br />
TuyaWifi my_device;<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
/* Data point define */<br />
#define DPID_SWITCH_1   1<br />
#define DPID_SWITCH_2   2<br />
#define DPID_SWITCH_3   3<br />
#define DPID_SWITCH_4   4<br />
#define DPID_SWITCH_5   5<br />
#define DPID_SWITCH_6   6<br />
#define DPID_SWITCH_7   113<br />
#define DPID_SWITCH_8   114<br />
#define DPID_SWITCH_9   115<br />
#define DPID_SWITCH_10  116<br />
#define DPID_SWITCH_11  117<br />
#define DPID_SWITCH_12  118<br />
#define DPID_SWITCH_13  119<br />
#define DPID_SWITCH_14  120<br />
#define DPID_SWITCH_15  121<br />
#define DPID_SWITCH_16  122<br />
#define DPID_SWITCH_17  123<br />
#define DPID_SWITCH_18  124<br />
#define DPID_SWITCH_19  125<br />
#define DPID_SWITCH_20  126<br />
#define DPID_SWITCH_21  127<br />
#define DPID_SWITCH_22  128<br />
#define DPID_SWITCH_23  129<br />
#define DPID_SWITCH_24  130<br />
#define DPID_SWITCH_25  131<br />
#define DPID_SWITCH_26  132<br />
#define DPID_SWITCH_27  133<br />
#define DPID_SWITCH_28  134<br />
#define DPID_SWITCH_29  135<br />
#define DPID_SWITCH_30  136<br />
#define DPID_SWITCH_31  137<br />
#define DPID_SWITCH_32  138<br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
unsigned char switch_1_state = 0;<br />
unsigned char switch_2_state = 0;<br />
unsigned char switch_3_state = 0;<br />
unsigned char switch_4_state = 0;<br />
unsigned char switch_5_state = 0;<br />
unsigned char switch_6_state = 0;<br />
<br />
unsigned char switch_7_state = 0;<br />
unsigned char switch_8_state = 0;<br />
unsigned char switch_9_state = 0;<br />
unsigned char switch_10_state = 0;<br />
unsigned char switch_11_state = 0;<br />
unsigned char switch_12_state = 0;<br />
unsigned char switch_13_state = 0;<br />
unsigned char switch_14_state = 0;<br />
unsigned char switch_15_state = 0;<br />
unsigned char switch_16_state = 0;<br />
unsigned char switch_17_state = 0;<br />
unsigned char switch_18_state = 0;<br />
unsigned char switch_19_state = 0;<br />
unsigned char switch_20_state = 0;<br />
unsigned char switch_21_state = 0;<br />
unsigned char switch_22_state = 0;<br />
unsigned char switch_23_state = 0;<br />
unsigned char switch_24_state = 0;<br />
unsigned char switch_25_state = 0;<br />
unsigned char switch_26_state = 0;<br />
unsigned char switch_27_state = 0;<br />
unsigned char switch_28_state = 0;<br />
unsigned char switch_29_state = 0;<br />
unsigned char switch_30_state = 0;<br />
unsigned char switch_31_state = 0;<br />
unsigned char switch_32_state = 0;<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_SWITCH_1, DP_TYPE_BOOL},{DPID_SWITCH_2, DP_TYPE_BOOL},{DPID_SWITCH_3, DP_TYPE_BOOL},{DPID_SWITCH_4, DP_TYPE_BOOL},{DPID_SWITCH_5, DP_TYPE_BOOL},{DPID_SWITCH_6, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_7, DP_TYPE_BOOL},{DPID_SWITCH_8, DP_TYPE_BOOL},{DPID_SWITCH_9, DP_TYPE_BOOL},{DPID_SWITCH_10, DP_TYPE_BOOL},{DPID_SWITCH_11, DP_TYPE_BOOL},{DPID_SWITCH_12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_13, DP_TYPE_BOOL},{DPID_SWITCH_14, DP_TYPE_BOOL},{DPID_SWITCH_15, DP_TYPE_BOOL},{DPID_SWITCH_16, DP_TYPE_BOOL},{DPID_SWITCH_17, DP_TYPE_BOOL},{DPID_SWITCH_18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_19, DP_TYPE_BOOL},{DPID_SWITCH_20, DP_TYPE_BOOL},{DPID_SWITCH_21, DP_TYPE_BOOL},{DPID_SWITCH_22, DP_TYPE_BOOL},{DPID_SWITCH_23, DP_TYPE_BOOL},{DPID_SWITCH_24, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_25, DP_TYPE_BOOL},{DPID_SWITCH_26, DP_TYPE_BOOL},{DPID_SWITCH_27, DP_TYPE_BOOL},{DPID_SWITCH_28, DP_TYPE_BOOL},{DPID_SWITCH_29, DP_TYPE_BOOL},{DPID_SWITCH_30, DP_TYPE_BOOL},<br />
 {DPID_SWITCH_31, DP_TYPE_BOOL},{DPID_SWITCH_32, DP_TYPE_BOOL},<br />
};<br />
<br />
unsigned char pid[] = {"3ocztklfu3q32jab"};<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
void setup() <br />
{<br />
  // Serial with tuyawifi<br />
  Serial.begin(9600,SERIAL_8N1,22,19);<br />
<br />
  Serial2.begin(9600,SERIAL_8N1,32,33);<br />
  Serial2.println("RS485 SEND is OK!!");<br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 1);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  while(Serial2.available()&gt;0){<br />
    Serial2.print((char)Serial2.read());<br />
   }<br />
   <br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  delay(10);<br />
}<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  switch(dpid) {<br />
    case DPID_SWITCH_1:<br />
      switch_1_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, length);<br />
    break;<br />
    case DPID_SWITCH_2:<br />
      switch_2_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, length);<br />
    break;<br />
    case DPID_SWITCH_3:<br />
      switch_3_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, length);<br />
    break;<br />
    case DPID_SWITCH_4:<br />
      switch_4_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, length);<br />
    break;<br />
    case DPID_SWITCH_5:<br />
      switch_5_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, length);<br />
    break;<br />
    case DPID_SWITCH_6:<br />
      switch_6_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, length);<br />
    break;<br />
    case DPID_SWITCH_7:<br />
      switch_7_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_7, switch_7_state, length);<br />
    break;<br />
    case DPID_SWITCH_8:<br />
      switch_8_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_8, switch_8_state, length);<br />
    break;<br />
    case DPID_SWITCH_9:<br />
      switch_9_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_9, switch_9_state, length);<br />
    break;<br />
    case DPID_SWITCH_10:<br />
      switch_10_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_10, switch_10_state, length);<br />
    break;<br />
    case DPID_SWITCH_11:<br />
      switch_11_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_11, switch_11_state, length);<br />
    break;<br />
    case DPID_SWITCH_12:<br />
      switch_12_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_12, switch_12_state, length);<br />
    break;<br />
    case DPID_SWITCH_13:<br />
      switch_13_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_13, switch_13_state, length);<br />
    break;<br />
    case DPID_SWITCH_14:<br />
      switch_14_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_14, switch_14_state, length);<br />
    break;<br />
    case DPID_SWITCH_15:<br />
      switch_15_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_15, switch_15_state, length);<br />
    break;<br />
    case DPID_SWITCH_16:<br />
      switch_16_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_16, switch_16_state, length);<br />
    break;<br />
    case DPID_SWITCH_17:<br />
      switch_17_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_17, switch_17_state, length);<br />
    break;<br />
    case DPID_SWITCH_18:<br />
      switch_18_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_18, switch_18_state, length);<br />
    break;<br />
    case DPID_SWITCH_19:<br />
      switch_19_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_19, switch_19_state, length);<br />
    break;<br />
    case DPID_SWITCH_20:<br />
      switch_20_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_20, switch_20_state, length);<br />
    break;<br />
    case DPID_SWITCH_21:<br />
      switch_21_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_21, switch_21_state, length);<br />
    break;<br />
    case DPID_SWITCH_22:<br />
      switch_22_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_22, switch_22_state, length);<br />
    break;<br />
    case DPID_SWITCH_23:<br />
      switch_23_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_23, switch_23_state, length);<br />
    break;<br />
    case DPID_SWITCH_24:<br />
      switch_24_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_24, switch_24_state, length);<br />
    break;<br />
    case DPID_SWITCH_25:<br />
      switch_25_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_25, switch_25_state, length);<br />
    break;<br />
    case DPID_SWITCH_26:<br />
      switch_26_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_26, switch_26_state, length);<br />
    break;<br />
    case DPID_SWITCH_27:<br />
      switch_27_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_27, switch_27_state, length);<br />
    break;<br />
    case DPID_SWITCH_28:<br />
      switch_28_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_28, switch_28_state, length);<br />
    break;<br />
    case DPID_SWITCH_29:<br />
      switch_29_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_29, switch_29_state, length);<br />
    break;<br />
    case DPID_SWITCH_30:<br />
      switch_30_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_30, switch_30_state, length);<br />
    break;<br />
    case DPID_SWITCH_31:<br />
      switch_31_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_31, switch_31_state, length);<br />
    break;<br />
    case DPID_SWITCH_32:<br />
      switch_32_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_32, switch_32_state, length);<br />
    break;<br />
    <br />
    default:break;<br />
  }<br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{<br />
  my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_7, switch_7_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_8, switch_8_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_9, switch_9_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_10, switch_10_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_11, switch_11_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_12, switch_12_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_13, switch_13_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_14, switch_14_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_15, switch_15_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_16, switch_16_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_17, switch_17_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_18, switch_18_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_19, switch_19_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_20, switch_20_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_21, switch_21_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_22, switch_22_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_23, switch_23_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_24, switch_24_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_25, switch_25_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_26, switch_26_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_27, switch_27_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_28, switch_28_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_29, switch_29_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_30, switch_30_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_31, switch_31_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_32, switch_32_state, 1);<br />
}</code></div></div><br />
arduino source code download: <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=4850" target="_blank" title="">voice-control.ino.zip</a> (Size: 2.31 KB / Downloads: 559)
<!-- end: postbit_attachments_attachment --><br />
<br />
esp32 board need V1.0.6 verision:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=4851" target="_blank" title="">esp32-board.png</a> (Size: 99.25 KB / Downloads: 1003)
<!-- end: postbit_attachments_attachment --><br />
<br />
tuya arduino library config file set to "0". because use ESP32's GPIO for BUTTON and LED with Tuya network function, just set as photo:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=4852" target="_blank" title="">tuya_arduion_library_config.jpg</a> (Size: 83.71 KB / Downloads: 973)
<!-- end: postbit_attachments_attachment -->]]></description>
			<content:encoded><![CDATA[<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include "HardwareSerial.h"<br />
TuyaWifi my_device;<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 25;<br />
<br />
/* Data point define */<br />
#define DPID_SWITCH_1   1<br />
#define DPID_SWITCH_2   2<br />
#define DPID_SWITCH_3   3<br />
#define DPID_SWITCH_4   4<br />
#define DPID_SWITCH_5   5<br />
#define DPID_SWITCH_6   6<br />
#define DPID_SWITCH_7   113<br />
#define DPID_SWITCH_8   114<br />
#define DPID_SWITCH_9   115<br />
#define DPID_SWITCH_10  116<br />
#define DPID_SWITCH_11  117<br />
#define DPID_SWITCH_12  118<br />
#define DPID_SWITCH_13  119<br />
#define DPID_SWITCH_14  120<br />
#define DPID_SWITCH_15  121<br />
#define DPID_SWITCH_16  122<br />
#define DPID_SWITCH_17  123<br />
#define DPID_SWITCH_18  124<br />
#define DPID_SWITCH_19  125<br />
#define DPID_SWITCH_20  126<br />
#define DPID_SWITCH_21  127<br />
#define DPID_SWITCH_22  128<br />
#define DPID_SWITCH_23  129<br />
#define DPID_SWITCH_24  130<br />
#define DPID_SWITCH_25  131<br />
#define DPID_SWITCH_26  132<br />
#define DPID_SWITCH_27  133<br />
#define DPID_SWITCH_28  134<br />
#define DPID_SWITCH_29  135<br />
#define DPID_SWITCH_30  136<br />
#define DPID_SWITCH_31  137<br />
#define DPID_SWITCH_32  138<br />
<br />
#define LED_WiFi  26<br />
//#define LED_User  33<br />
<br />
unsigned char switch_1_state = 0;<br />
unsigned char switch_2_state = 0;<br />
unsigned char switch_3_state = 0;<br />
unsigned char switch_4_state = 0;<br />
unsigned char switch_5_state = 0;<br />
unsigned char switch_6_state = 0;<br />
<br />
unsigned char switch_7_state = 0;<br />
unsigned char switch_8_state = 0;<br />
unsigned char switch_9_state = 0;<br />
unsigned char switch_10_state = 0;<br />
unsigned char switch_11_state = 0;<br />
unsigned char switch_12_state = 0;<br />
unsigned char switch_13_state = 0;<br />
unsigned char switch_14_state = 0;<br />
unsigned char switch_15_state = 0;<br />
unsigned char switch_16_state = 0;<br />
unsigned char switch_17_state = 0;<br />
unsigned char switch_18_state = 0;<br />
unsigned char switch_19_state = 0;<br />
unsigned char switch_20_state = 0;<br />
unsigned char switch_21_state = 0;<br />
unsigned char switch_22_state = 0;<br />
unsigned char switch_23_state = 0;<br />
unsigned char switch_24_state = 0;<br />
unsigned char switch_25_state = 0;<br />
unsigned char switch_26_state = 0;<br />
unsigned char switch_27_state = 0;<br />
unsigned char switch_28_state = 0;<br />
unsigned char switch_29_state = 0;<br />
unsigned char switch_30_state = 0;<br />
unsigned char switch_31_state = 0;<br />
unsigned char switch_32_state = 0;<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_SWITCH_1, DP_TYPE_BOOL},{DPID_SWITCH_2, DP_TYPE_BOOL},{DPID_SWITCH_3, DP_TYPE_BOOL},{DPID_SWITCH_4, DP_TYPE_BOOL},{DPID_SWITCH_5, DP_TYPE_BOOL},{DPID_SWITCH_6, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_7, DP_TYPE_BOOL},{DPID_SWITCH_8, DP_TYPE_BOOL},{DPID_SWITCH_9, DP_TYPE_BOOL},{DPID_SWITCH_10, DP_TYPE_BOOL},{DPID_SWITCH_11, DP_TYPE_BOOL},{DPID_SWITCH_12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_13, DP_TYPE_BOOL},{DPID_SWITCH_14, DP_TYPE_BOOL},{DPID_SWITCH_15, DP_TYPE_BOOL},{DPID_SWITCH_16, DP_TYPE_BOOL},{DPID_SWITCH_17, DP_TYPE_BOOL},{DPID_SWITCH_18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_19, DP_TYPE_BOOL},{DPID_SWITCH_20, DP_TYPE_BOOL},{DPID_SWITCH_21, DP_TYPE_BOOL},{DPID_SWITCH_22, DP_TYPE_BOOL},{DPID_SWITCH_23, DP_TYPE_BOOL},{DPID_SWITCH_24, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_25, DP_TYPE_BOOL},{DPID_SWITCH_26, DP_TYPE_BOOL},{DPID_SWITCH_27, DP_TYPE_BOOL},{DPID_SWITCH_28, DP_TYPE_BOOL},{DPID_SWITCH_29, DP_TYPE_BOOL},{DPID_SWITCH_30, DP_TYPE_BOOL},<br />
 {DPID_SWITCH_31, DP_TYPE_BOOL},{DPID_SWITCH_32, DP_TYPE_BOOL},<br />
};<br />
<br />
unsigned char pid[] = {"3ocztklfu3q32jab"};<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
void setup() <br />
{<br />
  // Serial with tuyawifi<br />
  Serial.begin(9600,SERIAL_8N1,22,19);<br />
<br />
  Serial2.begin(9600,SERIAL_8N1,32,33);<br />
  Serial2.println("RS485 SEND is OK!!");<br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
/*  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);*/<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 1);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  while(Serial2.available()&gt;0){<br />
    Serial2.print((char)Serial2.read());<br />
   }<br />
   <br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  delay(10);<br />
}<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  switch(dpid) {<br />
    case DPID_SWITCH_1:<br />
      switch_1_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, length);<br />
    break;<br />
    case DPID_SWITCH_2:<br />
      switch_2_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, length);<br />
    break;<br />
    case DPID_SWITCH_3:<br />
      switch_3_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, length);<br />
    break;<br />
    case DPID_SWITCH_4:<br />
      switch_4_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, length);<br />
    break;<br />
    case DPID_SWITCH_5:<br />
      switch_5_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, length);<br />
    break;<br />
    case DPID_SWITCH_6:<br />
      switch_6_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, length);<br />
    break;<br />
    case DPID_SWITCH_7:<br />
      switch_7_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_7, switch_7_state, length);<br />
    break;<br />
    case DPID_SWITCH_8:<br />
      switch_8_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_8, switch_8_state, length);<br />
    break;<br />
    case DPID_SWITCH_9:<br />
      switch_9_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_9, switch_9_state, length);<br />
    break;<br />
    case DPID_SWITCH_10:<br />
      switch_10_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_10, switch_10_state, length);<br />
    break;<br />
    case DPID_SWITCH_11:<br />
      switch_11_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_11, switch_11_state, length);<br />
    break;<br />
    case DPID_SWITCH_12:<br />
      switch_12_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_12, switch_12_state, length);<br />
    break;<br />
    case DPID_SWITCH_13:<br />
      switch_13_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_13, switch_13_state, length);<br />
    break;<br />
    case DPID_SWITCH_14:<br />
      switch_14_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_14, switch_14_state, length);<br />
    break;<br />
    case DPID_SWITCH_15:<br />
      switch_15_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_15, switch_15_state, length);<br />
    break;<br />
    case DPID_SWITCH_16:<br />
      switch_16_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_16, switch_16_state, length);<br />
    break;<br />
    case DPID_SWITCH_17:<br />
      switch_17_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_17, switch_17_state, length);<br />
    break;<br />
    case DPID_SWITCH_18:<br />
      switch_18_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_18, switch_18_state, length);<br />
    break;<br />
    case DPID_SWITCH_19:<br />
      switch_19_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_19, switch_19_state, length);<br />
    break;<br />
    case DPID_SWITCH_20:<br />
      switch_20_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_20, switch_20_state, length);<br />
    break;<br />
    case DPID_SWITCH_21:<br />
      switch_21_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_21, switch_21_state, length);<br />
    break;<br />
    case DPID_SWITCH_22:<br />
      switch_22_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_22, switch_22_state, length);<br />
    break;<br />
    case DPID_SWITCH_23:<br />
      switch_23_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_23, switch_23_state, length);<br />
    break;<br />
    case DPID_SWITCH_24:<br />
      switch_24_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_24, switch_24_state, length);<br />
    break;<br />
    case DPID_SWITCH_25:<br />
      switch_25_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_25, switch_25_state, length);<br />
    break;<br />
    case DPID_SWITCH_26:<br />
      switch_26_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_26, switch_26_state, length);<br />
    break;<br />
    case DPID_SWITCH_27:<br />
      switch_27_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_27, switch_27_state, length);<br />
    break;<br />
    case DPID_SWITCH_28:<br />
      switch_28_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_28, switch_28_state, length);<br />
    break;<br />
    case DPID_SWITCH_29:<br />
      switch_29_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_29, switch_29_state, length);<br />
    break;<br />
    case DPID_SWITCH_30:<br />
      switch_30_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_30, switch_30_state, length);<br />
    break;<br />
    case DPID_SWITCH_31:<br />
      switch_31_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_31, switch_31_state, length);<br />
    break;<br />
    case DPID_SWITCH_32:<br />
      switch_32_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_32, switch_32_state, length);<br />
    break;<br />
    <br />
    default:break;<br />
  }<br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{<br />
  my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_7, switch_7_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_8, switch_8_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_9, switch_9_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_10, switch_10_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_11, switch_11_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_12, switch_12_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_13, switch_13_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_14, switch_14_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_15, switch_15_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_16, switch_16_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_17, switch_17_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_18, switch_18_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_19, switch_19_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_20, switch_20_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_21, switch_21_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_22, switch_22_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_23, switch_23_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_24, switch_24_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_25, switch_25_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_26, switch_26_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_27, switch_27_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_28, switch_28_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_29, switch_29_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_30, switch_30_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_31, switch_31_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_32, switch_32_state, 1);<br />
}</code></div></div><br />
arduino source code download: <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=4850" target="_blank" title="">voice-control.ino.zip</a> (Size: 2.31 KB / Downloads: 559)
<!-- end: postbit_attachments_attachment --><br />
<br />
esp32 board need V1.0.6 verision:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=4851" target="_blank" title="">esp32-board.png</a> (Size: 99.25 KB / Downloads: 1003)
<!-- end: postbit_attachments_attachment --><br />
<br />
tuya arduino library config file set to "0". because use ESP32's GPIO for BUTTON and LED with Tuya network function, just set as photo:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=4852" target="_blank" title="">tuya_arduion_library_config.jpg</a> (Size: 83.71 KB / Downloads: 973)
<!-- end: postbit_attachments_attachment -->]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Tuya adapter V2 ESP32 I/O pin define]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=5279</link>
			<pubDate>Tue, 19 Mar 2024 09:58:14 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=1">admin</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=5279</guid>
			<description><![CDATA[RS485:<br />
RXD: 32<br />
TXD: 33<br />
<br />
Tuya module:<br />
RXD: 22<br />
TXD: 19<br />
<br />
Tuya Reset button: 25<br />
Tuya network LED: 26]]></description>
			<content:encoded><![CDATA[RS485:<br />
RXD: 32<br />
TXD: 33<br />
<br />
Tuya module:<br />
RXD: 22<br />
TXD: 19<br />
<br />
Tuya Reset button: 25<br />
Tuya network LED: 26]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Tuya_ESP32_Adapter virtual switch arduino source code for alexa and Google home speak]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=4125</link>
			<pubDate>Wed, 10 Jan 2024 14:47:54 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=1">admin</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=4125</guid>
			<description><![CDATA[firmware BIN file download with address 0x0: <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=3882" target="_blank" title="">voice-control_0x0.zip</a> (Size: 116.47 KB / Downloads: 659)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=3883" target="_blank" title="">esp32-board.png</a> (Size: 99.25 KB / Downloads: 1166)
<!-- end: postbit_attachments_attachment --><br />
arduino source code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
<br />
TuyaWifi my_device;<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 27;<br />
<br />
/* Data point define */<br />
#define DPID_SWITCH_1 &nbsp;&nbsp;1<br />
#define DPID_SWITCH_2 &nbsp;&nbsp;2<br />
#define DPID_SWITCH_3 &nbsp;&nbsp;3<br />
#define DPID_SWITCH_4 &nbsp;&nbsp;4<br />
#define DPID_SWITCH_5 &nbsp;&nbsp;5<br />
#define DPID_SWITCH_6 &nbsp;&nbsp;6<br />
#define DPID_SWITCH_7 &nbsp;&nbsp;113<br />
#define DPID_SWITCH_8 &nbsp;&nbsp;114<br />
#define DPID_SWITCH_9 &nbsp;&nbsp;115<br />
#define DPID_SWITCH_10  116<br />
#define DPID_SWITCH_11  117<br />
#define DPID_SWITCH_12  118<br />
#define DPID_SWITCH_13  119<br />
#define DPID_SWITCH_14  120<br />
#define DPID_SWITCH_15  121<br />
#define DPID_SWITCH_16  122<br />
#define DPID_SWITCH_17  123<br />
#define DPID_SWITCH_18  124<br />
#define DPID_SWITCH_19  125<br />
#define DPID_SWITCH_20  126<br />
#define DPID_SWITCH_21  127<br />
#define DPID_SWITCH_22  128<br />
#define DPID_SWITCH_23  129<br />
#define DPID_SWITCH_24  130<br />
#define DPID_SWITCH_25  131<br />
#define DPID_SWITCH_26  132<br />
#define DPID_SWITCH_27  133<br />
#define DPID_SWITCH_28  134<br />
#define DPID_SWITCH_29  135<br />
#define DPID_SWITCH_30  136<br />
#define DPID_SWITCH_31  137<br />
#define DPID_SWITCH_32  138<br />
<br />
#define LED_WiFi  26<br />
#define LED_User  33<br />
<br />
unsigned char switch_1_state = 0;<br />
unsigned char switch_2_state = 0;<br />
unsigned char switch_3_state = 0;<br />
unsigned char switch_4_state = 0;<br />
unsigned char switch_5_state = 0;<br />
unsigned char switch_6_state = 0;<br />
<br />
unsigned char switch_7_state = 0;<br />
unsigned char switch_8_state = 0;<br />
unsigned char switch_9_state = 0;<br />
unsigned char switch_10_state = 0;<br />
unsigned char switch_11_state = 0;<br />
unsigned char switch_12_state = 0;<br />
unsigned char switch_13_state = 0;<br />
unsigned char switch_14_state = 0;<br />
unsigned char switch_15_state = 0;<br />
unsigned char switch_16_state = 0;<br />
unsigned char switch_17_state = 0;<br />
unsigned char switch_18_state = 0;<br />
unsigned char switch_19_state = 0;<br />
unsigned char switch_20_state = 0;<br />
unsigned char switch_21_state = 0;<br />
unsigned char switch_22_state = 0;<br />
unsigned char switch_23_state = 0;<br />
unsigned char switch_24_state = 0;<br />
unsigned char switch_25_state = 0;<br />
unsigned char switch_26_state = 0;<br />
unsigned char switch_27_state = 0;<br />
unsigned char switch_28_state = 0;<br />
unsigned char switch_29_state = 0;<br />
unsigned char switch_30_state = 0;<br />
unsigned char switch_31_state = 0;<br />
unsigned char switch_32_state = 0;<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                   &nbsp;&nbsp;dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_SWITCH_1, DP_TYPE_BOOL},{DPID_SWITCH_2, DP_TYPE_BOOL},{DPID_SWITCH_3, DP_TYPE_BOOL},{DPID_SWITCH_4, DP_TYPE_BOOL},{DPID_SWITCH_5, DP_TYPE_BOOL},{DPID_SWITCH_6, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_7, DP_TYPE_BOOL},{DPID_SWITCH_8, DP_TYPE_BOOL},{DPID_SWITCH_9, DP_TYPE_BOOL},{DPID_SWITCH_10, DP_TYPE_BOOL},{DPID_SWITCH_11, DP_TYPE_BOOL},{DPID_SWITCH_12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_13, DP_TYPE_BOOL},{DPID_SWITCH_14, DP_TYPE_BOOL},{DPID_SWITCH_15, DP_TYPE_BOOL},{DPID_SWITCH_16, DP_TYPE_BOOL},{DPID_SWITCH_17, DP_TYPE_BOOL},{DPID_SWITCH_18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_19, DP_TYPE_BOOL},{DPID_SWITCH_20, DP_TYPE_BOOL},{DPID_SWITCH_21, DP_TYPE_BOOL},{DPID_SWITCH_22, DP_TYPE_BOOL},{DPID_SWITCH_23, DP_TYPE_BOOL},{DPID_SWITCH_24, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_25, DP_TYPE_BOOL},{DPID_SWITCH_26, DP_TYPE_BOOL},{DPID_SWITCH_27, DP_TYPE_BOOL},{DPID_SWITCH_28, DP_TYPE_BOOL},{DPID_SWITCH_29, DP_TYPE_BOOL},{DPID_SWITCH_30, DP_TYPE_BOOL},<br />
 {DPID_SWITCH_31, DP_TYPE_BOOL},{DPID_SWITCH_32, DP_TYPE_BOOL},<br />
};<br />
<br />
unsigned char pid[] = {"tsfqvoyi6pxqxq6o"};<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
void setup() <br />
{<br />
  // Serial with tuyawifi<br />
  Serial.begin(9600,SERIAL_8N1,14,13);<br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 1);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  delay(10);<br />
}<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  switch(dpid) {<br />
    case DPID_SWITCH_1:<br />
      switch_1_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, length);<br />
    break;<br />
    case DPID_SWITCH_2:<br />
      switch_2_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, length);<br />
    break;<br />
    case DPID_SWITCH_3:<br />
      switch_3_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, length);<br />
    break;<br />
    case DPID_SWITCH_4:<br />
      switch_4_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, length);<br />
    break;<br />
    case DPID_SWITCH_5:<br />
      switch_5_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, length);<br />
    break;<br />
    case DPID_SWITCH_6:<br />
      switch_6_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, length);<br />
    break;<br />
    case DPID_SWITCH_7:<br />
      switch_7_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_7, switch_7_state, length);<br />
    break;<br />
    case DPID_SWITCH_8:<br />
      switch_8_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_8, switch_8_state, length);<br />
    break;<br />
    case DPID_SWITCH_9:<br />
      switch_9_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_9, switch_9_state, length);<br />
    break;<br />
    case DPID_SWITCH_10:<br />
      switch_10_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_10, switch_10_state, length);<br />
    break;<br />
    case DPID_SWITCH_11:<br />
      switch_11_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_11, switch_11_state, length);<br />
    break;<br />
    case DPID_SWITCH_12:<br />
      switch_12_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_12, switch_12_state, length);<br />
    break;<br />
    case DPID_SWITCH_13:<br />
      switch_13_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_13, switch_13_state, length);<br />
    break;<br />
    case DPID_SWITCH_14:<br />
      switch_14_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_14, switch_14_state, length);<br />
    break;<br />
    case DPID_SWITCH_15:<br />
      switch_15_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_15, switch_15_state, length);<br />
    break;<br />
    case DPID_SWITCH_16:<br />
      switch_16_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_16, switch_16_state, length);<br />
    break;<br />
    case DPID_SWITCH_17:<br />
      switch_17_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_17, switch_17_state, length);<br />
    break;<br />
    case DPID_SWITCH_18:<br />
      switch_18_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_18, switch_18_state, length);<br />
    break;<br />
    case DPID_SWITCH_19:<br />
      switch_19_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_19, switch_19_state, length);<br />
    break;<br />
    case DPID_SWITCH_20:<br />
      switch_20_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_20, switch_20_state, length);<br />
    break;<br />
    case DPID_SWITCH_21:<br />
      switch_21_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_21, switch_21_state, length);<br />
    break;<br />
    case DPID_SWITCH_22:<br />
      switch_22_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_22, switch_22_state, length);<br />
    break;<br />
    case DPID_SWITCH_23:<br />
      switch_23_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_23, switch_23_state, length);<br />
    break;<br />
    case DPID_SWITCH_24:<br />
      switch_24_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_24, switch_24_state, length);<br />
    break;<br />
    case DPID_SWITCH_25:<br />
      switch_25_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_25, switch_25_state, length);<br />
    break;<br />
    case DPID_SWITCH_26:<br />
      switch_26_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_26, switch_26_state, length);<br />
    break;<br />
    case DPID_SWITCH_27:<br />
      switch_27_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_27, switch_27_state, length);<br />
    break;<br />
    case DPID_SWITCH_28:<br />
      switch_28_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_28, switch_28_state, length);<br />
    break;<br />
    case DPID_SWITCH_29:<br />
      switch_29_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_29, switch_29_state, length);<br />
    break;<br />
    case DPID_SWITCH_30:<br />
      switch_30_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_30, switch_30_state, length);<br />
    break;<br />
    case DPID_SWITCH_31:<br />
      switch_31_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_31, switch_31_state, length);<br />
    break;<br />
    case DPID_SWITCH_32:<br />
      switch_32_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_32, switch_32_state, length);<br />
    break;<br />
    <br />
    default:break;<br />
  }<br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{<br />
  my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_7, switch_7_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_8, switch_8_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_9, switch_9_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_10, switch_10_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_11, switch_11_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_12, switch_12_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_13, switch_13_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_14, switch_14_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_15, switch_15_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_16, switch_16_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_17, switch_17_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_18, switch_18_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_19, switch_19_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_20, switch_20_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_21, switch_21_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_22, switch_22_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_23, switch_23_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_24, switch_24_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_25, switch_25_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_26, switch_26_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_27, switch_27_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_28, switch_28_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_29, switch_29_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_30, switch_30_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_31, switch_31_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_32, switch_32_state, 1);<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[firmware BIN file download with address 0x0: <!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=3882" target="_blank" title="">voice-control_0x0.zip</a> (Size: 116.47 KB / Downloads: 659)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="PNG Image" border="0" alt=".png" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=3883" target="_blank" title="">esp32-board.png</a> (Size: 99.25 KB / Downloads: 1166)
<!-- end: postbit_attachments_attachment --><br />
arduino source code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;Arduino.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
<br />
TuyaWifi my_device;<br />
<br />
/* Current LED status */<br />
unsigned char led_state = 0;<br />
/* Connect network button pin */<br />
int key_pin = 27;<br />
<br />
/* Data point define */<br />
#define DPID_SWITCH_1 &nbsp;&nbsp;1<br />
#define DPID_SWITCH_2 &nbsp;&nbsp;2<br />
#define DPID_SWITCH_3 &nbsp;&nbsp;3<br />
#define DPID_SWITCH_4 &nbsp;&nbsp;4<br />
#define DPID_SWITCH_5 &nbsp;&nbsp;5<br />
#define DPID_SWITCH_6 &nbsp;&nbsp;6<br />
#define DPID_SWITCH_7 &nbsp;&nbsp;113<br />
#define DPID_SWITCH_8 &nbsp;&nbsp;114<br />
#define DPID_SWITCH_9 &nbsp;&nbsp;115<br />
#define DPID_SWITCH_10  116<br />
#define DPID_SWITCH_11  117<br />
#define DPID_SWITCH_12  118<br />
#define DPID_SWITCH_13  119<br />
#define DPID_SWITCH_14  120<br />
#define DPID_SWITCH_15  121<br />
#define DPID_SWITCH_16  122<br />
#define DPID_SWITCH_17  123<br />
#define DPID_SWITCH_18  124<br />
#define DPID_SWITCH_19  125<br />
#define DPID_SWITCH_20  126<br />
#define DPID_SWITCH_21  127<br />
#define DPID_SWITCH_22  128<br />
#define DPID_SWITCH_23  129<br />
#define DPID_SWITCH_24  130<br />
#define DPID_SWITCH_25  131<br />
#define DPID_SWITCH_26  132<br />
#define DPID_SWITCH_27  133<br />
#define DPID_SWITCH_28  134<br />
#define DPID_SWITCH_29  135<br />
#define DPID_SWITCH_30  136<br />
#define DPID_SWITCH_31  137<br />
#define DPID_SWITCH_32  138<br />
<br />
#define LED_WiFi  26<br />
#define LED_User  33<br />
<br />
unsigned char switch_1_state = 0;<br />
unsigned char switch_2_state = 0;<br />
unsigned char switch_3_state = 0;<br />
unsigned char switch_4_state = 0;<br />
unsigned char switch_5_state = 0;<br />
unsigned char switch_6_state = 0;<br />
<br />
unsigned char switch_7_state = 0;<br />
unsigned char switch_8_state = 0;<br />
unsigned char switch_9_state = 0;<br />
unsigned char switch_10_state = 0;<br />
unsigned char switch_11_state = 0;<br />
unsigned char switch_12_state = 0;<br />
unsigned char switch_13_state = 0;<br />
unsigned char switch_14_state = 0;<br />
unsigned char switch_15_state = 0;<br />
unsigned char switch_16_state = 0;<br />
unsigned char switch_17_state = 0;<br />
unsigned char switch_18_state = 0;<br />
unsigned char switch_19_state = 0;<br />
unsigned char switch_20_state = 0;<br />
unsigned char switch_21_state = 0;<br />
unsigned char switch_22_state = 0;<br />
unsigned char switch_23_state = 0;<br />
unsigned char switch_24_state = 0;<br />
unsigned char switch_25_state = 0;<br />
unsigned char switch_26_state = 0;<br />
unsigned char switch_27_state = 0;<br />
unsigned char switch_28_state = 0;<br />
unsigned char switch_29_state = 0;<br />
unsigned char switch_30_state = 0;<br />
unsigned char switch_31_state = 0;<br />
unsigned char switch_32_state = 0;<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                   &nbsp;&nbsp;dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_SWITCH_1, DP_TYPE_BOOL},{DPID_SWITCH_2, DP_TYPE_BOOL},{DPID_SWITCH_3, DP_TYPE_BOOL},{DPID_SWITCH_4, DP_TYPE_BOOL},{DPID_SWITCH_5, DP_TYPE_BOOL},{DPID_SWITCH_6, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_7, DP_TYPE_BOOL},{DPID_SWITCH_8, DP_TYPE_BOOL},{DPID_SWITCH_9, DP_TYPE_BOOL},{DPID_SWITCH_10, DP_TYPE_BOOL},{DPID_SWITCH_11, DP_TYPE_BOOL},{DPID_SWITCH_12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_13, DP_TYPE_BOOL},{DPID_SWITCH_14, DP_TYPE_BOOL},{DPID_SWITCH_15, DP_TYPE_BOOL},{DPID_SWITCH_16, DP_TYPE_BOOL},{DPID_SWITCH_17, DP_TYPE_BOOL},{DPID_SWITCH_18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_19, DP_TYPE_BOOL},{DPID_SWITCH_20, DP_TYPE_BOOL},{DPID_SWITCH_21, DP_TYPE_BOOL},{DPID_SWITCH_22, DP_TYPE_BOOL},{DPID_SWITCH_23, DP_TYPE_BOOL},{DPID_SWITCH_24, DP_TYPE_BOOL},<br />
  {DPID_SWITCH_25, DP_TYPE_BOOL},{DPID_SWITCH_26, DP_TYPE_BOOL},{DPID_SWITCH_27, DP_TYPE_BOOL},{DPID_SWITCH_28, DP_TYPE_BOOL},{DPID_SWITCH_29, DP_TYPE_BOOL},{DPID_SWITCH_30, DP_TYPE_BOOL},<br />
 {DPID_SWITCH_31, DP_TYPE_BOOL},{DPID_SWITCH_32, DP_TYPE_BOOL},<br />
};<br />
<br />
unsigned char pid[] = {"tsfqvoyi6pxqxq6o"};<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
<br />
void setup() <br />
{<br />
  // Serial with tuyawifi<br />
  Serial.begin(9600,SERIAL_8N1,14,13);<br />
<br />
  //Initialize led port, turn off led.<br />
  pinMode(LED_WiFi, OUTPUT);<br />
  digitalWrite(LED_WiFi, LOW);<br />
<br />
  pinMode(LED_User, OUTPUT);<br />
  digitalWrite(LED_User, LOW);<br />
<br />
  //Initialize networking keys.<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
<br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 1);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{<br />
  my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin7 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  delay(10);<br />
}<br />
<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  switch(dpid) {<br />
    case DPID_SWITCH_1:<br />
      switch_1_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, length);<br />
    break;<br />
    case DPID_SWITCH_2:<br />
      switch_2_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, length);<br />
    break;<br />
    case DPID_SWITCH_3:<br />
      switch_3_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, length);<br />
    break;<br />
    case DPID_SWITCH_4:<br />
      switch_4_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, length);<br />
    break;<br />
    case DPID_SWITCH_5:<br />
      switch_5_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, length);<br />
    break;<br />
    case DPID_SWITCH_6:<br />
      switch_6_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, length);<br />
    break;<br />
    case DPID_SWITCH_7:<br />
      switch_7_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_7, switch_7_state, length);<br />
    break;<br />
    case DPID_SWITCH_8:<br />
      switch_8_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_8, switch_8_state, length);<br />
    break;<br />
    case DPID_SWITCH_9:<br />
      switch_9_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_9, switch_9_state, length);<br />
    break;<br />
    case DPID_SWITCH_10:<br />
      switch_10_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_10, switch_10_state, length);<br />
    break;<br />
    case DPID_SWITCH_11:<br />
      switch_11_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_11, switch_11_state, length);<br />
    break;<br />
    case DPID_SWITCH_12:<br />
      switch_12_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_12, switch_12_state, length);<br />
    break;<br />
    case DPID_SWITCH_13:<br />
      switch_13_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_13, switch_13_state, length);<br />
    break;<br />
    case DPID_SWITCH_14:<br />
      switch_14_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_14, switch_14_state, length);<br />
    break;<br />
    case DPID_SWITCH_15:<br />
      switch_15_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_15, switch_15_state, length);<br />
    break;<br />
    case DPID_SWITCH_16:<br />
      switch_16_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_16, switch_16_state, length);<br />
    break;<br />
    case DPID_SWITCH_17:<br />
      switch_17_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_17, switch_17_state, length);<br />
    break;<br />
    case DPID_SWITCH_18:<br />
      switch_18_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_18, switch_18_state, length);<br />
    break;<br />
    case DPID_SWITCH_19:<br />
      switch_19_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_19, switch_19_state, length);<br />
    break;<br />
    case DPID_SWITCH_20:<br />
      switch_20_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_20, switch_20_state, length);<br />
    break;<br />
    case DPID_SWITCH_21:<br />
      switch_21_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_21, switch_21_state, length);<br />
    break;<br />
    case DPID_SWITCH_22:<br />
      switch_22_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_22, switch_22_state, length);<br />
    break;<br />
    case DPID_SWITCH_23:<br />
      switch_23_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_23, switch_23_state, length);<br />
    break;<br />
    case DPID_SWITCH_24:<br />
      switch_24_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_24, switch_24_state, length);<br />
    break;<br />
    case DPID_SWITCH_25:<br />
      switch_25_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_25, switch_25_state, length);<br />
    break;<br />
    case DPID_SWITCH_26:<br />
      switch_26_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_26, switch_26_state, length);<br />
    break;<br />
    case DPID_SWITCH_27:<br />
      switch_27_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_27, switch_27_state, length);<br />
    break;<br />
    case DPID_SWITCH_28:<br />
      switch_28_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_28, switch_28_state, length);<br />
    break;<br />
    case DPID_SWITCH_29:<br />
      switch_29_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_29, switch_29_state, length);<br />
    break;<br />
    case DPID_SWITCH_30:<br />
      switch_30_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_30, switch_30_state, length);<br />
    break;<br />
    case DPID_SWITCH_31:<br />
      switch_31_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_31, switch_31_state, length);<br />
    break;<br />
    case DPID_SWITCH_32:<br />
      switch_32_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      //Status changes should be reported.<br />
      my_device.mcu_dp_update(DPID_SWITCH_32, switch_32_state, length);<br />
    break;<br />
    <br />
    default:break;<br />
  }<br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{<br />
  my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_7, switch_7_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_8, switch_8_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_9, switch_9_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_10, switch_10_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_11, switch_11_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_12, switch_12_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_13, switch_13_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_14, switch_14_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_15, switch_15_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_16, switch_16_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_17, switch_17_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_18, switch_18_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_19, switch_19_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_20, switch_20_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_21, switch_21_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_22, switch_22_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_23, switch_23_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_24, switch_24_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_25, switch_25_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_26, switch_26_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_27, switch_27_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_28, switch_28_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_29, switch_29_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_30, switch_30_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_31, switch_31_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_32, switch_32_state, 1);<br />
}</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Need Assistance with KC868-ATC Modbus Master (RS485 RTU) Communication in ESP32]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=3575</link>
			<pubDate>Wed, 29 Nov 2023 20:32:08 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=3098">ibrahim</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=3575</guid>
			<description><![CDATA[Greetings community,<br />
<br />
I trust this message finds you well.<br />
<br />
I'm currently engrossed in a project that requires the use of a KC868-ATC as a Modbus master using RS485 RTU to communicate with Modbus slave devices. Despite my efforts to create a simple program using the Modbus Master library for ESP32, I've encountered challenges and believe I'm missing some crucial details.<br />
<br />
Could anyone kindly share a basic program or offer guidance on reading holding register data from a Modbus slave device using KC868-ATC as the Modbus master with ESP32?<br />
<br />
Your insights and assistance would be immensely valuable. Thank you in advance!<br />
<br />
Best regards,]]></description>
			<content:encoded><![CDATA[Greetings community,<br />
<br />
I trust this message finds you well.<br />
<br />
I'm currently engrossed in a project that requires the use of a KC868-ATC as a Modbus master using RS485 RTU to communicate with Modbus slave devices. Despite my efforts to create a simple program using the Modbus Master library for ESP32, I've encountered challenges and believe I'm missing some crucial details.<br />
<br />
Could anyone kindly share a basic program or offer guidance on reading holding register data from a Modbus slave device using KC868-ATC as the Modbus master with ESP32?<br />
<br />
Your insights and assistance would be immensely valuable. Thank you in advance!<br />
<br />
Best regards,]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[TUYA_ESP32_Adapter arduino source code 7.1 -support TCP connection]]></title>
			<link>https://www.kincony.com/forum/showthread.php?tid=2339</link>
			<pubDate>Thu, 13 Oct 2022 09:53:49 +0800</pubDate>
			<dc:creator><![CDATA[<a href="https://www.kincony.com/forum/member.php?action=profile&uid=1">admin</a>]]></dc:creator>
			<guid isPermaLink="false">https://www.kincony.com/forum/showthread.php?tid=2339</guid>
			<description><![CDATA[this 7.1 source code version, have added TCP connection with KinCony's relay board by WiFi. so now you can use RS232 or RS485 or WiFi connect to KinCony's KC868-H32BS, KC868-H32B, KC868-H32L, KC868-H32 relay controller.<br />
just config relay board IP port for router by KC868-Adatper's AP (AP name is KC868-Tuya) webpage.<br />
<br />
here is firmware BIN file, you can download to ESP32 directly by USB cable:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1466" target="_blank" title="">KinCony-Tuya-adapter.nodemcu-32s-7.1.zip</a> (Size: 505.91 KB / Downloads: 889)
<!-- end: postbit_attachments_attachment --><br />
<br />
here is arduino source code:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1467" target="_blank" title="">KinCony-Tuya-adapter-source-code-7.1.zip</a> (Size: 8.22 KB / Downloads: 892)
<!-- end: postbit_attachments_attachment --><br />
<br />
Supported products list:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1470" target="_blank" title="">tuya-KC868-H32BS.jpg</a> (Size: 150.09 KB / Downloads: 3527)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1469" target="_blank" title="">tuya-KC868-H32B.jpg</a> (Size: 171.2 KB / Downloads: 3205)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1471" target="_blank" title="">tuya-KC868-H32L.jpg</a> (Size: 174.3 KB / Downloads: 2994)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1468" target="_blank" title="">tuya-KC868-H32.jpg</a> (Size: 262.64 KB / Downloads: 3146)
<!-- end: postbit_attachments_attachment --><br />
<br />
source code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>/*KC868-Tuya adapter have 5 LEDs<br />
 * LED D1: when D1-OFF: ready to enable AP for config ESP32 wifi, AP name is KC868-Tuya      D1-ON: ESP32 connect to router successful<br />
 * LED D2: when D2-ON: ESP32 connect to relay board by TCP connection successfully<br />
 * LED D3: when D3-ON: KC868-Tuya adapter work in RS232 or RS485 mode<br />
 * LED D4: when D4 blink: ready to config Tuya wifi module for router SSID<br />
 * LED D6: KC868-Tuya adapter power LED<br />
 * Button S1:Tuya wifi module RESET key<br />
 * Button DL: ESP32 wifi RESET<br />
 * Button RST: ESP32 module RESET<br />
 * Jumper IO23: Short:by RS485 &nbsp;&nbsp;disconnect:by RS232<br />
 * @Copyright: KinCony IoT<br />
 */<br />
#include &lt;WiFi.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include &lt;SoftwareSerial.h&gt;<br />
#include "CRC16.h"<br />
#include "CRC.h"<br />
#include &lt;WiFiManager.h&gt; <br />
#include "EEPROM.h"<br />
<br />
<br />
#define TRIGGER_PIN 0 &nbsp;&nbsp;// reset wifi button<br />
WiFiManager wm;<br />
// Replace with your network credentials<br />
const char* ssid   &nbsp;&nbsp;= "xxx"; //your router's ssid<br />
const char* password = "yyy"; //your router's password<br />
<br />
// Set web server port number to 80<br />
WiFiServer server(80);<br />
// Variable to store the HTTP request<br />
String header;<br />
<br />
TuyaWifi my_device;<br />
<br />
#define  RS485Address &nbsp;&nbsp;0x01<br />
#define  RS485Baud   &nbsp;&nbsp;9600<br />
#define  RS232Baud   &nbsp;&nbsp;115200<br />
#define  wifiReadDelay   &nbsp;&nbsp;3000<br />
#define  RS232ReadDelay   &nbsp;&nbsp;3000<br />
#define  RS485ReadDelay   &nbsp;&nbsp;3000<br />
#define  wifiRelayDelay    1000<br />
#define  RS232RelayDelay    500<br />
#define  RS485RelayDelay    500<br />
unsigned char pid[] = {"tsfqvoyi6pxqxq6o"};<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
//char c;<br />
<br />
//============================================<br />
<br />
<br />
// Current time<br />
unsigned long currentTime = millis();<br />
// Previous time<br />
unsigned long previousTime = 0; <br />
// Define timeout time in milliseconds (example: 2000ms = 2s)<br />
const long timeoutTime = 2000;<br />
int flag_read_di=0;<br />
<br />
<br />
<br />
#define  ADDRESS_TARGET_FIRST  10<br />
#define  ADDRESS_TARGET_IP   &nbsp;&nbsp;30<br />
#define  ADDRESS_TARGET_PORT &nbsp;&nbsp;60<br />
#define  ADDRESS_TARGET_MODE &nbsp;&nbsp;90<br />
<br />
<br />
#define RS232_state &nbsp;&nbsp;1<br />
#define RS485_state &nbsp;&nbsp;0<br />
#define wifi_read_inputcom &nbsp;&nbsp;0<br />
#define wifi_read_relaycom &nbsp;&nbsp;1<br />
#define RS232_read_inputcom &nbsp;&nbsp;0<br />
#define RS232_read_relaycom &nbsp;&nbsp;1<br />
#define RS485_read_inputcom &nbsp;&nbsp;0<br />
#define RS485_read_relaycom &nbsp;&nbsp;1<br />
#define DISABLE &nbsp;&nbsp;0<br />
#define ENABLE &nbsp;&nbsp;1<br />
#define NOCONNECTED &nbsp;&nbsp;0<br />
#define CONNECTED &nbsp;&nbsp;1<br />
uint16_t  CRC16_Data = 0;<br />
uint8_t  CRC16_Data_H = 0;<br />
uint8_t  CRC16_Data_L = 0;<br />
<br />
/* Current  status */<br />
unsigned char wifi_connected = NOCONNECTED;<br />
/* Current  status */<br />
unsigned char led_state = 0;<br />
//1=232 0=485<br />
unsigned char uart_state = 1;<br />
<br />
unsigned char wifi_state = ENABLE;<br />
unsigned char wifi_stop = DISABLE;<br />
<br />
unsigned char wifi_Check = ENABLE;<br />
unsigned char RS232_Check = ENABLE;<br />
unsigned char RS485_Check = ENABLE;<br />
<br />
unsigned char wifi_read_com = 1;<br />
//1= read RELAY  0 = read input<br />
unsigned char RS232_read_com = 1;<br />
//1= read RELAY  0 = read input<br />
unsigned char RS485_read_com = 1;<br />
//1= read RELAY  0 = read input<br />
<br />
unsigned char switch_8_1_nowstate = 0;<br />
unsigned char switch_16_9_nowstate = 0;<br />
unsigned char switch_24_17_nowstate = 0;<br />
unsigned char switch_32_25_nowstate = 0;<br />
unsigned char switch_8_1_tarstate = 0;<br />
unsigned char switch_16_9_tarstate = 0;<br />
unsigned char switch_24_17_tarstate = 0;<br />
unsigned char switch_32_25_tarstate = 0;<br />
<br />
unsigned char pre_switch_8_1_tarstate = 0;<br />
unsigned char pre_switch_16_9_tarstate = 0;<br />
unsigned char pre_switch_24_17_tarstate = 0;<br />
unsigned char pre_switch_32_25_tarstate = 0;<br />
unsigned char pre_switch_8_1_nowstate = 0;<br />
unsigned char pre_switch_16_9_nowstate = 0;<br />
unsigned char pre_switch_24_17_nowstate = 0;<br />
unsigned char pre_switch_32_25_nowstate = 0;<br />
<br />
unsigned char switch_1_8_tarstate = 0;<br />
unsigned char switch_9_16_tarstate = 0;<br />
unsigned char switch_17_24_tarstate = 0;<br />
unsigned char switch_25_32_tarstate = 0;<br />
<br />
unsigned char switch_1_state = 0;<br />
unsigned char switch_2_state = 0;<br />
unsigned char switch_3_state = 0;<br />
unsigned char switch_4_state = 0;<br />
unsigned char switch_5_state = 0;<br />
unsigned char switch_6_state = 0;<br />
<br />
unsigned char input_1_state = 0;<br />
unsigned char input_2_state = 0;<br />
unsigned char input_3_state = 0;<br />
unsigned char input_4_state = 0;<br />
unsigned char input_5_state = 0;<br />
unsigned char input_6_state = 0;<br />
<br />
unsigned char switch_7_state = 0;<br />
unsigned char switch_8_state = 0;<br />
unsigned char switch_9_state = 0;<br />
unsigned char switch_10_state = 0;<br />
unsigned char switch_11_state = 0;<br />
unsigned char switch_12_state = 0;<br />
unsigned char switch_13_state = 0;<br />
unsigned char switch_14_state = 0;<br />
unsigned char switch_15_state = 0;<br />
unsigned char switch_16_state = 0;<br />
unsigned char switch_17_state = 0;<br />
unsigned char switch_18_state = 0;<br />
unsigned char switch_19_state = 0;<br />
unsigned char switch_20_state = 0;<br />
unsigned char switch_21_state = 0;<br />
unsigned char switch_22_state = 0;<br />
unsigned char switch_23_state = 0;<br />
unsigned char switch_24_state = 0;<br />
unsigned char switch_25_state = 0;<br />
unsigned char switch_26_state = 0;<br />
unsigned char switch_27_state = 0;<br />
unsigned char switch_28_state = 0;<br />
unsigned char switch_29_state = 0;<br />
unsigned char switch_30_state = 0;<br />
unsigned char switch_31_state = 0;<br />
unsigned char switch_32_state = 0;<br />
<br />
<br />
unsigned char switch_wifi_delay_state = 0;<br />
unsigned char dpid_wifi_delay_switch = 0;<br />
unsigned char en_wifi_delay_switch = 0;<br />
//1= en delay &nbsp;&nbsp;0= dis delay<br />
<br />
unsigned char switch_232_delay_state = 0;<br />
unsigned char dpid_232_delay_switch = 0;<br />
unsigned char en_232_delay_switch = 0;<br />
//1= en delay &nbsp;&nbsp;0= dis delay<br />
<br />
unsigned char switch_485_delay_state = 0;<br />
unsigned char dpid_485_delay_switch = 0;<br />
unsigned char en_485_delay_switch = 0;<br />
//1= en delay &nbsp;&nbsp;0= dis delay<br />
<br />
unsigned char ini_state = 1;<br />
<br />
unsigned char Rs485_Str[20] = {0};<br />
/* Connect network button pin */<br />
int key_pin = 27;<br />
/*  232(1) 485(0)  */<br />
int sel_uart_pin = 32; <br />
<br />
int index_pos = 0;<br />
int index_begin = 0;<br />
int index_end = 0;<br />
<br />
#define BEGIN_DELAY &nbsp;&nbsp;1<br />
#define END_DELAY &nbsp;&nbsp;0<br />
<br />
/* Data point define */<br />
<br />
#define DPID_SWITCH_1 &nbsp;&nbsp;1<br />
#define DPID_SWITCH_2 &nbsp;&nbsp;2<br />
#define DPID_SWITCH_3 &nbsp;&nbsp;3<br />
#define DPID_SWITCH_4 &nbsp;&nbsp;4<br />
#define DPID_SWITCH_5 &nbsp;&nbsp;5<br />
#define DPID_SWITCH_6 &nbsp;&nbsp;6<br />
<br />
#define DPID_INPUT_1 &nbsp;&nbsp;101<br />
#define DPID_INPUT_2 &nbsp;&nbsp;102<br />
#define DPID_INPUT_3 &nbsp;&nbsp;103<br />
#define DPID_INPUT_4 &nbsp;&nbsp;104<br />
#define DPID_INPUT_5 &nbsp;&nbsp;105<br />
#define DPID_INPUT_6 &nbsp;&nbsp;106<br />
<br />
#define DPID_SWITCH7 &nbsp;&nbsp;113<br />
#define DPID_SWITCH8 &nbsp;&nbsp;114<br />
#define DPID_SWITCH9 &nbsp;&nbsp;115<br />
#define DPID_SWITCH10  116<br />
#define DPID_SWITCH11  117<br />
#define DPID_SWITCH12  118<br />
#define DPID_SWITCH13  119<br />
#define DPID_SWITCH14  120<br />
#define DPID_SWITCH15  121<br />
#define DPID_SWITCH16  122<br />
#define DPID_SWITCH17  123<br />
#define DPID_SWITCH18  124<br />
#define DPID_SWITCH19  125<br />
#define DPID_SWITCH20  126<br />
#define DPID_SWITCH21  127<br />
#define DPID_SWITCH22  128<br />
#define DPID_SWITCH23  129<br />
#define DPID_SWITCH24  130<br />
#define DPID_SWITCH25  131<br />
#define DPID_SWITCH26  132<br />
#define DPID_SWITCH27  133<br />
#define DPID_SWITCH28  134<br />
#define DPID_SWITCH29  135<br />
#define DPID_SWITCH30  136<br />
#define DPID_SWITCH31  137<br />
#define DPID_SWITCH32  138<br />
<br />
<br />
#define LED_TUYA_WiFi  26<br />
#define LED_User1  32<br />
#define LED_ESP_WiFi  33<br />
#define LED_User2  25<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                   &nbsp;&nbsp;dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_SWITCH_1, DP_TYPE_BOOL},{DPID_SWITCH_2, DP_TYPE_BOOL},{DPID_SWITCH_3, DP_TYPE_BOOL},{DPID_SWITCH_4, DP_TYPE_BOOL},{DPID_SWITCH_5, DP_TYPE_BOOL},{DPID_SWITCH_6, DP_TYPE_BOOL},<br />
  {DPID_INPUT_1, DP_TYPE_BOOL},{DPID_INPUT_2, DP_TYPE_BOOL},{DPID_INPUT_3, DP_TYPE_BOOL},{DPID_INPUT_4, DP_TYPE_BOOL},{DPID_INPUT_5, DP_TYPE_BOOL},{DPID_INPUT_6, DP_TYPE_BOOL},<br />
  {DPID_SWITCH7, DP_TYPE_BOOL},{DPID_SWITCH8, DP_TYPE_BOOL},{DPID_SWITCH9, DP_TYPE_BOOL},{DPID_SWITCH10, DP_TYPE_BOOL},{DPID_SWITCH11, DP_TYPE_BOOL},{DPID_SWITCH12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH13, DP_TYPE_BOOL},{DPID_SWITCH14, DP_TYPE_BOOL},{DPID_SWITCH15, DP_TYPE_BOOL},{DPID_SWITCH16, DP_TYPE_BOOL},{DPID_SWITCH17, DP_TYPE_BOOL},{DPID_SWITCH18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH19, DP_TYPE_BOOL},{DPID_SWITCH20, DP_TYPE_BOOL},{DPID_SWITCH21, DP_TYPE_BOOL},{DPID_SWITCH22, DP_TYPE_BOOL},{DPID_SWITCH23, DP_TYPE_BOOL},{DPID_SWITCH24, DP_TYPE_BOOL},<br />
  {DPID_SWITCH25, DP_TYPE_BOOL},{DPID_SWITCH26, DP_TYPE_BOOL},{DPID_SWITCH27, DP_TYPE_BOOL},{DPID_SWITCH28, DP_TYPE_BOOL},{DPID_SWITCH29, DP_TYPE_BOOL},{DPID_SWITCH30, DP_TYPE_BOOL},<br />
 {DPID_SWITCH31, DP_TYPE_BOOL},{DPID_SWITCH32, DP_TYPE_BOOL},<br />
};<br />
<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
unsigned long RS485_last_time = 0;<br />
unsigned long RS232_last_time = 0;<br />
unsigned long wifi_last_time = 0;<br />
<br />
unsigned long wifi_delay_last_time = 0;<br />
unsigned long wifi_relay_last_time = 0;<br />
unsigned long RS232_delay_last_time = 0;<br />
unsigned long RS232_relay_last_time = 0;<br />
unsigned long RS485_delay_last_time = 0;<br />
unsigned long RS485_relay_last_time = 0;<br />
<br />
String indata="";<br />
String val="";<br />
String outdata="";<br />
String relay_outdata="";<br />
String subval="";<br />
int numsubval[6]={0};<br />
String currentLine="";<br />
<br />
String  TARGET_FIRST="";    <br />
String  TARGET_IP="";    <br />
String  TARGET_PORT="";    <br />
String  TARGET_MODE="";    <br />
String  NEW_TARGET_IP="";    <br />
String  NEW_TARGET_PORT="";    <br />
String  NEW_TARGET_MODE="";    <br />
int address = 0;<br />
<br />
String relay1state = "off";// state of relay1<br />
String relay2state = "off";// state of relay2<br />
String relay3state = "off";// state of relay3<br />
String relay4state = "off";// state of relay4<br />
String relay5state = "off";// state of relay5<br />
String relay6state = "off";// state of relay6<br />
String relay7state = "off";// state of relay7<br />
String relay8state = "off";// state of relay8<br />
<br />
String relay9state = "off";// state of relay9<br />
String relay10state = "off";// state of relay10<br />
String relay11state = "off";// state of relay11<br />
String relay12state = "off";// state of relay12<br />
String relay13state = "off";// state of relay13<br />
String relay14state = "off";// state of relay14<br />
String relay15state = "off";// state of relay15<br />
String relay16state = "off";// state of relay16<br />
<br />
String relay17state = "off";// state of relay17<br />
String relay18state = "off";// state of relay18<br />
String relay19state = "off";// state of relay19<br />
String relay20state = "off";// state of relay20<br />
String relay21state = "off";// state of relay21<br />
String relay22state = "off";// state of relay22<br />
String relay23state = "off";// state of relay23<br />
String relay24state = "off";// state of relay24<br />
<br />
String relay25state = "off";// state of relay25<br />
String relay26state = "off";// state of relay26<br />
String relay27state = "off";// state of relay27<br />
String relay28state = "off";// state of relay28<br />
String relay29state = "off";// state of relay29<br />
String relay30state = "off";// state of relay30<br />
String relay31state = "off";// state of relay31<br />
String relay32state = "off";// state of relay32<br />
<br />
WiFiClient client  ; &nbsp;&nbsp;// clients<br />
<br />
void setup() <br />
{<br />
  ini_state = 1; <br />
  wifi_connected = NOCONNECTED;<br />
<br />
  //Initialize led port, turn off led.<br />
<br />
  pinMode(LED_TUYA_WiFi, OUTPUT);<br />
  digitalWrite(LED_TUYA_WiFi, LOW);<br />
  pinMode(LED_ESP_WiFi, OUTPUT);<br />
  digitalWrite(LED_ESP_WiFi, LOW);<br />
<br />
  pinMode(LED_User1, OUTPUT);<br />
  digitalWrite(LED_User1, LOW);<br />
  pinMode(LED_User2, OUTPUT);<br />
  digitalWrite(LED_User2, LOW);<br />
<br />
  // wifi reset button<br />
  pinMode(TRIGGER_PIN, INPUT_PULLUP); <br />
  //Initialize networking key<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
  //Initialize sel key<br />
  pinMode(sel_uart_pin, INPUT_PULLUP);<br />
<br />
  <br />
  //  0 SEL 485    1 SEL 232<br />
  uart_state = RS485_state;<br />
  if (digitalRead(sel_uart_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(sel_uart_pin) == LOW) {<br />
  uart_state = RS232_state;<br />
   &nbsp;&nbsp;}<br />
  }<br />
<br />
  if(uart_state == RS232_state)<br />
  {<br />
    Serial2.begin(RS232Baud,SERIAL_8N1,16,17);<br />
  }<br />
  else<br />
  {<br />
    Serial2.begin(RS485Baud,SERIAL_8N1,16,17); <br />
  }<br />
<br />
 // Serial with tuyawifi<br />
 Serial.begin(9600,SERIAL_8N1,14,13);<br />
    // Serial with debug<br />
 Serial1.begin(9600,SERIAL_8N1,3,1);<br />
<br />
<br />
 Serial1.println("&#92;nTesting EEPROM Library&#92;n");<br />
  if (!EEPROM.begin(1000)) {<br />
    Serial1.println("Failed to initialise EEPROM");<br />
    //Serial2.println("Restarting...");<br />
    delay(1000);<br />
    //ESP.restart();<br />
  }<br />
    <br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 1);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
<br />
  address = ADDRESS_TARGET_FIRST;<br />
  TARGET_FIRST = EEPROM.readString(address); <br />
  address = ADDRESS_TARGET_IP;<br />
  TARGET_IP = EEPROM.readString(address); <br />
  address = ADDRESS_TARGET_PORT;<br />
  TARGET_PORT = EEPROM.readString(address); <br />
  address = ADDRESS_TARGET_MODE;<br />
  TARGET_MODE = EEPROM.readString(address); <br />
<br />
  //default<br />
  if (TARGET_FIRST != "123456")<br />
    {<br />
   &nbsp;&nbsp;TARGET_IP="192.168.1.200";    <br />
   &nbsp;&nbsp;TARGET_PORT="4196";    <br />
   &nbsp;&nbsp;TARGET_MODE="wifi";    <br />
    }    <br />
<br />
 // Connect to Wi-Fi network with SSID and password<br />
  WiFi.mode(WIFI_STA);<br />
 //close wifi sleep<br />
  WiFi.setSleep(false);<br />
  <br />
  // Define a text box, 50 characters maximum<br />
  WiFiManagerParameter custom_text_box("my_text", "Enter Relay Board IP", TARGET_IP.c_str(), 50);<br />
  // Add custom parameter<br />
  wm.addParameter(&amp;custom_text_box);<br />
  // Define a text box, 50 characters maximum<br />
  WiFiManagerParameter custom_text_box2("my_text2", "Enter Relay Board Port", TARGET_PORT.c_str(), 50);<br />
  // Add custom parameter<br />
  wm.addParameter(&amp;custom_text_box2);<br />
  // Define a text box, 50 characters maximum<br />
  WiFiManagerParameter custom_text_box3("my_text3", "Enter wifi or 232 or 485", TARGET_MODE.c_str(), 50);<br />
  // Add custom parameter<br />
  wm.addParameter(&amp;custom_text_box3);<br />
  <br />
   &nbsp;&nbsp;bool res;<br />
     &nbsp;&nbsp;res = wm.autoConnect("KC868-Tuya"); // anonymous ap<br />
<br />
    if(!res) {<br />
        Serial1.println("Failed to connect");<br />
        //ESP.restart();<br />
    } <br />
    else {<br />
        //if you get here you have connected to the WiFi    <br />
        Serial1.println("connected...yeey :)");<br />
    } <br />
<br />
<br />
while (WiFi.status() != WL_CONNECTED) {<br />
    delay(500);<br />
    Serial1.print(".");<br />
  }<br />
  // Print local IP address and start web server<br />
  Serial1.println("");<br />
  Serial1.println("WiFi connected.");<br />
  Serial1.println("Local IP address: ");<br />
  Serial1.println(WiFi.localIP());<br />
    // Print custom text box value to serial monitor<br />
<br />
  NEW_TARGET_IP = custom_text_box.getValue();    <br />
  NEW_TARGET_PORT = custom_text_box2.getValue();    <br />
  NEW_TARGET_MODE = custom_text_box3.getValue();    <br />
<br />
  if(NEW_TARGET_IP != TARGET_IP)<br />
    {<br />
   &nbsp;&nbsp;TARGET_IP = NEW_TARGET_IP;<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_IP; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_IP);<br />
<br />
   &nbsp;&nbsp;TARGET_FIRST = "123456";<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_FIRST; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_FIRST);<br />
    <br />
   &nbsp;&nbsp;EEPROM.commit();<br />
    }<br />
<br />
  if(NEW_TARGET_PORT != TARGET_PORT)<br />
    {<br />
   &nbsp;&nbsp;TARGET_PORT = NEW_TARGET_PORT;<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_PORT; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_PORT);<br />
<br />
   &nbsp;&nbsp;TARGET_FIRST = "123456";<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_FIRST; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_FIRST);<br />
    <br />
   &nbsp;&nbsp;EEPROM.commit();<br />
    }<br />
<br />
  if(NEW_TARGET_MODE != TARGET_MODE)<br />
    {<br />
   &nbsp;&nbsp;TARGET_MODE = NEW_TARGET_MODE;<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_MODE; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_MODE);<br />
<br />
   &nbsp;&nbsp;TARGET_FIRST = "123456";<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_FIRST; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_FIRST);<br />
    <br />
   &nbsp;&nbsp;EEPROM.commit();<br />
    }<br />
<br />
  Serial1.print("target IP: ");<br />
  Serial1.println(TARGET_IP);<br />
  Serial1.print("target port: ");<br />
  Serial1.println(TARGET_PORT);<br />
  Serial1.print("target mode: ");<br />
  Serial1.println(TARGET_MODE);<br />
<br />
  //server.begin(); ???????????????????????????????<br />
 pinMode(LED_User1, OUTPUT);<br />
 digitalWrite(LED_User1, HIGH);<br />
                                                                                                             &nbsp;&nbsp;<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{ <br />
 <br />
 if((TARGET_MODE=="wifi")||(TARGET_MODE=="WIFI"))  <br />
 {<br />
 digitalWrite(LED_User2, LOW);<br />
<br />
 const char* host = TARGET_IP.c_str();<br />
 int httpPort = TARGET_PORT.toInt();<br />
 if (client.connect(host, httpPort)) {<br />
     &nbsp;&nbsp;wifi_state = ENABLE;<br />
     &nbsp;&nbsp;digitalWrite(LED_ESP_WiFi, HIGH);<br />
    while (client.connected() || client.available()) <br />
    {  <br />
    <br />
   &nbsp;&nbsp;// wifi_connected = CONNECTED;<br />
      if(client.available() )<br />
      {<br />
      String line = client.readStringUntil('&#92;n');<br />
      Serial1.print("line=");<br />
      Serial1.println(line);<br />
      //if(line.length()&lt;50) <br />
      // {<br />
      // indata = line;<br />
      // //Serial1.print("indata=");<br />
      // Serial1.println(indata);<br />
      // }<br />
      //client.write(line.c_str()); <br />
     &nbsp;&nbsp;if(line.indexOf("RELAY-SET_ALL-255") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-SET_ALL-255");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;else if(line.indexOf("RELAY-GET_INPUT-255") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-GET_INPUT-255");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;else if(line.indexOf("RELAY-STATE-255") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-STATE-255");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;else if(line.indexOf("RELAY-ALARM") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-ALARM");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;else if(line.indexOf("RELAY-DIARM") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-DIARM");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
      <br />
      }<br />
      <br />
     &nbsp;&nbsp;<br />
  /////////////wifi mode begin<br />
  if((en_wifi_delay_switch == BEGIN_DELAY)&amp;&amp;(wifi_state == ENABLE))<br />
  {<br />
    if((millis()-wifi_delay_last_time)&gt;=20)<br />
    {<br />
      my_device.mcu_dp_update(dpid_wifi_delay_switch, switch_wifi_delay_state, 1);<br />
      en_wifi_delay_switch = END_DELAY;<br />
    }<br />
  }<br />
<br />
    if((indata.length()&gt;0)&amp;&amp;(((uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE))||(wifi_state == ENABLE)))<br />
  {<br />
   &nbsp;&nbsp;val=indata; &nbsp;&nbsp;//<br />
<br />
  //----------INPUT ALARM--------<br />
      if(val=="RELAY-ALARM-1,OK") &nbsp;&nbsp;<br />
      {<br />
        input_1_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-1,OK") &nbsp;&nbsp;<br />
      {<br />
        input_1_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-2,OK") &nbsp;&nbsp;<br />
      {<br />
        input_2_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-2,OK") &nbsp;&nbsp;<br />
      {<br />
        input_2_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-3,OK") &nbsp;&nbsp;<br />
      {<br />
        input_3_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-3,OK") &nbsp;&nbsp;<br />
      {<br />
        input_3_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-4,OK") &nbsp;&nbsp;<br />
      {<br />
        input_4_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-4,OK") &nbsp;&nbsp;<br />
      {<br />
        input_4_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-5,OK") &nbsp;&nbsp;<br />
      {<br />
        input_5_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-5,OK") &nbsp;&nbsp;<br />
      {<br />
        input_5_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-6,OK") &nbsp;&nbsp;<br />
      {<br />
        input_6_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-6,OK") &nbsp;&nbsp;<br />
      {<br />
        input_6_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----INPUT READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,19)=="RELAY-GET_INPUT-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;<br />
     &nbsp;&nbsp;//wifi_stop = DISABLE;<br />
<br />
     &nbsp;&nbsp;subval =  val.substring(20);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        input_6_state = (numsubval[0]&amp;0x20)?0:1;<br />
        input_5_state = (numsubval[0]&amp;0x10)?0:1;<br />
        input_4_state = (numsubval[0]&amp;0x08)?0:1;<br />
        input_3_state = (numsubval[0]&amp;0x04)?0:1;<br />
        input_2_state = (numsubval[0]&amp;0x02)?0:1;<br />
        input_1_state = (numsubval[0]&amp;0x01)?0:1;<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----ALL-RELAY-STATE  READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,15)=="RELAY-STATE-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;//wifi_stop = DISABLE;<br />
<br />
     &nbsp;&nbsp;subval =  val.substring(16);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        switch_32_25_nowstate = numsubval[0];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = numsubval[1];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = numsubval[2];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
       &nbsp;&nbsp;switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = numsubval[3];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        if(ini_state==1)<br />
        {<br />
       &nbsp;&nbsp;ini_state=0 ;<br />
       &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_nowstate;<br />
       &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_nowstate;<br />
       &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_nowstate;<br />
       &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_nowstate;<br />
        }<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----RELAY SET READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,17)=="RELAY-SET_ALL-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;subval =  val.substring(18);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        switch_32_25_nowstate = numsubval[0];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = numsubval[1];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = numsubval[2];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = numsubval[3];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
<br />
<br />
         &nbsp;&nbsp;wifi_Check = ENABLE ;<br />
          <br />
      }      <br />
 <br />
   &nbsp;&nbsp;// wifi_last_time = millis();<br />
  } &nbsp;&nbsp;<br />
<br />
    indata=""; &nbsp;&nbsp;//clear indata<br />
//--------------------------------------------------<br />
 &nbsp;&nbsp;my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin27 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_TUYA_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
    if(wifi_state == ENABLE)<br />
  {<br />
    if ((pre_switch_8_1_nowstate != switch_8_1_nowstate) || (pre_switch_16_9_nowstate != switch_16_9_nowstate) || (pre_switch_24_17_nowstate != switch_24_17_nowstate) || (pre_switch_32_25_nowstate != switch_32_25_nowstate))<br />
    {<br />
      pre_switch_8_1_nowstate = switch_8_1_nowstate;<br />
      pre_switch_16_9_nowstate = switch_16_9_nowstate;<br />
      pre_switch_24_17_nowstate = switch_24_17_nowstate;<br />
      pre_switch_32_25_nowstate = switch_32_25_nowstate;<br />
<br />
      switch_8_1_tarstate = switch_8_1_nowstate ;<br />
      switch_16_9_tarstate = switch_16_9_nowstate ;<br />
      switch_24_17_tarstate = switch_24_17_nowstate ;<br />
      switch_32_25_tarstate = switch_32_25_nowstate ;<br />
      <br />
    }<br />
    else if ((switch_8_1_tarstate != switch_8_1_nowstate) || (switch_16_9_tarstate != switch_16_9_nowstate) || (switch_24_17_tarstate != switch_24_17_nowstate) || (switch_32_25_tarstate != switch_32_25_nowstate))<br />
    {<br />
    if (millis()- wifi_relay_last_time &gt;= wifiRelayDelay) <br />
    {<br />
      wifi_Check = DISABLE ;<br />
<br />
      wifi_relay_last_time = millis();<br />
      if((switch_8_1_tarstate == pre_switch_8_1_tarstate) &amp;&amp; (switch_16_9_tarstate == pre_switch_16_9_tarstate) &amp;&amp; (switch_24_17_tarstate == pre_switch_24_17_tarstate) &amp;&amp; (switch_32_25_tarstate == pre_switch_32_25_tarstate))<br />
      {<br />
<br />
      relay_outdata = "";<br />
      relay_outdata = relay_outdata + "RELAY-SET_ALL-255," + switch_32_25_tarstate + "," + switch_24_17_tarstate + "," + switch_16_9_tarstate + "," + switch_8_1_tarstate ;<br />
      client.print(relay_outdata);<br />
      Serial1.println(relay_outdata);<br />
      switch_8_1_nowstate = switch_8_1_tarstate ;<br />
      switch_16_9_nowstate = switch_16_9_tarstate ;<br />
      switch_24_17_nowstate = switch_24_17_tarstate ;<br />
      switch_32_25_nowstate = switch_32_25_tarstate ;<br />
      }<br />
      else<br />
      {<br />
      pre_switch_8_1_tarstate = switch_8_1_tarstate ;<br />
      pre_switch_16_9_tarstate = switch_16_9_tarstate ;<br />
      pre_switch_24_17_tarstate = switch_24_17_tarstate ;<br />
      pre_switch_32_25_tarstate = switch_32_25_tarstate ;<br />
      } <br />
    }<br />
    }<br />
  }<br />
<br />
  if(wifi_state == ENABLE)<br />
  {<br />
    if (millis()- wifi_last_time &gt;= wifiReadDelay) <br />
    {<br />
      wifi_last_time = millis();<br />
  <br />
   &nbsp;&nbsp;if(wifi_read_com == wifi_read_relaycom)<br />
   &nbsp;&nbsp;{<br />
   &nbsp;&nbsp;outdata = "RELAY-STATE-255";<br />
   &nbsp;&nbsp;client.print(outdata); <br />
   &nbsp;&nbsp;wifi_stop = ENABLE;<br />
   &nbsp;&nbsp;wifi_read_com = wifi_read_inputcom;<br />
   &nbsp;&nbsp;}<br />
   &nbsp;&nbsp;else if(wifi_read_com == wifi_read_inputcom)<br />
   &nbsp;&nbsp;{<br />
   &nbsp;&nbsp;outdata = "RELAY-GET_INPUT-255";<br />
   &nbsp;&nbsp;client.print(outdata);<br />
   &nbsp;&nbsp;wifi_stop = ENABLE;<br />
   &nbsp;&nbsp;wifi_read_com = wifi_read_relaycom;<br />
   &nbsp;&nbsp;}<br />
 <br />
    <br />
    }<br />
  }<br />
<br />
  if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}<br />
      <br />
    //wifi mode end<br />
    <br />
   &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;client.stop();<br />
    }<br />
 else<br />
    {<br />
     &nbsp;&nbsp;if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}<br />
 <br />
     &nbsp;&nbsp;wifi_state = DISABLE;<br />
     &nbsp;&nbsp;digitalWrite(LED_ESP_WiFi, LOW);<br />
     &nbsp;&nbsp;client.stop();<br />
    }<br />
   &nbsp;&nbsp;delay(5000);<br />
 }<br />
<br />
 else<br />
 {<br />
 &nbsp;&nbsp;digitalWrite(LED_User2, HIGH);<br />
   &nbsp;&nbsp;<br />
   &nbsp;&nbsp;wifi_state = DISABLE;<br />
   &nbsp;&nbsp;digitalWrite(LED_ESP_WiFi, LOW);<br />
  <br />
  //no wifi mode begin <br />
  if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}<br />
<br />
  if((uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
  while(Serial2.available()&gt;0)<br />
 &nbsp;&nbsp;{<br />
    indata+=char(Serial2.read());  //read KBOX via 232<br />
    delay(2);<br />
    if(Serial2.available()&lt;=0)<br />
   &nbsp;&nbsp;{<br />
        //Serial2.print(indata);<br />
   &nbsp;&nbsp;}<br />
 &nbsp;&nbsp;}    <br />
  }<br />
  else if((uart_state == RS485_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
  while(Serial2.available()&gt;0)<br />
 &nbsp;&nbsp;{<br />
    indata+=char(Serial2.read()); &nbsp;&nbsp;//read KBOX via 485<br />
    delay(2);<br />
    if(Serial2.available()&lt;=0)<br />
   &nbsp;&nbsp;{<br />
        //Serial2.print(indata);<br />
   &nbsp;&nbsp;}<br />
 &nbsp;&nbsp;}    <br />
  }<br />
<br />
  if((en_232_delay_switch == BEGIN_DELAY)&amp;&amp;(uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if((millis()-RS232_delay_last_time)&gt;=20)<br />
    {<br />
      my_device.mcu_dp_update(dpid_232_delay_switch, switch_232_delay_state, 1);<br />
      en_232_delay_switch = END_DELAY;<br />
    }<br />
  }<br />
<br />
    if((en_485_delay_switch == BEGIN_DELAY)&amp;&amp;(uart_state == RS485_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if((millis()-RS485_delay_last_time)&gt;=20)<br />
    {<br />
      my_device.mcu_dp_update(dpid_485_delay_switch, switch_485_delay_state, 1);<br />
      en_485_delay_switch = END_DELAY;<br />
    }<br />
  }<br />
<br />
<br />
    if((indata.length()&gt;0)&amp;&amp;((uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE)))<br />
  {<br />
   &nbsp;&nbsp;val=indata; &nbsp;&nbsp;//<br />
<br />
  //----------INPUT ALARM--------<br />
      if(val=="RELAY-ALARM-1,OK") &nbsp;&nbsp;<br />
      {<br />
        input_1_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-1,OK") &nbsp;&nbsp;<br />
      {<br />
        input_1_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-2,OK") &nbsp;&nbsp;<br />
      {<br />
        input_2_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-2,OK") &nbsp;&nbsp;<br />
      {<br />
        input_2_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-3,OK") &nbsp;&nbsp;<br />
      {<br />
        input_3_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-3,OK") &nbsp;&nbsp;<br />
      {<br />
        input_3_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-4,OK") &nbsp;&nbsp;<br />
      {<br />
        input_4_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-4,OK") &nbsp;&nbsp;<br />
      {<br />
        input_4_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-5,OK") &nbsp;&nbsp;<br />
      {<br />
        input_5_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-5,OK") &nbsp;&nbsp;<br />
      {<br />
        input_5_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-6,OK") &nbsp;&nbsp;<br />
      {<br />
        input_6_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-6,OK") &nbsp;&nbsp;<br />
      {<br />
        input_6_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----INPUT READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,19)=="RELAY-GET_INPUT-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;subval =  val.substring(20);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        input_6_state = (numsubval[0]&amp;0x20)?0:1;<br />
        input_5_state = (numsubval[0]&amp;0x10)?0:1;<br />
        input_4_state = (numsubval[0]&amp;0x08)?0:1;<br />
        input_3_state = (numsubval[0]&amp;0x04)?0:1;<br />
        input_2_state = (numsubval[0]&amp;0x02)?0:1;<br />
        input_1_state = (numsubval[0]&amp;0x01)?0:1;<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----ALL-RELAY-STATE  READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,15)=="RELAY-STATE-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;subval =  val.substring(16);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        switch_32_25_nowstate = numsubval[0];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = numsubval[1];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = numsubval[2];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = numsubval[3];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        if(ini_state==1)<br />
        {<br />
       &nbsp;&nbsp;ini_state=0 ;<br />
       &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_nowstate;<br />
       &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_nowstate;<br />
       &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_nowstate;<br />
       &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_nowstate;<br />
        }<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----RELAY SET READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,17)=="RELAY-SET_ALL-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;subval =  val.substring(18);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        switch_32_25_nowstate = numsubval[0];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = numsubval[1];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = numsubval[2];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = numsubval[3];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
<br />
<br />
         &nbsp;&nbsp;RS232_Check = ENABLE ;<br />
       &nbsp;&nbsp;<br />
         &nbsp;&nbsp;<br />
      }      <br />
<br />
  } &nbsp;&nbsp;<br />
<br />
  //====================================================================<br />
<br />
    if((indata.length()&gt;0)&amp;&amp;(uart_state == RS485_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
   &nbsp;&nbsp;val=indata; &nbsp;&nbsp;//<br />
<br />
  //----------INPUT READ BACK----------<br />
<br />
   &nbsp;&nbsp;if((val[0]==RS485Address)&amp;&amp;(val[1]==0x02)&amp;&amp;(val[2]==0x01))<br />
      {<br />
        input_1_state=(val[3]&amp;0x01)?1:0 ;<br />
        input_2_state=(val[3]&amp;0x02)?1:0 ;<br />
        input_3_state=(val[3]&amp;0x04)?1:0 ;<br />
        input_4_state=(val[3]&amp;0x08)?1:0 ;<br />
        input_5_state=(val[3]&amp;0x10)?1:0 ;<br />
        input_6_state=(val[3]&amp;0x20)?1:0 ;<br />
        <br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }  <br />
<br />
   &nbsp;&nbsp;//----ALL-RELAY-STATE  READ BACK<br />
    else if((val[0]==RS485Address)&amp;&amp;(val[1]==0x01)&amp;&amp;(val[2]==0x04))<br />
    {<br />
<br />
        switch_32_25_nowstate = val[3];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = val[4];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = val[5];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = val[6];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        if(ini_state==1)<br />
        {<br />
       &nbsp;&nbsp;ini_state=0 ;<br />
       &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_nowstate;<br />
       &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_nowstate;<br />
       &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_nowstate;<br />
       &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_nowstate;<br />
        }<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
    }<br />
<br />
   &nbsp;&nbsp;//----RELAY SET READ BACK<br />
   &nbsp;&nbsp;else if((val[0]==RS485Address)&amp;&amp;(val[1]==0x0F)&amp;&amp;(val[2]==0x00)&amp;&amp;(val[3]==0x00)&amp;&amp;(val[4]==0x00)&amp;&amp;(val[5]==0x20)) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;<br />
        //switch_32_25_nowstate = ;<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        //switch_24_17_nowstate = ;<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        //switch_16_9_nowstate = ;<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        //switch_8_1_nowstate = ;<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
<br />
        RS485_Check = ENABLE ;<br />
 &nbsp;&nbsp;}<br />
 &nbsp;&nbsp;} <br />
    <br />
    indata=""; &nbsp;&nbsp;//clear indata<br />
//--------------------------------------------------<br />
 &nbsp;&nbsp;my_device.uart_service();<br />
  <br />
  //Enter the connection network mode when Pin27 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_TUYA_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  if((uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if ((pre_switch_8_1_nowstate != switch_8_1_nowstate) || (pre_switch_16_9_nowstate != switch_16_9_nowstate) || (pre_switch_24_17_nowstate != switch_24_17_nowstate) || (pre_switch_32_25_nowstate != switch_32_25_nowstate))<br />
    {<br />
      pre_switch_8_1_nowstate = switch_8_1_nowstate;<br />
      pre_switch_16_9_nowstate = switch_16_9_nowstate;<br />
      pre_switch_24_17_nowstate = switch_24_17_nowstate;<br />
      pre_switch_32_25_nowstate = switch_32_25_nowstate;<br />
<br />
      switch_8_1_tarstate = switch_8_1_nowstate ;<br />
      switch_16_9_tarstate = switch_16_9_nowstate ;<br />
      switch_24_17_tarstate = switch_24_17_nowstate ;<br />
      switch_32_25_tarstate = switch_32_25_nowstate ;<br />
      <br />
    }<br />
    else if ((switch_8_1_tarstate != switch_8_1_nowstate) || (switch_16_9_tarstate != switch_16_9_nowstate) || (switch_24_17_tarstate != switch_24_17_nowstate) || (switch_32_25_tarstate != switch_32_25_nowstate))<br />
    {<br />
    if (millis()- RS232_relay_last_time &gt;= RS232RelayDelay) <br />
    {<br />
      RS232_Check = DISABLE ;<br />
<br />
      RS232_relay_last_time = millis();<br />
      if((switch_8_1_tarstate == pre_switch_8_1_tarstate) &amp;&amp; (switch_16_9_tarstate == pre_switch_16_9_tarstate) &amp;&amp; (switch_24_17_tarstate == pre_switch_24_17_tarstate) &amp;&amp; (switch_32_25_tarstate == pre_switch_32_25_tarstate))<br />
      {<br />
      relay_outdata = "";<br />
      relay_outdata = relay_outdata + "RELAY-SET_ALL-255," + switch_32_25_tarstate + "," + switch_24_17_tarstate + "," + switch_16_9_tarstate + "," + switch_8_1_tarstate ;<br />
      Serial2.print(relay_outdata);<br />
<br />
      switch_8_1_nowstate = switch_8_1_tarstate ;<br />
      switch_16_9_nowstate = switch_16_9_tarstate ;<br />
      switch_24_17_nowstate = switch_24_17_tarstate ;<br />
      switch_32_25_nowstate = switch_32_25_tarstate ;<br />
      <br />
      }<br />
      else<br />
      {<br />
      pre_switch_8_1_tarstate = switch_8_1_tarstate ;<br />
      pre_switch_16_9_tarstate = switch_16_9_tarstate ;<br />
      pre_switch_24_17_tarstate = switch_24_17_tarstate ;<br />
      pre_switch_32_25_tarstate = switch_32_25_tarstate ;<br />
      } <br />
    }<br />
    }<br />
  }<br />
<br />
  if((uart_state == RS485_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if ((pre_switch_8_1_nowstate != switch_8_1_nowstate) || (pre_switch_16_9_nowstate != switch_16_9_nowstate) || (pre_switch_24_17_nowstate != switch_24_17_nowstate) || (pre_switch_32_25_nowstate != switch_32_25_nowstate))<br />
    {<br />
      pre_switch_8_1_nowstate = switch_8_1_nowstate;<br />
      pre_switch_16_9_nowstate = switch_16_9_nowstate;<br />
      pre_switch_24_17_nowstate = switch_24_17_nowstate;<br />
      pre_switch_32_25_nowstate = switch_32_25_nowstate;<br />
<br />
      switch_8_1_tarstate = switch_8_1_nowstate ;<br />
      switch_16_9_tarstate = switch_16_9_nowstate ;<br />
      switch_24_17_tarstate = switch_24_17_nowstate ;<br />
      switch_32_25_tarstate = switch_32_25_nowstate ;<br />
      <br />
    }<br />
    else if ((switch_8_1_tarstate != switch_8_1_nowstate) || (switch_16_9_tarstate != switch_16_9_nowstate) || (switch_24_17_tarstate != switch_24_17_nowstate) || (switch_32_25_tarstate != switch_32_25_nowstate))<br />
    {<br />
    if (millis()- RS485_relay_last_time &gt;= RS485RelayDelay) <br />
    {<br />
      RS485_Check = DISABLE ;<br />
<br />
      RS485_relay_last_time = millis();<br />
      if((switch_8_1_tarstate == pre_switch_8_1_tarstate) &amp;&amp; (switch_16_9_tarstate == pre_switch_16_9_tarstate) &amp;&amp; (switch_24_17_tarstate == pre_switch_24_17_tarstate) &amp;&amp; (switch_32_25_tarstate == pre_switch_32_25_tarstate))<br />
      {<br />
     &nbsp;&nbsp;Rs485_Str[0] = RS485Address;<br />
     &nbsp;&nbsp;Rs485_Str[1] = 0x0F;<br />
     &nbsp;&nbsp;Rs485_Str[2] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[3] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[4] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[5] = 0x20;<br />
     &nbsp;&nbsp;Rs485_Str[6] = 0x04;<br />
<br />
     &nbsp;&nbsp;Rs485_Str[7] = switch_8_1_tarstate;<br />
     &nbsp;&nbsp;Rs485_Str[8] = switch_16_9_tarstate;<br />
     &nbsp;&nbsp;Rs485_Str[9] = switch_24_17_tarstate;<br />
     &nbsp;&nbsp;Rs485_Str[10] =  switch_32_25_tarstate;<br />
<br />
     &nbsp;&nbsp;outdata = "1234567890123";<br />
     &nbsp;&nbsp;outdata[0] = Rs485_Str[0];<br />
     &nbsp;&nbsp;outdata[1] = Rs485_Str[1];<br />
     &nbsp;&nbsp;outdata[2] = Rs485_Str[2];<br />
     &nbsp;&nbsp;outdata[3] = Rs485_Str[3];<br />
     &nbsp;&nbsp;outdata[4] = Rs485_Str[4];<br />
     &nbsp;&nbsp;outdata[5] = Rs485_Str[5];<br />
     &nbsp;&nbsp;outdata[6] = Rs485_Str[6];<br />
     &nbsp;&nbsp;outdata[7] = Rs485_Str[7];<br />
     &nbsp;&nbsp;outdata[8] = Rs485_Str[8];<br />
     &nbsp;&nbsp;outdata[9] = Rs485_Str[9];<br />
     &nbsp;&nbsp;outdata[10] = Rs485_Str[10];<br />
   &nbsp;&nbsp;<br />
     &nbsp;&nbsp;CRC16_Data = (crc16((uint8_t *) Rs485_Str, 11, 0x8005, 0xFFFF, 0, true, true));<br />
     &nbsp;&nbsp;CRC16_Data_H = CRC16_Data/256;<br />
     &nbsp;&nbsp;CRC16_Data_L = CRC16_Data%256;<br />
     &nbsp;&nbsp;outdata[11] = CRC16_Data_L;<br />
     &nbsp;&nbsp;outdata[12] = CRC16_Data_H;<br />
     &nbsp;&nbsp;Serial2.print(outdata);<br />
     &nbsp;&nbsp;<br />
     &nbsp;&nbsp;switch_32_25_nowstate = switch_32_25_tarstate;<br />
     &nbsp;&nbsp;switch_24_17_nowstate = switch_24_17_tarstate;<br />
     &nbsp;&nbsp;switch_16_9_nowstate = switch_16_9_tarstate;<br />
     &nbsp;&nbsp;switch_8_1_nowstate = switch_8_1_tarstate;<br />
<br />
      }<br />
      else<br />
      {<br />
      pre_switch_8_1_tarstate = switch_8_1_tarstate ;<br />
      pre_switch_16_9_tarstate = switch_16_9_tarstate ;<br />
      pre_switch_24_17_tarstate = switch_24_17_tarstate ;<br />
      pre_switch_32_25_tarstate = switch_32_25_tarstate ;<br />
      } <br />
    }<br />
    }<br />
  }<br />
<br />
<br />
 &nbsp;&nbsp;<br />
  if((uart_state == RS232_state) &amp;&amp; (RS232_Check == ENABLE)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if (millis()- RS232_last_time &gt;= RS232ReadDelay) <br />
    {<br />
      RS232_last_time = millis();<br />
   &nbsp;&nbsp;if(RS232_read_com == RS232_read_relaycom)<br />
   &nbsp;&nbsp;{<br />
   &nbsp;&nbsp;outdata = "RELAY-STATE-255";<br />
   &nbsp;&nbsp;Serial2.print(outdata);<br />
   &nbsp;&nbsp;RS232_read_com = RS232_read_inputcom;<br />
   &nbsp;&nbsp;}<br />
   &nbsp;&nbsp;else if(RS232_read_com == RS232_read_inputcom)<br />
   &nbsp;&nbsp;{<br />
   &nbsp;&nbsp;outdata = "RELAY-GET_INPUT-255";<br />
   &nbsp;&nbsp;Serial2.print(outdata);<br />
   &nbsp;&nbsp;RS232_read_com = RS232_read_relaycom;<br />
   &nbsp;&nbsp;}<br />
    }<br />
  }<br />
<br />
  if((uart_state == RS485_state) &amp;&amp; (RS485_Check == ENABLE)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if (millis()- RS485_last_time &gt;= RS485ReadDelay) <br />
    {<br />
      RS485_last_time = millis();<br />
   &nbsp;&nbsp;if(RS485_read_com == RS485_read_relaycom)<br />
   &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;//read all realay<br />
     &nbsp;&nbsp;Rs485_Str[0] = RS485Address;<br />
     &nbsp;&nbsp;Rs485_Str[1] = 0x01;<br />
     &nbsp;&nbsp;Rs485_Str[2] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[3] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[4] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[5] = 0x20;<br />
<br />
     &nbsp;&nbsp;outdata = "12345678";<br />
     &nbsp;&nbsp;outdata[0] = Rs485_Str[0];<br />
     &nbsp;&nbsp;outdata[1] = Rs485_Str[1];<br />
     &nbsp;&nbsp;outdata[2] = Rs485_Str[2];<br />
     &nbsp;&nbsp;outdata[3] = Rs485_Str[3];<br />
     &nbsp;&nbsp;outdata[4] = Rs485_Str[4];<br />
     &nbsp;&nbsp;outdata[5] = Rs485_Str[5];<br />
   &nbsp;&nbsp;<br />
     &nbsp;&nbsp;CRC16_Data = (crc16((uint8_t *) Rs485_Str, 6, 0x8005, 0xFFFF, 0, true, true));<br />
     &nbsp;&nbsp;CRC16_Data_H = CRC16_Data/256;<br />
     &nbsp;&nbsp;CRC16_Data_L = CRC16_Data%256;<br />
     &nbsp;&nbsp;outdata[6] = CRC16_Data_L;<br />
     &nbsp;&nbsp;outdata[7] = CRC16_Data_H;<br />
     &nbsp;&nbsp;Serial2.print(outdata);<br />
    <br />
     &nbsp;&nbsp;RS485_read_com = RS485_read_inputcom;<br />
   &nbsp;&nbsp;}<br />
   &nbsp;&nbsp;else if(RS485_read_com == RS485_read_inputcom)<br />
   &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;//read all inputs<br />
     &nbsp;&nbsp;Rs485_Str[0] = RS485Address;<br />
     &nbsp;&nbsp;Rs485_Str[1] = 0x02;<br />
     &nbsp;&nbsp;Rs485_Str[2] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[3] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[4] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[5] = 0x08;<br />
<br />
     &nbsp;&nbsp;outdata = "12345678";<br />
     &nbsp;&nbsp;outdata[0] = Rs485_Str[0];<br />
     &nbsp;&nbsp;outdata[1] = Rs485_Str[1];<br />
     &nbsp;&nbsp;outdata[2] = Rs485_Str[2];<br />
     &nbsp;&nbsp;outdata[3] = Rs485_Str[3];<br />
     &nbsp;&nbsp;outdata[4] = Rs485_Str[4];<br />
     &nbsp;&nbsp;outdata[5] = Rs485_Str[5];<br />
   &nbsp;&nbsp;<br />
     &nbsp;&nbsp;CRC16_Data = (crc16((uint8_t *) Rs485_Str, 6, 0x8005, 0xFFFF, 0, true, true));<br />
     &nbsp;&nbsp;CRC16_Data_H = CRC16_Data/256;<br />
     &nbsp;&nbsp;CRC16_Data_L = CRC16_Data%256;<br />
     &nbsp;&nbsp;outdata[6] = CRC16_Data_L;<br />
     &nbsp;&nbsp;outdata[7] = CRC16_Data_H;<br />
     &nbsp;&nbsp;Serial2.print(outdata);<br />
<br />
   &nbsp;&nbsp;RS485_read_com = RS485_read_relaycom;<br />
   &nbsp;&nbsp;}<br />
    }<br />
  }<br />
<br />
  delay(10);<br />
  <br />
 }<br />
    // return;<br />
}<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  if((uart_state == RS232_state) || (uart_state == RS485_state))<br />
  {<br />
  switch(dpid) {<br />
    case DPID_SWITCH_1:<br />
      switch_1_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_1_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(1-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(1-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH_2:<br />
      switch_2_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_2_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(2-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(2-1)));<br />
      }<br />
      //Status changes should be reported.<br />
    break;<br />
<br />
    case DPID_SWITCH_3:<br />
      switch_3_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_3_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(3-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(3-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH_4:<br />
      switch_4_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_4_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(4-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(4-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH_5:<br />
      switch_5_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_5_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(5-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(5-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH_6:<br />
      switch_6_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_6_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(6-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(6-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH7:<br />
      switch_7_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_7_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(7-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(7-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH8:<br />
      switch_8_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_8_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(8-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(8-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH9:<br />
      switch_9_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_9_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(1-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(1-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH10:<br />
      switch_10_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_10_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(2-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(2-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH11:<br />
      switch_11_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_11_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(3-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(3-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH12:<br />
      switch_12_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_12_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(4-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(4-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH13:<br />
      switch_13_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_13_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(5-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(5-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH14:<br />
      switch_14_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_14_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(6-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(6-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH15:<br />
      switch_15_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_15_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(7-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(7-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH16:<br />
      switch_16_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_16_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(8-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(8-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH17:<br />
      switch_17_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_17_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(1-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(1-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH18:<br />
      switch_18_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_18_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(2-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(2-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH19:<br />
      switch_19_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_19_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(3-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(3-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH20:<br />
      switch_20_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_20_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(4-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(4-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH21:<br />
      switch_21_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_21_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(5-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(5-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH22:<br />
      switch_22_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_22_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(6-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(6-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH23:<br />
      switch_23_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_23_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(7-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(7-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH24:<br />
      switch_24_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_24_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(8-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(8-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH25:<br />
      switch_25_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_25_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(1-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(1-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH26:<br />
      switch_26_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_26_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(2-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(2-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH27:<br />
      switch_27_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_27_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(3-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(3-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH28:<br />
      switch_28_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_28_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(4-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(4-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH29:<br />
      switch_29_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_29_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(5-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(5-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH30:<br />
      switch_30_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_30_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(6-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(6-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH31:<br />
      switch_31_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_31_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(7-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(7-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH32:<br />
      switch_32_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_32_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(8-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(8-1)));<br />
      }<br />
    break;<br />
    default:break;<br />
  }<br />
  }<br />
<br />
  //===============================================================================<br />
<br />
<br />
  <br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{<br />
  my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
<br />
  my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
<br />
  my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
}</code></div></div>]]></description>
			<content:encoded><![CDATA[this 7.1 source code version, have added TCP connection with KinCony's relay board by WiFi. so now you can use RS232 or RS485 or WiFi connect to KinCony's KC868-H32BS, KC868-H32B, KC868-H32L, KC868-H32 relay controller.<br />
just config relay board IP port for router by KC868-Adatper's AP (AP name is KC868-Tuya) webpage.<br />
<br />
here is firmware BIN file, you can download to ESP32 directly by USB cable:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1466" target="_blank" title="">KinCony-Tuya-adapter.nodemcu-32s-7.1.zip</a> (Size: 505.91 KB / Downloads: 889)
<!-- end: postbit_attachments_attachment --><br />
<br />
here is arduino source code:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/zip.png" title="ZIP File" border="0" alt=".zip" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1467" target="_blank" title="">KinCony-Tuya-adapter-source-code-7.1.zip</a> (Size: 8.22 KB / Downloads: 892)
<!-- end: postbit_attachments_attachment --><br />
<br />
Supported products list:<br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1470" target="_blank" title="">tuya-KC868-H32BS.jpg</a> (Size: 150.09 KB / Downloads: 3527)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1469" target="_blank" title="">tuya-KC868-H32B.jpg</a> (Size: 171.2 KB / Downloads: 3205)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1471" target="_blank" title="">tuya-KC868-H32L.jpg</a> (Size: 174.3 KB / Downloads: 2994)
<!-- end: postbit_attachments_attachment --><br />
<!-- start: postbit_attachments_attachment -->
<br /><!-- start: attachment_icon -->
<img src="https://www.kincony.com/forum/images/attachtypes/image.png" title="JPG Image" border="0" alt=".jpg" />
<!-- end: attachment_icon -->&nbsp;&nbsp;<a href="attachment.php?aid=1468" target="_blank" title="">tuya-KC868-H32.jpg</a> (Size: 262.64 KB / Downloads: 3146)
<!-- end: postbit_attachments_attachment --><br />
<br />
source code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>/*KC868-Tuya adapter have 5 LEDs<br />
 * LED D1: when D1-OFF: ready to enable AP for config ESP32 wifi, AP name is KC868-Tuya      D1-ON: ESP32 connect to router successful<br />
 * LED D2: when D2-ON: ESP32 connect to relay board by TCP connection successfully<br />
 * LED D3: when D3-ON: KC868-Tuya adapter work in RS232 or RS485 mode<br />
 * LED D4: when D4 blink: ready to config Tuya wifi module for router SSID<br />
 * LED D6: KC868-Tuya adapter power LED<br />
 * Button S1:Tuya wifi module RESET key<br />
 * Button DL: ESP32 wifi RESET<br />
 * Button RST: ESP32 module RESET<br />
 * Jumper IO23: Short:by RS485 &nbsp;&nbsp;disconnect:by RS232<br />
 * @Copyright: KinCony IoT<br />
 */<br />
#include &lt;WiFi.h&gt;<br />
#include &lt;TuyaWifi.h&gt;<br />
#include &lt;SoftwareSerial.h&gt;<br />
#include "CRC16.h"<br />
#include "CRC.h"<br />
#include &lt;WiFiManager.h&gt; <br />
#include "EEPROM.h"<br />
<br />
<br />
#define TRIGGER_PIN 0 &nbsp;&nbsp;// reset wifi button<br />
WiFiManager wm;<br />
// Replace with your network credentials<br />
const char* ssid   &nbsp;&nbsp;= "xxx"; //your router's ssid<br />
const char* password = "yyy"; //your router's password<br />
<br />
// Set web server port number to 80<br />
WiFiServer server(80);<br />
// Variable to store the HTTP request<br />
String header;<br />
<br />
TuyaWifi my_device;<br />
<br />
#define  RS485Address &nbsp;&nbsp;0x01<br />
#define  RS485Baud   &nbsp;&nbsp;9600<br />
#define  RS232Baud   &nbsp;&nbsp;115200<br />
#define  wifiReadDelay   &nbsp;&nbsp;3000<br />
#define  RS232ReadDelay   &nbsp;&nbsp;3000<br />
#define  RS485ReadDelay   &nbsp;&nbsp;3000<br />
#define  wifiRelayDelay    1000<br />
#define  RS232RelayDelay    500<br />
#define  RS485RelayDelay    500<br />
unsigned char pid[] = {"tsfqvoyi6pxqxq6o"};<br />
unsigned char mcu_ver[] = {"1.0.0"};<br />
//char c;<br />
<br />
//============================================<br />
<br />
<br />
// Current time<br />
unsigned long currentTime = millis();<br />
// Previous time<br />
unsigned long previousTime = 0; <br />
// Define timeout time in milliseconds (example: 2000ms = 2s)<br />
const long timeoutTime = 2000;<br />
int flag_read_di=0;<br />
<br />
<br />
<br />
#define  ADDRESS_TARGET_FIRST  10<br />
#define  ADDRESS_TARGET_IP   &nbsp;&nbsp;30<br />
#define  ADDRESS_TARGET_PORT &nbsp;&nbsp;60<br />
#define  ADDRESS_TARGET_MODE &nbsp;&nbsp;90<br />
<br />
<br />
#define RS232_state &nbsp;&nbsp;1<br />
#define RS485_state &nbsp;&nbsp;0<br />
#define wifi_read_inputcom &nbsp;&nbsp;0<br />
#define wifi_read_relaycom &nbsp;&nbsp;1<br />
#define RS232_read_inputcom &nbsp;&nbsp;0<br />
#define RS232_read_relaycom &nbsp;&nbsp;1<br />
#define RS485_read_inputcom &nbsp;&nbsp;0<br />
#define RS485_read_relaycom &nbsp;&nbsp;1<br />
#define DISABLE &nbsp;&nbsp;0<br />
#define ENABLE &nbsp;&nbsp;1<br />
#define NOCONNECTED &nbsp;&nbsp;0<br />
#define CONNECTED &nbsp;&nbsp;1<br />
uint16_t  CRC16_Data = 0;<br />
uint8_t  CRC16_Data_H = 0;<br />
uint8_t  CRC16_Data_L = 0;<br />
<br />
/* Current  status */<br />
unsigned char wifi_connected = NOCONNECTED;<br />
/* Current  status */<br />
unsigned char led_state = 0;<br />
//1=232 0=485<br />
unsigned char uart_state = 1;<br />
<br />
unsigned char wifi_state = ENABLE;<br />
unsigned char wifi_stop = DISABLE;<br />
<br />
unsigned char wifi_Check = ENABLE;<br />
unsigned char RS232_Check = ENABLE;<br />
unsigned char RS485_Check = ENABLE;<br />
<br />
unsigned char wifi_read_com = 1;<br />
//1= read RELAY  0 = read input<br />
unsigned char RS232_read_com = 1;<br />
//1= read RELAY  0 = read input<br />
unsigned char RS485_read_com = 1;<br />
//1= read RELAY  0 = read input<br />
<br />
unsigned char switch_8_1_nowstate = 0;<br />
unsigned char switch_16_9_nowstate = 0;<br />
unsigned char switch_24_17_nowstate = 0;<br />
unsigned char switch_32_25_nowstate = 0;<br />
unsigned char switch_8_1_tarstate = 0;<br />
unsigned char switch_16_9_tarstate = 0;<br />
unsigned char switch_24_17_tarstate = 0;<br />
unsigned char switch_32_25_tarstate = 0;<br />
<br />
unsigned char pre_switch_8_1_tarstate = 0;<br />
unsigned char pre_switch_16_9_tarstate = 0;<br />
unsigned char pre_switch_24_17_tarstate = 0;<br />
unsigned char pre_switch_32_25_tarstate = 0;<br />
unsigned char pre_switch_8_1_nowstate = 0;<br />
unsigned char pre_switch_16_9_nowstate = 0;<br />
unsigned char pre_switch_24_17_nowstate = 0;<br />
unsigned char pre_switch_32_25_nowstate = 0;<br />
<br />
unsigned char switch_1_8_tarstate = 0;<br />
unsigned char switch_9_16_tarstate = 0;<br />
unsigned char switch_17_24_tarstate = 0;<br />
unsigned char switch_25_32_tarstate = 0;<br />
<br />
unsigned char switch_1_state = 0;<br />
unsigned char switch_2_state = 0;<br />
unsigned char switch_3_state = 0;<br />
unsigned char switch_4_state = 0;<br />
unsigned char switch_5_state = 0;<br />
unsigned char switch_6_state = 0;<br />
<br />
unsigned char input_1_state = 0;<br />
unsigned char input_2_state = 0;<br />
unsigned char input_3_state = 0;<br />
unsigned char input_4_state = 0;<br />
unsigned char input_5_state = 0;<br />
unsigned char input_6_state = 0;<br />
<br />
unsigned char switch_7_state = 0;<br />
unsigned char switch_8_state = 0;<br />
unsigned char switch_9_state = 0;<br />
unsigned char switch_10_state = 0;<br />
unsigned char switch_11_state = 0;<br />
unsigned char switch_12_state = 0;<br />
unsigned char switch_13_state = 0;<br />
unsigned char switch_14_state = 0;<br />
unsigned char switch_15_state = 0;<br />
unsigned char switch_16_state = 0;<br />
unsigned char switch_17_state = 0;<br />
unsigned char switch_18_state = 0;<br />
unsigned char switch_19_state = 0;<br />
unsigned char switch_20_state = 0;<br />
unsigned char switch_21_state = 0;<br />
unsigned char switch_22_state = 0;<br />
unsigned char switch_23_state = 0;<br />
unsigned char switch_24_state = 0;<br />
unsigned char switch_25_state = 0;<br />
unsigned char switch_26_state = 0;<br />
unsigned char switch_27_state = 0;<br />
unsigned char switch_28_state = 0;<br />
unsigned char switch_29_state = 0;<br />
unsigned char switch_30_state = 0;<br />
unsigned char switch_31_state = 0;<br />
unsigned char switch_32_state = 0;<br />
<br />
<br />
unsigned char switch_wifi_delay_state = 0;<br />
unsigned char dpid_wifi_delay_switch = 0;<br />
unsigned char en_wifi_delay_switch = 0;<br />
//1= en delay &nbsp;&nbsp;0= dis delay<br />
<br />
unsigned char switch_232_delay_state = 0;<br />
unsigned char dpid_232_delay_switch = 0;<br />
unsigned char en_232_delay_switch = 0;<br />
//1= en delay &nbsp;&nbsp;0= dis delay<br />
<br />
unsigned char switch_485_delay_state = 0;<br />
unsigned char dpid_485_delay_switch = 0;<br />
unsigned char en_485_delay_switch = 0;<br />
//1= en delay &nbsp;&nbsp;0= dis delay<br />
<br />
unsigned char ini_state = 1;<br />
<br />
unsigned char Rs485_Str[20] = {0};<br />
/* Connect network button pin */<br />
int key_pin = 27;<br />
/*  232(1) 485(0)  */<br />
int sel_uart_pin = 32; <br />
<br />
int index_pos = 0;<br />
int index_begin = 0;<br />
int index_end = 0;<br />
<br />
#define BEGIN_DELAY &nbsp;&nbsp;1<br />
#define END_DELAY &nbsp;&nbsp;0<br />
<br />
/* Data point define */<br />
<br />
#define DPID_SWITCH_1 &nbsp;&nbsp;1<br />
#define DPID_SWITCH_2 &nbsp;&nbsp;2<br />
#define DPID_SWITCH_3 &nbsp;&nbsp;3<br />
#define DPID_SWITCH_4 &nbsp;&nbsp;4<br />
#define DPID_SWITCH_5 &nbsp;&nbsp;5<br />
#define DPID_SWITCH_6 &nbsp;&nbsp;6<br />
<br />
#define DPID_INPUT_1 &nbsp;&nbsp;101<br />
#define DPID_INPUT_2 &nbsp;&nbsp;102<br />
#define DPID_INPUT_3 &nbsp;&nbsp;103<br />
#define DPID_INPUT_4 &nbsp;&nbsp;104<br />
#define DPID_INPUT_5 &nbsp;&nbsp;105<br />
#define DPID_INPUT_6 &nbsp;&nbsp;106<br />
<br />
#define DPID_SWITCH7 &nbsp;&nbsp;113<br />
#define DPID_SWITCH8 &nbsp;&nbsp;114<br />
#define DPID_SWITCH9 &nbsp;&nbsp;115<br />
#define DPID_SWITCH10  116<br />
#define DPID_SWITCH11  117<br />
#define DPID_SWITCH12  118<br />
#define DPID_SWITCH13  119<br />
#define DPID_SWITCH14  120<br />
#define DPID_SWITCH15  121<br />
#define DPID_SWITCH16  122<br />
#define DPID_SWITCH17  123<br />
#define DPID_SWITCH18  124<br />
#define DPID_SWITCH19  125<br />
#define DPID_SWITCH20  126<br />
#define DPID_SWITCH21  127<br />
#define DPID_SWITCH22  128<br />
#define DPID_SWITCH23  129<br />
#define DPID_SWITCH24  130<br />
#define DPID_SWITCH25  131<br />
#define DPID_SWITCH26  132<br />
#define DPID_SWITCH27  133<br />
#define DPID_SWITCH28  134<br />
#define DPID_SWITCH29  135<br />
#define DPID_SWITCH30  136<br />
#define DPID_SWITCH31  137<br />
#define DPID_SWITCH32  138<br />
<br />
<br />
#define LED_TUYA_WiFi  26<br />
#define LED_User1  32<br />
#define LED_ESP_WiFi  33<br />
#define LED_User2  25<br />
<br />
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. <br />
 *                                   &nbsp;&nbsp;dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP<br />
*/<br />
unsigned char dp_array[][2] =<br />
{<br />
  {DPID_SWITCH_1, DP_TYPE_BOOL},{DPID_SWITCH_2, DP_TYPE_BOOL},{DPID_SWITCH_3, DP_TYPE_BOOL},{DPID_SWITCH_4, DP_TYPE_BOOL},{DPID_SWITCH_5, DP_TYPE_BOOL},{DPID_SWITCH_6, DP_TYPE_BOOL},<br />
  {DPID_INPUT_1, DP_TYPE_BOOL},{DPID_INPUT_2, DP_TYPE_BOOL},{DPID_INPUT_3, DP_TYPE_BOOL},{DPID_INPUT_4, DP_TYPE_BOOL},{DPID_INPUT_5, DP_TYPE_BOOL},{DPID_INPUT_6, DP_TYPE_BOOL},<br />
  {DPID_SWITCH7, DP_TYPE_BOOL},{DPID_SWITCH8, DP_TYPE_BOOL},{DPID_SWITCH9, DP_TYPE_BOOL},{DPID_SWITCH10, DP_TYPE_BOOL},{DPID_SWITCH11, DP_TYPE_BOOL},{DPID_SWITCH12, DP_TYPE_BOOL},<br />
  {DPID_SWITCH13, DP_TYPE_BOOL},{DPID_SWITCH14, DP_TYPE_BOOL},{DPID_SWITCH15, DP_TYPE_BOOL},{DPID_SWITCH16, DP_TYPE_BOOL},{DPID_SWITCH17, DP_TYPE_BOOL},{DPID_SWITCH18, DP_TYPE_BOOL},<br />
  {DPID_SWITCH19, DP_TYPE_BOOL},{DPID_SWITCH20, DP_TYPE_BOOL},{DPID_SWITCH21, DP_TYPE_BOOL},{DPID_SWITCH22, DP_TYPE_BOOL},{DPID_SWITCH23, DP_TYPE_BOOL},{DPID_SWITCH24, DP_TYPE_BOOL},<br />
  {DPID_SWITCH25, DP_TYPE_BOOL},{DPID_SWITCH26, DP_TYPE_BOOL},{DPID_SWITCH27, DP_TYPE_BOOL},{DPID_SWITCH28, DP_TYPE_BOOL},{DPID_SWITCH29, DP_TYPE_BOOL},{DPID_SWITCH30, DP_TYPE_BOOL},<br />
 {DPID_SWITCH31, DP_TYPE_BOOL},{DPID_SWITCH32, DP_TYPE_BOOL},<br />
};<br />
<br />
<br />
/* last time */<br />
unsigned long last_time = 0;<br />
unsigned long RS485_last_time = 0;<br />
unsigned long RS232_last_time = 0;<br />
unsigned long wifi_last_time = 0;<br />
<br />
unsigned long wifi_delay_last_time = 0;<br />
unsigned long wifi_relay_last_time = 0;<br />
unsigned long RS232_delay_last_time = 0;<br />
unsigned long RS232_relay_last_time = 0;<br />
unsigned long RS485_delay_last_time = 0;<br />
unsigned long RS485_relay_last_time = 0;<br />
<br />
String indata="";<br />
String val="";<br />
String outdata="";<br />
String relay_outdata="";<br />
String subval="";<br />
int numsubval[6]={0};<br />
String currentLine="";<br />
<br />
String  TARGET_FIRST="";    <br />
String  TARGET_IP="";    <br />
String  TARGET_PORT="";    <br />
String  TARGET_MODE="";    <br />
String  NEW_TARGET_IP="";    <br />
String  NEW_TARGET_PORT="";    <br />
String  NEW_TARGET_MODE="";    <br />
int address = 0;<br />
<br />
String relay1state = "off";// state of relay1<br />
String relay2state = "off";// state of relay2<br />
String relay3state = "off";// state of relay3<br />
String relay4state = "off";// state of relay4<br />
String relay5state = "off";// state of relay5<br />
String relay6state = "off";// state of relay6<br />
String relay7state = "off";// state of relay7<br />
String relay8state = "off";// state of relay8<br />
<br />
String relay9state = "off";// state of relay9<br />
String relay10state = "off";// state of relay10<br />
String relay11state = "off";// state of relay11<br />
String relay12state = "off";// state of relay12<br />
String relay13state = "off";// state of relay13<br />
String relay14state = "off";// state of relay14<br />
String relay15state = "off";// state of relay15<br />
String relay16state = "off";// state of relay16<br />
<br />
String relay17state = "off";// state of relay17<br />
String relay18state = "off";// state of relay18<br />
String relay19state = "off";// state of relay19<br />
String relay20state = "off";// state of relay20<br />
String relay21state = "off";// state of relay21<br />
String relay22state = "off";// state of relay22<br />
String relay23state = "off";// state of relay23<br />
String relay24state = "off";// state of relay24<br />
<br />
String relay25state = "off";// state of relay25<br />
String relay26state = "off";// state of relay26<br />
String relay27state = "off";// state of relay27<br />
String relay28state = "off";// state of relay28<br />
String relay29state = "off";// state of relay29<br />
String relay30state = "off";// state of relay30<br />
String relay31state = "off";// state of relay31<br />
String relay32state = "off";// state of relay32<br />
<br />
WiFiClient client  ; &nbsp;&nbsp;// clients<br />
<br />
void setup() <br />
{<br />
  ini_state = 1; <br />
  wifi_connected = NOCONNECTED;<br />
<br />
  //Initialize led port, turn off led.<br />
<br />
  pinMode(LED_TUYA_WiFi, OUTPUT);<br />
  digitalWrite(LED_TUYA_WiFi, LOW);<br />
  pinMode(LED_ESP_WiFi, OUTPUT);<br />
  digitalWrite(LED_ESP_WiFi, LOW);<br />
<br />
  pinMode(LED_User1, OUTPUT);<br />
  digitalWrite(LED_User1, LOW);<br />
  pinMode(LED_User2, OUTPUT);<br />
  digitalWrite(LED_User2, LOW);<br />
<br />
  // wifi reset button<br />
  pinMode(TRIGGER_PIN, INPUT_PULLUP); <br />
  //Initialize networking key<br />
  pinMode(key_pin, INPUT_PULLUP);<br />
  //Initialize sel key<br />
  pinMode(sel_uart_pin, INPUT_PULLUP);<br />
<br />
  <br />
  //  0 SEL 485    1 SEL 232<br />
  uart_state = RS485_state;<br />
  if (digitalRead(sel_uart_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(sel_uart_pin) == LOW) {<br />
  uart_state = RS232_state;<br />
   &nbsp;&nbsp;}<br />
  }<br />
<br />
  if(uart_state == RS232_state)<br />
  {<br />
    Serial2.begin(RS232Baud,SERIAL_8N1,16,17);<br />
  }<br />
  else<br />
  {<br />
    Serial2.begin(RS485Baud,SERIAL_8N1,16,17); <br />
  }<br />
<br />
 // Serial with tuyawifi<br />
 Serial.begin(9600,SERIAL_8N1,14,13);<br />
    // Serial with debug<br />
 Serial1.begin(9600,SERIAL_8N1,3,1);<br />
<br />
<br />
 Serial1.println("&#92;nTesting EEPROM Library&#92;n");<br />
  if (!EEPROM.begin(1000)) {<br />
    Serial1.println("Failed to initialise EEPROM");<br />
    //Serial2.println("Restarting...");<br />
    delay(1000);<br />
    //ESP.restart();<br />
  }<br />
    <br />
  //Enter the PID and MCU software version<br />
  my_device.init(pid, mcu_ver);<br />
  //incoming all DPs and their types array, DP numbers<br />
  my_device.set_dp_cmd_total(dp_array, 1);<br />
  //register DP download processing callback function<br />
  my_device.dp_process_func_register(dp_process);<br />
  //register upload all DP callback function<br />
  my_device.dp_update_all_func_register(dp_update_all);<br />
<br />
  address = ADDRESS_TARGET_FIRST;<br />
  TARGET_FIRST = EEPROM.readString(address); <br />
  address = ADDRESS_TARGET_IP;<br />
  TARGET_IP = EEPROM.readString(address); <br />
  address = ADDRESS_TARGET_PORT;<br />
  TARGET_PORT = EEPROM.readString(address); <br />
  address = ADDRESS_TARGET_MODE;<br />
  TARGET_MODE = EEPROM.readString(address); <br />
<br />
  //default<br />
  if (TARGET_FIRST != "123456")<br />
    {<br />
   &nbsp;&nbsp;TARGET_IP="192.168.1.200";    <br />
   &nbsp;&nbsp;TARGET_PORT="4196";    <br />
   &nbsp;&nbsp;TARGET_MODE="wifi";    <br />
    }    <br />
<br />
 // Connect to Wi-Fi network with SSID and password<br />
  WiFi.mode(WIFI_STA);<br />
 //close wifi sleep<br />
  WiFi.setSleep(false);<br />
  <br />
  // Define a text box, 50 characters maximum<br />
  WiFiManagerParameter custom_text_box("my_text", "Enter Relay Board IP", TARGET_IP.c_str(), 50);<br />
  // Add custom parameter<br />
  wm.addParameter(&amp;custom_text_box);<br />
  // Define a text box, 50 characters maximum<br />
  WiFiManagerParameter custom_text_box2("my_text2", "Enter Relay Board Port", TARGET_PORT.c_str(), 50);<br />
  // Add custom parameter<br />
  wm.addParameter(&amp;custom_text_box2);<br />
  // Define a text box, 50 characters maximum<br />
  WiFiManagerParameter custom_text_box3("my_text3", "Enter wifi or 232 or 485", TARGET_MODE.c_str(), 50);<br />
  // Add custom parameter<br />
  wm.addParameter(&amp;custom_text_box3);<br />
  <br />
   &nbsp;&nbsp;bool res;<br />
     &nbsp;&nbsp;res = wm.autoConnect("KC868-Tuya"); // anonymous ap<br />
<br />
    if(!res) {<br />
        Serial1.println("Failed to connect");<br />
        //ESP.restart();<br />
    } <br />
    else {<br />
        //if you get here you have connected to the WiFi    <br />
        Serial1.println("connected...yeey :)");<br />
    } <br />
<br />
<br />
while (WiFi.status() != WL_CONNECTED) {<br />
    delay(500);<br />
    Serial1.print(".");<br />
  }<br />
  // Print local IP address and start web server<br />
  Serial1.println("");<br />
  Serial1.println("WiFi connected.");<br />
  Serial1.println("Local IP address: ");<br />
  Serial1.println(WiFi.localIP());<br />
    // Print custom text box value to serial monitor<br />
<br />
  NEW_TARGET_IP = custom_text_box.getValue();    <br />
  NEW_TARGET_PORT = custom_text_box2.getValue();    <br />
  NEW_TARGET_MODE = custom_text_box3.getValue();    <br />
<br />
  if(NEW_TARGET_IP != TARGET_IP)<br />
    {<br />
   &nbsp;&nbsp;TARGET_IP = NEW_TARGET_IP;<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_IP; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_IP);<br />
<br />
   &nbsp;&nbsp;TARGET_FIRST = "123456";<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_FIRST; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_FIRST);<br />
    <br />
   &nbsp;&nbsp;EEPROM.commit();<br />
    }<br />
<br />
  if(NEW_TARGET_PORT != TARGET_PORT)<br />
    {<br />
   &nbsp;&nbsp;TARGET_PORT = NEW_TARGET_PORT;<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_PORT; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_PORT);<br />
<br />
   &nbsp;&nbsp;TARGET_FIRST = "123456";<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_FIRST; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_FIRST);<br />
    <br />
   &nbsp;&nbsp;EEPROM.commit();<br />
    }<br />
<br />
  if(NEW_TARGET_MODE != TARGET_MODE)<br />
    {<br />
   &nbsp;&nbsp;TARGET_MODE = NEW_TARGET_MODE;<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_MODE; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_MODE);<br />
<br />
   &nbsp;&nbsp;TARGET_FIRST = "123456";<br />
   &nbsp;&nbsp;address = ADDRESS_TARGET_FIRST; &nbsp;&nbsp;<br />
   &nbsp;&nbsp;EEPROM.writeString(address, TARGET_FIRST);<br />
    <br />
   &nbsp;&nbsp;EEPROM.commit();<br />
    }<br />
<br />
  Serial1.print("target IP: ");<br />
  Serial1.println(TARGET_IP);<br />
  Serial1.print("target port: ");<br />
  Serial1.println(TARGET_PORT);<br />
  Serial1.print("target mode: ");<br />
  Serial1.println(TARGET_MODE);<br />
<br />
  //server.begin(); ???????????????????????????????<br />
 pinMode(LED_User1, OUTPUT);<br />
 digitalWrite(LED_User1, HIGH);<br />
                                                                                                             &nbsp;&nbsp;<br />
  last_time = millis();<br />
}<br />
<br />
void loop() <br />
{ <br />
 <br />
 if((TARGET_MODE=="wifi")||(TARGET_MODE=="WIFI"))  <br />
 {<br />
 digitalWrite(LED_User2, LOW);<br />
<br />
 const char* host = TARGET_IP.c_str();<br />
 int httpPort = TARGET_PORT.toInt();<br />
 if (client.connect(host, httpPort)) {<br />
     &nbsp;&nbsp;wifi_state = ENABLE;<br />
     &nbsp;&nbsp;digitalWrite(LED_ESP_WiFi, HIGH);<br />
    while (client.connected() || client.available()) <br />
    {  <br />
    <br />
   &nbsp;&nbsp;// wifi_connected = CONNECTED;<br />
      if(client.available() )<br />
      {<br />
      String line = client.readStringUntil('&#92;n');<br />
      Serial1.print("line=");<br />
      Serial1.println(line);<br />
      //if(line.length()&lt;50) <br />
      // {<br />
      // indata = line;<br />
      // //Serial1.print("indata=");<br />
      // Serial1.println(indata);<br />
      // }<br />
      //client.write(line.c_str()); <br />
     &nbsp;&nbsp;if(line.indexOf("RELAY-SET_ALL-255") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-SET_ALL-255");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;else if(line.indexOf("RELAY-GET_INPUT-255") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-GET_INPUT-255");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;else if(line.indexOf("RELAY-STATE-255") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-STATE-255");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;else if(line.indexOf("RELAY-ALARM") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-ALARM");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;else if(line.indexOf("RELAY-DIARM") &gt;= 0)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;index_begin = line.indexOf("RELAY-DIARM");<br />
     &nbsp;&nbsp;index_end = line.indexOf("OK", index_begin);<br />
     &nbsp;&nbsp;index_end = index_end + 2;<br />
     &nbsp;&nbsp;indata = line.substring(index_begin,index_end);<br />
     &nbsp;&nbsp;//Serial1.print("indata=");<br />
     &nbsp;&nbsp;Serial1.println(indata);<br />
     &nbsp;&nbsp;}<br />
      <br />
      }<br />
      <br />
     &nbsp;&nbsp;<br />
  /////////////wifi mode begin<br />
  if((en_wifi_delay_switch == BEGIN_DELAY)&amp;&amp;(wifi_state == ENABLE))<br />
  {<br />
    if((millis()-wifi_delay_last_time)&gt;=20)<br />
    {<br />
      my_device.mcu_dp_update(dpid_wifi_delay_switch, switch_wifi_delay_state, 1);<br />
      en_wifi_delay_switch = END_DELAY;<br />
    }<br />
  }<br />
<br />
    if((indata.length()&gt;0)&amp;&amp;(((uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE))||(wifi_state == ENABLE)))<br />
  {<br />
   &nbsp;&nbsp;val=indata; &nbsp;&nbsp;//<br />
<br />
  //----------INPUT ALARM--------<br />
      if(val=="RELAY-ALARM-1,OK") &nbsp;&nbsp;<br />
      {<br />
        input_1_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-1,OK") &nbsp;&nbsp;<br />
      {<br />
        input_1_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-2,OK") &nbsp;&nbsp;<br />
      {<br />
        input_2_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-2,OK") &nbsp;&nbsp;<br />
      {<br />
        input_2_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-3,OK") &nbsp;&nbsp;<br />
      {<br />
        input_3_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-3,OK") &nbsp;&nbsp;<br />
      {<br />
        input_3_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-4,OK") &nbsp;&nbsp;<br />
      {<br />
        input_4_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-4,OK") &nbsp;&nbsp;<br />
      {<br />
        input_4_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-5,OK") &nbsp;&nbsp;<br />
      {<br />
        input_5_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-5,OK") &nbsp;&nbsp;<br />
      {<br />
        input_5_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-6,OK") &nbsp;&nbsp;<br />
      {<br />
        input_6_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-6,OK") &nbsp;&nbsp;<br />
      {<br />
        input_6_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----INPUT READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,19)=="RELAY-GET_INPUT-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;<br />
     &nbsp;&nbsp;//wifi_stop = DISABLE;<br />
<br />
     &nbsp;&nbsp;subval =  val.substring(20);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        input_6_state = (numsubval[0]&amp;0x20)?0:1;<br />
        input_5_state = (numsubval[0]&amp;0x10)?0:1;<br />
        input_4_state = (numsubval[0]&amp;0x08)?0:1;<br />
        input_3_state = (numsubval[0]&amp;0x04)?0:1;<br />
        input_2_state = (numsubval[0]&amp;0x02)?0:1;<br />
        input_1_state = (numsubval[0]&amp;0x01)?0:1;<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----ALL-RELAY-STATE  READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,15)=="RELAY-STATE-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;//wifi_stop = DISABLE;<br />
<br />
     &nbsp;&nbsp;subval =  val.substring(16);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        switch_32_25_nowstate = numsubval[0];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = numsubval[1];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = numsubval[2];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
       &nbsp;&nbsp;switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = numsubval[3];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        if(ini_state==1)<br />
        {<br />
       &nbsp;&nbsp;ini_state=0 ;<br />
       &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_nowstate;<br />
       &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_nowstate;<br />
       &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_nowstate;<br />
       &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_nowstate;<br />
        }<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----RELAY SET READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,17)=="RELAY-SET_ALL-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;subval =  val.substring(18);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        switch_32_25_nowstate = numsubval[0];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = numsubval[1];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = numsubval[2];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = numsubval[3];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
<br />
<br />
         &nbsp;&nbsp;wifi_Check = ENABLE ;<br />
          <br />
      }      <br />
 <br />
   &nbsp;&nbsp;// wifi_last_time = millis();<br />
  } &nbsp;&nbsp;<br />
<br />
    indata=""; &nbsp;&nbsp;//clear indata<br />
//--------------------------------------------------<br />
 &nbsp;&nbsp;my_device.uart_service();<br />
<br />
  //Enter the connection network mode when Pin27 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_TUYA_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
    if(wifi_state == ENABLE)<br />
  {<br />
    if ((pre_switch_8_1_nowstate != switch_8_1_nowstate) || (pre_switch_16_9_nowstate != switch_16_9_nowstate) || (pre_switch_24_17_nowstate != switch_24_17_nowstate) || (pre_switch_32_25_nowstate != switch_32_25_nowstate))<br />
    {<br />
      pre_switch_8_1_nowstate = switch_8_1_nowstate;<br />
      pre_switch_16_9_nowstate = switch_16_9_nowstate;<br />
      pre_switch_24_17_nowstate = switch_24_17_nowstate;<br />
      pre_switch_32_25_nowstate = switch_32_25_nowstate;<br />
<br />
      switch_8_1_tarstate = switch_8_1_nowstate ;<br />
      switch_16_9_tarstate = switch_16_9_nowstate ;<br />
      switch_24_17_tarstate = switch_24_17_nowstate ;<br />
      switch_32_25_tarstate = switch_32_25_nowstate ;<br />
      <br />
    }<br />
    else if ((switch_8_1_tarstate != switch_8_1_nowstate) || (switch_16_9_tarstate != switch_16_9_nowstate) || (switch_24_17_tarstate != switch_24_17_nowstate) || (switch_32_25_tarstate != switch_32_25_nowstate))<br />
    {<br />
    if (millis()- wifi_relay_last_time &gt;= wifiRelayDelay) <br />
    {<br />
      wifi_Check = DISABLE ;<br />
<br />
      wifi_relay_last_time = millis();<br />
      if((switch_8_1_tarstate == pre_switch_8_1_tarstate) &amp;&amp; (switch_16_9_tarstate == pre_switch_16_9_tarstate) &amp;&amp; (switch_24_17_tarstate == pre_switch_24_17_tarstate) &amp;&amp; (switch_32_25_tarstate == pre_switch_32_25_tarstate))<br />
      {<br />
<br />
      relay_outdata = "";<br />
      relay_outdata = relay_outdata + "RELAY-SET_ALL-255," + switch_32_25_tarstate + "," + switch_24_17_tarstate + "," + switch_16_9_tarstate + "," + switch_8_1_tarstate ;<br />
      client.print(relay_outdata);<br />
      Serial1.println(relay_outdata);<br />
      switch_8_1_nowstate = switch_8_1_tarstate ;<br />
      switch_16_9_nowstate = switch_16_9_tarstate ;<br />
      switch_24_17_nowstate = switch_24_17_tarstate ;<br />
      switch_32_25_nowstate = switch_32_25_tarstate ;<br />
      }<br />
      else<br />
      {<br />
      pre_switch_8_1_tarstate = switch_8_1_tarstate ;<br />
      pre_switch_16_9_tarstate = switch_16_9_tarstate ;<br />
      pre_switch_24_17_tarstate = switch_24_17_tarstate ;<br />
      pre_switch_32_25_tarstate = switch_32_25_tarstate ;<br />
      } <br />
    }<br />
    }<br />
  }<br />
<br />
  if(wifi_state == ENABLE)<br />
  {<br />
    if (millis()- wifi_last_time &gt;= wifiReadDelay) <br />
    {<br />
      wifi_last_time = millis();<br />
  <br />
   &nbsp;&nbsp;if(wifi_read_com == wifi_read_relaycom)<br />
   &nbsp;&nbsp;{<br />
   &nbsp;&nbsp;outdata = "RELAY-STATE-255";<br />
   &nbsp;&nbsp;client.print(outdata); <br />
   &nbsp;&nbsp;wifi_stop = ENABLE;<br />
   &nbsp;&nbsp;wifi_read_com = wifi_read_inputcom;<br />
   &nbsp;&nbsp;}<br />
   &nbsp;&nbsp;else if(wifi_read_com == wifi_read_inputcom)<br />
   &nbsp;&nbsp;{<br />
   &nbsp;&nbsp;outdata = "RELAY-GET_INPUT-255";<br />
   &nbsp;&nbsp;client.print(outdata);<br />
   &nbsp;&nbsp;wifi_stop = ENABLE;<br />
   &nbsp;&nbsp;wifi_read_com = wifi_read_relaycom;<br />
   &nbsp;&nbsp;}<br />
 <br />
    <br />
    }<br />
  }<br />
<br />
  if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}<br />
      <br />
    //wifi mode end<br />
    <br />
   &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;client.stop();<br />
    }<br />
 else<br />
    {<br />
     &nbsp;&nbsp;if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}<br />
 <br />
     &nbsp;&nbsp;wifi_state = DISABLE;<br />
     &nbsp;&nbsp;digitalWrite(LED_ESP_WiFi, LOW);<br />
     &nbsp;&nbsp;client.stop();<br />
    }<br />
   &nbsp;&nbsp;delay(5000);<br />
 }<br />
<br />
 else<br />
 {<br />
 &nbsp;&nbsp;digitalWrite(LED_User2, HIGH);<br />
   &nbsp;&nbsp;<br />
   &nbsp;&nbsp;wifi_state = DISABLE;<br />
   &nbsp;&nbsp;digitalWrite(LED_ESP_WiFi, LOW);<br />
  <br />
  //no wifi mode begin <br />
  if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}<br />
<br />
  if((uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
  while(Serial2.available()&gt;0)<br />
 &nbsp;&nbsp;{<br />
    indata+=char(Serial2.read());  //read KBOX via 232<br />
    delay(2);<br />
    if(Serial2.available()&lt;=0)<br />
   &nbsp;&nbsp;{<br />
        //Serial2.print(indata);<br />
   &nbsp;&nbsp;}<br />
 &nbsp;&nbsp;}    <br />
  }<br />
  else if((uart_state == RS485_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
  while(Serial2.available()&gt;0)<br />
 &nbsp;&nbsp;{<br />
    indata+=char(Serial2.read()); &nbsp;&nbsp;//read KBOX via 485<br />
    delay(2);<br />
    if(Serial2.available()&lt;=0)<br />
   &nbsp;&nbsp;{<br />
        //Serial2.print(indata);<br />
   &nbsp;&nbsp;}<br />
 &nbsp;&nbsp;}    <br />
  }<br />
<br />
  if((en_232_delay_switch == BEGIN_DELAY)&amp;&amp;(uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if((millis()-RS232_delay_last_time)&gt;=20)<br />
    {<br />
      my_device.mcu_dp_update(dpid_232_delay_switch, switch_232_delay_state, 1);<br />
      en_232_delay_switch = END_DELAY;<br />
    }<br />
  }<br />
<br />
    if((en_485_delay_switch == BEGIN_DELAY)&amp;&amp;(uart_state == RS485_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if((millis()-RS485_delay_last_time)&gt;=20)<br />
    {<br />
      my_device.mcu_dp_update(dpid_485_delay_switch, switch_485_delay_state, 1);<br />
      en_485_delay_switch = END_DELAY;<br />
    }<br />
  }<br />
<br />
<br />
    if((indata.length()&gt;0)&amp;&amp;((uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE)))<br />
  {<br />
   &nbsp;&nbsp;val=indata; &nbsp;&nbsp;//<br />
<br />
  //----------INPUT ALARM--------<br />
      if(val=="RELAY-ALARM-1,OK") &nbsp;&nbsp;<br />
      {<br />
        input_1_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-1,OK") &nbsp;&nbsp;<br />
      {<br />
        input_1_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-2,OK") &nbsp;&nbsp;<br />
      {<br />
        input_2_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-2,OK") &nbsp;&nbsp;<br />
      {<br />
        input_2_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-3,OK") &nbsp;&nbsp;<br />
      {<br />
        input_3_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-3,OK") &nbsp;&nbsp;<br />
      {<br />
        input_3_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-4,OK") &nbsp;&nbsp;<br />
      {<br />
        input_4_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-4,OK") &nbsp;&nbsp;<br />
      {<br />
        input_4_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-5,OK") &nbsp;&nbsp;<br />
      {<br />
        input_5_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-5,OK") &nbsp;&nbsp;<br />
      {<br />
        input_5_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-ALARM-6,OK") &nbsp;&nbsp;<br />
      {<br />
        input_6_state = 1;<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;else if(val=="RELAY-DIARM-6,OK") &nbsp;&nbsp;<br />
      {<br />
        input_6_state = 0;<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----INPUT READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,19)=="RELAY-GET_INPUT-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;subval =  val.substring(20);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        input_6_state = (numsubval[0]&amp;0x20)?0:1;<br />
        input_5_state = (numsubval[0]&amp;0x10)?0:1;<br />
        input_4_state = (numsubval[0]&amp;0x08)?0:1;<br />
        input_3_state = (numsubval[0]&amp;0x04)?0:1;<br />
        input_2_state = (numsubval[0]&amp;0x02)?0:1;<br />
        input_1_state = (numsubval[0]&amp;0x01)?0:1;<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----ALL-RELAY-STATE  READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,15)=="RELAY-STATE-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;subval =  val.substring(16);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        switch_32_25_nowstate = numsubval[0];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = numsubval[1];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = numsubval[2];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = numsubval[3];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        if(ini_state==1)<br />
        {<br />
       &nbsp;&nbsp;ini_state=0 ;<br />
       &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_nowstate;<br />
       &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_nowstate;<br />
       &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_nowstate;<br />
       &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_nowstate;<br />
        }<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
      }      <br />
   &nbsp;&nbsp;//----RELAY SET READ BACK<br />
   &nbsp;&nbsp;else if((val.substring(0,17)=="RELAY-SET_ALL-255")) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;subval =  val.substring(18);<br />
     &nbsp;&nbsp;int j=0;<br />
     &nbsp;&nbsp;int m=subval.length();<br />
     &nbsp;&nbsp;for(int i=0; i&lt;m; i++)<br />
     &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;if(subval[i]==',')<br />
        {<br />
       &nbsp;&nbsp;j++; <br />
       &nbsp;&nbsp;}<br />
        else<br />
        {<br />
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');<br />
        }<br />
     &nbsp;&nbsp;}<br />
     &nbsp;&nbsp;<br />
        switch_32_25_nowstate = numsubval[0];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = numsubval[1];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = numsubval[2];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = numsubval[3];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        numsubval[0]=0;<br />
        numsubval[1]=0;<br />
        numsubval[2]=0;<br />
        numsubval[3]=0;<br />
        numsubval[4]=0;<br />
        numsubval[5]=0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
<br />
<br />
         &nbsp;&nbsp;RS232_Check = ENABLE ;<br />
       &nbsp;&nbsp;<br />
         &nbsp;&nbsp;<br />
      }      <br />
<br />
  } &nbsp;&nbsp;<br />
<br />
  //====================================================================<br />
<br />
    if((indata.length()&gt;0)&amp;&amp;(uart_state == RS485_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
   &nbsp;&nbsp;val=indata; &nbsp;&nbsp;//<br />
<br />
  //----------INPUT READ BACK----------<br />
<br />
   &nbsp;&nbsp;if((val[0]==RS485Address)&amp;&amp;(val[1]==0x02)&amp;&amp;(val[2]==0x01))<br />
      {<br />
        input_1_state=(val[3]&amp;0x01)?1:0 ;<br />
        input_2_state=(val[3]&amp;0x02)?1:0 ;<br />
        input_3_state=(val[3]&amp;0x04)?1:0 ;<br />
        input_4_state=(val[3]&amp;0x08)?1:0 ;<br />
        input_5_state=(val[3]&amp;0x10)?1:0 ;<br />
        input_6_state=(val[3]&amp;0x20)?1:0 ;<br />
        <br />
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
      }  <br />
<br />
   &nbsp;&nbsp;//----ALL-RELAY-STATE  READ BACK<br />
    else if((val[0]==RS485Address)&amp;&amp;(val[1]==0x01)&amp;&amp;(val[2]==0x04))<br />
    {<br />
<br />
        switch_32_25_nowstate = val[3];<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_24_17_nowstate = val[4];<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_16_9_nowstate = val[5];<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        switch_8_1_nowstate = val[6];<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        if(ini_state==1)<br />
        {<br />
       &nbsp;&nbsp;ini_state=0 ;<br />
       &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_nowstate;<br />
       &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_nowstate;<br />
       &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_nowstate;<br />
       &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_nowstate;<br />
        }<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
    }<br />
<br />
   &nbsp;&nbsp;//----RELAY SET READ BACK<br />
   &nbsp;&nbsp;else if((val[0]==RS485Address)&amp;&amp;(val[1]==0x0F)&amp;&amp;(val[2]==0x00)&amp;&amp;(val[3]==0x00)&amp;&amp;(val[4]==0x00)&amp;&amp;(val[5]==0x20)) &nbsp;&nbsp;<br />
      {<br />
     &nbsp;&nbsp;<br />
        //switch_32_25_nowstate = ;<br />
        switch_32_state = (switch_32_25_nowstate&amp;0x80)?1:0;<br />
        switch_31_state = (switch_32_25_nowstate&amp;0x40)?1:0;<br />
        switch_30_state = (switch_32_25_nowstate&amp;0x20)?1:0;<br />
        switch_29_state = (switch_32_25_nowstate&amp;0x10)?1:0;<br />
        switch_28_state = (switch_32_25_nowstate&amp;0x08)?1:0;<br />
        switch_27_state = (switch_32_25_nowstate&amp;0x04)?1:0;<br />
        switch_26_state = (switch_32_25_nowstate&amp;0x02)?1:0;<br />
        switch_25_state = (switch_32_25_nowstate&amp;0x01)?1:0;<br />
<br />
        //switch_24_17_nowstate = ;<br />
        switch_24_state = (switch_24_17_nowstate&amp;0x80)?1:0;<br />
        switch_23_state = (switch_24_17_nowstate&amp;0x40)?1:0;<br />
        switch_22_state = (switch_24_17_nowstate&amp;0x20)?1:0;<br />
        switch_21_state = (switch_24_17_nowstate&amp;0x10)?1:0;<br />
        switch_20_state = (switch_24_17_nowstate&amp;0x08)?1:0;<br />
        switch_19_state = (switch_24_17_nowstate&amp;0x04)?1:0;<br />
        switch_18_state = (switch_24_17_nowstate&amp;0x02)?1:0;<br />
        switch_17_state = (switch_24_17_nowstate&amp;0x01)?1:0;<br />
<br />
        //switch_16_9_nowstate = ;<br />
        switch_16_state = (switch_16_9_nowstate&amp;0x80)?1:0;<br />
        switch_15_state = (switch_16_9_nowstate&amp;0x40)?1:0;<br />
        switch_14_state = (switch_16_9_nowstate&amp;0x20)?1:0;<br />
        switch_13_state = (switch_16_9_nowstate&amp;0x10)?1:0;<br />
        switch_12_state = (switch_16_9_nowstate&amp;0x08)?1:0;<br />
        switch_11_state = (switch_16_9_nowstate&amp;0x04)?1:0;<br />
        switch_10_state = (switch_16_9_nowstate&amp;0x02)?1:0;<br />
        switch_9_state =  (switch_16_9_nowstate&amp;0x01)?1:0;<br />
<br />
        //switch_8_1_nowstate = ;<br />
        switch_8_state = (switch_8_1_nowstate&amp;0x80)?1:0;<br />
        switch_7_state = (switch_8_1_nowstate&amp;0x40)?1:0;<br />
        switch_6_state = (switch_8_1_nowstate&amp;0x20)?1:0;<br />
        switch_5_state = (switch_8_1_nowstate&amp;0x10)?1:0;<br />
        switch_4_state = (switch_8_1_nowstate&amp;0x08)?1:0;<br />
        switch_3_state = (switch_8_1_nowstate&amp;0x04)?1:0;<br />
        switch_2_state = (switch_8_1_nowstate&amp;0x02)?1:0;<br />
        switch_1_state = (switch_8_1_nowstate&amp;0x01)?1:0;<br />
<br />
        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
<br />
        RS485_Check = ENABLE ;<br />
 &nbsp;&nbsp;}<br />
 &nbsp;&nbsp;} <br />
    <br />
    indata=""; &nbsp;&nbsp;//clear indata<br />
//--------------------------------------------------<br />
 &nbsp;&nbsp;my_device.uart_service();<br />
  <br />
  //Enter the connection network mode when Pin27 is pressed.<br />
  if (digitalRead(key_pin) == LOW) {<br />
    delay(80);<br />
    if (digitalRead(key_pin) == LOW) {<br />
      my_device.mcu_set_wifi_mode(SMART_CONFIG);<br />
    }<br />
  }<br />
  /* LED blinks when network is being connected */<br />
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) &amp;&amp; (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {<br />
    if (millis()- last_time &gt;= 500) {<br />
      last_time = millis();<br />
<br />
      if (led_state == LOW) {<br />
        led_state = HIGH;<br />
      } else {<br />
        led_state = LOW;<br />
      }<br />
      digitalWrite(LED_TUYA_WiFi, led_state);<br />
    }<br />
  }<br />
<br />
  if((uart_state == RS232_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if ((pre_switch_8_1_nowstate != switch_8_1_nowstate) || (pre_switch_16_9_nowstate != switch_16_9_nowstate) || (pre_switch_24_17_nowstate != switch_24_17_nowstate) || (pre_switch_32_25_nowstate != switch_32_25_nowstate))<br />
    {<br />
      pre_switch_8_1_nowstate = switch_8_1_nowstate;<br />
      pre_switch_16_9_nowstate = switch_16_9_nowstate;<br />
      pre_switch_24_17_nowstate = switch_24_17_nowstate;<br />
      pre_switch_32_25_nowstate = switch_32_25_nowstate;<br />
<br />
      switch_8_1_tarstate = switch_8_1_nowstate ;<br />
      switch_16_9_tarstate = switch_16_9_nowstate ;<br />
      switch_24_17_tarstate = switch_24_17_nowstate ;<br />
      switch_32_25_tarstate = switch_32_25_nowstate ;<br />
      <br />
    }<br />
    else if ((switch_8_1_tarstate != switch_8_1_nowstate) || (switch_16_9_tarstate != switch_16_9_nowstate) || (switch_24_17_tarstate != switch_24_17_nowstate) || (switch_32_25_tarstate != switch_32_25_nowstate))<br />
    {<br />
    if (millis()- RS232_relay_last_time &gt;= RS232RelayDelay) <br />
    {<br />
      RS232_Check = DISABLE ;<br />
<br />
      RS232_relay_last_time = millis();<br />
      if((switch_8_1_tarstate == pre_switch_8_1_tarstate) &amp;&amp; (switch_16_9_tarstate == pre_switch_16_9_tarstate) &amp;&amp; (switch_24_17_tarstate == pre_switch_24_17_tarstate) &amp;&amp; (switch_32_25_tarstate == pre_switch_32_25_tarstate))<br />
      {<br />
      relay_outdata = "";<br />
      relay_outdata = relay_outdata + "RELAY-SET_ALL-255," + switch_32_25_tarstate + "," + switch_24_17_tarstate + "," + switch_16_9_tarstate + "," + switch_8_1_tarstate ;<br />
      Serial2.print(relay_outdata);<br />
<br />
      switch_8_1_nowstate = switch_8_1_tarstate ;<br />
      switch_16_9_nowstate = switch_16_9_tarstate ;<br />
      switch_24_17_nowstate = switch_24_17_tarstate ;<br />
      switch_32_25_nowstate = switch_32_25_tarstate ;<br />
      <br />
      }<br />
      else<br />
      {<br />
      pre_switch_8_1_tarstate = switch_8_1_tarstate ;<br />
      pre_switch_16_9_tarstate = switch_16_9_tarstate ;<br />
      pre_switch_24_17_tarstate = switch_24_17_tarstate ;<br />
      pre_switch_32_25_tarstate = switch_32_25_tarstate ;<br />
      } <br />
    }<br />
    }<br />
  }<br />
<br />
  if((uart_state == RS485_state)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if ((pre_switch_8_1_nowstate != switch_8_1_nowstate) || (pre_switch_16_9_nowstate != switch_16_9_nowstate) || (pre_switch_24_17_nowstate != switch_24_17_nowstate) || (pre_switch_32_25_nowstate != switch_32_25_nowstate))<br />
    {<br />
      pre_switch_8_1_nowstate = switch_8_1_nowstate;<br />
      pre_switch_16_9_nowstate = switch_16_9_nowstate;<br />
      pre_switch_24_17_nowstate = switch_24_17_nowstate;<br />
      pre_switch_32_25_nowstate = switch_32_25_nowstate;<br />
<br />
      switch_8_1_tarstate = switch_8_1_nowstate ;<br />
      switch_16_9_tarstate = switch_16_9_nowstate ;<br />
      switch_24_17_tarstate = switch_24_17_nowstate ;<br />
      switch_32_25_tarstate = switch_32_25_nowstate ;<br />
      <br />
    }<br />
    else if ((switch_8_1_tarstate != switch_8_1_nowstate) || (switch_16_9_tarstate != switch_16_9_nowstate) || (switch_24_17_tarstate != switch_24_17_nowstate) || (switch_32_25_tarstate != switch_32_25_nowstate))<br />
    {<br />
    if (millis()- RS485_relay_last_time &gt;= RS485RelayDelay) <br />
    {<br />
      RS485_Check = DISABLE ;<br />
<br />
      RS485_relay_last_time = millis();<br />
      if((switch_8_1_tarstate == pre_switch_8_1_tarstate) &amp;&amp; (switch_16_9_tarstate == pre_switch_16_9_tarstate) &amp;&amp; (switch_24_17_tarstate == pre_switch_24_17_tarstate) &amp;&amp; (switch_32_25_tarstate == pre_switch_32_25_tarstate))<br />
      {<br />
     &nbsp;&nbsp;Rs485_Str[0] = RS485Address;<br />
     &nbsp;&nbsp;Rs485_Str[1] = 0x0F;<br />
     &nbsp;&nbsp;Rs485_Str[2] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[3] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[4] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[5] = 0x20;<br />
     &nbsp;&nbsp;Rs485_Str[6] = 0x04;<br />
<br />
     &nbsp;&nbsp;Rs485_Str[7] = switch_8_1_tarstate;<br />
     &nbsp;&nbsp;Rs485_Str[8] = switch_16_9_tarstate;<br />
     &nbsp;&nbsp;Rs485_Str[9] = switch_24_17_tarstate;<br />
     &nbsp;&nbsp;Rs485_Str[10] =  switch_32_25_tarstate;<br />
<br />
     &nbsp;&nbsp;outdata = "1234567890123";<br />
     &nbsp;&nbsp;outdata[0] = Rs485_Str[0];<br />
     &nbsp;&nbsp;outdata[1] = Rs485_Str[1];<br />
     &nbsp;&nbsp;outdata[2] = Rs485_Str[2];<br />
     &nbsp;&nbsp;outdata[3] = Rs485_Str[3];<br />
     &nbsp;&nbsp;outdata[4] = Rs485_Str[4];<br />
     &nbsp;&nbsp;outdata[5] = Rs485_Str[5];<br />
     &nbsp;&nbsp;outdata[6] = Rs485_Str[6];<br />
     &nbsp;&nbsp;outdata[7] = Rs485_Str[7];<br />
     &nbsp;&nbsp;outdata[8] = Rs485_Str[8];<br />
     &nbsp;&nbsp;outdata[9] = Rs485_Str[9];<br />
     &nbsp;&nbsp;outdata[10] = Rs485_Str[10];<br />
   &nbsp;&nbsp;<br />
     &nbsp;&nbsp;CRC16_Data = (crc16((uint8_t *) Rs485_Str, 11, 0x8005, 0xFFFF, 0, true, true));<br />
     &nbsp;&nbsp;CRC16_Data_H = CRC16_Data/256;<br />
     &nbsp;&nbsp;CRC16_Data_L = CRC16_Data%256;<br />
     &nbsp;&nbsp;outdata[11] = CRC16_Data_L;<br />
     &nbsp;&nbsp;outdata[12] = CRC16_Data_H;<br />
     &nbsp;&nbsp;Serial2.print(outdata);<br />
     &nbsp;&nbsp;<br />
     &nbsp;&nbsp;switch_32_25_nowstate = switch_32_25_tarstate;<br />
     &nbsp;&nbsp;switch_24_17_nowstate = switch_24_17_tarstate;<br />
     &nbsp;&nbsp;switch_16_9_nowstate = switch_16_9_tarstate;<br />
     &nbsp;&nbsp;switch_8_1_nowstate = switch_8_1_tarstate;<br />
<br />
      }<br />
      else<br />
      {<br />
      pre_switch_8_1_tarstate = switch_8_1_tarstate ;<br />
      pre_switch_16_9_tarstate = switch_16_9_tarstate ;<br />
      pre_switch_24_17_tarstate = switch_24_17_tarstate ;<br />
      pre_switch_32_25_tarstate = switch_32_25_tarstate ;<br />
      } <br />
    }<br />
    }<br />
  }<br />
<br />
<br />
 &nbsp;&nbsp;<br />
  if((uart_state == RS232_state) &amp;&amp; (RS232_Check == ENABLE)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if (millis()- RS232_last_time &gt;= RS232ReadDelay) <br />
    {<br />
      RS232_last_time = millis();<br />
   &nbsp;&nbsp;if(RS232_read_com == RS232_read_relaycom)<br />
   &nbsp;&nbsp;{<br />
   &nbsp;&nbsp;outdata = "RELAY-STATE-255";<br />
   &nbsp;&nbsp;Serial2.print(outdata);<br />
   &nbsp;&nbsp;RS232_read_com = RS232_read_inputcom;<br />
   &nbsp;&nbsp;}<br />
   &nbsp;&nbsp;else if(RS232_read_com == RS232_read_inputcom)<br />
   &nbsp;&nbsp;{<br />
   &nbsp;&nbsp;outdata = "RELAY-GET_INPUT-255";<br />
   &nbsp;&nbsp;Serial2.print(outdata);<br />
   &nbsp;&nbsp;RS232_read_com = RS232_read_relaycom;<br />
   &nbsp;&nbsp;}<br />
    }<br />
  }<br />
<br />
  if((uart_state == RS485_state) &amp;&amp; (RS485_Check == ENABLE)&amp;&amp;(wifi_state == DISABLE))<br />
  {<br />
    if (millis()- RS485_last_time &gt;= RS485ReadDelay) <br />
    {<br />
      RS485_last_time = millis();<br />
   &nbsp;&nbsp;if(RS485_read_com == RS485_read_relaycom)<br />
   &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;//read all realay<br />
     &nbsp;&nbsp;Rs485_Str[0] = RS485Address;<br />
     &nbsp;&nbsp;Rs485_Str[1] = 0x01;<br />
     &nbsp;&nbsp;Rs485_Str[2] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[3] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[4] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[5] = 0x20;<br />
<br />
     &nbsp;&nbsp;outdata = "12345678";<br />
     &nbsp;&nbsp;outdata[0] = Rs485_Str[0];<br />
     &nbsp;&nbsp;outdata[1] = Rs485_Str[1];<br />
     &nbsp;&nbsp;outdata[2] = Rs485_Str[2];<br />
     &nbsp;&nbsp;outdata[3] = Rs485_Str[3];<br />
     &nbsp;&nbsp;outdata[4] = Rs485_Str[4];<br />
     &nbsp;&nbsp;outdata[5] = Rs485_Str[5];<br />
   &nbsp;&nbsp;<br />
     &nbsp;&nbsp;CRC16_Data = (crc16((uint8_t *) Rs485_Str, 6, 0x8005, 0xFFFF, 0, true, true));<br />
     &nbsp;&nbsp;CRC16_Data_H = CRC16_Data/256;<br />
     &nbsp;&nbsp;CRC16_Data_L = CRC16_Data%256;<br />
     &nbsp;&nbsp;outdata[6] = CRC16_Data_L;<br />
     &nbsp;&nbsp;outdata[7] = CRC16_Data_H;<br />
     &nbsp;&nbsp;Serial2.print(outdata);<br />
    <br />
     &nbsp;&nbsp;RS485_read_com = RS485_read_inputcom;<br />
   &nbsp;&nbsp;}<br />
   &nbsp;&nbsp;else if(RS485_read_com == RS485_read_inputcom)<br />
   &nbsp;&nbsp;{<br />
     &nbsp;&nbsp;//read all inputs<br />
     &nbsp;&nbsp;Rs485_Str[0] = RS485Address;<br />
     &nbsp;&nbsp;Rs485_Str[1] = 0x02;<br />
     &nbsp;&nbsp;Rs485_Str[2] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[3] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[4] = 0x00;<br />
     &nbsp;&nbsp;Rs485_Str[5] = 0x08;<br />
<br />
     &nbsp;&nbsp;outdata = "12345678";<br />
     &nbsp;&nbsp;outdata[0] = Rs485_Str[0];<br />
     &nbsp;&nbsp;outdata[1] = Rs485_Str[1];<br />
     &nbsp;&nbsp;outdata[2] = Rs485_Str[2];<br />
     &nbsp;&nbsp;outdata[3] = Rs485_Str[3];<br />
     &nbsp;&nbsp;outdata[4] = Rs485_Str[4];<br />
     &nbsp;&nbsp;outdata[5] = Rs485_Str[5];<br />
   &nbsp;&nbsp;<br />
     &nbsp;&nbsp;CRC16_Data = (crc16((uint8_t *) Rs485_Str, 6, 0x8005, 0xFFFF, 0, true, true));<br />
     &nbsp;&nbsp;CRC16_Data_H = CRC16_Data/256;<br />
     &nbsp;&nbsp;CRC16_Data_L = CRC16_Data%256;<br />
     &nbsp;&nbsp;outdata[6] = CRC16_Data_L;<br />
     &nbsp;&nbsp;outdata[7] = CRC16_Data_H;<br />
     &nbsp;&nbsp;Serial2.print(outdata);<br />
<br />
   &nbsp;&nbsp;RS485_read_com = RS485_read_relaycom;<br />
   &nbsp;&nbsp;}<br />
    }<br />
  }<br />
<br />
  delay(10);<br />
  <br />
 }<br />
    // return;<br />
}<br />
/**<br />
 * @description: DP download callback function.<br />
 * @param {unsigned char} dpid<br />
 * @param {const unsigned char} value<br />
 * @param {unsigned short} length<br />
 * @return {unsigned char}<br />
 */<br />
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)<br />
{<br />
  if((uart_state == RS232_state) || (uart_state == RS485_state))<br />
  {<br />
  switch(dpid) {<br />
    case DPID_SWITCH_1:<br />
      switch_1_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_1_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(1-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(1-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH_2:<br />
      switch_2_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_2_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(2-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(2-1)));<br />
      }<br />
      //Status changes should be reported.<br />
    break;<br />
<br />
    case DPID_SWITCH_3:<br />
      switch_3_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_3_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(3-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(3-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH_4:<br />
      switch_4_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_4_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(4-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(4-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH_5:<br />
      switch_5_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_5_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(5-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(5-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH_6:<br />
      switch_6_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_6_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(6-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(6-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH7:<br />
      switch_7_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_7_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(7-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(7-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH8:<br />
      switch_8_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_8_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate | (1&lt;&lt;(8-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_8_1_tarstate = switch_8_1_tarstate &amp; (~(1&lt;&lt;(8-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH9:<br />
      switch_9_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_9_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(1-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(1-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH10:<br />
      switch_10_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_10_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(2-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(2-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH11:<br />
      switch_11_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_11_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(3-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(3-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH12:<br />
      switch_12_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_12_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(4-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(4-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH13:<br />
      switch_13_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_13_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(5-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(5-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH14:<br />
      switch_14_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_14_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(6-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(6-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH15:<br />
      switch_15_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_15_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(7-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(7-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH16:<br />
      switch_16_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_16_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate | (1&lt;&lt;(8-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_16_9_tarstate = switch_16_9_tarstate &amp; (~(1&lt;&lt;(8-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH17:<br />
      switch_17_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_17_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(1-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(1-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH18:<br />
      switch_18_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_18_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(2-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(2-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH19:<br />
      switch_19_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_19_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(3-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(3-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH20:<br />
      switch_20_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_20_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(4-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(4-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH21:<br />
      switch_21_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_21_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(5-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(5-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH22:<br />
      switch_22_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_22_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(6-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(6-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH23:<br />
      switch_23_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_23_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(7-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(7-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH24:<br />
      switch_24_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_24_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate | (1&lt;&lt;(8-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_24_17_tarstate = switch_24_17_tarstate &amp; (~(1&lt;&lt;(8-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH25:<br />
      switch_25_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_25_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(1-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(1-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH26:<br />
      switch_26_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_26_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(2-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(2-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH27:<br />
      switch_27_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_27_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(3-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(3-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH28:<br />
      switch_28_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_28_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(4-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(4-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH29:<br />
      switch_29_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_29_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(5-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(5-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH30:<br />
      switch_30_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_30_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(6-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(6-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH31:<br />
      switch_31_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_31_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(7-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(7-1)));<br />
      }<br />
    break;<br />
<br />
    case DPID_SWITCH32:<br />
      switch_32_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */<br />
      if (switch_32_state) {<br />
        //Turn on<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate | (1&lt;&lt;(8-1));<br />
      } else {<br />
        //Turn off<br />
     &nbsp;&nbsp;switch_32_25_tarstate = switch_32_25_tarstate &amp; (~(1&lt;&lt;(8-1)));<br />
      }<br />
    break;<br />
    default:break;<br />
  }<br />
  }<br />
<br />
  //===============================================================================<br />
<br />
<br />
  <br />
  return TY_SUCCESS;<br />
}<br />
<br />
/**<br />
 * @description: Upload all DP status of the current device.<br />
 * @param {*}<br />
 * @return {*}<br />
 */<br />
void dp_update_all(void)<br />
{<br />
  my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);<br />
<br />
  my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);<br />
  my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);<br />
<br />
  my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);<br />
  my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);<br />
}</code></div></div>]]></content:encoded>
		</item>
	</channel>
</rss>