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

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 5,018
» Latest member: enviropestcontrol
» Forum threads: 2,436
» Forum posts: 12,587

Full Statistics

Online Users
There are currently 32 online users.
» 0 Member(s) | 19 Guest(s)
Bytespider, Crawl, Google, PetalBot, WordPress/, Yandex, bot

Latest Threads
M30 Troubles
Forum: KC868-M16 / M1 / MB / M30
Last Post: Annas
21 minutes ago
» Replies: 4
» Views: 14
[Arduino IDE demo source ...
Forum: KC868-A16
Last Post: admin
7 hours ago
» Replies: 2
» Views: 1,969
Default firmware
Forum: B16M
Last Post: admin
8 hours ago
» Replies: 1
» Views: 3
KC868-A16 - Own Arduino f...
Forum: Development
Last Post: admin
8 hours ago
» Replies: 1
» Views: 3
AS ESPHome yaml for home ...
Forum: KinCony AS
Last Post: admin
11-12-2024, 10:17 PM
» Replies: 6
» Views: 336
KC868-A8 board esphome + ...
Forum: KC868-A8
Last Post: gnetinternet@gmail.com
11-12-2024, 07:19 AM
» Replies: 10
» Views: 86
"KCS" v3.0.3 firmware for...
Forum: "KCS" firmware system
Last Post: admin
11-11-2024, 10:01 PM
» Replies: 0
» Views: 29
Kc868-a16 v1.6
Forum: DIY Project
Last Post: admin
11-11-2024, 11:17 AM
» Replies: 1
» Views: 28
KCS firmware - MQTT LWT?
Forum: KC868-A series and Uair Smart Controller
Last Post: admin
11-11-2024, 11:15 AM
» Replies: 35
» Views: 3,354
"KCS" v2.2.7 firmware BIN...
Forum: "KCS" firmware system
Last Post: admin
11-11-2024, 11:09 AM
» Replies: 0
» Views: 70

  TUYA_ESP32_Adapter arduino source code 7.1 -support TCP connection
Posted by: admin - 10-13-2022, 01:53 AM - Forum: KC868-ATC / Tuya adapter V2 - Replies (15)

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: 369)

here is arduino source code:

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

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

Print this item

  How to solve error during install esp home in home assistant nstall
Posted by: engmohades - 10-12-2022, 06:46 PM - Forum: KC868-A4 - Replies (8)

INFO Reading configuration /config/esphome/smart-a4.yaml...
WARNING GPIO2 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-a...pping-pins
WARNING GPIO15 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-a...pping-pins
WARNING GPIO4 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-a...pping-pins
INFO Generating C++ source...
INFO Compiling app...
Processing kc868-a4 (board: esp32dev; framework: arduino; platform: platformio/espressif32 @ 3.5.0)
--------------------------------------------------------------------------------
Library Manager: Installing Hash
INFO Installing Hash
Library Manager: Installing ESP8266WiFi
INFO Installing ESP8266WiFi
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
Dependency Graph
|-- AsyncTCP-esphome @ 1.2.2
|-- WiFi @ 1.0
|-- FS @ 1.0
|-- Update @ 1.0
|-- ESPAsyncWebServer-esphome @ 2.1.0
|  |-- AsyncTCP-esphome @ 1.2.2
|-- DNSServer @ 1.1.0
|-- ESPmDNS @ 1.0
RAM:  [=        ]  12.7% (used 41756 bytes from 327680 bytes)
Flash: [=====    ]  49.5% (used 908014 bytes from 1835008 bytes)
========================= [SUCCESS] Took 9.83 seconds =========================
INFO Successfully compiled program.
esptool.py v3.3.1
Serial port /dev/ttyUSB0
Connecting..........
Chip is ESP32-D0WD-V3 (revision 3)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 30:c6:f7:58:6e:34
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash will be erased from 0x00010000 to 0x000edfff...
Flash will be erased from 0x00001000 to 0x00005fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Compressed 908128 bytes to 519505...
Writing at 0x000681f7... (31 %)
A fatal error occurred: Serial data stream stopped: Possible serial noise or corruption.
INFO Upload with baud rate 460800 failed. Trying again with baud rate 115200.
esptool.py v3.3.1
Serial port /dev/ttyUSB0
Connecting.......
ERROR Running command failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
ERROR Please try running esptool.py --before default_reset --after hard_reset --baud 115200 --port /dev/ttyUSB0 --chip esp32 write_flash -z --flash_size detect 0x10000 /data/kc868-a4/.pioenvs/kc868-a4/firmware.bin 0x1000 /data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/bin/bootloader_dio_40m.bin 0x8000 /data/kc868-a4/.pioenvs/kc868-a4/partitions.bin 0xe000 /data/cache/platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin locally.



