浏览量: 1,145 次浏览

esp8266、mqtt获取温湿度

2020年2月4日 0 作者 Nie Hen

使用esp8266-01s,以及mqtt 传输传感器DHT11 获取到的温度以及湿度值
这个应用非常的多 也非常的实用

DHT11

DHT11 是一款温湿度传感器,可以用来测试环境温湿度,该传感器的的温度测量范围为 0 ~ 50°C,误差为 2 °C;湿度测量范围为在 0°C时为30%-90%RH,环境温度为 25°C时,湿度测量范围为20%-90%RH,在50°C时,测量范围是20%-80%。

引脚 对应接口
VCC 5V
Ddat 串行数据单总线(任一IO)
GND 负极接地

arduino IDE配置
在项目 加载库 库管理中 搜索dht 下载DHT sensor by Adafruit (1.30)
enter description here
安装好之后搜索 adafruit unifi 下载A大fruit Unified Sensor (1.02)
enter description here
然后就可以去测试温湿度模块

使用esp模块

可以使用esp8266-01s模块
使用GPIO2 来进行传送数据

连接电路

vcc 和en 接3.3v电源
GPIO2接到 dht11 输出信号
GND接地

不太会的同学 可以参考学习
esp8266 mqtt使用arduino 开发

esp8266 mqtt 使用arduino开发教程

esp8266-01s使用介绍

esp8266-01s介绍与使用

完整代码


\#include <ESP8266WiFi.h> #include "DHT.h" #include <PubSubClient.h> #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); const char* ssid = "nihen_pc"; const char* password = "niehen1234"; const char* mqtt_server = "192.168.137.41"; WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; float h; // humidity float t; // temperature void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); // Switch on the LED if an 1 was received as first character if ((char)payload[0] == '1') { digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level // but actually the LED is on; this is because // it is acive low on the ESP-01) } else { digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH } } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP8266Client")) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("weather/temp", "init..."); client.publish("weather/humidity", "init..."); // ... and resubscribe client.subscribe("weather/control"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup(void){ pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); delay(2000); // delay for DHT11 init. } void loop(void){ if (!client.connected()) { reconnect(); } client.loop(); long now = millis(); if (now - lastMsg > 5000) { lastMsg = now; gettemp(); Serial.print("Publish message: "); Serial.println(t); char tmp[5]; dtostrf(t,4,3,tmp); client.publish("weather/temp", tmp); Serial.print("Publish message: "); Serial.println(h); dtostrf(h,4,3,tmp); client.publish("weather/humidity", tmp); } } void gettemp() { h = dht.readHumidity(); t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } }

可参考 https://www.bilibili.com/video/av23531283/