/*
 * @Author: dugulingping
 * @Date: 2022-06-09 17:14:49
 * @LastEditTime: 2022-06-17 13:21:04
 * @LastEditors: dugulingping
 * @Description: 
 * @FilePath: \project\BaFa_lutai_switch\BaFa_lutai_switch.ino
 */
#include <ESP8266WiFi.h>
#include "PubSubClient.h"

// 引脚定义
#define LED_LOCAL 2
#define SWITCH_PIN 0

void turnOn();
void turnOff();

const char* ssid = "mdd520";           //修改，你的路由去WIFI名字
const char* password = "mj1688888888";     //你的WIFI密码
#define ID_MQTT  "ba6c540974354e998818b47472a8407d"     //用户私钥，控制台获取
const char*  topic = "lutaiValveSwitch006";     //主题名字

const char* mqtt_server = "bemfa.com"; //默认，MQTT服务器
const int mqtt_server_port = 9501;      //默认，MQTT服务器


WiFiClient espClient;
PubSubClient client(espClient);

void Bink_led(int ms){
    Serial.print(">");
    digitalWrite(LED_LOCAL, 0);
    delay(ms);
    digitalWrite(LED_LOCAL, 1);
    delay(ms);
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Bink_led(100);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print("Topic:");
    Serial.println(topic);
    String msg = "";
    for (int i = 0; i < length; i++) {
        msg += (char)payload[i];
    }
    Serial.print("Msg:");
    Serial.println(msg);
    if (msg == "on") {        //on
        turnOn();                   //开
    } else if (msg == "off") {    //off
        turnOff();               //关
    }
    msg = "";
}

void reconnect() {
    int num = 0;
    // Loop until we're reconnected
    while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        Bink_led(500);
        // Attempt to connect
        if (client.connect(ID_MQTT)) {
            Serial.println("connected");
            Serial.print("subscribe:");
            Serial.println(topic);
            //订阅主题，如果需要订阅多个主题，
            //可发送多条订阅指令client.subscribe(topic2);client.subscribe(topic3);
            client.subscribe(topic);
        }
        else if(num >= 100){
            delay(5000);
            ESP.restart();
        } else {
            Serial.print("failed, rc=");
            Serial.print(client.state());
            Serial.println(" try again in 5 seconds");
            delay(5000);
        }
        num++;
    }
}

//开
void turnOn() {
    Serial.println("turn on swich");
    digitalWrite(SWITCH_PIN, LOW);
}
//关
void turnOff() {
    Serial.println("turn off swich");
    digitalWrite(SWITCH_PIN, HIGH);
}


void setup() {
    Serial.begin(115200);
    pinMode(LED_LOCAL, OUTPUT);
    pinMode(SWITCH_PIN, OUTPUT);
    delay(500);
    turnOff();      // 关闭
    setup_wifi();
    client.setServer(mqtt_server, mqtt_server_port);//设置mqtt服务器
    client.setCallback(callback); //mqtt消息处理
    
}

void loop() {
    if (WiFi.status() != WL_CONNECTED) {
        setup_wifi();
        reconnect();
    }
    if (!client.connected()){
        reconnect();
    }
    client.loop();

}