[官方案例教程]阿里云IoT灯光控制 精华

2019-3-14 16:21:44 [显示全部楼层]
39957浏览
楼主: wuji

[官方案例教程] 阿里云IoT灯光控制

[复制链接]

wuji  中级技师
 楼主|

发表于 2019-4-18 13:36:20

hnyzcj 发表于 2019-4-18 09:52
可能是我表达不够清晰,我想做一下事情:一个主控上挂LED3个(D2,D3,D4),人体D5,温湿度D6,土壤A2,蜂鸣 ...

可以的,在阿里云平台上创建一个产品,为它定义LED1,LED2,LED3,人体温度5等等的功能属性,每个功能都有特定的标识符,再为这个产品创建一个设备,这个设备会继承该产品的所有属性,操作这些标识符就能实现控制和获取信息,关联ESP32和设备,通过上报和订阅和标识符,就能控制ESP32上连接的设备
回复

使用道具 举报

hnyzcj  版主

发表于 2019-4-18 14:11:04

wuji 发表于 2019-4-18 13:36
可以的,在阿里云平台上创建一个产品,为它定义LED1,LED2,LED3,人体温度5等等的功能属性,每个功能都 ...

有QQ留一个
回复

使用道具 举报

龙神ldy  学徒

发表于 2019-4-22 14:41:13

阿里云发布web以后。指示图标 没有变化,数据已经配置了。开关都正常,就是图标没有反应,按道理 关了是一种颜色,开了是一种颜色。但是怎么就没改变,。
回复

使用道具 举报

wuji  中级技师
 楼主|

发表于 2019-4-23 11:02:44

龙神ldy 发表于 2019-4-22 14:41
阿里云发布web以后。指示图标 没有变化,数据已经配置了。开关都正常,就是图标没有反应,按道理 关了是一 ...

关掉物理设备,使用阿里云的在线模拟调试虚拟设备,看看有没有反应
回复

使用道具 举报

DFByjrZTF9V  学徒

发表于 2019-5-1 14:41:14

DFrkjrpHu_E 发表于 2019-3-30 10:16
是的,我也是一样的问题,你有解决吗?

我也一样
回复

使用道具 举报

DFByjrZTF9V  学徒

发表于 2019-5-1 14:43:35

本帖最后由 DFByjrZTF9V 于 2019-5-4 19:21 编辑

为什么我通过手机控制灯的状态,但灯总是一闪一闪的
回复

使用道具 举报

wuji  中级技师
 楼主|

发表于 2019-5-16 09:16:56

DFByjrZTF9V 发表于 2019-5-1 14:43
为什么我通过手机控制灯的状态,但灯总是一闪一闪的

检查下程序上报的逻辑
回复

使用道具 举报

时间都去哪了  学徒

发表于 2019-7-3 17:01:32

本帖最后由 时间都去哪了 于 2019-7-3 17:33 编辑

您好楼主,我想问一下,我遇到这种情况应该怎么解决,我是直接复制你的程序代码的。
在用Arduino 1.8.9编译时候提示如下错误:
StaticjsonBuffer是Arduinokson 5中的一个类。请访问arduinokson.org/upgrade了解如何将您的程序升级到arduinokson。
我应该如何解决,谢谢
回复

使用道具 举报

Z先生  学徒

发表于 2019-12-4 20:07:06

你好,请教一下,如果我需要控制两个led,那么回调函数该怎么写呢,一块esp32上有10个数字输出,我在其中两个引脚各连接一个LED。但LED的识别码是不同的,那么Json信息该如何解码呢。
回复

使用道具 举报

HaVr1rBv  学徒

发表于 2019-12-26 17:04:54

您好,我想问一下手机APP一直下载不了,有遇到同样的情况吗?有解决办法吗?
201912269703..png
回复

使用道具 举报

Nicholas  见习技师

发表于 2020-1-2 05:47:04

楼主很给力
回复

使用道具 举报

Nicholas  见习技师

发表于 2020-1-2 06:56:11

努力加油奋斗
回复

使用道具 举报

Nicholas  见习技师

发表于 2020-1-2 08:18:44











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

这是什么情况呀
回复

使用道具 举报

Nicholas  见习技师

发表于 2020-1-2 08:24:06

非常好









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

回复

使用道具 举报

DFHymJkpq1I  学徒

发表于 2020-1-2 09:37:31

exit status 1
'D2' was not declared in this scope
回复

使用道具 举报

DFS123456  学徒

发表于 2020-3-4 12:33:14

连接不成功,rc=2
回复

使用道具 举报

DFHk-0ykaN8  见习技师

发表于 2020-3-5 10:24:07

