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

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 5,367
» Latest member: talljane
» Forum threads: 2,574
» Forum posts: 13,309

Full Statistics

Online Users
There are currently 36 online users.
» 0 Member(s) | 28 Guest(s)
Crawl, Google, PetalBot, Yandex, bot

Latest Threads
How can I power multiple ...
Forum: KC868-A series and Uair Smart Controller
Last Post: ingersoj
39 minutes ago
» Replies: 9
» Views: 111
Problem with IFTTT automa...
Forum: "KCS" firmware system
Last Post: admin
1 hour ago
» Replies: 6
» Views: 42
A32 Pro ESPHome yaml incl...
Forum: KC868-A32/A32 Pro
Last Post: admin
2 hours ago
» Replies: 18
» Views: 183
KC868-A2 ESP32 I/O pin de...
Forum: KC868-A2
Last Post: admin
2 hours ago
» Replies: 8
» Views: 2,266
change wake up name
Forum: KinCony AS
Last Post: gal
Yesterday, 07:36 AM
» Replies: 12
» Views: 85
Need help with configurat...
Forum: KC868-HxB series Smart Controller
Last Post: admin
Yesterday, 04:32 AM
» Replies: 32
» Views: 394
ESP32 S3 set up issue
Forum: Extender module
Last Post: admin
12-17-2024, 11:43 PM
» Replies: 10
» Views: 68
KC868-A8 Schematic
Forum: KC868-A8
Last Post: admin
12-17-2024, 11:40 PM
» Replies: 7
» Views: 51
"KCS" v2.2.8 firmware BIN...
Forum: "KCS" firmware system
Last Post: admin
12-17-2024, 11:38 PM
» Replies: 2
» Views: 178
Dimensions/drawings of bo...
Forum: Schematic and diagram
Last Post: admin
12-17-2024, 11:37 PM
» Replies: 1
» Views: 23

  Kincony a-16 using esphome toggle relays at boot up time
Posted by: nikhuge - 09-27-2022, 01:59 PM - Forum: KC868-A16 - Replies (2)

I am using kincony a16 with 16 relay board with esphome running but at the boot time all the relays  toggle  and make the relays on and off ...is their a way to stop this in the software side..this is become a big problem for me due all the appliance connect to relays suddenly become active and then turn off at the a16 booting up....below is my config


Code:
substitutions:
  name: kc868-a16

esphome:
  name: ${name}
  name_add_mac_suffix: false
 
  project:
    name: kincony.kc868-a16
    version: "1"

esp32:
  board: esp32dev


web_server:
  port: 80

dashboard_import:
  package_import_url: github://hzkincony/kc868-a16/kc868-a16.yaml@main

ota:
  password: "1234"

wifi:
  ssid: "router"
  password: "1234"
  use_address: 192.168.8.114
  manual_ip:
    static_ip: 192.168.8.114
    gateway: 192.168.8.1
    subnet: 255.255.255.0
# Example configuration entry for ESP32
i2c:
  sda: 4
  scl: 5
  scan: false
  id: bus_a

# Example configuration entry
# ethernet:
#  type: LAN8720
#  mdc_pin: GPIO23
#  mdio_pin: GPIO18
#  clk_mode: GPIO17_OUT
#  phy_addr: 0

# Example configuration entry
pcf8574:
  - id: "pcf8574_hub_out_1" # for output channel 1-8
    address: 0x24

  - id: "pcf8574_hub_out_2" # for output channel 9-16
    address: 0x25

  - id: "pcf8574_hub_in_1" # for input channel 1-8
    address: 0x21

  - id: "pcf8574_hub_in_2" # for input channel 9-16
    address: 0x22



# Individual outputs
switch:

  - platform: gpio
    name: "light2"
    id: light2
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 1
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light3"
    id: light3
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 2
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light4"
    id: light4
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 3
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light5"
    id: light5
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 4
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light6"
    id: light6
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 5
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light7"
    id: light7
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 6
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light8"
    id: light8
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 7
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light9"
    id: light9
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 0
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light10"
    id: light10
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 1
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light11"
    id: light11
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 2
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light12"
    id: light12
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 3
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light13"
    id: light13
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 4
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light14"
    id: light14
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 5
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light15"
    id: light15
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 6
      mode: OUTPUT
      inverted: false

  - platform: gpio
    name: "light16"
    id: light16
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 7
      mode: OUTPUT
      inverted: false

