/* *****************************************************************
 *
 * Download latest Blinker library here:
 * https://github.com/blinker-iot/blinker-library/archive/master.zip
 * 
 * 
 * Blinker is a cross-hardware, cross-platform solution for the IoT. 
 * It provides APP, device and server support, 
 * and uses public cloud services for data transmission and storage.
 * It can be used in smart home, data monitoring and other fields 
 * to help users build Internet of Things projects better and faster.
 * 
 * Make sure installed 2.7.4 or later ESP8266/Arduino package,
 * if use ESP8266 with Blinker.
 * https://github.com/esp8266/Arduino/releases
 * 
 * Make sure installed 1.0.5 or later ESP32/Arduino package,
 * if use ESP32 with Blinker.
 * https://github.com/espressif/arduino-esp32/releases
 * 
 * Docs: https://diandeng.tech/doc
 *       
 * 
 * *****************************************************************
 * 
 * Blinker 库下载地址:
 * https://github.com/blinker-iot/blinker-library/archive/master.zip
 * 
 * Blinker 是一套跨硬件、跨平台的物联网解决方案，提供APP端、设备端、
 * 服务器端支持，使用公有云服务进行数据传输存储。可用于智能家居、
 * 数据监测等领域，可以帮助用户更好更快地搭建物联网项目。
 * 
 * 如果使用 ESP8266 接入 Blinker,
 * 请确保安装了 2.7.4 或更新的 ESP8266/Arduino 支持包。
 * https://github.com/esp8266/Arduino/releases
 * 
 * 如果使用 ESP32 接入 Blinker,
 * 请确保安装了 1.0.5 或更新的 ESP32/Arduino 支持包。
 * https://github.com/espressif/arduino-esp32/releases
 * 
 * 文档: https://diandeng.tech/doc
 *       
 * 
 * *****************************************************************/

#define BLINKER_WIFI

#include <Blinker.h>
#include "ESP32_CAM_SERVER.h"

char ssid[] = "webserver";
char wifipwd[] = "www.360xyz.cn";
char auth[] = "7eb873d19e73";
int D12 = 12;
int D13 = 13;
int D15 = 15;
int D14 = 14;
BlinkerButton Button1("btn-xyb");  //
BlinkerButton Button2("btn-902");  //
BlinkerButton Button3("btn-fmc");  //
BlinkerButton Button4("btn-126");  //

bool d1 = false;
bool d2 = false;
bool d5 = false;
bool d6 = false;
bool setup_camera = false;

void dataRead(const String& data) {
  BLINKER_LOG("Blinker readString: ", data);

  Blinker.vibrate();

  uint32_t BlinkerTime = millis();

  Blinker.print("millis", BlinkerTime);
}

void setup() {
  pinMode(D12, OUTPUT);
  pinMode(D13, OUTPUT);
  pinMode(D15, OUTPUT);
  pinMode(D14, OUTPUT);
  digitalWrite(D12, LOW);
  digitalWrite(D13, LOW);
  digitalWrite(D15, LOW);
  digitalWrite(D14, 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);
}

void loop() {
  Blinker.run();

  if (Blinker.connected() && !setup_camera) {
    setupCamera();
    setup_camera = true;

    Blinker.printObject("video", "{\"str\":\"mjpg\",\"url\":\"http://" + WiFi.localIP().toString() + "\"}");
  }
}
void button1_callback(const String& state) {
  if (d1 == true) {
    d1 = false;
    digitalWrite(D12, LOW);
    Button1.color("#000000");
    Button1.text("关");
  } else {
    d1 = true;
    digitalWrite(D12, HIGH);
    Button1.color("#00FF00");
    Button1.text("开");
  }
  Button1.print();
}
void button2_callback(const String& state) {
  if (d2 == true) {
    d2 = false;
    digitalWrite(D13, LOW);
    Button2.color("#000000");
    Button2.text("关");
  } else {
    d2 = true;
    digitalWrite(D13, HIGH);
    Button2.color("#00FF00");
    Button2.text("开");
  }
  Button2.print();
}
void button3_callback(const String& state) {
  if (d5 == true) {
    d5 = false;
    digitalWrite(D15, LOW);
    Button3.color("#000000");
    Button3.text("关");
  } else {
    d5 = true;
    digitalWrite(D15, HIGH);
    Button3.color("#00FF00");
    Button3.text("开");
  }
  Button3.print();
}
void button4_callback(const String& state) {
  if (d6 == true) {
    d6 = false;
    digitalWrite(D14, LOW);
    Button4.color("#000000");
    Button4.text("关");
  } else {
    d6 = true;
    digitalWrite(D14, HIGH);
    Button4.color("#00FF00");
    Button4.text("开");
  }
  Button4.print();
}
