使用 NodeMCU 和 DHT11 传感器通过 MQTT 上传温湿度数据

使用 NodeMCU 和 DHT11 传感器通过 MQTT 上传温湿度数据

简介

本示例将演示如何通过 NodeMCU, DHT11 收集温湿度并通过 MQTT 协议将数据上报到 EMQX MQTT broker。
DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器,
Node MCU 底层集成了 ESP8266, 能提供完整且自成体系的Wi-Fi网络解决方案,
MQTT 是基于 发布(Publish)/订阅(Subscribe) 模式来进行通信及数据交换的,
EMQX 是基于 Erlang 的 MQTT broker 在开源社区中是最成熟的 MQTT 消息中间件, 它易于配置和使用,可很好地扩展。

配置

硬件配置
  • NodeMCU board x 1
  • DHT11 temperature/humidity sensor x 1
  • Breadboard x 1
  • jumper wires
  • Connection Graph:
    image
Arduino 配置
  • 下载并安装 CH340G USB 驱动
  • 安装 Esp8266模块
  • 安装 PubSubClient 库(by Nick O’Leary)
    Sketch -> Include Library -> Manage Libraries… -> Type PubSub in Search field -> Install
MQTT broker 配置
unzip emqx-macosx-v3.2.5.zip
cd emqx
./bin/emqx start

代码编写

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

#include "DHT.h"

#define DHTPIN D4     // what pin we're connected to
#define wifi_ssid "xxxxx"
#define wifi_password "xxxxx"

#define mqtt_server "broker.emqx.io"  // emqx broker url
#define humidity_topic "humidity"
#define temperature_topic "temperature"

#define DHTTYPE DHT11   // DHT 11

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);

void setup() {
   
    Serial.begin
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值