有大神能给解答一下,小爱同学命令可以发送串口信息吗
我有一个8266继电器,他的开合是通过串口发送16位码来开合,小爱同学有这个命令吗,试了半天没有成功。有大神能给解答一下吗 你收到blinker传来的小爱指令后,你自己发就行了 void miotPowerState(const String & state){
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(D1, HIGH);
delay(200);
Serial.write(data1on, 4);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(D1, LOW);
delay(200);
Serial.write(data1off, 4);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = false;
}
}
这样是不是不行,收到小爱指令是说的收到回调函数吗? minqqq 发表于 2020-6-10 17:54
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
这样可以,将delay换成blinker.delay 我开始用的就是blinker.delay,但是这样用小爱控制没有反应,点灯的按钮控制没问题 minqqq 发表于 2020-6-11 12:17
我开始用的就是blinker.delay,但是这样用小爱控制没有反应,点灯的按钮控制没问题 ...
把你的delay和串口操作都放到MIOT.print后面,你放前面,就会导致反馈超时 放在后面了还是没用,顺便问一下小爱会返回动作信息吗?
#define BLINKER_PRINT Serial
#define BLINKER_MIOT_OUTLET
#define BLINKER_WIFI
#define BLINKER_ESP_SMARTCONFIG
#include <Blinker.h>
char auth[] = "";//设备key
BlinkerButton Button1("btn-1");
BlinkerButton Button2("btn-2");
BlinkerButton Button3("btn-3");
char data1on = {0xA0, 0x01, 0x01, 0xA2};
char data1off = {0xA0, 0x01, 0x00, 0xA1};
char data2on = {0xA0, 0x02, 0x01, 0xA3};
char data2off = {0xA0, 0x02, 0x00, 0xA2};
unsigned long Timers={0};
char i;
int D1=0;
int D2=2;
bool oState=false;
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(D1, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
Blinker.delay(200);
Serial.write(data1on, 4);
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(D1, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
Blinker.delay(200);
Serial.write(data1off, 4);
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 button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(D2, LOW);
Blinker.delay(50);
digitalWrite(D1, HIGH);
Blinker.delay(50);
Serial.write(data2off, 4);
Blinker.delay(50);
Serial.write(data1on, 4);
Button1.print("on");
Button1.icon("fas fa-lightbulb");
Button1.color("#fddb00");
Button1.text("打开啦");
Button2.print("off");
Button2.icon("fas fa-lightbulb");
Button2.color("#000000");
Button2.text("关灯");
}
void button2_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(D1, LOW);
Blinker.delay(50);
digitalWrite(D2, HIGH);
Blinker.delay(50);
Serial.write(data1off, 4);
Blinker.delay(50);
Serial.write(data2on, 4);
Button2.print("on");
Button2.icon("fas fa-lightbulb");
Button2.color("#fddb00");
Button2.text("打开啦");
Button1.print("off");
Button1.icon("fas fa-lightbulb");
Button1.color("#000000");
Button1.text("关灯");
}
void button3_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(D1, LOW);
Blinker.delay(50);
digitalWrite(D2, LOW);
Blinker.delay(50);
Serial.write(data1off, 4);
Blinker.delay(50);
Serial.write(data2off, 4);
Blinker.delay(50);
Button1.print("off");
Button1.icon("fas fa-lightbulb");
Button1.color("#000000");
Button1.text("关灯");
Button2.print("off");
Button2.icon("fas fa-lightbulb");
Button2.color("#000000");
Button2.text("关灯");
Button3.print("off");
Button3.icon("fas fa-lightbulb");
Button3.color("#000000");
Button3.text("关闭");
}
void heartbeat(){
BLINKER_LOG("状态同步!");
if(digitalRead(D1)==HIGH){
Button1.print("on");
Button1.icon("fas fa-lightbulb");
Button1.color("#fddb00");
Button1.text("打开啦");
}
else{
Button1.print("off");
Button1.icon("fas fa-lightbulb");
Button1.color("#000000");
Button1.text("关灯");
}
if(digitalRead(D2)==HIGH){
Button2.print("on");
Button2.icon("fas fa-lightbulb");
Button2.color("#fddb00");
Button2.text("打开啦");
}
else{
Button2.print("off");
Button2.icon("fas fa-lightbulb");
Button2.color("#000000");
Button2.text("关灯");
}
}
void setup() {
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化有LED的IO
pinMode(D1, OUTPUT);
digitalWrite(D1, LOW);
pinMode(D2, OUTPUT);
digitalWrite(D2, LOW);
// 初始化blinker
Blinker.begin(auth);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Blinker.attachHeartbeat(heartbeat);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Blinker.attachData(dataRead);
}
void loop() {
Blinker.run();
}
这是我的代码,麻烦大神看看 要是不加delay可以吗
页:
[1]