Attached Files
.txt   logs_smart-a4_upload.txt (Size: 3.1 KB / Downloads: 204)
Print this item

  24vdc power supply
Posted by: GDN20 - 10-12-2022, 07:18 AM - Forum: KC868-A8S - Replies (1)

hi, can I connect  KC868-A8S to 24vdc?

Print this item

  Specific libraries to compile the KC868-A32 Demo
Posted by: VanPeerce - 10-11-2022, 08:33 PM - Forum: KC868-A32/A32 Pro - Replies (9)

Could you specify the exact libraries to compile the demo for:

PCF8574.h  (there are many available to install)

WiFiManager.h

Sorry if this is an obvious answer to experienced users, I get numerous errors and I've tried many libraries


WARNING: library WiFiManager-master claims to run on esp8266, esp32 architecture(s) and may be incompatible with your current board which runs on avr architecture(s).
KC868-A32_Web_Server:11:1: error: 'WiFiManager' does not name a type; did you mean 'WiFiManager_h'?
WiFiManager wm;
^~~~~~~~~~~
WiFiManager_h
KC868-A32_Web_Server:34:27: error: no matching function for call to 'TwoWire::TwoWire(int)'
TwoWire I2Cone = TwoWire(0);
                          ^
In file included from C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:38:0,
                from C:\Users\spvis\Documents\Arduino\KC868-A32\KC868-A32_Web_Server\KC868-A32_Web_Server.ino:8:
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:52:5: note: candidate: TwoWire::TwoWire()
    TwoWire();
    ^~~~~~~
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:52:5: note:  candidate expects 0 arguments, 1 provided
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:34:7: note: candidate: constexpr TwoWire::TwoWire(const TwoWire&)
class TwoWire : public Stream
      ^~~~~~~
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:34:7: note:  no known conversion for argument 1 from 'int' to 'const TwoWire&'
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:34:7: note: candidate: constexpr TwoWire::TwoWire(TwoWire&&)
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:34:7: note:  no known conversion for argument 1 from 'int' to 'TwoWire&&'
KC868-A32_Web_Server:35:27: error: no matching function for call to 'TwoWire::TwoWire(int)'
TwoWire I2Ctwo = TwoWire(1);
                          ^
In file included from C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:38:0,
                from C:\Users\spvis\Documents\Arduino\KC868-A32\KC868-A32_Web_Server\KC868-A32_Web_Server.ino:8:
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:52:5: note: candidate: TwoWire::TwoWire()
    TwoWire();
    ^~~~~~~
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:52:5: note:  candidate expects 0 arguments, 1 provided
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:34:7: note: candidate: constexpr TwoWire::TwoWire(const TwoWire&)
class TwoWire : public Stream
      ^~~~~~~
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:34:7: note:  no known conversion for argument 1 from 'int' to 'const TwoWire&'
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:34:7: note: candidate: constexpr TwoWire::TwoWire(TwoWire&&)
C:\Users\spvis\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:34:7: note:  no known conversion for argument 1 from 'int' to 'TwoWire&&'
KC868-A32_Web_Server:37:41: error: no matching function for call to 'PCF8574:TongueCF8574(TwoWire*, int, int, int)'
PCF8574 pcf8574_R1(&I2Ctwo, 0x24, 15, 13);
                                        ^
