3214
- 收藏
- 点赞
- 分享
- 举报
通过 NodeMCU (ESP8266) 将传感器数据上传至 MQTT 云服务
MQTT 是轻量级的、灵活的物联网消息交换和数据传递协议,致力于为 IoT 开发人员实现灵活性与硬件/网络资源的平衡。
[ESP8266](https://www.espressif.com/zh-hans) 提供了⼀套⾼度集成的 Wi-Fi SoC 解决⽅案,其低功耗、 紧凑设计和⾼稳定性可以满⾜⽤户的需求。ESP8266 拥有完整的且⾃成体系的 Wi-Fi ⽹络功能,既能够独⽴应⽤,也可以作为从机搭载于其他主机 MCU 运⾏。
在此项目中我们将实现 ESP8266 连接到 [EMQ X Cloud](https://cloud.emqx.io/cn/) 运营和维护的免费公共 MQTT 服务器,并使用 Arduino IDE 来对 ESP8266 进行编程。 EMQ X Cloud 是由 [EMQ](https://www.emqx.io/cn/) 推出的安全的 MQTT 物联网云服务平台,它提供一站式运维代管、独有隔离环境的 **MQTT 5.0** 接入服务。
所需物联网组件
ESP8266
Arduino IDE
[MQTT X](https://mqttx.app/cn/): 优雅的跨平台 MQTT 5.0 客户端工具
免费的公共 MQTT 服务器
Broker: **broker.emqx.io**
TCP Port: **1883**
Websocket Port: **8083**
ESP8266 Pub/Sub 示意图

ESP8266 代码编写
首先我们将导入 **ESP8266WiFi** 和 **PubSubClient** 库,ESP8266WiFi 库能够将 ESP8266 连接到 Wi-Fi 网络,PubSubClient 库能使 ESP8266 连接到 MQTT 服务器发布消息及订阅主题。
#include
#include
设置 Wi-Fi 名称和密码,以及 MQTT 服务器连接地址和端口
const char *ssid = "name"; // Enter your WiFi name
const char *password = "pass"; // Enter WiFi password
const char *mqtt_broker = "broker.emqx.io";
const int mqtt_port = 1883;
打开一个串行连接,以便于输出程序的结果并且连接到 Wi-Fi 网络
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
设置 MQTT 服务器,并编写回调函数,同时将连接信息打印到串口监视器上
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to public emqx mqtt broker.....");
if (client.connect("esp8266-client")) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload);
}
Serial.println();
Serial.println("-----------------------");
}
MQTT 服务器连接成功后,ESP8266 将向 MQTT 服务器发布消息和订阅主题
// publish and subscribe
client.publish("esp8266/test", "hello emqx");
client.subscribe("esp8266/test");
将主题名称打印到串行端口,然后打印收到消息的每个字节
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload);
}
Serial.println();
Serial.println("-----------------------");
}
MQTT 服务器的连接和测试
请使用 [Arduino IDE](https://www.arduino.cc/en/Main/Software) 将完整代码上传到 ESP8266,并打开串口监视器

建立 MQTT X 客户端 与 MQTT 服务器的连接, 并向 ESP8266 发送消息

在串口监视器查看 ESP8266 接收到的消息

完整代码
#include
#include
const char *ssid = "name"; // Enter your WiFi name
const char *password = "pass"; // Enter WiFi password
const char *mqtt_broker = "broker.emqx.io";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
//connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to public emqx mqtt broker.....");
if (client.connect("esp8266-client")) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// publish and subscribe
client.publish("esp8266/test", "hello emqx");
client.subscribe("esp8266/test");
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload);
}
Serial.println();
Serial.println("-----------------------");
}
void loop() {
client.loop();
}
总结
至此,我们已成功使 ESP8266 连接到 EMQ X Cloud 提供的公共 MQTT 服务器。 在本项目中我们简单的将 ESP8266 连接到 MQTT 服务器,这只是 ESP8266 较为基础的能力之一,ESP8266 其实还能与各类物联网传感器相连,并将传感器数据上报至 MQTT 服务器。
[ESP8266](https://www.espressif.com/zh-hans) 提供了⼀套⾼度集成的 Wi-Fi SoC 解决⽅案,其低功耗、 紧凑设计和⾼稳定性可以满⾜⽤户的需求。ESP8266 拥有完整的且⾃成体系的 Wi-Fi ⽹络功能,既能够独⽴应⽤,也可以作为从机搭载于其他主机 MCU 运⾏。
在此项目中我们将实现 ESP8266 连接到 [EMQ X Cloud](https://cloud.emqx.io/cn/) 运营和维护的免费公共 MQTT 服务器,并使用 Arduino IDE 来对 ESP8266 进行编程。 EMQ X Cloud 是由 [EMQ](https://www.emqx.io/cn/) 推出的安全的 MQTT 物联网云服务平台,它提供一站式运维代管、独有隔离环境的 **MQTT 5.0** 接入服务。
所需物联网组件
ESP8266
Arduino IDE
[MQTT X](https://mqttx.app/cn/): 优雅的跨平台 MQTT 5.0 客户端工具
免费的公共 MQTT 服务器
Broker: **broker.emqx.io**
TCP Port: **1883**
Websocket Port: **8083**
ESP8266 Pub/Sub 示意图

ESP8266 代码编写
首先我们将导入 **ESP8266WiFi** 和 **PubSubClient** 库,ESP8266WiFi 库能够将 ESP8266 连接到 Wi-Fi 网络,PubSubClient 库能使 ESP8266 连接到 MQTT 服务器发布消息及订阅主题。
#include
#include
设置 Wi-Fi 名称和密码,以及 MQTT 服务器连接地址和端口
const char *ssid = "name"; // Enter your WiFi name
const char *password = "pass"; // Enter WiFi password
const char *mqtt_broker = "broker.emqx.io";
const int mqtt_port = 1883;
打开一个串行连接,以便于输出程序的结果并且连接到 Wi-Fi 网络
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
设置 MQTT 服务器,并编写回调函数,同时将连接信息打印到串口监视器上
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to public emqx mqtt broker.....");
if (client.connect("esp8266-client")) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload);
}
Serial.println();
Serial.println("-----------------------");
}
MQTT 服务器连接成功后,ESP8266 将向 MQTT 服务器发布消息和订阅主题
// publish and subscribe
client.publish("esp8266/test", "hello emqx");
client.subscribe("esp8266/test");
将主题名称打印到串行端口,然后打印收到消息的每个字节
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload);
}
Serial.println();
Serial.println("-----------------------");
}
MQTT 服务器的连接和测试
请使用 [Arduino IDE](https://www.arduino.cc/en/Main/Software) 将完整代码上传到 ESP8266,并打开串口监视器

建立 MQTT X 客户端 与 MQTT 服务器的连接, 并向 ESP8266 发送消息

在串口监视器查看 ESP8266 接收到的消息

完整代码
#include
#include
const char *ssid = "name"; // Enter your WiFi name
const char *password = "pass"; // Enter WiFi password
const char *mqtt_broker = "broker.emqx.io";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
//connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to public emqx mqtt broker.....");
if (client.connect("esp8266-client")) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// publish and subscribe
client.publish("esp8266/test", "hello emqx");
client.subscribe("esp8266/test");
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload);
}
Serial.println();
Serial.println("-----------------------");
}
void loop() {
client.loop();
}
总结
至此,我们已成功使 ESP8266 连接到 EMQ X Cloud 提供的公共 MQTT 服务器。 在本项目中我们简单的将 ESP8266 连接到 MQTT 服务器,这只是 ESP8266 较为基础的能力之一,ESP8266 其实还能与各类物联网传感器相连,并将传感器数据上报至 MQTT 服务器。
我来回答
回答0个
时间排序
认可量排序

