Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 8,720
» Latest member: sanavnish
» Forum threads: 3,823
» Forum posts: 19,572

Full Statistics

Online Users
There are currently 123 online users.
» 0 Member(s) | 104 Guest(s)
AhrefsBot, Amazonbot, Applebot, Bing, Bytespider, Semrush, bot

Latest Threads
"KCS" v2.2.19 firmware BI...
Forum: "KCS" v2 firmware system
Last Post: admin
7 hours ago
» Replies: 0
» Views: 9
kWh resolution
Forum: N30
Last Post: admin
7 hours ago
» Replies: 22
» Views: 451
Custom ESPHome firmware a...
Forum: KC868-A6
Last Post: admin
Yesterday, 01:33 PM
» Replies: 37
» Views: 8,448
sample code to receive ht...
Forum: F16
Last Post: admin
Yesterday, 06:33 AM
» Replies: 19
» Views: 697
A32pro issue
Forum: News
Last Post: admin
Yesterday, 05:53 AM
» Replies: 3
» Views: 37
N10 port modbus
Forum: N10
Last Post: admin
03-07-2026, 11:52 PM
» Replies: 13
» Views: 153
KC868-HAv2 work with F24 ...
Forum: KC868-HA /HA v2
Last Post: admin
03-06-2026, 11:29 PM
» Replies: 13
» Views: 1,262
N60/N30/N20/N10 PC softwa...
Forum: N60
Last Post: admin
03-06-2026, 08:22 AM
» Replies: 3
» Views: 640
N60 N30 N20 N10 ARM CPU f...
Forum: N60
Last Post: admin
03-06-2026, 08:17 AM
» Replies: 0
» Views: 32
KinCony ALR ESP32 I/O pin...
Forum: KinCony ALR
Last Post: admin
03-06-2026, 01:29 AM
» Replies: 6
» Views: 1,575

  KC868-E16T ESP32 and Tuya I/O pin define
Posted by: admin - 09-26-2022, 02:00 PM - Forum: KC868-E16T - No Replies

ANALOG_A1  GPIO36
ANALOG_A2  GPIO35
ANALOG_A3  GPIO34
ANALOG_A4  GPIO39

IIC SDA:GPIO16
IIC SCL:GPIO15

PCF8574:U27 (relay1-8): 100 001 = 0x21
PCF8574:U28 (relay9-16): 100 101 = 0x25

PCF8574:U23 (DI1-8): 100 010 = 0x22
PCF8574:U24 (DI9-16): 100 100 = 0x24

RF433MHz wireless receiver: GPIO4

BEEP:GPIO2

Ethernet (LAN8720) I/O define:

#define ETH_ADDR        0
#define ETH_POWER_PIN  -1
#define ETH_MDC_PIN    23
#define ETH_MDIO_PIN  18
#define ETH_TYPE      ETH_PHY_LAN8720
#define ETH_CLK_MODE  ETH_CLOCK_GPIO17_OUT

--------------------
RS485:
RXD:GPIO13
TXD:GPIO32

TUYA wifi module (3.3v TTL level):
RXD:GPIO14
TXD:GPIO33
LED: GPIO12

Print this item

  KinCony-TUYA_ESP32_Adapter ESP32 arduino source code
Posted by: admin - 09-26-2022, 01:55 PM - Forum: KC868-ATC / Tuya adapter V2 - Replies (28)

This demo is based on the Arduino Esp32 , and the board are controlled by the Tuya Smart App. So you can also use Alexa and Google home speaker directly.

* use RS232 or RS485 control KC868-H32BS or KC868-H32B or KC868-H32L relay controller. You can change output command for any third party device. When Tuya app press ON/OFF button will send command by RS485 or RS232.

here is arduino source code download:

.zip   KinCony-ESP32-Tuya.zip (Size: 5.81 KB / Downloads: 827)

Code:
/*
* @FileName: KinCony-ESP32-Tuya.ino
* @Copyright: KinCony IoT
* @Company: KinCony IoT
* @Description: This demo is based on the Arduino Esp32 , and the board are controlled by the Tuya Smart App.
* use RS232 or RS485 control KC868-H32BS or KC868-H32B or KC868-H32L relay controller. You can change output command for any third party device
*/

#include <TuyaWifi.h>
#include <SoftwareSerial.h>
#include "CRC16.h"
#include "CRC.h"

TuyaWifi my_device;

#define  RS485Address   0x01
#define  RS485Baud     9600
#define  RS232Baud     115200
#define  RS232ReadDelay     3000
#define  RS485ReadDelay     3000
#define  RS232RelayDelay    500
#define  RS485RelayDelay    500
unsigned char pid[] = {"tsfqvoyi6pxqxq6o"};
unsigned char mcu_ver[] = {"1.0.0"};


//============================================



#define RS232_state   1
#define RS485_state   0
#define RS232_read_inputcom   0
#define RS232_read_relaycom   1
#define RS485_read_inputcom   0
#define RS485_read_relaycom   1
#define DISABLE   0
#define ENABLE   1
uint16_t  CRC16_Data = 0;
uint8_t  CRC16_Data_H = 0;
uint8_t  CRC16_Data_L = 0;

/* Current  status */
unsigned char led_state = 0;
//1=232 0=485
unsigned char uart_state = 1;

unsigned char RS232_Check = ENABLE;
unsigned char RS485_Check = ENABLE;

unsigned char RS232_read_com = 1;
//1= read RELAY  0 = read input
unsigned char RS485_read_com = 1;
//1= read RELAY  0 = read input

unsigned char switch_8_1_nowstate = 0;
unsigned char switch_16_9_nowstate = 0;
unsigned char switch_24_17_nowstate = 0;
unsigned char switch_32_25_nowstate = 0;
unsigned char switch_8_1_tarstate = 0;
unsigned char switch_16_9_tarstate = 0;
unsigned char switch_24_17_tarstate = 0;
unsigned char switch_32_25_tarstate = 0;

unsigned char pre_switch_8_1_tarstate = 0;
unsigned char pre_switch_16_9_tarstate = 0;
unsigned char pre_switch_24_17_tarstate = 0;
unsigned char pre_switch_32_25_tarstate = 0;
unsigned char pre_switch_8_1_nowstate = 0;
unsigned char pre_switch_16_9_nowstate = 0;
unsigned char pre_switch_24_17_nowstate = 0;
unsigned char pre_switch_32_25_nowstate = 0;

unsigned char switch_1_8_tarstate = 0;
unsigned char switch_9_16_tarstate = 0;
unsigned char switch_17_24_tarstate = 0;
unsigned char switch_25_32_tarstate = 0;

unsigned char switch_1_state = 0;
unsigned char switch_2_state = 0;
unsigned char switch_3_state = 0;
unsigned char switch_4_state = 0;
unsigned char switch_5_state = 0;
unsigned char switch_6_state = 0;

unsigned char input_1_state = 0;
unsigned char input_2_state = 0;
unsigned char input_3_state = 0;
unsigned char input_4_state = 0;
unsigned char input_5_state = 0;
unsigned char input_6_state = 0;

unsigned char switch_7_state = 0;
unsigned char switch_8_state = 0;
unsigned char switch_9_state = 0;
unsigned char switch_10_state = 0;
unsigned char switch_11_state = 0;
unsigned char switch_12_state = 0;
unsigned char switch_13_state = 0;
unsigned char switch_14_state = 0;
unsigned char switch_15_state = 0;
unsigned char switch_16_state = 0;
unsigned char switch_17_state = 0;
unsigned char switch_18_state = 0;
unsigned char switch_19_state = 0;
unsigned char switch_20_state = 0;
unsigned char switch_21_state = 0;
unsigned char switch_22_state = 0;
unsigned char switch_23_state = 0;
unsigned char switch_24_state = 0;
unsigned char switch_25_state = 0;
unsigned char switch_26_state = 0;
unsigned char switch_27_state = 0;
unsigned char switch_28_state = 0;
unsigned char switch_29_state = 0;
unsigned char switch_30_state = 0;
unsigned char switch_31_state = 0;
unsigned char switch_32_state = 0;

