#define BLINKER_WIFI
#include <Blinker.h>
char auth[] = "f71c23621ed1";
char ssid[] = "webserver";
char pswd[] = "www.360xyz.cn";
int dgxd = 0;

BlinkerButton Button1("btn-z2f");  //上升
BlinkerButton Button2("btn-biu");  //停止
BlinkerButton Button3("btn-3l9");  //下降
BlinkerButton Button4("btn-wmf");  //刷新温度湿度
BlinkerButton Button5("btn-u8w");  //广告开关

BlinkerNumber Number1("num-du7");  //温度
BlinkerNumber Number2("num-0j1");  //湿度

BlinkerText Text1("tex-je9");
BlinkerText Text2("tex-08u");  //广告牌状态
//上升
void button1_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  Text1.print("上升");
  String tstr = "mydoor1.↑";
  Serial.write(tstr.c_str(), tstr.length());
}
//停止
void button2_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  //如果已经在上限或下限时只更新响应时间
  Text1.print("停止");
  String tstr = "mydoor1.■";
  Serial.write(tstr.c_str(), tstr.length());
}
//下降
void button3_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  Text1.print("下降");
  String tstr = "mydoor1.↓";
  Serial.write(tstr.c_str(), tstr.length());
}
void button4_callback(const String& state) {
 // BLINKER_LOG("get button state: ", state);
  // 获取温度事件并打印其值.
  String tstr = "mydoor1.温湿度";
  Serial.write(tstr.c_str(), tstr.length());
  //Number1.print(temperature);
  //Number2.print(humidity);
}
void button5_callback(const String& state) {
  //BLINKER_LOG("get button state: ", state);
  if (dgxd == 0) {
    Text2.print("开");
    String tstr = "mydoor1.灯箱开";
    Serial.write(tstr.c_str(), tstr.length());
  } else {
    Text2.print("关");
    String tstr = "mydoor1.灯箱关";
    Serial.write(tstr.c_str(), tstr.length());
  }
  //限位开关导通时为低电平表明还没有达到限位,并且没到达到下降最大限制  能正常下降,断开为高电平表明达到限位了
}

void setup() {
  // 初始化串口
  Serial.begin(115200);
  //BLINKER_DEBUG.stream(Serial);
  //BLINKER_DEBUG.debugAll();
  // 初始化blinker
  Blinker.begin(auth, ssid, pswd);

  //Blinker.attachData(dataRead);
  Button1.attach(button1_callback);
  Button2.attach(button2_callback);
  Button3.attach(button3_callback);
  Button4.attach(button4_callback);
  Button5.attach(button5_callback);
}

void loop() {
  Blinker.run();
  if (Serial.available()) {                // 检查是否有可用的数据
    String message = Serial.readString();  // 读取接收到的字符串
    if (message.indexOf("°C") != -1) {
      String temperature = message.substring(0, 4);
      Number1.print(temperature.toFloat());
      String humidity = message.substring(9);
      Number2.print(humidity.toInt());
    } else {
      if (message.indexOf("灯箱") != -1) {
        if (message.indexOf("灯箱开") != -1) {
          dgxd = 1;
          Text1.print(message);
        } else {
          dgxd = 0;
          Text1.print(message);
        }
      }

      Text1.print(message);
    }
    Serial.println("Received: " + message);  // 打印接收到的字符串
  }
}
