这套代码在新版的阿里云Iot(现在的时间是2023-02-01)会有问题了,问题如下:
LED灯会一直被关闭
问题原因:
新版的阿里云Iot,在添加完设备后,会为设备自动定义如下主题,如下图所示:
https://mc.dfrobot.com.cn/forum.php?mod=attachment&aid=MTU0Mjc3fDI3OGU0Y2U2NDk5ODIyYWY3NjMyMmI5OWU3ODhhM2VifDE2ODYzMTk0MzE%3D&request=yes&_f=.png
出现问题的地方是,当设备订阅这个主题:/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply 后
调用如下方法
/*需要操作的产品标识符*/
String Identifier = "LightStatus";
/*需要上报和订阅的两个TOPIC*/
const char * subTopic = "/sys/a17x7FncZl0/DPR-LED/thing/service/property/set";//****set
const char * pubTopic = "/sys/a17x7FncZl0/DPR-LED/thing/event/property/post";//******post
...
void callback(char * topic, byte * payload, unsigned int len){
Serial.print("Recevice [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < len; i++){
Serial.print((char)payload[i]);
}
Serial.println();
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((const char *)payload);
if(!root.success()){
Serial.println("parseObject() failed");
return;
}
const uint16_t LightStatus = root["params"][Identifier];
if(LightStatus == 1){
openLight();
}else{
closeLight();
}
String tempMseg = "{"id":"+ClientId+","params":{""+Identifier+"":"+(String)LightStatus+"},"method":"thing.event.property.post"}";
char sendMseg[tempMseg.length()];
strcpy(sendMseg,tempMseg.c_str());
client.publish(pubTopic,sendMseg);
}
void setup(){
...
/*开机先关灯*/
closeLight();
/*上报关灯信息*/
client.publish(pubTopic,("{"id":"+ClientId+","params":{""+Identifier+"":0},"method":"thing.event.property.post"}").c_str());
}
复制代码
在函数setup()中调用: client.publish(pubTopic,("{"id":"+ClientId+","params":{""+Identifier+"":0},"method":"thing.event.property.post"}").c_str()); 复制代码
阿里云收到消息后,在这个主题“/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply” 发送消息,在串口窗口,可以看到返回的参数是:
15:00:16.777 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
复制代码
可以看到,返回Json对象,根本没有属性const uint16_t LightStatus = root["params"][Identifier]; 复制代码
故这里得到 LightStatus 最终为0,根据下面的代码,回关闭LED。
if(LightStatus == 1){
openLight();
}else{
closeLight();
; 复制代码
跟离谱的是,接着下面的代码
}
String tempMseg = "{"id":"+ClientId+","params":{""+Identifier+"":"+(String)LightStatus+"},"method":"thing.event.property.post"}";
char sendMseg[tempMseg.length()];
strcpy(sendMseg,tempMseg.c_str());
client.publish(pubTopic,sendMseg) 复制代码
又会在这个主题
const char * pubTopic = "/sys/a17x7FncZl0/DPR-LED/thing/event/property/post";//******post 复制代码
上发送消息,又引发回调函数 callback() 响应主题/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply 复制代码
然后造成一些循环,串口将一直输出:
15:00:12.029 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:12.236 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:12.338 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:12.476 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:12.649 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:12.853 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:13.060 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:13.161 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:13.369 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:13.506 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:13.677 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:13.882 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:13.985 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"}
15:00:14.090 -> Recevice [/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply] {"code":200,"data":{},"id":"12345","message":"success","method":"thing.event.property.post","version":"1.0"} 复制代码
灯将一直被关闭。
在这里,希望官方看看是否更新先示例代码。