IoT-Switch

再放一下之前做的智能开关的设备,使用ESP-8266,连接舵机,主要功能是管理常见的86插座,使用热熔胶将舵机粘到86开关盒边上,使得该舵机的方向拨杆可以完成关灯操作,即可。主要是为了冬天懒得下床再关灯而设计的,然而后来我买了米家的智能灯泡,可以直接米家APP接入,就不需要了。
有智能升级,之前应该是接入了 blinker的,可以实现blinker app的控制,然后依稀记得 之前还实现了升级功能,不知道是真的假的,备份一下。

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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET

#include <Blinker.h>
#include<ESP8266WiFi.h>
#include<Servo.h>
#include<ESP8266HTTPClient.h>
#include<ESP8266httpUpdate.h>
#include<ESP8266WebServer.h>



/**
* @brief
*
* 需求描述:使用网线供电,需要使用USB分离器和分线器来配合使用,
*
* 1.先执行8266的WIFI入网功能,
* 2.在执行webCLient的初始化功能将auth字符设置值,
* 3.如何保证auth字段不被重置?那要不就新建模式写文件,写到auth字段,
* 4.将auth字段设置给程序,然后继续往下走接口鉴权,
* 5.再然后就自动鉴权,接口设备联网自行运转,
* 6.设备Mac地址需要抓出来,配置WIFI使用,
* 7.但是还需要获取一个mac地址,和IP地址,
*/


char auth[] = "blinker-auth-key";
char ssid[] = "PDCN";
char passwd[] = "wifi-password";

ESP8266HTTPUpdate httpUpdate;
ESP8266WebServer webServer(80);

bool updateState = false;
bool oState = false;
Servo servo;
int pos = 0;

BlinkerButton btn1 = BlinkerButton("btn1");


void doAction(const String & state) {

BLINKER_LOG("doAction : ", state);
if(state == BLINKER_CMD_ON) {
//暂时先空着,先不留开开关的
BLINKER_LOG("开关 开了");
} else if(state == BLINKER_CMD_OFF) {
BLINKER_LOG("开关 关了");

for (pos = 90; pos >= 0; pos --) { // 从180°到0°
servo.write(pos); // 舵机角度写入
delay(15); // 等待转动到指定角度
}
delay(200);
for (pos = 0; pos <= 90; pos ++) { // 0°到180°
// in steps of 1 degree
servo.write(pos); // 舵机角度写入
delay(15); // 等待转动到指定角度
}
delay(200);//延时0.2s
}
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);

if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
doAction(state);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();

oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
doAction(state);
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 update_started() {
BLINKER_LOG("8266 update start");
}

void update_finished() {
BLINKER_LOG("8266 update finished");
}
void update_process(int cur, int total) {
BLINKER_LOG("update process at ", cur, " of ", total, " bytes");
}

void update_error(int err) {
BLINKER_LOG("update error code ", err);
}

void update(String url) {

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
}

WiFiClient client;
httpUpdate.onStart(update_started);
httpUpdate.onEnd(update_finished);
httpUpdate.onError(update_error);
httpUpdate.onProgress(update_process);

t_httpUpdate_return ret = httpUpdate.update(client, url);
switch(ret) {
case HTTP_UPDATE_FAILED:
BLINKER_LOG("update failed ", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
BLINKER_LOG("update result no updates");
break;
case HTTP_UPDATE_OK:
BLINKER_LOG("update ok");
break;
}
}


void handleUpdate() {
webServer.send(200, "text/plain", "ok");
String url = webServer.arg("url");
Serial.print("url: " + url);
update(url);
}

void handleRoot() {
webServer.send(200, "text/html;charset=utf-8", "<form action='/update'><input name='url' type='input'></input> <input type='submit'> submit</input></form>");
}

void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);

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

servo.attach(D0);


Blinker.begin(auth, ssid, passwd);
Blinker.attachData(dataRead);

BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
doAction("off");
webServer.begin();
webServer.on("/", handleRoot);
webServer.on("/update", handleUpdate);
}

void loop()
{
Blinker.run();
webServer.handleClient();
}