unsigned char switch_232_delay_state = 0;
unsigned char dpid_232_delay_switch = 0;
unsigned char en_232_delay_switch = 0;
//1= en delay   0= dis delay
unsigned char switch_485_delay_state = 0;
unsigned char dpid_485_delay_switch = 0;
unsigned char en_485_delay_switch = 0;
//1= en delay   0= dis delay

unsigned char ini_state = 1;

unsigned char Rs485_Str[20] = {0};
/* Connect network button pin */
int key_pin = 27;
/*  232(1) 485(0)  */
int sel_uart_pin = 23;



#define BEGIN_DELAY   1
#define END_DELAY   0

/* Data point define */

#define DPID_SWITCH_1   1
#define DPID_SWITCH_2   2
#define DPID_SWITCH_3   3
#define DPID_SWITCH_4   4
#define DPID_SWITCH_5   5
#define DPID_SWITCH_6   6

#define DPID_INPUT_1   101
#define DPID_INPUT_2   102
#define DPID_INPUT_3   103
#define DPID_INPUT_4   104
#define DPID_INPUT_5   105
#define DPID_INPUT_6   106

#define DPID_SWITCH7   113
#define DPID_SWITCH8   114
#define DPID_SWITCH9   115
#define DPID_SWITCH10  116
#define DPID_SWITCH11  117
#define DPID_SWITCH12  118
#define DPID_SWITCH13  119
#define DPID_SWITCH14  120
#define DPID_SWITCH15  121
#define DPID_SWITCH16  122
#define DPID_SWITCH17  123
#define DPID_SWITCH18  124
#define DPID_SWITCH19  125
#define DPID_SWITCH20  126
#define DPID_SWITCH21  127
#define DPID_SWITCH22  128
#define DPID_SWITCH23  129
#define DPID_SWITCH24  130
#define DPID_SWITCH25  131
#define DPID_SWITCH26  132
#define DPID_SWITCH27  133
#define DPID_SWITCH28  134
#define DPID_SWITCH29  135
#define DPID_SWITCH30  136
#define DPID_SWITCH31  137
#define DPID_SWITCH32  138


#define LED_WiFi  26
#define LED_User  33


/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type.
*                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP
*/
unsigned char dp_array[][2] =
{
  {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},
  {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},
  {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},
  {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},
  {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},
  {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},
{DPID_SWITCH31, DP_TYPE_BOOL},{DPID_SWITCH32, DP_TYPE_BOOL},
};


/* last time */
unsigned long last_time = 0;
unsigned long RS485_last_time = 0;
unsigned long RS232_last_time = 0;
unsigned long RS232_delay_last_time = 0;
unsigned long RS232_relay_last_time = 0;
unsigned long RS485_delay_last_time = 0;
unsigned long RS485_relay_last_time = 0;

String indata="";
String val="";
String outdata="";
String relay_outdata="";
String subval="";
int numsubval[6]={0};

void setup()
{
  ini_state = 1;
  // Serial with tuyawifi
  Serial.begin(9600,SERIAL_8N1,14,13);

  //Initialize led port, turn off led.
  pinMode(LED_WiFi, OUTPUT);
  digitalWrite(LED_WiFi, LOW);

  pinMode(LED_User, OUTPUT);
  digitalWrite(LED_User, LOW);

  //Initialize networking key
  pinMode(key_pin, INPUT_PULLUP);
  //Initialize sel key
  pinMode(sel_uart_pin, INPUT_PULLUP);

 
  //  0 SEL 485    1 SEL 232
  uart_state = RS485_state;
  if (digitalRead(sel_uart_pin) == LOW) {
    delay(80);
    if (digitalRead(sel_uart_pin) == LOW) {
  uart_state = RS232_state;
     }
  }

  // uart_state = RS485_state;

  if(uart_state == RS232_state)
  {
    //Serial2.begin(RS232Baud,SERIAL_8N1,3,1);
    Serial2.begin(RS232Baud,SERIAL_8N1,16,17);
  }
  else
  {
    //Serial2.begin(RS485Baud,SERIAL_8N1,3,1);
    Serial2.begin(RS485Baud,SERIAL_8N1,16,17);
  }
   
  //Enter the PID and MCU software version
  my_device.init(pid, mcu_ver);
  //incoming all DPs and their types array, DP numbers
  my_device.set_dp_cmd_total(dp_array, 1);
  //register DP download processing callback function
  my_device.dp_process_func_register(dp_process);
  //register upload all DP callback function
  my_device.dp_update_all_func_register(dp_update_all);

  last_time = millis();
}