In file included from C:\Users\spvis\Documents\Arduino\KC868-A32\KC868-A32_Web_Server\KC868-A32_Web_Server.ino:8:0:
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:110:2: note: candidate: PCF8574:TongueCF8574(uint8_t, uint8_t, void (*)())
  PCF8574(uint8_t address, uint8_t interruptPin,  void (*interruptFunction)() );
  ^~~~~~~


                                        ^
In file included from C:\Users\spvis\Documents\Arduino\KC868-A32\KC868-A32_Web_Server\KC868-A32_Web_Server.ino:8:0:
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:110:2: note: candidate: PCF8574:TongueCF8574(uint8_t, uint8_t, void (*)())
  PCF8574(uint8_t address, uint8_t interruptPin,  void (*interruptFunction)() );
  ^~~~~~~
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:110:2: note:  candidate expects 3 arguments, 4 provided
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:109:2: note: candidate: PCF8574:TongueCF8574(uint8_t)
  PCF8574(uint8_t address);
  ^~~~~~~
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:109:2: note:  candidate expects 1 argument, 4 provided
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note: candidate: constexpr PCF8574:TongueCF8574(const PCF8574&)
class PCF8574 {
      ^~~~~~~
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note:  candidate expects 1 argument, 4 provided
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note: candidate: constexpr PCF8574:TongueCF8574(PCF8574&&)
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note:  candidate expects 1 argument, 4 provided
KC868-A32_Web_Server:39:41: error: no matching function for call to 'PCF8574:TongueCF8574(TwoWire*, int, int, int)'
PCF8574 pcf8574_R3(&I2Ctwo, 0x21, 15, 13);
                                        ^
In file included from C:\Users\spvis\Documents\Arduino\KC868-A32\KC868-A32_Web_Server\KC868-A32_Web_Server.ino:8:0:
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:110:2: note: candidate: PCF8574:TongueCF8574(uint8_t, uint8_t, void (*)())
  PCF8574(uint8_t address, uint8_t interruptPin,  void (*interruptFunction)() );
  ^~~~~~~
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:110:2: note:  candidate expects 3 arguments, 4 provided
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:109:2: note: candidate: PCF8574:TongueCF8574(uint8_t)
  PCF8574(uint8_t address);
  ^~~~~~~
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:109:2: note:  candidate expects 1 argument, 4 provided
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note: candidate: constexpr PCF8574:TongueCF8574(const PCF8574&)
class PCF8574 {
      ^~~~~~~


      ^~~~~~~
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note:  candidate expects 1 argument, 4 provided
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note: candidate: constexpr PCF8574:TongueCF8574(PCF8574&&)
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note:  candidate expects 1 argument, 4 provided
KC868-A32_Web_Server:45:39: error: no matching function for call to 'PCF8574:TongueCF8574(TwoWire*, int, int, int)'
PCF8574 pcf8574_I3(&I2Cone, 0x21, 4, 5);
                                      ^
In file included from C:\Users\spvis\Documents\Arduino\KC868-A32\KC868-A32_Web_Server\KC868-A32_Web_Server.ino:8:0:
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:110:2: note: candidate: PCF8574:TongueCF8574(uint8_t, uint8_t, void (*)())
  PCF8574(uint8_t address, uint8_t interruptPin,  void (*interruptFunction)() );
  ^~~~~~~

C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note:  candidate expects 1 argument, 4 provided
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note: candidate: constexpr PCF8574:TongueCF8574(PCF8574&&)
C:\Users\spvis\Documents\Arduino\libraries\PCF8574_library/PCF8574.h:106:7: note:  candidate expects 1 argument, 4 provided
C:\Users\spvis\Documents\Arduino\KC868-A32\KC868-A32_Web_Server\KC868-A32_Web_Server.ino: In function 'void setup()':
KC868-A32_Web_Server:132:8: error: 'class WiFiClass' has no member named 'mode'
  WiFi.mode(WIFI_STA);
        ^~~~
KC868-A32_Web_Server:132:13: error: 'WIFI_STA' was not declared in this scope
  WiFi.mode(WIFI_STA);
            ^~~~~~~~
KC868-A32_Web_Server:134:5: error: 'wm' was not declared in this scope
    wm.setConfigPortalTimeout(timeout);
    ^~
C:\Users\spvis\Documents\Arduino\KC868-A32\KC868-A32_Web_Server\KC868-A32_Web_Server.ino: In function 'void loop()':
KC868-A32_Web_Server:449:42: error: 'wm' was not declared in this scope
  if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}
                                          ^~
KC868-A32_Web_Server:449:61: error: 'ESP' was not declared in this scope
  if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}
                                                            ^~~
