#define BLINKER_WIFI
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Blinker.h>
#include <DHT.h>
#include <DHT_U.h>
char ssid[] = "webserver";
char wifipwd[] = "www.360xyz.cn";
char auth[] = "7eb873d19e73";

int DD1 = 5;
int DD2 = 4;
int DD5 = 14;
int DD6 = 12;
bool bl1 = false;
bool bl2 = false;
bool bl3 = false;
bool bl4 = false;
BlinkerButton Button1("btn-xyb");  //
BlinkerButton Button2("btn-902");  //
BlinkerButton Button3("btn-fmc");  //
BlinkerButton Button4("btn-126");  //
BlinkerButton Button5("btn-7se");  //
float temperature = 0;
int humidity = 0;
BlinkerNumber Number1("num-1e7");  //温
BlinkerNumber Number2("num-wlj");  //湿度

#define DHTPIN 13
#define DHTTYPE DHT11  // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
#define d7 13
OneWire oneWire(d7);
void setup() {
  pinMode(DD1, OUTPUT);
  pinMode(DD2, OUTPUT);
  pinMode(DD5, OUTPUT);
  pinMode(DD6, OUTPUT);
  digitalWrite(DD1, LOW);
  digitalWrite(DD2, LOW);
  digitalWrite(DD5, LOW);
  digitalWrite(DD6, LOW);
  Serial.begin(115200);
  BLINKER_DEBUG.stream(Serial);

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

  Blinker.begin(auth, ssid, wifipwd);
  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();
}
void button1_callback(const String& state) {
  if (bl1 == true) {
    bl1 = false;
    digitalWrite(DD1, LOW);
    Button1.color("#000000");
    Button1.text("关");
  } else {
    bl1 = true;
    digitalWrite(DD1, HIGH);
    Button1.color("#00FF00");
    Button1.text("开");
  }
  Button1.print();
}
void button2_callback(const String& state) {
  if (bl2 == true) {
    bl2 = false;
    digitalWrite(DD2, LOW);
    Button2.color("#000000");
    Button2.text("关");
  } else {
    bl2 = true;
    digitalWrite(DD2, HIGH);
    Button2.color("#00FF00");
    Button2.text("开");
  }
  Button2.print();
}
void button3_callback(const String& state) {
  if (bl3 == true) {
    bl3 = false;
    digitalWrite(DD5, LOW);
    Button3.color("#000000");
    Button3.text("关");
  } else {
    bl3 = true;
    digitalWrite(DD5, HIGH);
    Button3.color("#00FF00");
    Button3.text("开");
  }
  Button3.print();
}
void button4_callback(const String& state) {
  if (bl4 == true) {
    bl4 = false;
    digitalWrite(DD6, LOW);
    Button4.color("#000000");
    Button4.text("关");
  } else {
    bl4 = true;
    digitalWrite(DD6, HIGH);
    Button4.color("#00FF00");
    Button4.text("开");
  }
  Button4.print();
}
void button5_callback(const String& state) {
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  } else {
    temperature = event.temperature;
  }
  delay(500);
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  } else {
    humidity = event.relative_humidity;
  }
  Number1.print(temperature);
  Number2.print(humidity);
}
void dataRead(const String& data) {
  BLINKER_LOG("Blinker readString: ", data);
}