void loop()
{

     
  //uart_state = RS485_state;
 
  if(uart_state == RS232_state)
  {
  while(Serial2.available()>0)
   {
    indata+=char(Serial2.read());  //read KBOX via 232
    delay(2);
    if(Serial2.available()<=0)
     {
        //Serial2.print(indata);
     }
   }   
  }
  else if(uart_state == RS485_state)
  {
  while(Serial2.available()>0)
   {
    indata+=char(Serial2.read());   //read KBOX via 485
    delay(2);
    if(Serial2.available()<=0)
     {
        //Serial2.print(indata);
     }
   }   
  }

    if((en_232_delay_switch == BEGIN_DELAY)&&(uart_state == RS232_state))
  {
    if((millis()-RS232_delay_last_time)>=20)
    {
      my_device.mcu_dp_update(dpid_232_delay_switch, switch_232_delay_state, 1);
      en_232_delay_switch = END_DELAY;
    }
  }

    if((en_485_delay_switch == BEGIN_DELAY)&&(uart_state == RS485_state))
  {
    if((millis()-RS485_delay_last_time)>=20)
    {
      my_device.mcu_dp_update(dpid_485_delay_switch, switch_485_delay_state, 1);
      en_485_delay_switch = END_DELAY;
    }
  }

    if((indata.length()>0)&&(uart_state == RS232_state))
  {
     val=indata;   //

  //----------INPUT ALARM--------
      if(val=="RELAY-ALARM-1,OK")   
      {
        input_1_state = 1;
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);
      }     
     else if(val=="RELAY-DIARM-1,OK")   
      {
        input_1_state = 0;
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);
      }     
     else if(val=="RELAY-ALARM-2,OK")   
      {
        input_2_state = 1;
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);
      }     
     else if(val=="RELAY-DIARM-2,OK")   
      {
        input_2_state = 0;
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);
      }     
     else if(val=="RELAY-ALARM-3,OK")   
      {
        input_3_state = 1;
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);
      }     
     else if(val=="RELAY-DIARM-3,OK")   
      {
        input_3_state = 0;
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);
      }     
     else if(val=="RELAY-ALARM-4,OK")   
      {
        input_4_state = 1;
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);
      }     
     else if(val=="RELAY-DIARM-4,OK")   
      {
        input_4_state = 0;
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);
      }     
     else if(val=="RELAY-ALARM-5,OK")   
      {
        input_5_state = 1;
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);
      }     
     else if(val=="RELAY-DIARM-5,OK")   
      {
        input_5_state = 0;
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);
      }     
     else if(val=="RELAY-ALARM-6,OK")   
      {
        input_6_state = 1;
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);
      }     
     else if(val=="RELAY-DIARM-6,OK")   
      {
        input_6_state = 0;
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);
      }     
     //----INPUT READ BACK
     else if((val.substring(0,19)=="RELAY-GET_INPUT-255"))   
      {
       subval =  val.substring(20);
       int j=0;
       int m=subval.length();
       for(int i=0; i<m; i++)
       {
       if(subval[i]==',')
        {
         j++;
         }
        else
        {
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');
        }
       }
       
        input_6_state = (numsubval[0]&0x20)?0:1;
        input_5_state = (numsubval[0]&0x10)?0:1;
        input_4_state = (numsubval[0]&0x08)?0:1;
        input_3_state = (numsubval[0]&0x04)?0:1;
        input_2_state = (numsubval[0]&0x02)?0:1;
        input_1_state = (numsubval[0]&0x01)?0:1;

        numsubval[0]=0;
        numsubval[1]=0;
        numsubval[2]=0;
        numsubval[3]=0;
        numsubval[4]=0;
        numsubval[5]=0;

        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);
      }     
     //----ALL-RELAY-STATE  READ BACK
     else if((val.substring(0,15)=="RELAY-STATE-255"))   
      {
       subval =  val.substring(16);
       int j=0;
       int m=subval.length();
       for(int i=0; i<m; i++)
       {
       if(subval[i]==',')
        {
         j++;
         }
        else
        {
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');
        }
       }
       
        switch_32_25_nowstate = numsubval[0];
        switch_32_state = (switch_32_25_nowstate&0x80)?1:0;
        switch_31_state = (switch_32_25_nowstate&0x40)?1:0;
        switch_30_state = (switch_32_25_nowstate&0x20)?1:0;
        switch_29_state = (switch_32_25_nowstate&0x10)?1:0;
        switch_28_state = (switch_32_25_nowstate&0x08)?1:0;
        switch_27_state = (switch_32_25_nowstate&0x04)?1:0;
        switch_26_state = (switch_32_25_nowstate&0x02)?1:0;
        switch_25_state = (switch_32_25_nowstate&0x01)?1:0;

        switch_24_17_nowstate = numsubval[1];
        switch_24_state = (switch_24_17_nowstate&0x80)?1:0;
        switch_23_state = (switch_24_17_nowstate&0x40)?1:0;
        switch_22_state = (switch_24_17_nowstate&0x20)?1:0;
        switch_21_state = (switch_24_17_nowstate&0x10)?1:0;
        switch_20_state = (switch_24_17_nowstate&0x08)?1:0;
        switch_19_state = (switch_24_17_nowstate&0x04)?1:0;
        switch_18_state = (switch_24_17_nowstate&0x02)?1:0;
        switch_17_state = (switch_24_17_nowstate&0x01)?1:0;

        switch_16_9_nowstate = numsubval[2];
        switch_16_state = (switch_16_9_nowstate&0x80)?1:0;
        switch_15_state = (switch_16_9_nowstate&0x40)?1:0;
        switch_14_state = (switch_16_9_nowstate&0x20)?1:0;
        switch_13_state = (switch_16_9_nowstate&0x10)?1:0;
        switch_12_state = (switch_16_9_nowstate&0x08)?1:0;
        switch_11_state = (switch_16_9_nowstate&0x04)?1:0;
        switch_10_state = (switch_16_9_nowstate&0x02)?1:0;
        switch_9_state =  (switch_16_9_nowstate&0x01)?1:0;

        switch_8_1_nowstate = numsubval[3];
        switch_8_state = (switch_8_1_nowstate&0x80)?1:0;
        switch_7_state = (switch_8_1_nowstate&0x40)?1:0;
        switch_6_state = (switch_8_1_nowstate&0x20)?1:0;
        switch_5_state = (switch_8_1_nowstate&0x10)?1:0;
        switch_4_state = (switch_8_1_nowstate&0x08)?1:0;
        switch_3_state = (switch_8_1_nowstate&0x04)?1:0;
        switch_2_state = (switch_8_1_nowstate&0x02)?1:0;
        switch_1_state = (switch_8_1_nowstate&0x01)?1:0;

        if(ini_state==1)
        {
         ini_state=0 ;
         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;
        }

        numsubval[0]=0;
        numsubval[1]=0;
        numsubval[2]=0;
        numsubval[3]=0;
        numsubval[4]=0;
        numsubval[5]=0;

        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);
      }     
     //----RELAY SET READ BACK
     else if((val.substring(0,17)=="RELAY-SET_ALL-255"))   
      {
       subval =  val.substring(18);
       int j=0;
       int m=subval.length();
       for(int i=0; i<m; i++)
       {
       if(subval[i]==',')
        {
         j++;
         }
        else
        {
          numsubval[j]=numsubval[j]*10+(subval[i]-'0');
        }
       }
       
        switch_32_25_nowstate = numsubval[0];
        switch_32_state = (switch_32_25_nowstate&0x80)?1:0;
        switch_31_state = (switch_32_25_nowstate&0x40)?1:0;
        switch_30_state = (switch_32_25_nowstate&0x20)?1:0;
        switch_29_state = (switch_32_25_nowstate&0x10)?1:0;
        switch_28_state = (switch_32_25_nowstate&0x08)?1:0;
        switch_27_state = (switch_32_25_nowstate&0x04)?1:0;
        switch_26_state = (switch_32_25_nowstate&0x02)?1:0;
        switch_25_state = (switch_32_25_nowstate&0x01)?1:0;

        switch_24_17_nowstate = numsubval[1];
        switch_24_state = (switch_24_17_nowstate&0x80)?1:0;
        switch_23_state = (switch_24_17_nowstate&0x40)?1:0;
        switch_22_state = (switch_24_17_nowstate&0x20)?1:0;
        switch_21_state = (switch_24_17_nowstate&0x10)?1:0;
        switch_20_state = (switch_24_17_nowstate&0x08)?1:0;
        switch_19_state = (switch_24_17_nowstate&0x04)?1:0;
        switch_18_state = (switch_24_17_nowstate&0x02)?1:0;
        switch_17_state = (switch_24_17_nowstate&0x01)?1:0;

        switch_16_9_nowstate = numsubval[2];
        switch_16_state = (switch_16_9_nowstate&0x80)?1:0;
        switch_15_state = (switch_16_9_nowstate&0x40)?1:0;
        switch_14_state = (switch_16_9_nowstate&0x20)?1:0;
        switch_13_state = (switch_16_9_nowstate&0x10)?1:0;
        switch_12_state = (switch_16_9_nowstate&0x08)?1:0;
        switch_11_state = (switch_16_9_nowstate&0x04)?1:0;
        switch_10_state = (switch_16_9_nowstate&0x02)?1:0;
        switch_9_state =  (switch_16_9_nowstate&0x01)?1:0;

        switch_8_1_nowstate = numsubval[3];
        switch_8_state = (switch_8_1_nowstate&0x80)?1:0;
        switch_7_state = (switch_8_1_nowstate&0x40)?1:0;
        switch_6_state = (switch_8_1_nowstate&0x20)?1:0;
        switch_5_state = (switch_8_1_nowstate&0x10)?1:0;
        switch_4_state = (switch_8_1_nowstate&0x08)?1:0;
        switch_3_state = (switch_8_1_nowstate&0x04)?1:0;
        switch_2_state = (switch_8_1_nowstate&0x02)?1:0;
        switch_1_state = (switch_8_1_nowstate&0x01)?1:0;

        numsubval[0]=0;
        numsubval[1]=0;
        numsubval[2]=0;
        numsubval[3]=0;
        numsubval[4]=0;
        numsubval[5]=0;

        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);

        RS232_Check = ENABLE ;
      }     

  }   

  //====================================================================

    if((indata.length()>0)&&(uart_state == RS485_state))
  {
     val=indata;   //

  //----------INPUT READ BACK----------

     if((val[0]==RS485Address)&&(val[1]==0x02)&&(val[2]==0x01))
      {
        input_1_state=(val[3]&0x01)?1:0 ;
        input_2_state=(val[3]&0x02)?1:0 ;
        input_3_state=(val[3]&0x04)?1:0 ;
        input_4_state=(val[3]&0x08)?1:0 ;
        input_5_state=(val[3]&0x10)?1:0 ;
        input_6_state=(val[3]&0x20)?1:0 ;
       
        my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);
        my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);
      } 

     //----ALL-RELAY-STATE  READ BACK
    else if((val[0]==RS485Address)&&(val[1]==0x01)&&(val[2]==0x04))
    {

        switch_32_25_nowstate = val[3];
        switch_32_state = (switch_32_25_nowstate&0x80)?1:0;
        switch_31_state = (switch_32_25_nowstate&0x40)?1:0;
        switch_30_state = (switch_32_25_nowstate&0x20)?1:0;
        switch_29_state = (switch_32_25_nowstate&0x10)?1:0;
        switch_28_state = (switch_32_25_nowstate&0x08)?1:0;
        switch_27_state = (switch_32_25_nowstate&0x04)?1:0;
        switch_26_state = (switch_32_25_nowstate&0x02)?1:0;
        switch_25_state = (switch_32_25_nowstate&0x01)?1:0;

        switch_24_17_nowstate = val[4];
        switch_24_state = (switch_24_17_nowstate&0x80)?1:0;
        switch_23_state = (switch_24_17_nowstate&0x40)?1:0;
        switch_22_state = (switch_24_17_nowstate&0x20)?1:0;
        switch_21_state = (switch_24_17_nowstate&0x10)?1:0;
        switch_20_state = (switch_24_17_nowstate&0x08)?1:0;
        switch_19_state = (switch_24_17_nowstate&0x04)?1:0;
        switch_18_state = (switch_24_17_nowstate&0x02)?1:0;
        switch_17_state = (switch_24_17_nowstate&0x01)?1:0;

        switch_16_9_nowstate = val[5];
        switch_16_state = (switch_16_9_nowstate&0x80)?1:0;
        switch_15_state = (switch_16_9_nowstate&0x40)?1:0;
        switch_14_state = (switch_16_9_nowstate&0x20)?1:0;
        switch_13_state = (switch_16_9_nowstate&0x10)?1:0;
        switch_12_state = (switch_16_9_nowstate&0x08)?1:0;
        switch_11_state = (switch_16_9_nowstate&0x04)?1:0;
        switch_10_state = (switch_16_9_nowstate&0x02)?1:0;
        switch_9_state =  (switch_16_9_nowstate&0x01)?1:0;

        switch_8_1_nowstate = val[6];
        switch_8_state = (switch_8_1_nowstate&0x80)?1:0;
        switch_7_state = (switch_8_1_nowstate&0x40)?1:0;
        switch_6_state = (switch_8_1_nowstate&0x20)?1:0;
        switch_5_state = (switch_8_1_nowstate&0x10)?1:0;
        switch_4_state = (switch_8_1_nowstate&0x08)?1:0;
        switch_3_state = (switch_8_1_nowstate&0x04)?1:0;
        switch_2_state = (switch_8_1_nowstate&0x02)?1:0;
        switch_1_state = (switch_8_1_nowstate&0x01)?1:0;

        if(ini_state==1)
        {
         ini_state=0 ;
         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;
        }

        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);
    }

     //----RELAY SET READ BACK
     else if((val[0]==RS485Address)&&(val[1]==0x0F)&&(val[2]==0x00)&&(val[3]==0x00)&&(val[4]==0x00)&&(val[5]==0x20))   
      {
       
        //switch_32_25_nowstate = ;
        switch_32_state = (switch_32_25_nowstate&0x80)?1:0;
        switch_31_state = (switch_32_25_nowstate&0x40)?1:0;
        switch_30_state = (switch_32_25_nowstate&0x20)?1:0;
        switch_29_state = (switch_32_25_nowstate&0x10)?1:0;
        switch_28_state = (switch_32_25_nowstate&0x08)?1:0;
        switch_27_state = (switch_32_25_nowstate&0x04)?1:0;
        switch_26_state = (switch_32_25_nowstate&0x02)?1:0;
        switch_25_state = (switch_32_25_nowstate&0x01)?1:0;

        //switch_24_17_nowstate = ;
        switch_24_state = (switch_24_17_nowstate&0x80)?1:0;
        switch_23_state = (switch_24_17_nowstate&0x40)?1:0;
        switch_22_state = (switch_24_17_nowstate&0x20)?1:0;
        switch_21_state = (switch_24_17_nowstate&0x10)?1:0;
        switch_20_state = (switch_24_17_nowstate&0x08)?1:0;
        switch_19_state = (switch_24_17_nowstate&0x04)?1:0;
        switch_18_state = (switch_24_17_nowstate&0x02)?1:0;
        switch_17_state = (switch_24_17_nowstate&0x01)?1:0;

        //switch_16_9_nowstate = ;
        switch_16_state = (switch_16_9_nowstate&0x80)?1:0;
        switch_15_state = (switch_16_9_nowstate&0x40)?1:0;
        switch_14_state = (switch_16_9_nowstate&0x20)?1:0;
        switch_13_state = (switch_16_9_nowstate&0x10)?1:0;
        switch_12_state = (switch_16_9_nowstate&0x08)?1:0;
        switch_11_state = (switch_16_9_nowstate&0x04)?1:0;
        switch_10_state = (switch_16_9_nowstate&0x02)?1:0;
        switch_9_state =  (switch_16_9_nowstate&0x01)?1:0;

        //switch_8_1_nowstate = ;
        switch_8_state = (switch_8_1_nowstate&0x80)?1:0;
        switch_7_state = (switch_8_1_nowstate&0x40)?1:0;
        switch_6_state = (switch_8_1_nowstate&0x20)?1:0;
        switch_5_state = (switch_8_1_nowstate&0x10)?1:0;
        switch_4_state = (switch_8_1_nowstate&0x08)?1:0;
        switch_3_state = (switch_8_1_nowstate&0x04)?1:0;
        switch_2_state = (switch_8_1_nowstate&0x02)?1:0;
        switch_1_state = (switch_8_1_nowstate&0x01)?1:0;

        my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);
        my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);

        RS485_Check = ENABLE ;
   }
   }
   
    indata="";   //clear indata