C:\Users\spvis\Documents\Arduino\KC868-A32\KC868-A32_Web_Server\KC868-A32_Web_Server.ino:449:61: note: suggested alternative: 'SP'
  if ( digitalRead(TRIGGER_PIN) == LOW) {wm.resetSettings();ESP.restart();}
                                                            ^~~
                                                            SP
exit status 1
'WiFiManager' does not name a type; did you mean 'WiFiManager_h'?

Print this item

  KC868-H32B Pro 32CH Smart Relay Module Released
Posted by: admin - 10-11-2022, 11:49 AM - Forum: News - No Replies

We have designed new 32 channel smart relay controller, it’s very powerful, it have 32 channel relay output, 6 digital input ports, ethernet + Tuya WiFi + ESP32 module, work with internet or without internet. support Tuya app, home assistant app, Google home app, Amazon alexa app, Apple homekit app, KinCony smart home app and KinCony KBOX app. KC868-H32B Pro can control by ESP32 write arduino code. We have made the arduino library for KC868-H32B Pro. it’s a good smart controller for home automation project.
 
[Image: details1_01.jpg][Image: details1_02.jpg]
[Image: details2_04.jpg]

Print this item

  ESPHone OTA fails to restart
Posted by: petez69 - 10-09-2022, 11:56 AM - Forum: KC868-A16 - Replies (7)

Hello

I have 3 KC868-A16 boards.  All ESPHome on Homeassistant.

1 board doesnt restart properly after config change and sending it via OTA.  Sometimes it restarts properly, sometimes no.

When it fails, both ethernet lights flash on and off.  Power cycle resets board and works fine.

When I delete board from ESPHome and add a new board, 1st time compile and OTA works ok.  If I add another PIN, recompile and send via OTA, both ethernet lights flash and I have to power cycle board.

I have tested changes, OTA and restarts on other 2 boards, works ok.

Can you please assist.....this is a new board.

Thankyou..Peter

Print this item

  Anyone want to buy 2x KC868-A16 Ethernet ESP32 Development Boards?
Posted by: davesmithr1 - 10-05-2022, 08:21 AM - Forum: DIY Project - Replies (6)

Anyone want to buy 2x KC868-A16 Ethernet ESP32 Development Boards?



Attached Files Thumbnail(s)
   
Print this item

  Need smart sound System
Posted by: engmohades - 10-04-2022, 06:49 PM - Forum: News - Replies (3)

Are there smart  sound System kincony

Or 
Can you suggest a compatible kincony smart sound System or home assistant ?

Print this item

  ESPHome Yaml file sample
Posted by: bal85 - 10-04-2022, 04:12 PM - Forum: KC868-AG / AG Pro / AG8 - Replies (1)

I am trying to setup my KC868-AG to receive both IR and RF. ( and later, send IR and RF). I can get it working with one or the other , but not both.
I believe I am missing something in the yaml file.
Would you have a sample yaml file that can do that ?
Thanks

Print this item

  absolute beginner
Posted by: hideki - 10-04-2022, 12:56 AM - Forum: KC868-A4 - Replies (3)

I bought this base.
The program will learn from this.
I want to build an environment for attachments.
Please guide me.



Please check pdf file. Smile



Attached Files
.pdf   hideki1.pdf (Size: 6.94 MB / Downloads: 414)
Print this item