#include <ESP8266MQTT.h>
//#include <ESP8266WiFi.h>
//#include <PubSubClient.h>
#include <ArduinoJson.h>
int open = 5;
int close = 4;

int d5 = 14;
int d6 = 12;

//开关状态
String doorstate;

int i = 0;

//网络连通 y否则n
String network = "y";

void onConnectionEstablished();

ESP8266MQTT client(
  "www.360xyz.cn",             // Wifi ssid
  "qq282246101",             // Wifi password
  "WO9H67HZ0V.iotcloud.tencentdevices.com",    // MQTT broker ip
  1883,               // MQTT broker port
  "WO9H67HZ0Vdoor1_kfb;12010126;8X9SI;1642803630",              // MQTT username
  "5582a4e48efbebc6748cbbe2d57a44d1041ff1fb8201af52a41babc7c2a08488;hmacsha256",         // MQTT password
  "mydoor1",            // Client name
  onConnectionEstablished, // Connection established callback
  true,               // Enable web updater
  true                // Enable debug messages
);

void setup() {
  Serial.begin(19200);
  pinMode(open, OUTPUT);
  pinMode(close, OUTPUT);

  //pinMode(stop, OUTPUT);
  pinMode(d5, INPUT_PULLUP);
  pinMode(d6, INPUT_PULLUP);
  digitalWrite(open, LOW);
  digitalWrite(close, LOW);

}

void onConnectionEstablished() {
  // 订阅主题并且将该主题内收到的消息通过串口发送
  client.subscribe("WO9H67HZ0V/door1_kfb/control", [](const String & payload) {
    Serial.println(payload);
    DynamicJsonDocument  jsonBuffer(200);
    deserializeJson(jsonBuffer, payload);
    JsonObject root = jsonBuffer.as<JsonObject>();
    String user = root["user"];
    String state = root["state"];
    //限位开关导通时为低电平表明还没有达到限位 能正常下降,断开为高电平表明达到限位了
    if (digitalRead(d6) == LOW) {
      if (state == "0" && state != "max_down") {
        //关 下降
        Serial.println("关 下降");

        digitalWrite(open, LOW);
        delay(200);
        digitalWrite(close, HIGH);
        //腾讯云 设置好了发布必须为json
        client.publish("WO9H67HZ0V/door1_kfb/event", payload);
      }
    }
    //限位开关导通为低电平 表明还没有达到限位 能正常上升,断开为高电平表明达到限位了
    if (digitalRead(d5) == LOW) {
      if (state == "1" && state != "max_up") {
        //开 上升
        Serial.println("开 上升");

        digitalWrite(close, LOW);
        delay(200);
        digitalWrite(open, HIGH);
        client.publish("WO9H67HZ0V/door1_kfb/event", payload);
      }
    }

    if (state == "2") {
      //停
      //每次状态改变的第一次执行 向服务器返回操作成功响应
      //如果已经在上限或下限时只更新响应时间
      if (state == "max_down" || state == "max_up") {
        if (state == "max_down") {
          Serial.println("max_down 停");

          delay(200);
          // 向某个主题发送消息
          client.publish("WO9H67HZ0V/door1_kfb/event", "max_down");
        } else {
          Serial.println("max_up 停");

          delay(200);
          // 向某个主题发送消息
          client.publish("WO9H67HZ0V/door1_kfb/event", "max_up");
        }

      } else {
        Serial.println("停");

        digitalWrite(open, LOW);
        digitalWrite(close, LOW);
        delay(200);
        // 向某个主题发送消息
        client.publish("WO9H67HZ0V/door1_kfb/event", payload);
      }
    }

    //硬重启系统(系统运行太久定期重启)
    if (state == "9") {
      client.publish("UDUWVDFKFZ/mydoor1/event", "restart exec ok");
      ESP.reset();
    }

    //Serial.println(payload);//此处可以编写一个函数来代替
  });

}

void loop() {
  client.loop();

  //操作运行时 上升达到限位了,限位开关断开
  if (digitalRead(d5) == HIGH && doorstate != "max_up") {
    //Serial.println("上升达到限位");
    digitalWrite(open, LOW);
    delay(500);
    doorstate = "max_up";
  }
  //操作运行时 下降达到限位了,限位开关开路
  if (digitalRead(d6) == HIGH && doorstate != "max_down") {
    //Serial.println("下降达到限位");
    digitalWrite(close, LOW);
    delay(500);
    doorstate = "max_down";
  }
  //即没有上限也没有下限状态
  if (digitalRead(d5) == LOW && digitalRead(d6) == LOW) {
    doorstate = "";
  }


}