//--------------------------------------------------

   my_device.uart_service();

  //Enter the connection network mode when Pin27 is pressed.
  if (digitalRead(key_pin) == LOW) {
    delay(80);
    if (digitalRead(key_pin) == LOW) {
      my_device.mcu_set_wifi_mode(SMART_CONFIG);
    }
  }
  /* LED blinks when network is being connected */
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) && (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) && (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {
    if (millis()- last_time >= 500) {
      last_time = millis();

      if (led_state == LOW) {
        led_state = HIGH;
      } else {
        led_state = LOW;
      }
      digitalWrite(LED_WiFi, led_state);
    }
  }

  if(uart_state == RS232_state)
  {
    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))
    {
      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;

      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 ;
     
    }
    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))
    {
    if (millis()- RS232_relay_last_time >= RS232RelayDelay)
    {
      RS232_Check = DISABLE ;

      RS232_relay_last_time = millis();
      if((switch_8_1_tarstate == pre_switch_8_1_tarstate) && (switch_16_9_tarstate == pre_switch_16_9_tarstate) && (switch_24_17_tarstate == pre_switch_24_17_tarstate) && (switch_32_25_tarstate == pre_switch_32_25_tarstate))
      {
      relay_outdata = "";
      relay_outdata = relay_outdata + "RELAY-SET_ALL-255," + switch_32_25_tarstate + "," + switch_24_17_tarstate + "," + switch_16_9_tarstate + "," + switch_8_1_tarstate ;
      Serial2.print(relay_outdata);
      }
      else
      {
      pre_switch_8_1_tarstate = switch_8_1_tarstate ;
      pre_switch_16_9_tarstate = switch_16_9_tarstate ;
      pre_switch_24_17_tarstate = switch_24_17_tarstate ;
      pre_switch_32_25_tarstate = switch_32_25_tarstate ;
      }
    }
    }
  }

  if(uart_state == RS485_state)
  {
    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))
    {
      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;

      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 ;
     
    }
    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))
    {
    if (millis()- RS485_relay_last_time >= RS485RelayDelay)
    {
      RS485_Check = DISABLE ;

      RS485_relay_last_time = millis();
      if((switch_8_1_tarstate == pre_switch_8_1_tarstate) && (switch_16_9_tarstate == pre_switch_16_9_tarstate) && (switch_24_17_tarstate == pre_switch_24_17_tarstate) && (switch_32_25_tarstate == pre_switch_32_25_tarstate))
      {
       Rs485_Str[0] = RS485Address;
       Rs485_Str[1] = 0x0F;
       Rs485_Str[2] = 0x00;
       Rs485_Str[3] = 0x00;
       Rs485_Str[4] = 0x00;
       Rs485_Str[5] = 0x20;
       Rs485_Str[6] = 0x04;

       //switch_1_8_tarstate = 0;
       //switch_9_16_tarstate = 0;
       //switch_17_24_tarstate = 0;
       //switch_25_32_tarstate = 0;
       
       //switch_1_8_tarstate = (switch_8_1_tarstate & 0x01)?(switch_1_8_tarstate | 0x80) : switch_1_8_tarstate ;
       //switch_1_8_tarstate = (switch_8_1_tarstate & 0x02)?(switch_1_8_tarstate | 0x40) : switch_1_8_tarstate ;
       //switch_1_8_tarstate = (switch_8_1_tarstate & 0x04)?(switch_1_8_tarstate | 0x20) : switch_1_8_tarstate ;
       //switch_1_8_tarstate = (switch_8_1_tarstate & 0x08)?(switch_1_8_tarstate | 0x10) : switch_1_8_tarstate ;
       //switch_1_8_tarstate = (switch_8_1_tarstate & 0x10)?(switch_1_8_tarstate | 0x08) : switch_1_8_tarstate ;
       //switch_1_8_tarstate = (switch_8_1_tarstate & 0x20)?(switch_1_8_tarstate | 0x04) : switch_1_8_tarstate ;
       //switch_1_8_tarstate = (switch_8_1_tarstate & 0x40)?(switch_1_8_tarstate | 0x02) : switch_1_8_tarstate ;
       //switch_1_8_tarstate = (switch_8_1_tarstate & 0x80)?(switch_1_8_tarstate | 0x01) : switch_1_8_tarstate ;

       //switch_9_16_tarstate = (switch_16_9_tarstate & 0x01)?(switch_9_16_tarstate | 0x80) : switch_9_16_tarstate ;
       //switch_9_16_tarstate = (switch_16_9_tarstate & 0x02)?(switch_9_16_tarstate | 0x40) : switch_9_16_tarstate ;
       //switch_9_16_tarstate = (switch_16_9_tarstate & 0x04)?(switch_9_16_tarstate | 0x20) : switch_9_16_tarstate ;
       //switch_9_16_tarstate = (switch_16_9_tarstate & 0x08)?(switch_9_16_tarstate | 0x10) : switch_9_16_tarstate ;
       //switch_9_16_tarstate = (switch_16_9_tarstate & 0x10)?(switch_9_16_tarstate | 0x08) : switch_9_16_tarstate ;
       //switch_9_16_tarstate = (switch_16_9_tarstate & 0x20)?(switch_9_16_tarstate | 0x04) : switch_9_16_tarstate ;
       //switch_9_16_tarstate = (switch_16_9_tarstate & 0x40)?(switch_9_16_tarstate | 0x02) : switch_9_16_tarstate ;
       //switch_9_16_tarstate = (switch_16_9_tarstate & 0x80)?(switch_9_16_tarstate | 0x01) : switch_9_16_tarstate ;

       //switch_17_24_tarstate = (switch_24_17_tarstate & 0x01)?(switch_17_24_tarstate | 0x80) : switch_17_24_tarstate ;
       //switch_17_24_tarstate = (switch_24_17_tarstate & 0x02)?(switch_17_24_tarstate | 0x40) : switch_17_24_tarstate ;
       //switch_17_24_tarstate = (switch_24_17_tarstate & 0x04)?(switch_17_24_tarstate | 0x20) : switch_17_24_tarstate ;
       //switch_17_24_tarstate = (switch_24_17_tarstate & 0x08)?(switch_17_24_tarstate | 0x10) : switch_17_24_tarstate ;
       //switch_17_24_tarstate = (switch_24_17_tarstate & 0x10)?(switch_17_24_tarstate | 0x08) : switch_17_24_tarstate ;
       //switch_17_24_tarstate = (switch_24_17_tarstate & 0x20)?(switch_17_24_tarstate | 0x04) : switch_17_24_tarstate ;
       //switch_17_24_tarstate = (switch_24_17_tarstate & 0x40)?(switch_17_24_tarstate | 0x02) : switch_17_24_tarstate ;
       //switch_17_24_tarstate = (switch_24_17_tarstate & 0x80)?(switch_17_24_tarstate | 0x01) : switch_17_24_tarstate ;

       //switch_25_32_tarstate = (switch_32_25_tarstate & 0x01)?(switch_25_32_tarstate | 0x80) : switch_25_32_tarstate ;
       //switch_25_32_tarstate = (switch_32_25_tarstate & 0x02)?(switch_25_32_tarstate | 0x40) : switch_25_32_tarstate ;
       //switch_25_32_tarstate = (switch_32_25_tarstate & 0x04)?(switch_25_32_tarstate | 0x20) : switch_25_32_tarstate ;
       //switch_25_32_tarstate = (switch_32_25_tarstate & 0x08)?(switch_25_32_tarstate | 0x10) : switch_25_32_tarstate ;
       //switch_25_32_tarstate = (switch_32_25_tarstate & 0x10)?(switch_25_32_tarstate | 0x08) : switch_25_32_tarstate ;
       //switch_25_32_tarstate = (switch_32_25_tarstate & 0x20)?(switch_25_32_tarstate | 0x04) : switch_25_32_tarstate ;
       //switch_25_32_tarstate = (switch_32_25_tarstate & 0x40)?(switch_25_32_tarstate | 0x02) : switch_25_32_tarstate ;
       //switch_25_32_tarstate = (switch_32_25_tarstate & 0x80)?(switch_25_32_tarstate | 0x01) : switch_25_32_tarstate ;

       //Rs485_Str[7] = switch_1_8_tarstate;
       //Rs485_Str[8] = switch_9_16_tarstate;
       //Rs485_Str[9] = switch_17_24_tarstate;
       //Rs485_Str[10] = switch_25_32_tarstate;

       Rs485_Str[7] = switch_8_1_tarstate;
       Rs485_Str[8] = switch_16_9_tarstate;
       Rs485_Str[9] = switch_24_17_tarstate;
       Rs485_Str[10] =  switch_32_25_tarstate;

       outdata = "1234567890123";
       outdata[0] = Rs485_Str[0];
       outdata[1] = Rs485_Str[1];
       outdata[2] = Rs485_Str[2];
       outdata[3] = Rs485_Str[3];
       outdata[4] = Rs485_Str[4];
       outdata[5] = Rs485_Str[5];
       outdata[6] = Rs485_Str[6];
       outdata[7] = Rs485_Str[7];
       outdata[8] = Rs485_Str[8];
       outdata[9] = Rs485_Str[9];
       outdata[10] = Rs485_Str[10];
     
       CRC16_Data = (crc16((uint8_t *) Rs485_Str, 11, 0x8005, 0xFFFF, 0, true, true));
       CRC16_Data_H = CRC16_Data/256;
       CRC16_Data_L = CRC16_Data%256;
       outdata[11] = CRC16_Data_L;
       outdata[12] = CRC16_Data_H;
       Serial2.print(outdata);

       switch_32_25_nowstate = switch_32_25_tarstate;
       switch_24_17_nowstate = switch_24_17_tarstate;
       switch_16_9_nowstate = switch_16_9_tarstate;
       switch_8_1_nowstate = switch_8_1_tarstate;

      }
      else
      {
      pre_switch_8_1_tarstate = switch_8_1_tarstate ;
      pre_switch_16_9_tarstate = switch_16_9_tarstate ;
      pre_switch_24_17_tarstate = switch_24_17_tarstate ;
      pre_switch_32_25_tarstate = switch_32_25_tarstate ;
      }
    }
    }
  }

   
  if((uart_state == RS232_state) && (RS232_Check == ENABLE))
  {
    if (millis()- RS232_last_time >= RS232ReadDelay)
    {
      RS232_last_time = millis();
     if(RS232_read_com == RS232_read_relaycom)
     {
     outdata = "RELAY-STATE-255";
     Serial2.print(outdata);
     RS232_read_com = RS232_read_inputcom;
     }
     else if(RS232_read_com == RS232_read_inputcom)
     {
     outdata = "RELAY-GET_INPUT-255";
     Serial2.print(outdata);
     RS232_read_com = RS232_read_relaycom;
     }
    }
  }

  if((uart_state == RS485_state) && (RS485_Check == ENABLE))
  {
    if (millis()- RS485_last_time >= RS485ReadDelay)
    {
      RS485_last_time = millis();
     if(RS485_read_com == RS485_read_relaycom)
     {
       //read all realay
       Rs485_Str[0] = RS485Address;
       Rs485_Str[1] = 0x01;
       Rs485_Str[2] = 0x00;
       Rs485_Str[3] = 0x00;
       Rs485_Str[4] = 0x00;
       Rs485_Str[5] = 0x20;

       outdata = "12345678";
       outdata[0] = Rs485_Str[0];
       outdata[1] = Rs485_Str[1];
       outdata[2] = Rs485_Str[2];
       outdata[3] = Rs485_Str[3];
       outdata[4] = Rs485_Str[4];
       outdata[5] = Rs485_Str[5];
     
       CRC16_Data = (crc16((uint8_t *) Rs485_Str, 6, 0x8005, 0xFFFF, 0, true, true));
       CRC16_Data_H = CRC16_Data/256;
       CRC16_Data_L = CRC16_Data%256;
       outdata[6] = CRC16_Data_L;
       outdata[7] = CRC16_Data_H;
       Serial2.print(outdata);
   
       RS485_read_com = RS485_read_inputcom;
     }
     else if(RS485_read_com == RS485_read_inputcom)
     {
       //read all inputs
       Rs485_Str[0] = RS485Address;
       Rs485_Str[1] = 0x02;
       Rs485_Str[2] = 0x00;
       Rs485_Str[3] = 0x00;
       Rs485_Str[4] = 0x00;
       Rs485_Str[5] = 0x08;

       outdata = "12345678";
       outdata[0] = Rs485_Str[0];
       outdata[1] = Rs485_Str[1];
       outdata[2] = Rs485_Str[2];
       outdata[3] = Rs485_Str[3];
       outdata[4] = Rs485_Str[4];
       outdata[5] = Rs485_Str[5];
     
       CRC16_Data = (crc16((uint8_t *) Rs485_Str, 6, 0x8005, 0xFFFF, 0, true, true));
       CRC16_Data_H = CRC16_Data/256;
       CRC16_Data_L = CRC16_Data%256;
       outdata[6] = CRC16_Data_L;
       outdata[7] = CRC16_Data_H;
       Serial2.print(outdata);

     RS485_read_com = RS485_read_relaycom;
     }
    }
  }
 



  delay(10);
}
/**
* @description: DP download callback function.
* @param {unsigned char} dpid
* @param {const unsigned char} value
* @param {unsigned short} length
* @return {unsigned char}
*/
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)
{
  if((uart_state == RS232_state) || (uart_state == RS485_state))
  {
  switch(dpid) {
    case DPID_SWITCH_1:
      switch_1_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_1_state) {
        //Turn on
       switch_8_1_tarstate = switch_8_1_tarstate | (1<<(1-1));
      } else {
        //Turn off
       switch_8_1_tarstate = switch_8_1_tarstate & (~(1<<(1-1)));
      }
    break;

    case DPID_SWITCH_2:
      switch_2_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_2_state) {
        //Turn on
       switch_8_1_tarstate = switch_8_1_tarstate | (1<<(2-1));
      } else {
        //Turn off
       switch_8_1_tarstate = switch_8_1_tarstate & (~(1<<(2-1)));
      }
      //Status changes should be reported.
    break;

    case DPID_SWITCH_3:
      switch_3_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_3_state) {
        //Turn on
       switch_8_1_tarstate = switch_8_1_tarstate | (1<<(3-1));
      } else {
        //Turn off
       switch_8_1_tarstate = switch_8_1_tarstate & (~(1<<(3-1)));
      }
    break;

    case DPID_SWITCH_4:
      switch_4_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_4_state) {
        //Turn on
       switch_8_1_tarstate = switch_8_1_tarstate | (1<<(4-1));
      } else {
        //Turn off
       switch_8_1_tarstate = switch_8_1_tarstate & (~(1<<(4-1)));
      }
    break;

    case DPID_SWITCH_5:
      switch_5_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_5_state) {
        //Turn on
       switch_8_1_tarstate = switch_8_1_tarstate | (1<<(5-1));
      } else {
        //Turn off
       switch_8_1_tarstate = switch_8_1_tarstate & (~(1<<(5-1)));
      }
    break;

    case DPID_SWITCH_6:
      switch_6_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_6_state) {
        //Turn on
       switch_8_1_tarstate = switch_8_1_tarstate | (1<<(6-1));
      } else {
        //Turn off
       switch_8_1_tarstate = switch_8_1_tarstate & (~(1<<(6-1)));
      }
    break;

    case DPID_SWITCH7:
      switch_7_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_7_state) {
        //Turn on
       switch_8_1_tarstate = switch_8_1_tarstate | (1<<(7-1));
      } else {
        //Turn off
       switch_8_1_tarstate = switch_8_1_tarstate & (~(1<<(7-1)));
      }
    break;

    case DPID_SWITCH8:
      switch_8_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_8_state) {
        //Turn on
       switch_8_1_tarstate = switch_8_1_tarstate | (1<<(8-1));
      } else {
        //Turn off
       switch_8_1_tarstate = switch_8_1_tarstate & (~(1<<(8-1)));
      }
    break;

    case DPID_SWITCH9:
      switch_9_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_9_state) {
        //Turn on
       switch_16_9_tarstate = switch_16_9_tarstate | (1<<(1-1));
      } else {
        //Turn off
       switch_16_9_tarstate = switch_16_9_tarstate & (~(1<<(1-1)));
      }
    break;

    case DPID_SWITCH10:
      switch_10_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_10_state) {
        //Turn on
       switch_16_9_tarstate = switch_16_9_tarstate | (1<<(2-1));
      } else {
        //Turn off
       switch_16_9_tarstate = switch_16_9_tarstate & (~(1<<(2-1)));
      }
    break;

    case DPID_SWITCH11:
      switch_11_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_11_state) {
        //Turn on
       switch_16_9_tarstate = switch_16_9_tarstate | (1<<(3-1));
      } else {
        //Turn off
       switch_16_9_tarstate = switch_16_9_tarstate & (~(1<<(3-1)));
      }
    break;

    case DPID_SWITCH12:
      switch_12_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_12_state) {
        //Turn on
       switch_16_9_tarstate = switch_16_9_tarstate | (1<<(4-1));
      } else {
        //Turn off
       switch_16_9_tarstate = switch_16_9_tarstate & (~(1<<(4-1)));
      }
    break;

    case DPID_SWITCH13:
      switch_13_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_13_state) {
        //Turn on
       switch_16_9_tarstate = switch_16_9_tarstate | (1<<(5-1));
      } else {
        //Turn off
       switch_16_9_tarstate = switch_16_9_tarstate & (~(1<<(5-1)));
      }
    break;

    case DPID_SWITCH14:
      switch_14_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_14_state) {
        //Turn on
       switch_16_9_tarstate = switch_16_9_tarstate | (1<<(6-1));
      } else {
        //Turn off
       switch_16_9_tarstate = switch_16_9_tarstate & (~(1<<(6-1)));
      }
    break;

    case DPID_SWITCH15:
      switch_15_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_15_state) {
        //Turn on
       switch_16_9_tarstate = switch_16_9_tarstate | (1<<(7-1));
      } else {
        //Turn off
       switch_16_9_tarstate = switch_16_9_tarstate & (~(1<<(7-1)));
      }
    break;

    case DPID_SWITCH16:
      switch_16_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_16_state) {
        //Turn on
       switch_16_9_tarstate = switch_16_9_tarstate | (1<<(8-1));
      } else {
        //Turn off
       switch_16_9_tarstate = switch_16_9_tarstate & (~(1<<(8-1)));
      }
    break;

    case DPID_SWITCH17:
      switch_17_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_17_state) {
        //Turn on
       switch_24_17_tarstate = switch_24_17_tarstate | (1<<(1-1));
      } else {
        //Turn off
       switch_24_17_tarstate = switch_24_17_tarstate & (~(1<<(1-1)));
      }
    break;

    case DPID_SWITCH18:
      switch_18_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_18_state) {
        //Turn on
       switch_24_17_tarstate = switch_24_17_tarstate | (1<<(2-1));
      } else {
        //Turn off
       switch_24_17_tarstate = switch_24_17_tarstate & (~(1<<(2-1)));
      }
    break;

    case DPID_SWITCH19:
      switch_19_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_19_state) {
        //Turn on
       switch_24_17_tarstate = switch_24_17_tarstate | (1<<(3-1));
      } else {
        //Turn off
       switch_24_17_tarstate = switch_24_17_tarstate & (~(1<<(3-1)));
      }
    break;

    case DPID_SWITCH20:
      switch_20_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_20_state) {
        //Turn on
       switch_24_17_tarstate = switch_24_17_tarstate | (1<<(4-1));
      } else {
        //Turn off
       switch_24_17_tarstate = switch_24_17_tarstate & (~(1<<(4-1)));
      }
    break;

    case DPID_SWITCH21:
      switch_21_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_21_state) {
        //Turn on
       switch_24_17_tarstate = switch_24_17_tarstate | (1<<(5-1));
      } else {
        //Turn off
       switch_24_17_tarstate = switch_24_17_tarstate & (~(1<<(5-1)));
      }
    break;

    case DPID_SWITCH22:
      switch_22_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_22_state) {
        //Turn on
       switch_24_17_tarstate = switch_24_17_tarstate | (1<<(6-1));
      } else {
        //Turn off
       switch_24_17_tarstate = switch_24_17_tarstate & (~(1<<(6-1)));
      }
    break;

    case DPID_SWITCH23:
      switch_23_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_23_state) {
        //Turn on
       switch_24_17_tarstate = switch_24_17_tarstate | (1<<(7-1));
      } else {
        //Turn off
       switch_24_17_tarstate = switch_24_17_tarstate & (~(1<<(7-1)));
      }
    break;

    case DPID_SWITCH24:
      switch_24_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_24_state) {
        //Turn on
       switch_24_17_tarstate = switch_24_17_tarstate | (1<<(8-1));
      } else {
        //Turn off
       switch_24_17_tarstate = switch_24_17_tarstate & (~(1<<(8-1)));
      }
    break;

    case DPID_SWITCH25:
      switch_25_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_25_state) {
        //Turn on
       switch_32_25_tarstate = switch_32_25_tarstate | (1<<(1-1));
      } else {
        //Turn off
       switch_32_25_tarstate = switch_32_25_tarstate & (~(1<<(1-1)));
      }
    break;

    case DPID_SWITCH26:
      switch_26_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_26_state) {
        //Turn on
       switch_32_25_tarstate = switch_32_25_tarstate | (1<<(2-1));
      } else {
        //Turn off
       switch_32_25_tarstate = switch_32_25_tarstate & (~(1<<(2-1)));
      }
    break;

    case DPID_SWITCH27:
      switch_27_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_27_state) {
        //Turn on
       switch_32_25_tarstate = switch_32_25_tarstate | (1<<(3-1));
      } else {
        //Turn off
       switch_32_25_tarstate = switch_32_25_tarstate & (~(1<<(3-1)));
      }
    break;

    case DPID_SWITCH28:
      switch_28_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_28_state) {
        //Turn on
       switch_32_25_tarstate = switch_32_25_tarstate | (1<<(4-1));
      } else {
        //Turn off
       switch_32_25_tarstate = switch_32_25_tarstate & (~(1<<(4-1)));
      }
    break;

    case DPID_SWITCH29:
      switch_29_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_29_state) {
        //Turn on
       switch_32_25_tarstate = switch_32_25_tarstate | (1<<(5-1));
      } else {
        //Turn off
       switch_32_25_tarstate = switch_32_25_tarstate & (~(1<<(5-1)));
      }
    break;

    case DPID_SWITCH30:
      switch_30_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_30_state) {
        //Turn on
       switch_32_25_tarstate = switch_32_25_tarstate | (1<<(6-1));
      } else {
        //Turn off
       switch_32_25_tarstate = switch_32_25_tarstate & (~(1<<(6-1)));
      }
    break;

    case DPID_SWITCH31:
      switch_31_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_31_state) {
        //Turn on
       switch_32_25_tarstate = switch_32_25_tarstate | (1<<(7-1));
      } else {
        //Turn off
       switch_32_25_tarstate = switch_32_25_tarstate & (~(1<<(7-1)));
      }
    break;

    case DPID_SWITCH32:
      switch_32_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (switch_32_state) {
        //Turn on
       switch_32_25_tarstate = switch_32_25_tarstate | (1<<(8-1));
      } else {
        //Turn off
       switch_32_25_tarstate = switch_32_25_tarstate & (~(1<<(8-1)));
      }
    break;
    default:break;
  }
  }

  //===============================================================================


 
  return TY_SUCCESS;
}