温湿度D6,土壤A2,蜂鸣器D7同时使用的话是否可以实现?引脚够用就可以吗?
回复

使用道具 举报

DFHk-0ykaN8  见习技师

发表于 2020-3-17 14:05:51

大神请收下我的膝盖!大神请收下我的膝盖!
回复

使用道具 举报

HugoWang  学徒 来自手机

发表于 2020-3-29 15:09:58

wuji 发表于 2019-3-14 16:21
阿里云IoT之灯光控制



能否信息我一下,有偿服务,谢谢
回复

使用道具 举报

kevin_wk  学徒

发表于 2023-2-1 15:40:49

这套代码在新版的阿里云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

调用如下方法
  1. /*需要操作的产品标识符*/
  2. String Identifier = "LightStatus";
  3. /*需要上报和订阅的两个TOPIC*/
  4. const char * subTopic = "/sys/a17x7FncZl0/DPR-LED/thing/service/property/set";//****set
  5. const char * pubTopic = "/sys/a17x7FncZl0/DPR-LED/thing/event/property/post";//******post
  6. ...
  7. void callback(char * topic, byte * payload, unsigned int len){
  8.   Serial.print("Recevice [");
  9.   Serial.print(topic);
  10.   Serial.print("] ");
  11.   for (int i = 0; i < len; i++){
  12.     Serial.print((char)payload[i]);
  13.   }
  14.   Serial.println();
  15.   StaticJsonBuffer<300> jsonBuffer;
  16.   JsonObject& root = jsonBuffer.parseObject((const char *)payload);
  17.   if(!root.success()){
  18.     Serial.println("parseObject() failed");
  19.     return;
  20.   }
  21.   const uint16_t LightStatus = root["params"][Identifier];
  22.   if(LightStatus == 1){
  23.     openLight();
  24.   }else{
  25.     closeLight();
  26.   }
  27.   String tempMseg = "{"id":"+ClientId+","params":{""+Identifier+"":"+(String)LightStatus+"},"method":"thing.event.property.post"}";
  28.   char sendMseg[tempMseg.length()];
  29.   strcpy(sendMseg,tempMseg.c_str());
  30.   client.publish(pubTopic,sendMseg);
  31. }
  32. void setup(){
  33.   ...
  34.   /*开机先关灯*/
  35.   closeLight();
  36.   
  37.   /*上报关灯信息*/
  38.   client.publish(pubTopic,("{"id":"+ClientId+","params":{""+Identifier+"":0},"method":"thing.event.property.post"}").c_str());
  39. }
复制代码



在函数setup()中调用:
  1.   client.publish(pubTopic,("{"id":"+ClientId+","params":{""+Identifier+"":0},"method":"thing.event.property.post"}").c_str());
复制代码


阿里云收到消息后,在这个主题“/sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply” 发送消息,在串口窗口,可以看到返回的参数是:
  1. 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对象,根本没有属性
  1. const uint16_t LightStatus = root["params"][Identifier];
复制代码

故这里得到 LightStatus 最终为0,根据下面的代码,回关闭LED。
  1.   if(LightStatus == 1){
  2.     openLight();
  3.   }else{
  4.     closeLight();
  5. ;
复制代码



跟离谱的是,接着下面的代码
  1. }
  2.   String tempMseg = "{"id":"+ClientId+","params":{""+Identifier+"":"+(String)LightStatus+"},"method":"thing.event.property.post"}";
  3.   char sendMseg[tempMseg.length()];
  4.   strcpy(sendMseg,tempMseg.c_str());
  5.   client.publish(pubTopic,sendMseg)
复制代码


又会在这个主题
  1. const char * pubTopic = "/sys/a17x7FncZl0/DPR-LED/thing/event/property/post";//******post
复制代码

上发送消息,又引发回调函数 callback() 响应主题
  1. /sys/a17x7FncZl0/DPR-LED/thing/event/property/post_reply
复制代码

然后造成一些循环,串口将一直输出:
  1. 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"}
  2. 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"}
  3. 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"}
  4. 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"}
  5. 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"}
  6. 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"}
  7. 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"}
  8. 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"}
  9. 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"}
  10. 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"}
  11. 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"}
  12. 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"}
  13. 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"}
  14. 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"}
复制代码


灯将一直被关闭。

在这里,希望官方看看是否更新先示例代码。




WedFebruary-202302014937..png
回复

使用道具 举报

成果者也  学徒

发表于 2023-3-26 23:25:12

kevin_wk 发表于 2023-2-1 15:40
这套代码在新版的阿里云Iot(现在的时间是2023-02-01)会有问题了,问题如下:
LED灯会一直被关闭

那怎么办啊大佬
回复

使用道具 举报

12
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail