|
本帖最后由 houguanghaha 于 2020-5-29 12:00 编辑
做了一个卧室灯开关并安装到墙壁86盒中,实现了小爱同学,墙壁开关点动控制开关灯.看起来一切正常.但是有时半夜没有操作灯自动亮了,可能是某些信息号干扰,但不知如何解决?
接线图如下
程序如下
[mw_shl_code=arduino,true]#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#define BLINKER_PRINT Serial
#include <Blinker.h>
#include <ESP8266WiFi.h>
WiFiServer server(80);
char auth[] = "123";
char ssid[] = "123";
char pswd[] = "123";
int GPIO = 0;
//实体按钮开关
int switch_button = 2;
int temp = 0;
BlinkerButton ButtonOn("btn-on");
BlinkerButton ButtonOff("btn-off");
BlinkerButton ButtonState("btn-state");
void button_on_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(GPIO, HIGH);
ButtonState.print("on");
ButtonState.color("#00BB00");
ButtonState.text("灯亮了");
Blinker.vibrate();
}
void button_off_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(GPIO, LOW);
ButtonState.print("off");
ButtonState.color("#FF0000");
ButtonState.text("灯关了");
Blinker.vibrate();
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(GPIO, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(GPIO, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
void heartbeat()
{
BLINKER_LOG("状态同步!");
if (digitalRead(GPIO) == LOW)
{
ButtonState.print("on");
ButtonState.color("#00BB00");
ButtonState.text("灯亮了");
}
else
{
ButtonState.print("off");
ButtonState.color("#FF0000");
ButtonState.text("灯关了");
}
}
void setup() {
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(GPIO, OUTPUT);
pinMode(switch_button, INPUT);
digitalWrite(GPIO, LOW);
Blinker.begin(auth, ssid, pswd);
ButtonOn.attach(button_on_callback);
ButtonOff.attach(button_off_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
Blinker.attachHeartbeat(heartbeat);
server.begin();
Serial.println(F("Server started"));
}
void loop() {
Blinker.run();
temp = digitalRead(switch_button);
if (temp == LOW) {
digitalWrite(GPIO, !digitalRead(GPIO));
pinMode(switch_button, INPUT_PULLUP);
delay(300);
}
WiFiClient client = server.available();
if (!client) {
delay(100);
return;
}
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
int val;
if (req.indexOf("/gpio/on") != -1)
digitalWrite(GPIO, HIGH);
else if (req.indexOf("/gpio/off") != -1)
digitalWrite(GPIO, LOW);
else {
Serial.println("invalid request");
client.print("HTTP/1.1 404\r\n");
client.stop();
return;
}
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nWelcome to 666! ";
s += "</html>\n";
client.print(s);
delay(1);
Serial.println("Client disonnected");
}[/mw_shl_code]
|
|