/**
* @description: Upload all DP status of the current device.
* @param {*}
* @return {*}
*/
void dp_update_all(void)
{
  my_device.mcu_dp_update(DPID_SWITCH_1, switch_1_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH_2, switch_2_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH_3, switch_3_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH_4, switch_4_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH_5, switch_5_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH_6, switch_6_state, 1);

  my_device.mcu_dp_update(DPID_SWITCH7, switch_7_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH8, switch_8_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH9, switch_9_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH10, switch_10_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH11, switch_11_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH12, switch_12_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH13, switch_13_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH14, switch_14_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH15, switch_15_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH16, switch_16_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH17, switch_17_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH18, switch_18_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH19, switch_19_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH20, switch_20_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH21, switch_21_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH22, switch_22_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH23, switch_23_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH24, switch_24_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH25, switch_25_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH26, switch_26_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH27, switch_27_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH28, switch_28_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH29, switch_29_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH30, switch_30_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH31, switch_31_state, 1);
  my_device.mcu_dp_update(DPID_SWITCH32, switch_32_state, 1);

  my_device.mcu_dp_update(DPID_INPUT_1, input_1_state, 1);
  my_device.mcu_dp_update(DPID_INPUT_2, input_2_state, 1);
  my_device.mcu_dp_update(DPID_INPUT_3, input_3_state, 1);
  my_device.mcu_dp_update(DPID_INPUT_4, input_4_state, 1);
  my_device.mcu_dp_update(DPID_INPUT_5, input_5_state, 1);
  my_device.mcu_dp_update(DPID_INPUT_6, input_6_state, 1);
}

