ESP32C2 ESP-IDF温度振动传感器详细设计
硬件选型
- 主控芯片:ESP32C2(集成WiFi和BLE,低功耗设计)
- 温度传感器:DS18B20(数字输出,±0.5℃精度)或DHT22(需注意ESP32C2的GPIO电压兼容性)
- 振动传感器:SW-420(机械式振动开关)或ADXL345(数字三轴加速度计,需I2C接口)
- 供电模块:3.3V LDO稳压器(如AMS1117-3.3),支持锂电池充放电管理
- 其他:10K上拉电阻(DS18B20)、0.1μF去耦电容
硬件连接
- DS18B20数据线接GPIO4,外接4.7K上拉电阻
- ADXL345的SCL接GPIO5,SDA接GPIO6(I2C默认引脚)
- SW-420输出接GPIO7,通过比较器转换为数字信号
代码实现(ESP-IDF V5.0+)
传感器数据采集
// DS18B20配置(需安装OneWire组件)
#include <ds18b20.h>
#define DS18B20_GPIO 4
ds18b20_handle_t temp_sensor;
void init_temperature_sensor() {
ds18b20_config_t cfg = { .bus = DS18B20_GPIO };
ds18b20_new_sensor(&cfg, &temp_sensor);
}
float read_temperature() {
float temp;
ds18b20_get_temperature(temp_sensor, &temp);
return temp;
}
// ADXL345配置
#include <adxl345.h>
adxl345_handle_t accel;
void init_vibration_sensor() {
i2c_config_t i2c_cfg = {
.mode = I2C_MODE_MASTER,
.sda_io_num = 6,
.scl_io_num = 5,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE
};
i2c_param_config(I2C_NUM_0, &i2c_cfg);
adxl345_new_sensor(I2C_NUM_0, ADXL345_ADDR, &accel);
}
void read_vibration(int16_t *x, int16_t *y, int16_t *z) {
adxl345_get_acceleration(accel, x, y, z);
}
MQTT数据上传
- 配置MQTT客户端:
#include <mqtt_client.h>
esp_mqtt_client_handle_t mqtt_client;
void mqtt_event_handler(void *handler_args, esp_event_base_t base,
int32_t event_id, void *event_data) {
esp_mqtt_event_handle_t event = event_data;
switch(event->event_id) {
case MQTT_EVENT_CONNECTED:
esp_mqtt_client_subscribe(mqtt_client, "device/ota", 0);
break;
case MQTT_EVENT_DATA:
// 处理OTA指令
break;
}
}
void init_mqtt() {
esp_mqtt_client_config_t mqtt_cfg = {
.uri = "mqtt://broker.emqx.io",
.username = "device_001",
.password = "your_password"
};
mqtt_client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
esp_mqtt_client_start(mqtt_client);
}
- 发布传感器数据:
void publish_sensor_data() {
cJSON *payload = cJSON_CreateObject();
cJSON_AddNumberToObject(payload, "temp", read_temperature());
int16_t x,y,z;
read_vibration(&x, &y, &z);
cJSON_AddNumberToObject(payload, "vib_x", x);
cJSON_AddNumberToObject(payload, "vib_y", y);
cJSON_AddNumberToObject(payload, "vib_z", z);
char *json_str = cJSON_PrintUnformatted(payload);
esp_mqtt_client_publish(mqtt_client, "sensor/data", json_str, 0, 1, 0);
cJSON_Delete(payload);
free(json_str);
}
OTA升级实现
-
HTTP服务器端准备:
- 将固件.bin文件放置在Web服务器(如Nginx)的
/ota/firmware.bin - 提供版本信息接口
/ota/version.json
- 将固件.bin文件放置在Web服务器(如Nginx)的
-
ESP32C2端代码:
#include <esp_https_ota.h>
void check_ota_update() {
esp_http_client_config_t http_cfg = {
.url = "http://your-server.com/ota/version.json",
.cert_pem = NULL // 若用HTTPS需配置证书
};
esp_http_client_handle_t client = esp_http_client_init(&http_cfg);
esp_http_client_perform(client);
int firmware_ver = esp_http_client_get_status_code(client);
if (firmware_ver > CURRENT_FIRMWARE_VERSION) {
start_ota_update();
}
esp_http_client_cleanup(client);
}
void start_ota_update() {
esp_http_client_config_t ota_cfg = {
.url = "http://your-server.com/ota/firmware.bin",
};
esp_https_ota_config_t ota_config = {
.http_config = &ota_cfg,
};
esp_https_ota_handle_t ota_handle = NULL;
esp_https_ota_begin(&ota_config, &ota_handle);
while (1) {
esp_err_t err = esp_https_ota_perform(ota_handle);
if (err == ESP_ERR_HTTPS_OTA_IN_PROGRESS) continue;
if (err == ESP_OK) {
esp_https_ota_finish(ota_handle);
esp_restart();
} else {
ESP_LOGE("OTA", "Update failed!");
break;
}
}
}
数据库存储设计(MQTT Broker端)
-
EMQX Broker配置:
- 启用规则引擎,创建规则:
SELECT payload.temp as temperature, payload.vib_x as vibration_x, payload.timestamp as timestamp FROM "sensor/data"- 添加动作:存储到MySQL/PostgreSQL/MongoDB
- 配置SQL模板:
INSERT INTO sensor_data (device_id, temperature, vibration, timestamp) VALUES ( ${clientid}, ${temperature}, JSON_ARRAY(${vibration_x}, ${vibration_y}, ${vibration_z}), FROM_UNIXTIME(${timestamp}/1000) ) -
数据库表结构:
CREATE TABLE sensor_data (
id INT AUTO_INCREMENT PRIMARY KEY,
device_id VARCHAR(32) NOT NULL,
temperature FLOAT,
vibration JSON,
timestamp DATETIME,
INDEX(device_id, timestamp)
);
关键注意事项
- ESP32C2的GPIO电压为3.3V,需确保传感器兼容
- 低功耗设计建议:
- 使用
esp_sleep_enable_timer_wakeup()实现定时唤醒 - 采集后立即进入Deep Sleep模式
- 使用
- OTA更新时需预留至少2个分区(ota_0/ota_1)
- MQTT消息建议添加时间戳:
cJSON_AddNumberToObject(payload, "timestamp", esp_log_timestamp());
649

被折叠的 条评论
为什么被折叠?



