147 lines
4.3 KiB
C
Executable File
147 lines
4.3 KiB
C
Executable File
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_check.h"
|
|
#include "esp_log.h"
|
|
#include "wifi-connect.h" // 包含 Wi-Fi 配网模块头文件(提供 Wi-Fi 连接状态查询和初始化接口)
|
|
#include "agri_env.h" // 包含农业环境模块头文件(提供 MQTT 功能接口)
|
|
#include "bh1750_use.h" // 包含 BH1750 封装接口
|
|
#include <dht.h>
|
|
#include "driver/gpio.h"
|
|
#include "relay_ctrl.h" // 包含继电器控制模块头文件(提供继电器控制接口)
|
|
|
|
static const char *TAG = "main";
|
|
|
|
// 等待 Wi-Fi 连接成功,超时后返回当前连接状态
|
|
static bool wait_for_wifi_connected(TickType_t timeout_ticks)
|
|
{
|
|
const TickType_t start_ticks = xTaskGetTickCount();
|
|
while ((xTaskGetTickCount() - start_ticks) < timeout_ticks)
|
|
{
|
|
if (wifi_connect_get_status() == WIFI_CONNECT_STATUS_CONNECTED)
|
|
{
|
|
return true;
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(200));
|
|
}
|
|
return wifi_connect_get_status() == WIFI_CONNECT_STATUS_CONNECTED;
|
|
}
|
|
|
|
// 根据 menuconfig 中的选择定义传感器类型常量
|
|
#if defined(CONFIG_EXAMPLE_TYPE_DHT11)
|
|
#define SENSOR_TYPE DHT_TYPE_DHT11
|
|
#elif defined(CONFIG_EXAMPLE_TYPE_AM2301)
|
|
#define SENSOR_TYPE DHT_TYPE_AM2301
|
|
#elif defined(CONFIG_EXAMPLE_TYPE_SI7021)
|
|
#define SENSOR_TYPE DHT_TYPE_SI7021
|
|
#else
|
|
#error "未在 menuconfig 中选择任何 DHT 传感器类型!"
|
|
#endif
|
|
|
|
void dht_test(void *pvParameters)
|
|
{
|
|
float temperature, humidity;
|
|
const gpio_num_t dht_gpio = (gpio_num_t)CONFIG_EXAMPLE_DATA_GPIO;
|
|
uint32_t timeout_count = 0;
|
|
|
|
ESP_LOGI(TAG, "正在启动 DHT 测试任务,引脚: GPIO%d", dht_gpio);
|
|
ESP_LOGI(TAG, "使用的传感器类型: %d (0:DHT11, 1:AM2301, 2:SI7021)", SENSOR_TYPE);
|
|
|
|
// 传感器上电后先等待稳定,避免首读超时
|
|
vTaskDelay(pdMS_TO_TICKS(1500));
|
|
|
|
#ifdef CONFIG_EXAMPLE_INTERNAL_PULLUP
|
|
gpio_set_pull_mode(dht_gpio, GPIO_PULLUP_ONLY);
|
|
#endif
|
|
|
|
while (1)
|
|
{
|
|
esp_err_t res = ESP_FAIL;
|
|
for (int attempt = 1; attempt <= 3; ++attempt)
|
|
{
|
|
res = dht_read_float_data(SENSOR_TYPE, dht_gpio, &humidity, &temperature);
|
|
if (res == ESP_OK)
|
|
{
|
|
break;
|
|
}
|
|
// 给总线一点恢复时间,再重试
|
|
vTaskDelay(pdMS_TO_TICKS(200));
|
|
}
|
|
|
|
if (res == ESP_OK)
|
|
{
|
|
timeout_count = 0;
|
|
ESP_LOGI(TAG, "湿度: %.1f%% 温度: %.1f°C", humidity, temperature);
|
|
}
|
|
else
|
|
{
|
|
if (res == ESP_ERR_TIMEOUT)
|
|
{
|
|
timeout_count++;
|
|
}
|
|
ESP_LOGW(TAG, "读取失败: %s", esp_err_to_name(res));
|
|
|
|
if (timeout_count >= 3)
|
|
{
|
|
ESP_LOGW(TAG, "DHT 连续超时,建议检查: 1) DATA 线上拉电阻(4.7k~10k) 2) 传感器供电与地线 3) 是否更换为 GPIO2/3 等普通 IO");
|
|
timeout_count = 0;
|
|
}
|
|
}
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(2000));
|
|
}
|
|
}
|
|
|
|
void app_main(void)
|
|
{
|
|
ESP_ERROR_CHECK(wifi_connect_init()); // 初始化 Wi-Fi 配网模块
|
|
ESP_ERROR_CHECK(bh1750_user_init()); // 初始化光照传感器
|
|
|
|
ESP_ERROR_CHECK(relay_ctrl_init(GPIO_NUM_6, GPIO_NUM_7, true)); //
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(2000)); // 等待系统稳定
|
|
|
|
|
|
if (wait_for_wifi_connected(pdMS_TO_TICKS(60000)))
|
|
{
|
|
esp_err_t err = agri_env_mqtt_start();
|
|
if (err != ESP_OK)
|
|
{
|
|
ESP_LOGW(TAG, "MQTT 启动失败: %s", esp_err_to_name(err));
|
|
}
|
|
|
|
// 测试读取光照数据
|
|
float lux;
|
|
if (bh1750_user_read(&lux) == ESP_OK)
|
|
{
|
|
ESP_LOGI(TAG, "测试读取光照强度: %.2f Lux", lux);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGW(TAG, "Wi-Fi 连接超时,暂时跳过 MQTT 启动");
|
|
}
|
|
|
|
// 启动 DHT 传感器测试任务
|
|
if (xTaskCreate(dht_test, "dht_test", 2048, NULL, 5, NULL) != pdPASS)
|
|
{
|
|
ESP_LOGE(TAG, "创建 DHT 任务失败");
|
|
}
|
|
|
|
// 启动一个心跳任务
|
|
while (1)
|
|
{
|
|
ESP_LOGI(TAG, "系统心跳...");
|
|
// 测试读取光照数据
|
|
float lux;
|
|
if (bh1750_user_read(&lux) == ESP_OK)
|
|
{
|
|
ESP_LOGI(TAG, "测试读取光照强度: %.2f Lux", lux);
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(10000));
|
|
}
|
|
}
|