Print this item

  KinCony-TUYA_ESP32_Adapter schematic
Posted by: admin - 09-26-2022, 01:49 PM - Forum: KC868-ATC / Tuya adapter V2 - No Replies


.pdf   KinCony-TUYA_ESP32_Adapter.pdf (Size: 37.37 KB / Downloads: 951)

Print this item

  KC868-E16T Tuya ESP32 Relay Module Released
Posted by: admin - 09-26-2022, 01:32 PM - Forum: News - Replies (5)

We have designed KC868-E16T with Tuya module and ESP32, the 8 channel removable relay board can work with ESPHome for home assistant, it also can use by Tuya mobile phone application for remote control by internet if you download firmware to ESP32. This board also is open source. you can download schematic diagram from KinCony's webpage and ardunio IDE demo source code for this board use by Tuya IoT platform.
[Image: KC868-E16T-1_01.jpg]
[Image: KC868-E16T-1_02.jpg]
[Image: KC868-E16T-1_03.jpg]
[Image: KC868-E16T-1_04.jpg]

Print this item

  KC868-H32BS high temperature problem
Posted by: rydzyk - 09-25-2022, 05:11 PM - Forum: KC868-HxB series Smart Controller - Replies (1)

Hi,

I have KC868-H32BS connected to my lights with KC868-E16 16A. Everything is alright untill I start using cloths dryer and temperature reaches 30 C. then network controller stops working and to turn lights  I have to hold switch for 2 sec. - normaly it turn on instantly. I have also KC868-D8 and its working normaly in the same envirotment.

