-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMi-Light.ino
118 lines (101 loc) · 2.79 KB
/
Mi-Light.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
#include <Servo.h>
#define PIN_SERVO D4
Servo myservo;
char auth[] = "Blinker Key";
char ssid[] = "Wifi name";
char pswd[] = "wifi key";
bool oState = false;
BlinkerButton Button1("Light");
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
myservo.write(32);
digitalWrite(D3, HIGH);
Button1.text("灯已开启");
delay(500);
digitalWrite(D3, LOW);
myservo.write(90);
Button1.print("on");
}
else if (state == BLINKER_CMD_OFF) {
myservo.write(134);
digitalWrite(D3, HIGH);
Button1.text("灯已关闭");
delay(500);
digitalWrite(D3, LOW);
myservo.write(90);
Button1.print("off");
}
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
myservo.write(32);
digitalWrite(D3, HIGH);
delay(500);
digitalWrite(D3, LOW);
myservo.write(90);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
myservo.write(134);
digitalWrite(D3, HIGH);
Button1.text("灯已关闭");
delay(500);
digitalWrite(D3, LOW);
myservo.write(90);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = false;
}
}
void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(D3, OUTPUT);
digitalWrite(D3, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
}
void loop()
{
Blinker.run();
}