暑期总结1-ESP8266模块通过MQTT协议连接华为云(物联网上云)

参考文章第二部分  部署华为云物联网平台基于STM32+华为云IOT设计的智能冰箱(华为云IOT)_stm32查看华为云影子数据-CSDN博客

仅作为日后回忆,可能会有摘抄自其他博主的内容。

1.1 物联网平台介绍

        华为云物联网平台(IoT 设备接入云服务)提供海量设备的接入和管理能力,将物理设备联接到云,支撑设备数据采集上云和云端下发命令给设备进行远程控制,配合华为云其他产品,帮助我们快速构筑物联网解决方案。

        使用物联网平台构建一个完整的物联网解决方案主要包括3部分:物联网平台、业务应用和设备。

        物联网平台作为连接业务应用和设备的中间层,屏蔽了各种复杂的设备接口,实现设备的快速接入;同时提供强大的开放能力,支撑行业用户构建各种物联网解决方案。

        设备可以通过固网、2G/3G/4G/5G、NB-IoT、Wifi等多种网络接入物联网平台,并使用LWM2M/CoAP、MQTT、HTTPS协议将业务数据上报到平台,平台也可以将控制命令下发给设备。

        业务应用通过调用物联网平台提供的API,实现设备数据采集、命令下发、设备管理等业务场景。

        华为云官网: https://www.huaweicloud.com/,打开官网,搜索物联网,就能快速找到"设备接入IoTDA")

1.2 开通物联网服务

地址: https://www.huaweicloud.com/product/iothub.html

点击立即创建。

正在创建标准版实例,需要等待片刻。

创建完成之后,点击实例名称。 可以看到标准版实例的设备接入端口和地址。

在上面也能看到 免费单元的限制。

开通之后,点击总览,也能查看接入信息。 我们当前设备准备采用MQTT协议接入华为云平台,这里可以看到MQTT协议的地址和端口号等信息。

总结:

端口号:   MQTT (1883)| MQTTS (8883)    
接入地址:ad635970a1.st1.iotda-device.cn-north-4.myhuaweicloud.com

**根据域名地址得到IP地址信息: **

打开Windows电脑的命令行控制台终端(菜单窗口输入cmd),使用ping 命令ping,复制输入到cmd窗口即可: ad635970a1.st1.iotda-device.cn-north-4.myhuaweicloud.com

返回结果为

Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。

安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows

PS C:\Users\29397> ping ad635970a1.st1.iotda-device.cn-north-4.myhuaweicloud.com

正在 Ping ad635970a1.st1.iotda-device.cn-north-4.myhuaweicloud.com [117.78.5.125] 具有 32 字节的数据:
来自 117.78.5.125 的回复: 字节=32 时间=52ms TTL=45
来自 117.78.5.125 的回复: 字节=32 时间=41ms TTL=45
来自 117.78.5.125 的回复: 字节=32 时间=31ms TTL=45
来自 117.78.5.125 的回复: 字节=32 时间=28ms TTL=45

117.78.5.125 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 28ms,最长 = 52ms,平均 = 38ms

MQTT协议接入端口号有两个,1883是非加密端口,8883是证书加密端口,单片机无法加载证书,所以使用1883端口比较合适。 接下来的ESP8266就采用1883端口连接华为云物联网平台。

1.3 创建产品

未完,待续.......

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是基于STM32和ESP8266采用MQTT方式连接华为云的代码: ``` #include <ESP8266WiFi.h> #include <PubSubClient.h> #include <SoftwareSerial.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> #define WIFI_SSID "your_wifi_ssid" #define WIFI_PASSWORD "your_wifi_password" #define MQTT_SERVER "your_mqtt_server" #define MQTT_PORT 1883 #define MQTT_USERNAME "your_mqtt_username" #define MQTT_PASSWORD "your_mqtt_password" #define MQTT_TOPIC "your_mqtt_topic" SoftwareSerial espSerial(2, 3); // RX, TX WiFiClient wifiClient; PubSubClient mqttClient(wifiClient); LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { Serial.begin(115200); espSerial.begin(115200); lcd.begin(); lcd.backlight(); lcd.print("Connecting to WiFi"); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); lcd.setCursor(0, 1); lcd.print("."); } lcd.clear(); lcd.print("WiFi Connected"); mqttClient.setServer(MQTT_SERVER, MQTT_PORT); mqttClient.setCallback(mqttCallback); } void loop() { if (!mqttClient.connected()) { mqttReconnect(); } mqttClient.loop(); String temp = readTemperature(); mqttClient.publish(MQTT_TOPIC, temp.c_str()); lcd.setCursor(0, 0); lcd.print("Temp: "); lcd.print(temp); delay(1000); } void mqttReconnect() { while (!mqttClient.connected()) { Serial.println("Connecting to MQTT broker..."); if (mqttClient.connect("ESP8266", MQTT_USERNAME, MQTT_PASSWORD)) { Serial.println("MQTT connected"); mqttClient.subscribe(MQTT_TOPIC); } else { Serial.print("MQTT connection failed, rc="); Serial.print(mqttClient.state()); Serial.println(" retrying in 5 seconds"); delay(5000); } } } void mqttCallback(char* topic, byte* payload, unsigned int length) { Serial.print("Received message ["); Serial.print(topic); Serial.print("]: "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } String readTemperature() { String temp; espSerial.println("AT+CIPSTART=\"TCP\",\"api.openweathermap.org\",80"); if (espSerial.find("OK")) { Serial.println("TCP connection established"); String request = "GET /data/2.5/weather?q=London,uk&APPID=your_api_key HTTP/1.1\r\nHost: api.openweathermap.org\r\nConnection: close\r\n\r\n"; espSerial.print("AT+CIPSEND="); espSerial.println(request.length()); if (espSerial.find(">")) { Serial.println("Sending request"); espSerial.print(request); if (espSerial.find("CLOSED")) { Serial.println("TCP connection closed"); } else { Serial.println("Error closing TCP connection"); } } else { Serial.println("Error sending request"); } } else { Serial.println("Error establishing TCP connection"); } return temp; } ``` 这段代码连接到Wi-Fi并建立MQTT连接,然后每秒钟读取温度并将其发布到MQTT主题“your_mqtt_topic”中。当接收到来自MQTT主题的消息时,它将在串行监视器中打印出来。请注意,您需要将代码中的"WIFI_SSID","WIFI_PASSWORD","MQTT_SERVER","MQTT_USERNAME","MQTT_PASSWORD"和"MQTT_TOPIC"替换为您自己的值。也需要更改“readTemperature”函数中的API密钥和查询参数,以获取您所需的实际温度值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值