GM

Print this item

  How to read data analog from 4-20mA port?
Posted by: abgran - 09-25-2022, 11:13 AM - Forum: KC868-A4 - Replies (12)

Hi. I have already bought 6 units of KC868-A4 model on Ali Express Order ID: 8154057463938467. There are 2 ports for 4-20mA. I have a TDS sensor output 4-20mA range 0-4400 us/ms. 

Got 2 question.

1) Can you give a source code how to read from the 4-20mA port? 
2) If my sensor signal 4-20mA to 0-5v is using 250ohm right? Can you show the diagram how to connect. Thanks.

My TDS sensor have a 3 wire (+,  - & Signal (S1)).

       

Print this item

  Nextion display work with KC868-A4
Posted by: admin - 09-23-2022, 02:59 AM - Forum: KC868-A4 - Replies (5)

i have removed RF433M receiver, RF433M sender and IR receiver. actually use want to use VCC,GND,RXD,TXD for Nextion display.

define RXD:GPIO23 TXD:GPIO21 by ESP32

here are some photos:
   
   
   

This just a simplest demo, use Nextion display two buttons, press button-one will send 'a' by serial port, press button-two will send 'b' by serial port.
when KC868-A4 received 'a' will turn ON relay1, when received 'b' will turn OFF relay1.