或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片
相关问答
-
2020-06-19 10:53:47
-
2018-12-03 15:01:57
-
2019-01-03 13:56:05
-
2020-11-29 16:12:42
-
2018-12-21 10:26:41
-
2020-11-22 11:36:06
-
2018-12-17 13:24:32
-
2020-10-22 14:16:43
-
2018-12-15 10:12:50
-
2021-01-02 22:44:30
-
2019-01-02 09:36:28
-
2020-10-05 19:10:51
-
2019-01-07 10:11:30
-
2019-10-22 15:11:29
-
2018-12-07 09:48:27
-
2020-12-09 09:31:18
-
2018-12-21 09:03:54
-
2019-01-09 10:22:21
-
2018-11-15 14:26:44
无更多相似问答 去提问

点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
10海思3559的VGS模块在VI上画线问题
-
53519dv500接lvds的sensor mn34120,图像出现很多竖线,sensor板接以前的3519v101没问题
-
103403外接hdmi口1024*600显示屏报错
-
5SS928点DC camera的6946,全屏紫色
-
5hi3519 的 网络传输的MTU值可以修改到比1500大嘛?
-
10WS73V100星闪扫描不到设备sle
-
5SS928/SD3403 录像失败 venc stream time out, exit thread; venc 2 stream buffer is full
-
10Hi3516DV500无法运行PQTool软件
-
10君正T23+1084带TF卡插卡(给该主板适配TF卡驱动,电机驱动,适配GPIO)
-
5Hi3536A从vpss获取1080分辨率的yuv数据异常
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认