请教如何自动同步ESP8266引脚状态到blinker APP
小爱音箱控制ESP-01S打开/关闭继电器后,如何同步继电器状态到blinker APP?现在的状态是接通电后,APP按键控制,模块和继电器指示灯同时亮灭,一旦小爱音箱控制一次,指示灯状态就会正好相反!请教代码改进,下面是我现在的代码:#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
char auth[] = "cb5******1cf"; // Blinker APP中添加设备时生成的Secret Key
char ssid[] = "ty****op"; // WIFI名称
char pswd[] = "155******25"; // WIFI密码
int GPIO = 0; // 定义继电器输入引脚为GPIO/0
bool oState = false;
// 新建组件对象
BlinkerButton Button1("btn-abc");
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
// digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
digitalWrite(GPIO, !digitalRead(GPIO));
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) {
BLINKER_LOG("Button tap!");
Button1.icon("fal fa-siren");
Button1.color("#0000FF");
// Button1.text("Your button name or describe");
Button1.print();
}
else if (state == BLINKER_CMD_BUTTON_PRESSED) {
BLINKER_LOG("Button pressed!");
Button1.icon("fal fa-siren-on");
Button1.color("#FFFF00");
// Button1.text("Your button name or describe");
Button1.print();
}
else if (state == BLINKER_CMD_BUTTON_RELEASED) {
BLINKER_LOG("Button released!");
Button1.icon("fal fa-siren");
Button1.color("#FF0000");
// Button1.text("Your button name or describe");
Button1.print();
}
else if (state == BLINKER_CMD_ON) {
BLINKER_LOG("Toggle on!");
Button1.icon("fal fa-light-switch-on");
Button1.color("#0000FF");
Button1.text("ON");
Button1.print("on");
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("Toggle off!");
Button1.icon("fal fa-light-switch-off");
Button1.color("#FF0000");
Button1.text("OFF");
Button1.print("off");
}
else {
BLINKER_LOG("Get user setting: ", state);
Button1.icon("fal fa-light-switch");
Button1.color("#FFFF00");
// Button1.text("Your button name or describe");
Button1.print();
}
}
// 小爱电源类的操作接口
// 用户自定义电源类操作的回调函数
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
// digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(GPIO, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
// digitalWrite(LED_BUILTIN, LOW);
digitalWrite(GPIO, HIGH);
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);
// BLINKER_DEBUG.debugAll();
// 初始化有LED的IO
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, HIGH);
pinMode(GPIO, OUTPUT); // 设定GPIO0为输出引脚
digitalWrite(GPIO, HIGH); // 设定GPIO0为高电位
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
BlinkerMIOT.attachPowerState(miotPowerState); // 注册小爱电源类操作回调函数
BlinkerMIOT.attachQuery(miotQuery); // 注册小爱设备查询的回调函数
}
void loop() {
Blinker.run();
}
if (state == BLINKER_CMD_BUTTON_TAP) {
BLINKER_LOG("Button tap!");
Button1.icon("fal fa-siren");
Button1.color("#0000FF");
// Button1.text("Your button name or describe");
Button1.print();
}
else if (state == BLINKER_CMD_BUTTON_PRESSED) {
BLINKER_LOG("Button pressed!");
Button1.icon("fal fa-siren-on");
Button1.color("#FFFF00");
// Button1.text("Your button name or describe");
Button1.print();
}
else if (state == BLINKER_CMD_BUTTON_RELEASED) {
BLINKER_LOG("Button released!");
Button1.icon("fal fa-siren");
Button1.color("#FF0000");
// Button1.text("Your button name or describe");
Button1.print();
} molun 发表于 2020-7-12 20:52
普通按键这一块,一般什么情况下用呢,TAP、PRESSED、RELEASED 1.blinker只负责通信,如何处理收到的指令,是你自己写的。你自己把GPIO状态按需求写就行了。
2.tap、press、pressup 对应的是短按、长按、长按释放,什么时候需要,也是你自己决定的。 本帖最后由 skygz 于 2020-7-13 23:12 编辑
void MIOTpowerState(void)
{//根据oState 标志 同步给小爱同学,在button1_callback里面加入MIOTpowerState()调用
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
}
void HeartBeat(void)
{ //根据oState 标志 同步 按钮当前状态到APP里
//Button1.text(oState ? "已开启" : "已关闭");
Button1.print(oState ? "on" : "off");
}
void button1_callback(const String & state)
{
digitalWrite(GPIO, !digitalRead(GPIO));
oState = !oState; //标志也要 改变
MIOTpowerState(); //同步给小爱
HeartBeat();//同步给APP
//app里用的是普通按键 那么都是tap状态, 后面判断长按 短按什么的, 用不上的话可以移除。
//以上代码, 不论是 长按还是短按 都会触发。如果需要判断状态则如下
if (state == "tap"){
//将上面的几句代码 都移进入这里。。。
}
}
void setup()
{
..........省略。
Blinker.attachHeartbeat(HeartBeat);
}
skygz 发表于 2020-7-13 23:02
void MIOTpowerState(void)
{//根据oState 标志 同步给小爱同学,在button1_callback里面加入MIOTpowerSt ...
感谢 指导
页:
[1]