当前位置:网站首页>ESP8266_通过MQTT协议连接阿里云
ESP8266_通过MQTT协议连接阿里云
2022-06-11 09:14:00 【小老虎_IOT】
1、阿里云的操作
1.1、准备
登录阿里云官网,注册账号,实名认证;
找到产品→物联网IoT→企业物联网平台→进入控制台→公共实例;
1.2、创建产品及产品下的两个设备
创建产品,比如一个ESP8266控制灯的产品;
这个产品要有两个云端设备,ESP8266本身以及用户端,用户端通过向阿里云端发送控制指令,指令再通过云端转发给ESP8266设备;


1.3、查看MQTT连接的参数,用mqtt.fx测试能否连上云端
选中设备,在设备信息里面,我圈出来了“MQTT连接参数”,点击“查看”就有了,复制这些信息到mqtt.fx的配置文件,可以成功连接;

1.4、创建规则,实现设备间数据传输
首先要了解订阅发布主题名,在产品的Topic类列表中有;
- 默认订阅的主题名:/ProductKey/DeviceName/user/get
- 默认发布的主题名:/ProductKey/DeviceName/user/update
然后在“规则引擎”、“云产品流转”、旧版,添加两个规则,其中mqttfx数据转发到esp8266的规则如下;

创建好规则后,启动这两个规则,开两个mqttfx客户端进行测试,成功;

2、代码及现象
2.1、esp8266代码
要修改mqtt_config.h中的服务器域名、端口、ClientID、MQTT用户名、密码、持有人标识;
还有user_main.c中的订阅主题名、发布主题名、收到PUBLISH报文主题名;
ClientID我是一眼就看出来了,辣么长,肯定超过64位了,所以把config.h中的“device_id”改成了128位,也确认了在使用这个参数时没有问题;
下载、运行,返回了如下信息:

找一下自己写过的MQTT那篇文章ESP8266_MQTT协议_小老虎_IOT的博客-CSDN博客,看看CONNACK报文的32,2,0,4代表了啥,好家伙,无效的用户名或密码;

难道是用户名密码失效了?像上次百度云那样?
去设备详情的“MQTT连接参数”复制了新的用户名、密码、客户端ID,再烧录运行,还是不行;
emmm,我刚忘了改持有人标识,改了再试,又是一顿操作还是不行;
然后我数了数密码的长度,刚好64位,emmm,一点都不刚好好吧,记得代码中那个密码数组64位,字符串还有个'\0'呢,所以要把config.h中的“mqtt_pass”改成128位,同样,也确认了在使用这个参数时没有问题,果然就好了;
// 系统参数
//-------------------------------------------------------------------------------------
typedef struct{
uint32_t cfg_holder; // 持有人标识(只有更新此数值,系统参数才会更新)
uint8_t device_id[128]; // 客户端ID[128]
uint8_t sta_ssid[64]; // WIFI名[64]
uint8_t sta_pwd[64]; // WIFI密码[64]
uint32_t sta_type; // STA类型
uint8_t mqtt_host[64]; // MQTT服务端域名[64]
uint32_t mqtt_port; // MQTT端口
uint8_t mqtt_user[64]; // MQTT用户名[64]
uint8_t mqtt_pass[128]; // MQTT密码[128]
uint32_t mqtt_keepalive; // 保持连接时长
uint8_t security; // 安全类型
} SYSCFG;2.2、现象

吐槽:我后悔从第一张图把密钥打了马赛克,后面就不得不打马赛克,每当我劝寄几不用打,没关系,仿佛都听到另一个声音,不行,前面已经打了马赛克了,而且你现在不打会显得忘了一样,是傻fufu的,不管我怎么说另一个我都不听不听,甚至还控制我的手,所谓内耗就是如此吧,我裂开;
边栏推荐
- Design of wrist sphygmomanometer based on sic32f911ret6
- Telecommuting best practices and Strategies
- Method (common method), method execution memory analysis, method overloading mechanism, method recursion
- Set MySQL as externally connectable
- Development of PCBA circuit board for small oxygen generator
- 机器学习笔记 - Kaggle大师Janio Martinez Bachmann的故事
- 从企业评价的方历来看ERP软件成功与失利
- Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)
- Sed explanation of shell script (SED command, sed -e, sed s/ new / old /...)
- 【软件】大企业ERP选型的方法
猜你喜欢

Talk about how to customize data desensitization
![Error [detectionnetwork (1)][warning]network compiled for 6 shapes, maximum available 10, compiling for 5 S](/img/54/f42146ae649836fe7070ac90f2160e.png)
Error [detectionnetwork (1)][warning]network compiled for 6 shapes, maximum available 10, compiling for 5 S

Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)

MSF adds back door to normal program

Type-C docking station adaptive power supply patent protection case
![[image processing] spatial domain image enhancement](/img/d9/6afee1006570651ccf93ed8da95dd3.png)
[image processing] spatial domain image enhancement

Use of MSF evaluation module

Detailed explanation of this and static

Technical practice of dolphin dispatching in kubernetes system

Shandong University project training (IV) -- wechat applet scans web QR code to realize web login
随机推荐
山东大学增强现实实验四
OpenCV CEO教你用OAK(四):创建复杂的管道
Clothing ERP: how do enterprises carry out implementation planning?
ERP体系的这些优势,你知道吗?
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
OpenCV OAK相机对比及介绍
剑指 Offer II 041. 滑动窗口的平均值
【分享】企業如何進行施行規劃?
Set up redis highly available cluster environment
Redis source code analysis hash object (z\u hash)
Machine learning notes - in depth Learning Skills Checklist
[TiO websocket] IV. the TiO websocket server implements the custom cluster mode
Development of PCBA circuit board for small oxygen generator
制作业信息化为什么难施行?
MSF SMB based information collection
Openstack explanation (XXIII) -- other configurations, database initialization and service startup of neutron
Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
[image processing] spatial domain image enhancement
【方案开发】红外体温计测温仪方案
Analysis of high frequency interview questions in massive data processing