Print this item

  Nextion display work with KC868-A6
Posted by: admin - 09-27-2022, 05:34 AM - Forum: KC868-A6 - Replies (9)

i have used on board extend serial port. actually use want to use VCC,GND,RXD,TXD for Nextion display.

define RXD:GPIO13 TXD:GPIO12 by ESP32

weld Vcc 5V from LM1117 output pin.

here are some photos:

   
   
   

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

firstly create two buttons UI by Nextion Editor PC software:
[Image: attachment.php?aid=1429]   
[Image: attachment.php?aid=1430]   

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


.zip   nextion_KC868-A6.ino.nodemcu-32s.zip (Size: 108.86 KB / Downloads: 266)
here are source code:

Code:
#include "PCF8574.h"

String indata="";
String val="";

// Set i2c address
PCF8574 pcf8574(0x24);

void setup() {

  Serial1.begin(9600,SERIAL_8N1,13,12);
  Serial.begin(9600);
  pcf8574.pinMode(P0, OUTPUT);
  pcf8574.pinMode(P1, OUTPUT);
  pcf8574.pinMode(P2, OUTPUT);
  pcf8574.pinMode(P3, OUTPUT);
  pcf8574.pinMode(P4, OUTPUT);
  pcf8574.pinMode(P5, OUTPUT);

  Serial.print("Init pcf8574...");
  if (pcf8574.begin()){
    Serial.println("OK");
  }else{
    Serial.println("KO");
  } 
  pcf8574.digitalWrite(P0, HIGH);
  pcf8574.digitalWrite(P1, HIGH);
  pcf8574.digitalWrite(P2, HIGH);
  pcf8574.digitalWrite(P3, HIGH);
  pcf8574.digitalWrite(P4, HIGH);
  pcf8574.digitalWrite(P5, HIGH);
}

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

Print this item

  KC868-H32B V4.45 new firmware update
Posted by: admin - 09-27-2022, 12:36 AM - Forum: News - No Replies


improvement:
support set "interlock" mode for motor curtain. Because it make the logical by hardware, so it will be safety. when set relay in "interlock" mode, in group relay will never ON together at the same time.
firmware download link:

.zip   Relay32_V445_220925.zip (Size: 49.51 KB / Downloads: 239)

   
   

Print this item

  KC868-E16T work with contact and wall switch by Tuya app
Posted by: admin - 09-27-2022, 12:23 AM - Forum: Schematic and diagram - No Replies

   

Print this item

  KC868-E16T demo configure for ESPhome
Posted by: admin - 09-26-2022, 02:03 PM - Forum: KC868-E16T - No Replies


.zip   Made_for_ESPHome_KC868-E16T.zip (Size: 887 bytes / Downloads: 346)

esphome:
  name: kc868-e16t
  platform: ESP32
  board: esp32dev
 
 
# Example configuration entry for ESP32
i2c:
  sda: 16
  scl: 15
  scan: true
  id: bus_a


# Example configuration entry
ethernet:
  type: LAN8720
  mdc_pin: GPIO23
  mdio_pin: GPIO18
  clk_mode: GPIO17_OUT
  phy_addr: 0

  # Optional manual IP
#  manual_ip:
#    static_ip: 192.168.1.199
#    gateway: 192.168.1.1
#    subnet: 255.255.255.0 


# Example configuration entry
pcf8574:
  - id: 'pcf8574_hub_out_1'  # for output channel 1-8
    address: 0x21

  - id: 'pcf8574_hub_out_2'  # for output channel 9-16
    address: 0x25

  - id: 'pcf8574_hub_in_1'  # for input channel 1-8
    address: 0x22

  - id: 'pcf8574_hub_in_2'  # for input channel 9-16
    address: 0x24

# Individual outputs
switch:
  - platform: gpio
    name: "e16t-output1"
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 0
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "e16t-output2"
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 1
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output3"
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 2
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output4"
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 3
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output5"
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 4
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output6"
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 5
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "e16t-output7"
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 6
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output8"
    pin:
      pcf8574: pcf8574_hub_out_1
      number: 7
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output9"
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 0
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "e16t-output10"
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 1
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "e16t-output11"
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 2
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output12"
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 3
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output13"
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 4
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output14"
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 5
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output15"
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 6
      mode: OUTPUT
      inverted: true
     
  - platform: gpio
    name: "e16t-output16"
    pin:
      pcf8574: pcf8574_hub_out_2
      number: 7
      mode: OUTPUT
      inverted: true
     
