小爱同学语音控制时说 先帮你操作了,看看设备状态吧esp01s控制继电器开关灯,用的官方的代码,app按钮可以正常控制开关灯,米家也同步设备了,但是小爱同学语音控制就是没反应,小爱控制时串口也不打印数据
- //esp01通过blinker联网
- #define BLINKER_WIFI//定义Blinker,初始化函数
- #define BLINKER_MIOT_LIGHT//配置小爱同学开关灯接口
- #include <Blinker.h>//导入Blinker包
- char auth[] = "a263110020cf";//blinker app获取的产品密钥,通信服务商选择 阿里云
- char ssid[] = "TP-LINK_35BF";//WiFi名称
- char pswd[] = "qingdao12.";//WiFi密码
- BlinkerButton Switch1("btn-01");//注册开关按钮组件
- //按下开关按键执行此函数,state为app按钮状态
- void switch1_onoff(const String & state){
- BLINKER_LOG("开关状态:",state);
-
- if(state=="on"){
- Switch1.text("开");//设置开关显示文本
- digitalWrite(0,HIGH);
- Switch1.print("on");//反馈回app按钮状态,并将以上设置一并发送给app
-
- }else if(state=="off"){
- Switch1.text("关");
- digitalWrite(0,LOW);
- Switch1.print("off");
-
- }
- Blinker.vibrate();//手机震动500ms
- }
- void heartbeat(){
- //心跳包 检测GPIO0引脚状态,高电平修改开关状态为on,低电平改为off
- if(digitalRead(0) == HIGH){
- Serial.print("心跳包检测到GPIO0引脚值为:");
- Serial.println(digitalRead(0));
- Switch1.text("开");//设置app开关按钮文本
- Switch1.print("on");//设置app开关按钮状态
- }else if(digitalRead(0) == LOW){
- Serial.print("心跳包检测到GPIO0引脚值为:");
- Serial.println(digitalRead(0));
- Switch1.text("关");
- Switch1.print("off");
- }
- }
- bool miotState = false;//定义一个布尔型变量,方便给小爱同学查询开关数据
- //小爱同学开关灯函数
- void miotPowerState(const String & state){
- BLINKER_LOG("小爱同学开关状态: ", state);
- //小爱同学控制开关灯状态,说开就开,说关就关
- if (state == BLINKER_CMD_ON) {
- digitalWrite(0, HIGH);
- Switch1.text("开");//设置开关显示文本
- Switch1.print("on");//反馈回app按钮状态,并将以上设置一并发送给app
- BlinkerMIOT.powerState("on");
- BlinkerMIOT.print();
- miotState = true;
- }
- else if (state == BLINKER_CMD_OFF) {
- digitalWrite(0,LOW);
- Switch1.text("关");
- Switch1.print("off");
- BlinkerMIOT.powerState("off");
- BlinkerMIOT.print();
- miotState = 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(miotState ? "on" : "off");
- BlinkerMIOT.print();
- break;
- case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
- BLINKER_LOG("MIOT Query Power State");
- BlinkerMIOT.powerState(miotState ? "on" : "off");
- BlinkerMIOT.print();
- break;
- default :
- BlinkerMIOT.powerState(miotState ? "on" : "off");
- BlinkerMIOT.print();
- break;
- }
- }
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);//初始化串口,设置波特率为115200
- BLINKER_DEBUG.stream(Serial);//调试信息输出到串口,可删除
- //BLINKER_DEBUG.debugAll();//更详细的串口调试信息
- pinMode(LED_BUILTIN,OUTPUT);//初始化板载led
- digitalWrite(LED_BUILTIN,LOW);
- pinMode(0,OUTPUT);//初始化GPIO0引脚为输出工作模式
- digitalWrite(0,HIGH);
- Blinker.begin(auth,ssid,pswd);//初始化blinker
- Switch1.attach(switch1_onoff);//开关按键按下执行switch1_onoff回调函数
- BlinkerMIOT.attachPowerState(miotPowerState);//小爱同学开关灯回调函数
- BlinkerMIOT.attachQuery(miotQuery);//小爱同学查询回调函数
-
- Blinker.attachHeartbeat(heartbeat);//注册一个心跳包
- }
- void loop() {
- // put your main code here, to run repeatedly:
- Blinker.run();
- }
复制代码
|