项目概述
基于 ESP32-S3 + DHT22 传感器的温湿度监测站,支持 Wi-Fi 联网上传数据到 MQTT 服务器,并通过网页仪表盘实时查看数据。
硬件清单
| 组件 | 型号 | 数量 |
|---|---|---|
| 主控 | ESP32-S3-DevKitC | 1 |
| 温湿度传感器 | DHT22 | 1 |
| OLED 屏 | SSD1306 0.96寸 | 1 |
| 杜邦线 | - | 若干 |
核心代码
cpp
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
client.setServer("mqtt.example.com", 1883);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (!isnan(h) && !isnan(t)) {
if (!client.connected()) {
client.connect("monitor_01");
}
client.publish("sensor/temp", String(t).c_str());
client.publish("sensor/humi", String(h).c_str());
}
delay(2000);
}项目特点
- 低功耗设计,支持深度睡眠模式
- 数据本地缓存,断网自动重传
- OTA 远程固件更新
- Web 配置界面
后续计划
- 增加 CO2 传感器
- 接入 Home Assistant
- 设计定制 PCB
DISCUSSION
评论
还没有评论。你可以留下第一条有价值的讨论。