binary_sensor:
  - platform: gpio
    name: "e16t-input1"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 0
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input2"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 1
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input3"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 2
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input4"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 3
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input5"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 4
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input6"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 5
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input7"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 6
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input8"
    pin:
      pcf8574: pcf8574_hub_in_1
      number: 7
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input9"
    pin:
      pcf8574: pcf8574_hub_in_2
      number: 0
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input10"
    pin:
      pcf8574: pcf8574_hub_in_2
      number: 1
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input11"
    pin:
      pcf8574: pcf8574_hub_in_2
      number: 2
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input12"
    pin:
      pcf8574: pcf8574_hub_in_2
      number: 3
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input13"
    pin:
      pcf8574: pcf8574_hub_in_2
      number: 4
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input14"
    pin:
      pcf8574: pcf8574_hub_in_2
      number: 5
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input15"
    pin:
      pcf8574: pcf8574_hub_in_2
      number: 6
      mode: INPUT
      inverted: true

  - platform: gpio
    name: "e16t-input16"
    pin:
      pcf8574: pcf8574_hub_in_2
      number: 7
      mode: INPUT
      inverted: true

# Enable logging
logger:

# Enable Home Assistant API
api:

Print this item

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

ANALOG_A1  GPIO36
ANALOG_A2  GPIO35
ANALOG_A3  GPIO34
ANALOG_A4  GPIO39

IIC SDA:GPIO16
IIC SCL:GPIO15

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

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

RF433MHz wireless receiver: GPIO4

BEEP:GPIO2

Ethernet (LAN8720) I/O define:

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

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

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

Print this item

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

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

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

here is arduino source code download:

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

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

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

TuyaWifi my_device;

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


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



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

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

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

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

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

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

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

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

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

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

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

unsigned char ini_state = 1;

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



#define BEGIN_DELAY   1
#define END_DELAY   0

/* Data point define */

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

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

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


#define LED_WiFi  26
#define LED_User  33


/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type.
*                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP
*/
unsigned char dp_array[][2] =
{
  {DPID_SWITCH_1, DP_TYPE_BOOL},{DPID_SWITCH_2, DP_TYPE_BOOL},{DPID_SWITCH_3, DP_TYPE_BOOL},{DPID_SWITCH_4, DP_TYPE_BOOL},{DPID_SWITCH_5, DP_TYPE_BOOL},{DPID_SWITCH_6, DP_TYPE_BOOL},
  {DPID_INPUT_1, DP_TYPE_BOOL},{DPID_INPUT_2, DP_TYPE_BOOL},{DPID_INPUT_3, DP_TYPE_BOOL},{DPID_INPUT_4, DP_TYPE_BOOL},{DPID_INPUT_5, DP_TYPE_BOOL},{DPID_INPUT_6, DP_TYPE_BOOL},
  {DPID_SWITCH7, DP_TYPE_BOOL},{DPID_SWITCH8, DP_TYPE_BOOL},{DPID_SWITCH9, DP_TYPE_BOOL},{DPID_SWITCH10, DP_TYPE_BOOL},{DPID_SWITCH11, DP_TYPE_BOOL},{DPID_SWITCH12, DP_TYPE_BOOL},
  {DPID_SWITCH13, DP_TYPE_BOOL},{DPID_SWITCH14, DP_TYPE_BOOL},{DPID_SWITCH15, DP_TYPE_BOOL},{DPID_SWITCH16, DP_TYPE_BOOL},{DPID_SWITCH17, DP_TYPE_BOOL},{DPID_SWITCH18, DP_TYPE_BOOL},
  {DPID_SWITCH19, DP_TYPE_BOOL},{DPID_SWITCH20, DP_TYPE_BOOL},{DPID_SWITCH21, DP_TYPE_BOOL},{DPID_SWITCH22, DP_TYPE_BOOL},{DPID_SWITCH23, DP_TYPE_BOOL},{DPID_SWITCH24, DP_TYPE_BOOL},
  {DPID_SWITCH25, DP_TYPE_BOOL},{DPID_SWITCH26, DP_TYPE_BOOL},{DPID_SWITCH27, DP_TYPE_BOOL},{DPID_SWITCH28, DP_TYPE_BOOL},{DPID_SWITCH29, DP_TYPE_BOOL},{DPID_SWITCH30, DP_TYPE_BOOL},
{DPID_SWITCH31, DP_TYPE_BOOL},{DPID_SWITCH32, DP_TYPE_BOOL},
};


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

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

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

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

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

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

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

  // uart_state = RS485_state;

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

  last_time = millis();
}

