#define BLINKER_WIFI
#define BLINKER_DUEROS_OUTLET
#include <Blinker.h>
#include <Servo.h>
char auth[] = "86c8b6acd763";
char ssid[] = "zcc";
char pswd[] = "1234567890";
BlinkerButton Button1("ddl1");
Servo servo_00;
bool oState = false;
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
BLINKER_LOG("Toggle on!");
servo_00.write(60);
delay(1);
delay(1000);
servo_00.write(95);
Button1.color("#7CFC00");
Button1.print("on");
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("Toggle off!");
servo_00.write(130);
delay(1);
delay(1000);
servo_00.write(95);
Button1.color("#FF4500");
Button1.print("off");
oState = false;
}
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.report();
}
void duerPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
servo_00.write(60);
delay(1);
delay(1000);
servo_00.write(95);
BlinkerDuerOS.powerState("on");
BlinkerDuerOS.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
servo_00.write(130);
delay(1);
delay(1000);
servo_00.write(95);
BlinkerDuerOS.powerState("off");
BlinkerDuerOS.print();
oState = false;
}
}
void duerQuery(int32_t queryCode)
{
BLINKER_LOG("DuerOS Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("DuerOS Query power state");
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
break;
case BLINKER_CMD_QUERY_TIME_NUMBER :
BLINKER_LOG("DuerOS Query time");
BlinkerDuerOS.time(millis());
BlinkerDuerOS.print();
break;
default :
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
break;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
BlinkerDuerOS.powerState("on");
BlinkerDuerOS.report();
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
servo_00.attach(0);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerDuerOS.attachPowerState(duerPowerState);
BlinkerDuerOS.attachQuery(duerQuery);
Button1.attach(button1_callback);
}
void loop()
{
Blinker.run();
}