Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TUYA_ESP32_Adapter arduino source code 7.1 -support TCP connection
#1
this 7.1 source code version, have added TCP connection with KinCony's relay board by WiFi. so now you can use RS232 or RS485 or WiFi connect to KinCony's KC868-H32BS, KC868-H32B, KC868-H32L, KC868-H32 relay controller.
just config relay board IP port for router by KC868-Adatper's AP (AP name is KC868-Tuya) webpage.

here is firmware BIN file, you can download to ESP32 directly by USB cable:

.zip   KinCony-Tuya-adapter.nodemcu-32s-7.1.zip (Size: 505.91 KB / Downloads: 271)

here is arduino source code:

.zip   KinCony-Tuya-adapter-source-code-7.1.zip (Size: 8.22 KB / Downloads: 288)

Supported products list:
   
   
   
   

source code:
Code:
/*KC868-Tuya adapter have 5 LEDs
* LED D1: when D1-OFF: ready to enable AP for config ESP32 wifi, AP name is KC868-Tuya      D1-ON: ESP32 connect to router successful
* LED D2: when D2-ON: ESP32 connect to relay board by TCP connection successfully
* LED D3: when D3-ON: KC868-Tuya adapter work in RS232 or RS485 mode
* LED D4: when D4 blink: ready to config Tuya wifi module for router SSID
* LED D6: KC868-Tuya adapter power LED
* Button S1:Tuya wifi module RESET key
* Button DL: ESP32 wifi RESET
* Button RST: ESP32 module RESET
* Jumper IO23: Short:by RS485   disconnect:by RS232
* @Copyright: KinCony IoT
*/
#include <WiFi.h>
#include <TuyaWifi.h>
#include <SoftwareSerial.h>
#include "CRC16.h"
#include "CRC.h"
#include <WiFiManager.h>
#include "EEPROM.h"


#define TRIGGER_PIN 0   // reset wifi button
WiFiManager wm;
// Replace with your network credentials
const char* ssid     = "xxx"; //your router's ssid
const char* password = "yyy"; //your router's password

// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;

TuyaWifi my_device;

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

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


// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
int flag_read_di=0;



#define  ADDRESS_TARGET_FIRST  10
#define  ADDRESS_TARGET_IP     30
#define  ADDRESS_TARGET_PORT   60
#define  ADDRESS_TARGET_MODE   90


#define RS232_state   1
#define RS485_state   0
#define wifi_read_inputcom   0
#define wifi_read_relaycom   1
#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
#define NOCONNECTED   0
#define CONNECTED   1
uint16_t  CRC16_Data = 0;
uint8_t  CRC16_Data_H = 0;
uint8_t  CRC16_Data_L = 0;

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

unsigned char wifi_state = ENABLE;
unsigned char wifi_stop = DISABLE;

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

unsigned char wifi_read_com = 1;
//1= read RELAY  0 = read input
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_wifi_delay_state = 0;
unsigned char dpid_wifi_delay_switch = 0;
unsigned char en_wifi_delay_switch = 0;
//1= en delay   0= dis delay

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 = 32;

int index_pos = 0;
int index_begin = 0;
int index_end = 0;

#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_TUYA_WiFi  26
#define LED_User1  32
#define LED_ESP_WiFi  33
#define LED_User2  25

