楼主: wuji
|
[官方案例教程] 阿里云IoT灯光控制 |
本帖最后由 DFByjrZTF9V 于 2019-5-4 19:21 编辑 为什么我通过手机控制灯的状态,但灯总是一闪一闪的 |
C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void openLight()': sketch_jan02a:6: error: 'D2' was not declared in this scope #define BEDROOD_LIGHT D2 ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:30:16: note: in expansion of macro 'BEDROOD_LIGHT' digitalWrite(BEDROOD_LIGHT, HIGH); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void closeLight()': sketch_jan02a:6: error: 'D2' was not declared in this scope #define BEDROOD_LIGHT D2 ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:34:16: note: in expansion of macro 'BEDROOD_LIGHT' digitalWrite(BEDROOD_LIGHT, LOW); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void connectWiFi()': C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:40:37: warning: invalid conversion from 'const char*' to 'char*' [-fpermissive] WiFi.begin(WIFI_SSID,WIFI_PASSWORD); ^ In file included from C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:1:0: C:\Users\acer\Desktop\arduino-1.8.3\libraries\WiFi\src/WiFi.h:79:9: note: initializing argument 1 of 'int WiFiClass::begin(char*, const char*)' int begin(char* ssid, const char *passphrase); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void callback(char*, byte*, unsigned int)': sketch_jan02a:74: error: 'client' was not declared in this scope client.publish(pubTopic,sendMseg); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void ConnectAliyun()': sketch_jan02a:78: error: 'client' was not declared in this scope while(!client.connected()){ ^ sketch_jan02a:81: error: 'myAliyun' was not declared in this scope if(client.connect(myAliyun.client_id,myAliyun.username,myAliyun.password)){ ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void setup()': sketch_jan02a:6: error: 'D2' was not declared in this scope #define BEDROOD_LIGHT D2 ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:94:11: note: in expansion of macro 'BEDROOD_LIGHT' pinMode(BEDROOD_LIGHT,OUTPUT); ^ sketch_jan02a:100: error: 'myAliyun' was not declared in this scope myAliyun.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret); ^ sketch_jan02a:102: error: 'client' was not declared in this scope client.setServer(myAliyun.mqtt_server,PORT); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void loop()': sketch_jan02a:118: error: 'client' was not declared in this scope if(!client.connected()){ ^ sketch_jan02a:121: error: 'client' was not declared in this scope client.loop(); ^ exit status 1 'D2' was not declared in this scope 这是什么情况呀 |
非常好 C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void openLight()': sketch_jan02a:6: error: 'D2' was not declared in this scope #define BEDROOD_LIGHT D2 ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:30:16: note: in expansion of macro 'BEDROOD_LIGHT' digitalWrite(BEDROOD_LIGHT, HIGH); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void closeLight()': sketch_jan02a:6: error: 'D2' was not declared in this scope #define BEDROOD_LIGHT D2 ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:34:16: note: in expansion of macro 'BEDROOD_LIGHT' digitalWrite(BEDROOD_LIGHT, LOW); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void connectWiFi()': C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:40:37: warning: invalid conversion from 'const char*' to 'char*' [-fpermissive] WiFi.begin(WIFI_SSID,WIFI_PASSWORD); ^ In file included from C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:1:0: C:\Users\acer\Desktop\arduino-1.8.3\libraries\WiFi\src/WiFi.h:79:9: note: initializing argument 1 of 'int WiFiClass::begin(char*, const char*)' int begin(char* ssid, const char *passphrase); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void callback(char*, byte*, unsigned int)': sketch_jan02a:74: error: 'client' was not declared in this scope client.publish(pubTopic,sendMseg); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void ConnectAliyun()': sketch_jan02a:78: error: 'client' was not declared in this scope while(!client.connected()){ ^ sketch_jan02a:81: error: 'myAliyun' was not declared in this scope if(client.connect(myAliyun.client_id,myAliyun.username,myAliyun.password)){ ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void setup()': sketch_jan02a:6: error: 'D2' was not declared in this scope #define BEDROOD_LIGHT D2 ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino:94:11: note: in expansion of macro 'BEDROOD_LIGHT' pinMode(BEDROOD_LIGHT,OUTPUT); ^ sketch_jan02a:100: error: 'myAliyun' was not declared in this scope myAliyun.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret); ^ sketch_jan02a:102: error: 'client' was not declared in this scope client.setServer(myAliyun.mqtt_server,PORT); ^ C:\Users\acer\Desktop\sketch_jan02a\sketch_jan02a.ino: In function 'void loop()': sketch_jan02a:118: error: 'client' was not declared in this scope if(!client.connected()){ ^ sketch_jan02a:121: error: 'client' was not declared in this scope client.loop(); ^ exit status 1 'D2' was not declared in this scope |
这套代码在新版的阿里云Iot(现在的时间是2023-02-01)会有问题了,问题如下: LED灯会一直被关闭 问题原因: 新版的阿里云Iot,在添加完设备后,会为设备自动定义如下主题,如下图所示: https://mc.dfrobot.com.cn/forum.php?mod=attachment&aid=MTU0Mjc3fDczMmRjOGE0YmU3MDdiYTYzN2U2MjQxZWYzNTY4ZGQyfDE3MzQ1NDgyMzU%3D&request=yes&_f=.png 出现问题的地方是,当设备订阅这个主题: /sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply 调用如下方法
在函数setup()中调用:
阿里云收到消息后,在这个主题“/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply” 发送消息,在串口窗口,可以看到返回的参数是:
可以看到,返回Json对象,根本没有属性
故这里得到 LightStatus 最终为0,根据下面的代码,回关闭LED。
跟离谱的是,接着下面的代码
又会在这个主题
上发送消息,又引发回调函数 callback() 响应主题
然后造成一些循环,串口将一直输出:
灯将一直被关闭。 在这里,希望官方看看是否更新先示例代码。 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed