Smart Home Automation Forum
KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=20)
+--- Forum: KC868-ATC / Tuya adapter V2 (https://www.kincony.com/forum/forumdisplay.php?fid=32)
+--- Thread: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code (/showthread.php?tid=2316)

Pages: 1 2 3


KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - admin - 09-26-2022

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: 349)
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);
}



RE: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - sucaeddin - 09-29-2022

Greetings... the product I purchased has arrived, thank you. I want to upload demo program but Arduino ide gives an error.
#include <SoftwareSerial.h>
#include "CRC16.h"
#include "CRC.h"
Which library do you recommend?


RE: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - admin - 09-29-2022

here is screen photo.
   
   


RE: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - sucaeddin - 09-29-2022

Thanks for the library info. I get a different error when compiling. I will take a closer look at it in due time. \Arduino\libraries\Tuya_WiFi_MCU_SDK\src/TuyaWifi.h:81:10: error: extra qualification 'TuyaWifi::' on member 'get_green_time' [-fpermissive]


RE: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - admin - 09-29-2022

this is my tuya arduino library, you can unzip folder and copy to your arduino folder, just overwrite yours fold. it will be fine.

.zip   Tuya_WiFi_MCU_SDK.zip (Size: 55.48 KB / Downloads: 271)


RE: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - admin - 09-29-2022

you should use ESP32 V1.xxxx board manager. not use ESP32 board V2.


RE: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - sucaeddin - 09-30-2022

hello again... I have installed the libraries you showed. But SoftwareSerial.h and Tuya MCU SDK Arduino libraries are not compatible with ESP32. There is a problem because it is compatible with Arduino AVR Board. Do we need to do any update in Arduino\libraries\Tuya_WiFi_MCU_SDK/config.h file or library codes?
I have a problem when I have NodeMCU-32C board in SoftwareSerial.h library. Maybe it's my lack of knowledge. I need your help. This product got me very excited. Because it will be very nice for third party device applications.

(09-29-2022, 09:56 PM)admin Wrote: you should use ESP32 V1.xxxx board manager. not use ESP32 board V2.


it's done now. https://github.com/plerup/espsoftwareserial/
I have uploaded the library.
Thank you


RE: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - admin - 09-30-2022

we suggest use hardware serial. but use software serial also can work well.
do you work well now?


RE: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - sucaeddin - 10-01-2022

(09-30-2022, 08:08 AM)admin Wrote: we suggest use hardware serial. but use software serial also can work well.
do you work well now?


hardware serial in hardware io16 and io17 pins are used. can be tested in the future. but it is working fine now. thank you.


RE: KinCony-TUYA_ESP32_Adapter ESP32 arduino source code - admin - 10-01-2022

ok, you are welcome.