 uint16_t g_body_len = 0;
 uint16_t g_recv_len = 0;
 
-char g_status[CHILD_NUM];
+Child *g_child[CHILD_NUM];
 
 char g_need_report = 1;
 unsigned long g_check_ms = 0;
+
+
+class Child {
+private:
+  int pin_ctrl; /* 控制继电器 */
+  int pin_led; /* 控制开关的指示LED */
+  int pin_pwm; /* 输出PWM给触摸面板 */
+  int pin_touch; /* 接受来自触摸面板的开关信号  */
+
+  int ctrl_value; /* 当前继电器状态 */
+
+  int touch_value; /* 当前触摸面板认为的开关状态 */
+  int low_cnt; /* 触摸面板开关信号为0的计数, 当超过阈值, 认为是关信号(因为面板输出不稳定的脉冲) */
+
+public:
+  Child(int pin_ctrl, int pin_led, int pin_pwm, int pin_touch) {
+    this->pin_ctrl = pin_ctrl;
+    this->pin_led = pin_led;
+    this->pin_pwm = pin_pwm;
+    this->pin_touch = pin_touch;
+
+    pinMode(this->pin_ctrl, OUTPUT);
+    digitalWrite(this->pin_ctrl, LOW);
+
+    pinMode(this->pin_led, INPUT);
+
+    SetPinFrequencySafe(this->pin_pwm, 50);
+    pwmWrite(this->pin_pwm, 127);
+
+    pinMode(this->pin_touch, INPUT);
+
+    this->ctrl_value = 0;
+    this->touch_value = 0;
+    this->low_cnt = 0;
+  }
+
+  void loop() {
+    int value = check_touch();
+    if (this->touch_value != value) {
+      Serial.println("touch signal!");
+      SetCtrl(!ctrl_value);
+      this->touch_value = value;
+      g_need_report = 1;
+    }
+  }
+
+  int check_touch() {
+    int i = 0;
+    i = analogRead(this->pin_touch);
+  
+    if (i < MAX_LOW_ANNALOG) {
+      ++this->low_cnt;
+      if (this->low_cnt >= MAX_LOW_CNT) {
+        this->low_cnt = 0;
+        return 0;
+      }
+    } else {
+      this->low_cnt = 0;
+      return 1;
+    }
+    return this->touch_value; 
+  }
+
+  int GetCtrl() {
+    return this->ctrl_value;
+  }
+
+  void SetCtrl(int value) {
+    Serial.println("set ctrl:");
+    Serial.println(value);
+    if (value) {
+      value = 1;
+    }
+    if (value == this->ctrl_value) {
+      return;
+    }
+    this->ctrl_value = value;
+    if (value) {
+      digitalWrite(this->pin_ctrl, HIGH);
+      pinMode(this->pin_led, INPUT);
+      digitalWrite(this->pin_led, LOW);
+    } else {
+      digitalWrite(this->pin_ctrl, LOW);
+      pinMode(this->pin_led, INPUT);
+    }
+  }
+};
+
 
 void recv_header() {
   while (Serial.available() >= HEADER_LEN) {

/* serial data format:
 *  header:
 *    flag: 1 byte, 0xaa, filter out debug message
 *    type: 1 bytes, 1: upstream, 2: downstream
 *    body length: 2 bytes, network byte order
 *  body:
 *    json data from yunba service
 */ 
#include <ArduinoJson.h>

#define MSG_TYPE_UP 0x01
#define MSG_TYPE_DOWN 0x02

  g_buf[3] = ((uint8_t *)&g_body_len)[0];

  Serial.write(g_buf, g_body_len + HEADER_LEN);
  ///  Serial.flush();
}

void report_status() {
