初涉arduino,初涉esp8266,初涉Blinker,写了2个程序。
第二个,复制电脑开机键到手机APP。可以app控制,可以小爱语音控制。
用的硬件是ESP01S、对应继电器以及杜邦线(选用公母的,需要两头有插头时拆开一头)。
方便在烧录程序时就可以看到操作效果,esp上的led利用起来(程序还把esp上的led亮灭用于刷新按钮状态,心跳函数里面,因为小爱语音控制后并不能即时刷新)。
代码如下:
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
char auth[] = "***";
char ssid[] = "***";
char pswd[] = "***";
int GPIO = 0;
bool oState = false;
BlinkerButton Button1("btn-on");
//BlinkerButton Button2("bb");
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) {//响应短按
if (digitalRead(LED_BUILTIN) == HIGH) { //若电脑是关闭状态(电脑没有通过APP启动)
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(GPIO, HIGH);
BLINKER_LOG("Button tap!");
Blinker.delay(150);
digitalWrite(GPIO, LOW);
Button1.color("#FFC800");
Button1.text("运行中");
Button1.print();
}
// else{//短按时关机,需配合电脑电源键策略,默认策略可行
// digitalWrite(GPIO, HIGH);//继电器接通
// BLINKER_LOG("Button pressed!");
// Blinker.delay(150);
// digitalWrite(LED_BUILTIN, HIGH);//eps-01s灯灭
// digitalWrite(GPIO, LOW);
// Button1.color("#CCCCCC");
// Button1.text("已关闭");
// Button1.print();
// }
}
else if (state == BLINKER_CMD_BUTTON_PRESSUP) {//响应长按
if (digitalRead(LED_BUILTIN) == LOW) { //若电脑是开启状态(电脑通过APP启动)
digitalWrite(GPIO, HIGH);//继电器接通
BLINKER_LOG("Button pressed!");
Blinker.delay(5000);
digitalWrite(LED_BUILTIN, HIGH);//eps-01s灯灭
digitalWrite(GPIO, LOW);//5秒后继电器断开,相当于长按电脑开机键5秒
Button1.color("#CCCCCC");
Button1.text("已关闭");
Button1.print();
}
else { //关闭状态时LED_BUILTIN为高电平,不亮
Button1.text("没开机");//按键提示“没开机”
Button1.print();
Blinker.delay(3000);
Button1.color("#CCCCCC");
Button1.text("已关闭");//“没开机”变更为“已关闭”
Button1.print();
}
}
}
//下面这段是在开机或关机状态下测试esp引脚高低电平的,结果对比数据,根本不能利用起来。解决不了文章最后提出的问题。
//void button2_callback(const String & state)
//{
// BLINKER_LOG("get button state: ", state);
// int i;
// if (state == BLINKER_CMD_BUTTON_TAP) {//响应短按
// for (i = 0; i < 17; i++) {
// Button2.print(String(digitalRead(i)));
// Blinker.delay(200);
// }
// }
//}
void miotPowerState(const String & state, uint8_t num)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(GPIO, HIGH);
Blinker.delay(150);
digitalWrite(GPIO, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(GPIO, HIGH);
Blinker.delay(5000);//改成100-300,响应短按关机,需配合电脑电源键策略,默认策略可行;而且数值2000以上,小爱会提示出问题(会响应关机动作)
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(GPIO, LOW);
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 heartbeat()//小爱模块调用这不起作用,可能是我调用方法不对,不了解
{
BLINKER_LOG("状态同步!");
if (digitalRead(LED_BUILTIN) == LOW) {
Button1.color("#FFC800");
Button1.text("运行中");
Button1.print();
}
else {
Button1.color("#CCCCCC");
Button1.text("已关闭");
Button1.print();
}
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);//这块esp8266(指示灯)默认高电平关闭状态
pinMode(GPIO, OUTPUT);
digitalWrite(GPIO, LOW); //继电器(指示灯)默认低电平关闭状态
Blinker.begin(auth, ssid, pswd);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Button1.attach(button1_callback);
// Button2.attach(button2_callback);
Blinker.attachHeartbeat(heartbeat);
}
void loop()
{
Blinker.run();
}
存在的问题:若用电脑win菜单关机,esp01s的LED不会灭(对强迫症患者不友好,可以选择关机后再拔下插头或者一直用app关机)。
|