/* 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 wifi_last_time = 0;

unsigned long wifi_delay_last_time = 0;
unsigned long wifi_relay_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};
String currentLine="";

String  TARGET_FIRST="";   
String  TARGET_IP="";   
String  TARGET_PORT="";   
String  TARGET_MODE="";   
String  NEW_TARGET_IP="";   
String  NEW_TARGET_PORT="";   
String  NEW_TARGET_MODE="";   
int address = 0;

String relay1state = "off";// state of relay1
String relay2state = "off";// state of relay2
String relay3state = "off";// state of relay3
String relay4state = "off";// state of relay4
String relay5state = "off";// state of relay5
String relay6state = "off";// state of relay6
String relay7state = "off";// state of relay7
String relay8state = "off";// state of relay8

String relay9state = "off";// state of relay9
String relay10state = "off";// state of relay10
String relay11state = "off";// state of relay11
String relay12state = "off";// state of relay12
String relay13state = "off";// state of relay13
String relay14state = "off";// state of relay14
String relay15state = "off";// state of relay15
String relay16state = "off";// state of relay16

String relay17state = "off";// state of relay17
String relay18state = "off";// state of relay18
String relay19state = "off";// state of relay19
String relay20state = "off";// state of relay20
String relay21state = "off";// state of relay21
String relay22state = "off";// state of relay22
String relay23state = "off";// state of relay23
String relay24state = "off";// state of relay24

String relay25state = "off";// state of relay25
String relay26state = "off";// state of relay26
String relay27state = "off";// state of relay27
String relay28state = "off";// state of relay28
String relay29state = "off";// state of relay29
String relay30state = "off";// state of relay30
String relay31state = "off";// state of relay31
String relay32state = "off";// state of relay32

WiFiClient client  ;   // clients

void setup()
{
  ini_state = 1;
  wifi_connected = NOCONNECTED;

  //Initialize led port, turn off led.

  pinMode(LED_TUYA_WiFi, OUTPUT);
  digitalWrite(LED_TUYA_WiFi, LOW);
  pinMode(LED_ESP_WiFi, OUTPUT);
  digitalWrite(LED_ESP_WiFi, LOW);

  pinMode(LED_User1, OUTPUT);
  digitalWrite(LED_User1, LOW);
  pinMode(LED_User2, OUTPUT);
  digitalWrite(LED_User2, LOW);

  // wifi reset button
  pinMode(TRIGGER_PIN, INPUT_PULLUP);
  //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;
     }
  }

  if(uart_state == RS232_state)
  {
    Serial2.begin(RS232Baud,SERIAL_8N1,16,17);
  }
  else
  {
    Serial2.begin(RS485Baud,SERIAL_8N1,16,17);
  }

// Serial with tuyawifi
Serial.begin(9600,SERIAL_8N1,14,13);
    // Serial with debug
Serial1.begin(9600,SERIAL_8N1,3,1);


Serial1.println("\nTesting EEPROM Library\n");
  if (!EEPROM.begin(1000)) {
    Serial1.println("Failed to initialise EEPROM");
    //Serial2.println("Restarting...");
    delay(1000);
    //ESP.restart();
  }
   
  //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);

  address = ADDRESS_TARGET_FIRST;
  TARGET_FIRST = EEPROM.readString(address);
  address = ADDRESS_TARGET_IP;
  TARGET_IP = EEPROM.readString(address);
  address = ADDRESS_TARGET_PORT;
  TARGET_PORT = EEPROM.readString(address);
  address = ADDRESS_TARGET_MODE;
  TARGET_MODE = EEPROM.readString(address);

  //default
  if (TARGET_FIRST != "123456")
    {
     TARGET_IP="192.168.1.200";   
     TARGET_PORT="4196";   
     TARGET_MODE="wifi";   
    }   

// Connect to Wi-Fi network with SSID and password
  WiFi.mode(WIFI_STA);
//close wifi sleep
  WiFi.setSleep(false);
 
  // Define a text box, 50 characters maximum
  WiFiManagerParameter custom_text_box("my_text", "Enter Relay Board IP", TARGET_IP.c_str(), 50);
  // Add custom parameter
  wm.addParameter(&custom_text_box);
  // Define a text box, 50 characters maximum
  WiFiManagerParameter custom_text_box2("my_text2", "Enter Relay Board Port", TARGET_PORT.c_str(), 50);
  // Add custom parameter
  wm.addParameter(&custom_text_box2);
  // Define a text box, 50 characters maximum
  WiFiManagerParameter custom_text_box3("my_text3", "Enter wifi or 232 or 485", TARGET_MODE.c_str(), 50);
  // Add custom parameter
  wm.addParameter(&custom_text_box3);
 
     bool res;
       res = wm.autoConnect("KC868-Tuya"); // anonymous ap

    if(!res) {
        Serial1.println("Failed to connect");
        //ESP.restart();
    }
    else {
        //if you get here you have connected to the WiFi   
        Serial1.println("connected...yeey :)");
    }


while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial1.print(".");
  }
  // Print local IP address and start web server
  Serial1.println("");
  Serial1.println("WiFi connected.");
  Serial1.println("Local IP address: ");
  Serial1.println(WiFi.localIP());
    // Print custom text box value to serial monitor

  NEW_TARGET_IP = custom_text_box.getValue();   
  NEW_TARGET_PORT = custom_text_box2.getValue();   
  NEW_TARGET_MODE = custom_text_box3.getValue();   

  if(NEW_TARGET_IP != TARGET_IP)
    {
     TARGET_IP = NEW_TARGET_IP;
     address = ADDRESS_TARGET_IP;   
     EEPROM.writeString(address, TARGET_IP);

     TARGET_FIRST = "123456";
     address = ADDRESS_TARGET_FIRST;   
     EEPROM.writeString(address, TARGET_FIRST);
   
     EEPROM.commit();
    }

  if(NEW_TARGET_PORT != TARGET_PORT)
    {
     TARGET_PORT = NEW_TARGET_PORT;
     address = ADDRESS_TARGET_PORT;   
     EEPROM.writeString(address, TARGET_PORT);

     TARGET_FIRST = "123456";
     address = ADDRESS_TARGET_FIRST;   
     EEPROM.writeString(address, TARGET_FIRST);
   
     EEPROM.commit();
    }

  if(NEW_TARGET_MODE != TARGET_MODE)
    {
     TARGET_MODE = NEW_TARGET_MODE;
     address = ADDRESS_TARGET_MODE;   
     EEPROM.writeString(address, TARGET_MODE);

     TARGET_FIRST = "123456";
     address = ADDRESS_TARGET_FIRST;   
     EEPROM.writeString(address, TARGET_FIRST);
   
     EEPROM.commit();
    }

  Serial1.print("target IP: ");
  Serial1.println(TARGET_IP);
  Serial1.print("target port: ");
  Serial1.println(TARGET_PORT);
  Serial1.print("target mode: ");
  Serial1.println(TARGET_MODE);

  //server.begin(); ???????????????????????????????
pinMode(LED_User1, OUTPUT);
digitalWrite(LED_User1, HIGH);
                                                                                                               
  last_time = millis();
}

void loop()
{

if((TARGET_MODE=="wifi")||(TARGET_MODE=="WIFI")) 
{
digitalWrite(LED_User2, LOW);

const char* host = TARGET_IP.c_str();
int httpPort = TARGET_PORT.toInt();
if (client.connect(host, httpPort)) {
       wifi_state = ENABLE;
       digitalWrite(LED_ESP_WiFi, HIGH);
    while (client.connected() || client.available())
    { 
   
     // wifi_connected = CONNECTED;
      if(client.available() )
      {
      String line = client.readStringUntil('\n');
      Serial1.print("line=");
      Serial1.println(line);
      //if(line.length()<50)
      // {
      // indata = line;
      // //Serial1.print("indata=");
      // Serial1.println(indata);
      // }
      //client.write(line.c_str());
       if(line.indexOf("RELAY-SET_ALL-255") >= 0)
       {
       index_begin = line.indexOf("RELAY-SET_ALL-255");
       index_end = line.indexOf("OK", index_begin);
       index_end = index_end + 2;
       indata = line.substring(index_begin,index_end);
       //Serial1.print("indata=");
       Serial1.println(indata);
       }
       else if(line.indexOf("RELAY-GET_INPUT-255") >= 0)
       {
       index_begin = line.indexOf("RELAY-GET_INPUT-255");
       index_end = line.indexOf("OK", index_begin);
       index_end = index_end + 2;
       indata = line.substring(index_begin,index_end);
       //Serial1.print("indata=");
       Serial1.println(indata);
       }
       else if(line.indexOf("RELAY-STATE-255") >= 0)
       {
       index_begin = line.indexOf("RELAY-STATE-255");
       index_end = line.indexOf("OK", index_begin);
       index_end = index_end + 2;
       indata = line.substring(index_begin,index_end);
       //Serial1.print("indata=");
       Serial1.println(indata);
       }
       else if(line.indexOf("RELAY-ALARM") >= 0)
       {
       index_begin = line.indexOf("RELAY-ALARM");
       index_end = line.indexOf("OK", index_begin);
       index_end = index_end + 2;
       indata = line.substring(index_begin,index_end);
       //Serial1.print("indata=");
       Serial1.println(indata);
       }
       else if(line.indexOf("RELAY-DIARM") >= 0)
       {
       index_begin = line.indexOf("RELAY-DIARM");
       index_end = line.indexOf("OK", index_begin);
       index_end = index_end + 2;
       indata = line.substring(index_begin,index_end);
       //Serial1.print("indata=");
       Serial1.println(indata);
       }
     
      }
     
       
  /////////////wifi mode begin
  if((en_wifi_delay_switch == BEGIN_DELAY)&&(wifi_state == ENABLE))
  {
    if((millis()-wifi_delay_last_time)>=20)
    {
      my_device.mcu_dp_update(dpid_wifi_delay_switch, switch_wifi_delay_state, 1);
      en_wifi_delay_switch = END_DELAY;
    }
  }

    if((indata.length()>0)&&(((uart_state == RS232_state)&&(wifi_state == DISABLE))||(wifi_state == ENABLE)))
  {
     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"))   
      {
       
       //wifi_stop = DISABLE;

       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"))   
      {
       //wifi_stop = DISABLE;

       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);


           wifi_Check = ENABLE ;
         
      }     

     // wifi_last_time = millis();
  }   

    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_TUYA_WiFi, led_state);
    }
  }

    if(wifi_state == ENABLE)
  {
    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()- wifi_relay_last_time >= wifiRelayDelay)
    {
      wifi_Check = DISABLE ;

      wifi_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 ;
      client.print(relay_outdata);
      Serial1.println(relay_outdata);
      switch_8_1_nowstate = switch_8_1_tarstate ;
      switch_16_9_nowstate = switch_16_9_tarstate ;
      switch_24_17_nowstate = switch_24_17_tarstate ;
      switch_32_25_nowstate = switch_32_25_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(wifi_state == ENABLE)
  {
    if (millis()- wifi_last_time >= wifiReadDelay)
    {
      wifi_last_time = millis();
 
     if(wifi_read_com == wifi_read_relaycom)
     {
     outdata = "RELAY-STATE-255";
     client.print(outdata);
     wifi_stop = ENABLE;
     wifi_read_com = wifi_read_inputcom;
     }
     else if(wifi_read_com == wifi_read_inputcom)
     {
     outdata = "RELAY-GET_INPUT-255";
     client.print(outdata);
     wifi_stop = ENABLE;
     wifi_read_com = wifi_read_relaycom;
     }

   
    }
  }

  if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}
     
    //wifi mode end
   
     }
       client.stop();
    }
else
    {
       if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}

       wifi_state = DISABLE;
       digitalWrite(LED_ESP_WiFi, LOW);
       client.stop();
    }
     delay(5000);
}

else
{
   digitalWrite(LED_User2, HIGH);
     
     wifi_state = DISABLE;
     digitalWrite(LED_ESP_WiFi, LOW);
 
  //no wifi mode begin
  if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}

  if((uart_state == RS232_state)&&(wifi_state == DISABLE))
  {
  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)&&(wifi_state == DISABLE))
  {
  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)&&(wifi_state == DISABLE))
  {
    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)&&(wifi_state == DISABLE))
  {
    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)&&(wifi_state == DISABLE)))
  {
     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)&&(wifi_state == DISABLE))
  {
     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_TUYA_WiFi, led_state);
    }
  }

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

      switch_8_1_nowstate = switch_8_1_tarstate ;
      switch_16_9_nowstate = switch_16_9_tarstate ;
      switch_24_17_nowstate = switch_24_17_tarstate ;
      switch_32_25_nowstate = switch_32_25_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 == RS485_state)&&(wifi_state == DISABLE))
  {
    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;

       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)&&(wifi_state == DISABLE))
  {
    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)&&(wifi_state == DISABLE))
  {
    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);
 
}
    // return;
}
/**
* @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);
}
Reply
#2
LED-D1: OFF: ready for config ESP32 wifi. ON: config wifi completed
LED-D2: ON: connect to relay controller by wifi network successfully
LED-D3: ON: work on RS232/RS485 mode
LED-D4: indicator for Tuya module: Blink: ready to config wifi
LED-D6: Power LED
LEDs on PCB:

Button:
RST: reset for ESP32
D.L.: factory reset (clear all data)
Reply
#3
here is new BIN file can download to ESP32:
   
   
   

.zip   flash_download_tool_3.9.3.zip (Size: 29.57 MB / Downloads: 237)

config ESP32 wifi firstly, then you can set relay controller's IP and Port in ESP32 webpage. make sure your relay controller work mode= TCP Server. if you don't want to use by TCP connection, you can set to RS232/RS485 mode, then you can begin config Tuya wifi.
Reply
#4
all firmware files download:


Attached Files
.zip   boot_app0.zip (Size: 200 bytes / Downloads: 5)
.zip   bootloader_dio_40m.zip (Size: 11.35 KB / Downloads: 6)
.zip   KinCony-Tuya-adapter.nodemcu-32s-7.1.zip (Size: 505.91 KB / Downloads: 4)
.zip   default.zip (Size: 266 bytes / Downloads: 6)
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)