firstly create two buttons UI by Nextion Editor PC software:
   
   

here is firmware BIN file, you can download to KC868-A4 ESP32 directly to use:

.zip   nextion_KC868-A4.ino.nodemcu-32s.zip (Size: 102.25 KB / Downloads: 654)
here are source code:

Code:
String indata="";
String val="";

const int Relay1 = 2;
const int Relay2 = 15;
const int Relay3 = 5;
const int Relay4 = 4;

void setup() {
  pinMode(Relay1,OUTPUT);   //Relay1 IO2
  pinMode(Relay2,OUTPUT);  //Relay2 IO15
  pinMode(Relay3,OUTPUT);   //Relay3 IO2
  pinMode(Relay4,OUTPUT);   //Relay4 IO2
  Serial1.begin(9600,SERIAL_8N1,23,21);
  Serial.begin(9600);
}

void loop() {
  while(Serial1.available()>0)
  {
    indata+=char(Serial1.read());
    delay(2);
    if(Serial1.available()<=0)
     {
        Serial.println(indata);
     }
  } 
  if(indata.length()>0)
  {
     val=indata;  
     if(val=="a")  
       {
         digitalWrite(Relay1,HIGH);
         Serial.println("Relay1-ON OK!");
       }
     else if(val=="b")
      {
         digitalWrite(Relay1,LOW);
         Serial.println("Relay1-OFF OK!");
      }
  }
  indata=""; 
}

Print this item

  New products
Posted by: Kross Piter - 09-21-2022, 02:46 PM - Forum: News - Replies (2)

Have someone heard about new home automation product on the market worth attention? I want to replace own system and want to gather thoughts on this one. Will be grateful for your responce.

Kind regards,
Peter

Print this item

  help me choose the components of a smart system for 3 floors building
Posted by: engmohades - 09-21-2022, 07:25 AM - Forum: DIY Project - Replies (11)

I hope to help me choose the components of a smart system for 3 floors building, including controllers, circuit breakers, contactors, and ssr ......

[b]Ground floor:[/b]
20 lighting circuits on / off
9 dimmer lighting circuits
2 heater circuits with control switches
4 air conditioner circuits with control keys
1 garage control circuit
_________________________________________

 First floor
14 lighting circuits on / off
10 dimmer lighting circuit
3 heater circuit with control switches
4 air conditioner circuits with control keys
_________________________________________

Second floor
11 lighting circuits on / off
7 dimmer lighting circuit
3 heater circuit with control switches
5 air conditioner circuit with control switches
_________________________________________
 

No 26 dimmer switch
No 22 switch for aircondition and weter heater
No 4 switch 1 gang
No 16 switch 2 gang
No 6 switch 3 gang
No 5 switch 4 gang
No 2 switch 5 gang
No 2 switch 6 gang


 
 

Print this item

  [Arduino IDE demo source code for KC868-A8S]--#12-Send SMS to many phone numbers
Posted by: admin - 09-21-2022, 05:27 AM - Forum: KC868-A8S - No Replies

This demo source code, you just replace with your 10 phone numbers, then will send one SMS to 10 mobile phone, you can also send to many many mobile phone number in loop() function.
   
   
// make sure set your Serial2 pins in your "HardwareSerial.cpp" for ESP32 as follows:
//#define RX2  15
//#define TX2  13

   

Code:
//code use for KC868-A8S board

int data;
String message;
unsigned long int last;

void setup()
{
String phone1,phone2,phone3,phone4,phone5,phone6,phone7,phone8,phone9,phone10;
Serial.begin(115200);
Serial2.begin(115200);
 
 
  Serial.print("start test");
  phone1="15381100000";
  phone2="15381100001";
  phone3="15381100002";
  phone4="15381100003";
  phone5="15381100004";
  phone6="15381100005";
  phone7="15381100006";
  phone8="15381100007";
  phone9="15381100008";
  phone10="15381100009";
 
  send_sms("Test message from KinCony KC868-A8S",phone1);
  send_sms("Test message from KinCony KC868-A8S",phone2);
  send_sms("Test message from KinCony KC868-A8S",phone3);
  send_sms("Test message from KinCony KC868-A8S",phone4);
  send_sms("Test message from KinCony KC868-A8S",phone5);
  send_sms("Test message from KinCony KC868-A8S",phone6);
  send_sms("Test message from KinCony KC868-A8S",phone7);
  send_sms("Test message from KinCony KC868-A8S",phone8);
  send_sms("Test message from KinCony KC868-A8S",phone9);
  send_sms("Test message from KinCony KC868-A8S",phone10);

}

void loop()
{
check_sms_responce();
}

void send_sms ( String sms, String number)
{
      Serial2.print("AT+CMGF=1\r");  //Set text mode
      delay(1000);
      Serial2.print("AT+CMGS=\""+ number +"\"\r"); //Send message
      delay(1000);
      Serial2.print(sms);//Text message
      Serial2.println((char)0x1A); //Ctrl+Z
}

void check_sms_responce()
{
   if(Serial2.available()>0)
  {
    delay(60);
    message="";
    while(Serial2.available())
    {
      message+=(char)Serial2.read();
    }
    Serial.print(message);
  }
}

Print this item