我正在使用 ESP-IDF 中的 ESP32 制作应用程序。我是 ESP-IDF 的新手。我正在尝试从 AHT25 获取温度和湿度数据并将其更新到服务器中。 这是主要的。c
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_http_server.h"
#include "esp_netif.h"
#include "aht25_library.h"
static EventGroupHandle_t s_wifi_event_group;
static httpd_handle_t server = NULL;
#define EXAMPLE_ESP_WIFI_SSID "Saranya's Galaxy
F22"
#define EXAMPLE_ESP_WIFI_PASS "saran142407"
#define EXAMPLE_ESP_MAXIMUM_RETRY 5
#define I2C_PORT I2C_NUM_0
#define I2C_SCL_PIN 22
#define I2C_SDA_PIN 21
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static const char *TAG = "wifi station";
static int s_retry_num = 0;
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_sta finished.");
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
}
static esp_err_t root_handler(httpd_req_t *req)
{
float temperature, humidity;
esp_err_t err;
err = aht25_read_temperature(I2C_PORT, &temperature);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error reading temperature");
httpd_resp_send_500(req);
return ESP_FAIL;
}
err = aht25_read_humidity(I2C_PORT, &humidity);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error reading humidity");
httpd_resp_send_500(req);
return ESP_FAIL;
}
char json_response[128];
snprintf(json_response, sizeof(json_response),
"{\"temperature\": %.2f, \"humidity\": %.2f}", temperature, humidity);
httpd_resp_set_type(req, "application/json");
httpd_resp_send(req, json_response, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
void http_server_task(void *pvParameters)
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
ESP_LOGI(TAG, "Starting HTTP server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
ESP_LOGI(TAG, "HTTP server started successfully");
httpd_uri_t root_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = root_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &root_uri);
}
vTaskDelete(NULL);
}
void app_main()
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t wifi_config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_config));
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_SDA_PIN,
.scl_io_num = I2C_SCL_PIN,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000
};
i2c_param_config(I2C_PORT, &i2c_config);
i2c_driver_install(I2C_PORT, I2C_MODE_MASTER, 0, 0, 0);
aht25_init(I2C_PORT);
xTaskCreate(http_server_task, "http_server_task", 4096, NULL, 5, NULL);
esp_netif_ip_info_t ip_info;
if (esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info) == ESP_OK) {
ESP_LOGI(TAG, "IP Address: " IPSTR, IP2STR(&ip_info.ip));
} else {
ESP_LOGE(TAG, "Failed to get IP address");
}
}
aht25_library.c
#include "aht25_library.h"
#include "driver/i2c.h"
#define AHT25_I2C_ADDRESS 0x38
#define AHT25_READ_TEMP_COMMAND 0x01
#define AHT25_READ_HUMI_COMMAND 0x02
#define AHT25_INIT_COMMAND 0xE1
esp_err_t aht25_init(i2c_port_t i2c_port) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (AHT25_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, AHT25_INIT_COMMAND, true);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, pdMS_TO_TICKS(1000));
i2c_cmd_link_delete(cmd);
return ret;
}
esp_err_t aht25_read_temperature(i2c_port_t i2c_port,
float *temperature) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (AHT25_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, AHT25_READ_TEMP_COMMAND, true);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, pdMS_TO_TICKS(1000));
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
return ret; // Return the I2C communication error
}
uint8_t temp_data[3];
ret = i2c_master_read(i2c_port, temp_data, sizeof(temp_data), I2C_MASTER_LAST_NACK);
if (ret != ESP_OK) {
return ret; // Return the I2C communication error
}
return ESP_OK;
}
esp_err_t aht25_read_humidity(i2c_port_t i2c_port, float *humidity) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (AHT25_I2C_ADDRESS << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, AHT25_READ_HUMI_COMMAND, true);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, pdMS_TO_TICKS(1000));
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
return ret; // Return the I2C communication error
}
uint8_t humi_data[3];
ret = i2c_master_read(i2c_port, humi_data, sizeof(humi_data), I2C_MASTER_LAST_NACK);
if (ret != ESP_OK) {
return ret; // Return the I2C communication error
}
return ESP_OK;
}
aht25_library.h
#ifndef AHT25_LIBRARY_H
#define AHT25_LIBRARY_H
#include "driver/i2c.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
#define AHT25_I2C_ADDRESS 0x38
esp_err_t aht25_init(i2c_port_t i2c_port);
esp_err_t aht25_read_temperature(i2c_port_t i2c_port, float *temperature);
esp_err_t aht25_read_humidity(i2c_port_t i2c_port, float *humidity);
#ifdef __cplusplus
}
#endif
#endif /* AHT25_LIBRARY_H */
我的代码没有错误,但串口监视器上无法显示IP地址。显示为 0.0.0.0
有人可以帮助我吗?我是这个工具的新手。
我认为您的代码缺少对 wifi_init_sta 函数的调用。将其添加到您的应用程序主目录中,如下所示:
void app_main()
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t wifi_config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_config));
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_SDA_PIN,
.scl_io_num = I2C_SCL_PIN,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000
};
i2c_param_config(I2C_PORT, &i2c_config);
i2c_driver_install(I2C_PORT, I2C_MODE_MASTER, 0, 0, 0);
aht25_init(I2C_PORT);
wifi_init_sta(); // Initialize WiFi connection
xTaskCreate(http_server_task, "http_server_task", 4096, NULL, 5, NULL);
esp_netif_ip_info_t ip_info;
if (esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info) == ESP_OK) {
ESP_LOGI(TAG, "IP Address: " IPSTR, IP2STR(&ip_info.ip));
} else {
ESP_LOGE(TAG, "Failed to get IP address");
}
}