-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartswitch.ino
189 lines (150 loc) · 3.85 KB
/
smartswitch.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
#include <Servo.h>
Servo myservo;//定义舵机
char auth[] = ""; //点灯Key
char ssid[] = ""; //wifi名称
char pswd[] = ""; //wifi密码
// 新建组件对象
BlinkerButton Button1("test");
BlinkerButton Button2("resetc");
BlinkerButton Button3("tjdw");
int counter = 0;
int light_state=-1;
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
myservo.write(140);
delay(110);
myservo.write(90);
light_state=1;
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
myservo.write(45);
delay(100);
myservo.write(90);
light_state=0;
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
void miotColor(int32_t color)
{
BLINKER_LOG("need set color: ", color);
if(light_state==-1 or light_state==1)
{
BlinkerMIOT.print();
myservo.write(45);
delay(100);
myservo.write(90);
light_state=0;
delay(350);
myservo.write(140);
delay(110);
myservo.write(90);
light_state=1;
}
else if(light_state==0)
{
BlinkerMIOT.print();
myservo.write(140);
delay(110);
myservo.write(90);
light_state=1;
}
BlinkerMIOT.color(color);
}
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state=="on")
{
if(light_state==-1 or light_state==0)
{
myservo.write(140);
delay(110);
myservo.write(90);
light_state=1;
Blinker.print("命令已经执行。");
}
else if(light_state==1)
{
Blinker.print("命令没有执行,因为处于锁定状态。");
}
} else if(state=="tap")
{
if(light_state==-1 or light_state==1)
{
myservo.write(35);
delay(100);
myservo.write(90);
light_state=0;
Blinker.print("命令已经执行。");
}
else if(light_state==0)
{
Blinker.print("命令没有执行,因为处于锁定状态。");
}
}
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
light_state=-1;
Blinker.print("执行了一次解除锁定操作。");
}
void button3_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(light_state==-1 or light_state==1)
{
myservo.write(45);
delay(100);
myservo.write(90);
light_state=0;
delay(350);
myservo.write(140);
delay(110);
myservo.write(90);
light_state=1;
}
else if(light_state==0)
{
myservo.write(140);
delay(110);
myservo.write(90);
light_state=1;
}
Blinker.print("执行了一次调节灯温操作。");
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
myservo.attach(2);
myservo.write(90);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachColor(miotColor);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
Blinker.run();
digitalWrite(LED_BUILTIN, HIGH);
}