11 - 项目案例
ESP32 实际项目实现
项目一:智能温湿度计
功能需求
- 实时采集 DHT22 温湿度数据
- OLED 显示屏实时显示
- 数据上报至 MQTT 服务器
- 按键切换显示/休眠模式
硬件清单
| 组件 | 数量 | 说明 |
|---|
| ESP32 DEVKIT V1 | 1 | 主控 |
| DHT22 | 1 | 温湿度传感器 |
| OLED 0.96” | 1 | I2C 接口 |
| 轻触按键 | 1 | GPIO0 |
| 电阻 10kΩ | 1 | 上拉电阻 |
硬件连接
ESP32 DHT22 OLED Button
GPIO4 ──────► Data GPIO21 ──────► SDA
GPIO22 ──────► SCL
3.3V ────────► VCC
GND ────────► GND
GPIO0 ──────► Button ─────┘
完整代码
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/gpio.h>
#include <driver/i2c.h>
#include <esp_wifi.h>
#include <esp_mqtt.h>
#include "dht.h"
#include "ssd1306.h"
#define DHT_PIN GPIO_NUM_4
#define I2C_SDA 21
#define I2C_SCL 22
#define MQTT_BROKER "mqtt://broker.emqx.io"
extern "C" void app_main(void)
{
// 初始化 I2C
i2c_init();
// 初始化 DHT
dht_init(DHT_PIN, DHT22);
// 初始化 OLED
ssd1306_init();
ssd1306_clear();
ssd1306_show_string(0, 0, "Smart Sensor", 16);
// 初始化 WiFi 和 MQTT
wifi_init_sta();
mqtt_init(MQTT_BROKER);
// 按键初始化
gpio_set_direction(GPIO_NUM_0, GPIO_MODE_INPUT);
float temp, humi;
char buf[32];
while (1) {
// 读取温湿度
if (dht_read_float(DHT_PIN, &humi, &temp) == ESP_OK) {
// OLED 显示
snprintf(buf, sizeof(buf), "Temp: %.1f C", temp);
ssd1306_show_string(0, 2, buf, 16);
snprintf(buf, sizeof(buf), "Humi: %.1f %%", humi);
ssd1306_show_string(0, 4, buf, 16);
// MQTT 上报
snprintf(buf, sizeof(buf), "{\"temp\":%.1f,\"humi\":%.1f}", temp, humi);
mqtt_publish("esp32/sensor/data", buf);
}
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
项目二:WiFi 门铃
功能需求
- 按钮触发门铃
- 播放音乐提示
- 发送推送通知到手机
- 拍照功能 (可选)
硬件清单
| 组件 | 说明 |
|---|
| ESP32-CAM | 集成摄像头 |
| 蜂鸣器 | 有源蜂鸣器 |
| 轻触按钮 | 门铃按键 |
代码实现
#include "esp_http_client.h"
#include "esp_camera.h"
#define BUTTON_GPIO GPIO_NUM_2
#define BEEP_GPIO GPIO_NUM_4
void beep_play(void)
{
for (int i = 0; i < 3; i++) {
gpio_set_level(BEEP_GPIO, 1);
vTaskDelay(pdMS_TO_TICKS(200));
gpio_set_level(BEEP_GPIO, 0);
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void send_notification(void)
{
// 调用 IFTTT Webhook 或自定义服务
esp_http_client_config_t config = {
.url = "https://maker.ifttt.com/trigger/doorbell/with/key/YOUR_KEY"
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_perform(client);
esp_http_client_cleanup(client);
}
void button_task(void *arg)
{
while (1) {
if (gpio_get_level(BUTTON_GPIO) == 0) {
vTaskDelay(pdMS_TO_TICKS(50)); // 消抖
if (gpio_get_level(BUTTON_GPIO) == 0) {
beep_play();
send_notification();
}
while (gpio_get_level(BUTTON_GPIO) == 0) {
vTaskDelay(pdMS_TO_TICKS(10));
}
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
项目三:智能家庭气象站
功能清单
- 温湿度采集 (DHT22)
- 气压监测 (BMP280)
- 光照强度 (BH1750)
- PM2.5 监测 (PMS5003)
- 数据存储 (SD卡)
- 云端上报 (MQTT)
系统架构
┌─────────────────────────────────────────────┐
│ ESP32 气象站 │
├─────────────────────────────────────────────┤
│ 传感器层 │
│ ┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ │
│ │DHT22 │ │BMP280│ │BH1750│ │PMS5003│ │
│ └──┬───┘ └──┬───┘ └──┬───┘ └──┬───┘ │
│ │ │ │ │ │
├─────┴────────┴────────┴────────┴────────────┤
│ 数据处理层 │
│ ┌──────────────────────────────────────┐ │
│ │ 数据融合与格式转换 │ │
│ └──────────────────────────────────────┘ │
├─────────────────────────────────────────────┤
│ 存储层 │ 网络层 │
│ ┌──────┐ │ ┌──────┐ │
│ │SD卡 │ │ │MQTT │ │
│ └──────┘ │ └──────┘ │
└────────────────┴───────────────────────────┘
数据结构
typedef struct {
float temperature; // 温度 (℃)
float humidity; // 湿度 (%)
float pressure; // 气压 (Pa)
float pm25; // PM2.5 (μg/m³)
uint32_t lux; // 光照 (lux)
uint32_t timestamp; // 时间戳
} weather_data_t;
项目四:智能灯光控制
功能
- WiFi 控制 RGB 灯带
- 手机 App 远程调节
- 预设场景模式
- 语音控制兼容 (Alexa/小爱)
控制板设计
ESP32
│
├─ GPIO2 ───► R (PWM)
├─ GPIO4 ───► G (PWM)
├─ GPIO15 ───► B (PWM)
│
└─ GPIO13 ───► 继电器控制 (220V灯带)
MQTT 主题
| 主题 | 用途 |
|---|
home/lights/set | 设置灯光 |
home/lights/status | 状态反馈 |
home/lights/scene | 场景切换 |
控制协议
// 发送
{
"device": "bedroom_light",
"r": 255,
"g": 128,
"b": 64,
"brightness": 80,
"effect": "solid"
}
// 响应
{
"device": "bedroom_light",
"status": "ok",
"r": 255,
"g": 128,
"b": 64
}
项目模板
基本项目结构
project/
├── main/
│ ├── CMakeLists.txt
│ ├── main.c
│ └── Kconfig.projbuild
├── components/
│ ├── sensor/
│ │ ├── CMakeLists.txt
│ │ ├── sensor.c
│ │ └── include/sensor.h
│ └── network/
│ ├── CMakeLists.txt
│ ├── network.c
│ └── include/network.h
├── test/
└── README.md
CMakeLists.txt 示例
idf_component_register(
SRCS
"main.c"
"components/sensor/sensor.c"
"components/network/network.c"
INCLUDE_DIRS
"."
"components/sensor/include"
"components/network/include"
REQUIRES
esp_wifi
esp_mqtt
driver
)
参考资源
相关链接
反向链接
进阶学习