void loop()
{

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

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

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

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

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

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

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

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

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

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

        if(ini_state==1)
        {
         ini_state=0 ;
         switch_8_1_tarstate = switch_8_1_nowstate;
         switch_16_9_tarstate = switch_16_9_nowstate;
         switch_24_17_tarstate = switch_24_17_nowstate;
         switch_32_25_tarstate = switch_32_25_nowstate;
        }

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

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

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

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

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

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

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

        RS232_Check = ENABLE ;
      }     

  }   

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

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

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

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

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

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

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

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

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

        if(ini_state==1)
        {
         ini_state=0 ;
         switch_8_1_tarstate = switch_8_1_nowstate;
         switch_16_9_tarstate = switch_16_9_nowstate;
         switch_24_17_tarstate = switch_24_17_nowstate;
         switch_32_25_tarstate = switch_32_25_nowstate;
        }

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

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

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

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

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

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

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

   my_device.uart_service();

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

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

  if(uart_state == RS232_state)
  {
    if ((pre_switch_8_1_nowstate != switch_8_1_nowstate) || (pre_switch_16_9_nowstate != switch_16_9_nowstate) || (pre_switch_24_17_nowstate != switch_24_17_nowstate) || (pre_switch_32_25_nowstate != switch_32_25_nowstate))
    {
      pre_switch_8_1_nowstate = switch_8_1_nowstate;
      pre_switch_16_9_nowstate = switch_16_9_nowstate;
      pre_switch_24_17_nowstate = switch_24_17_nowstate;
      pre_switch_32_25_nowstate = switch_32_25_nowstate;

      switch_8_1_tarstate = switch_8_1_nowstate ;
      switch_16_9_tarstate = switch_16_9_nowstate ;
      switch_24_17_tarstate = switch_24_17_nowstate ;
      switch_32_25_tarstate = switch_32_25_nowstate ;
     
    }
    else if ((switch_8_1_tarstate != switch_8_1_nowstate) || (switch_16_9_tarstate != switch_16_9_nowstate) || (switch_24_17_tarstate != switch_24_17_nowstate) || (switch_32_25_tarstate != switch_32_25_nowstate))
    {
    if (millis()- RS232_relay_last_time >= RS232RelayDelay)
    {
      RS232_Check = DISABLE ;

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

  if(uart_state == RS485_state)
  {
    if ((pre_switch_8_1_nowstate != switch_8_1_nowstate) || (pre_switch_16_9_nowstate != switch_16_9_nowstate) || (pre_switch_24_17_nowstate != switch_24_17_nowstate) || (pre_switch_32_25_nowstate != switch_32_25_nowstate))
    {
      pre_switch_8_1_nowstate = switch_8_1_nowstate;
      pre_switch_16_9_nowstate = switch_16_9_nowstate;
      pre_switch_24_17_nowstate = switch_24_17_nowstate;
      pre_switch_32_25_nowstate = switch_32_25_nowstate;

      switch_8_1_tarstate = switch_8_1_nowstate ;
      switch_16_9_tarstate = switch_16_9_nowstate ;
      switch_24_17_tarstate = switch_24_17_nowstate ;
      switch_32_25_tarstate = switch_32_25_nowstate ;
     
    }
    else if ((switch_8_1_tarstate != switch_8_1_nowstate) || (switch_16_9_tarstate != switch_16_9_nowstate) || (switch_24_17_tarstate != switch_24_17_nowstate) || (switch_32_25_tarstate != switch_32_25_nowstate))
    {
    if (millis()- RS485_relay_last_time >= RS485RelayDelay)
    {
      RS485_Check = DISABLE ;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

     RS485_read_com = RS485_read_relaycom;
     }
    }
  }
 



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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


 
  return TY_SUCCESS;
}

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

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

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

Print this item

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


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

Print this item

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

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

Print this item

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

Hi,

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

GM

Print this item