refactor
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
build
|
||||
managed_components
|
||||
*.swp
|
||||
.cache
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "esp_http_server.h"
|
||||
#include "ygg_http_server.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "YGG_HTTP_SERVER";
|
||||
|
||||
httpd_handle_t ygg_start_webserver(void)
|
||||
{
|
||||
httpd_handle_t server = NULL;
|
||||
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) {
|
||||
return server;
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "Error starting server!");
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef YGG_HTTP_SERV
|
||||
#define YGG_HTTP_SERV
|
||||
|
||||
httpd_handle_t ygg_start_webserver(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,90 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_log.h"
|
||||
#include "ygg_wifi.h"
|
||||
|
||||
#define WIFI_SSID "TP-Link_8D0E"
|
||||
#define WIFI_PASS "49824643"
|
||||
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static const char *TAG = "YGG_WIFI";
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
static int s_retry_num = 0;
|
||||
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 < 5) {
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
ESP_LOGI(TAG, "Resuming connection to AP...");
|
||||
} else {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
ESP_LOGI(TAG, "Connection to AP failed");
|
||||
} 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, "IP acquired:" 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,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
&instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
&instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = WIFI_SSID,
|
||||
.password = WIFI_PASS,
|
||||
},
|
||||
};
|
||||
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, "Initialization wifi_init_sta done.");
|
||||
|
||||
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", WIFI_SSID);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(TAG, "Connection error SSID:%s", WIFI_SSID);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef YGG_WIFI
|
||||
#define YGG_WIFI
|
||||
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_event.h"
|
||||
|
||||
void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data);
|
||||
void wifi_init_sta(void);
|
||||
|
||||
#endif
|
||||
@@ -1,2 +0,0 @@
|
||||
idf_component_register(SRCS "thermo-sensor.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -1,190 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_system.h>
|
||||
#include <bmp280.h>
|
||||
#include <string.h>
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#define I2C_MASTER_SDA 19
|
||||
#define I2C_MASTER_SCL 23
|
||||
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
#define WIFI_SSID "TP-Link_8D0E"
|
||||
#define WIFI_PASS "49824643"
|
||||
|
||||
static const char *TAG = "thermo_sensor";
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
static float last_temperature = 0.0f;
|
||||
|
||||
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
static int s_retry_num = 0;
|
||||
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 < 5) {
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
ESP_LOGI(TAG, "Ponawianie połączenia z AP...");
|
||||
} else {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
ESP_LOGI(TAG, "Połączenie z AP nie powiodło się");
|
||||
} 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, "Uzyskano 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,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
&instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
&instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = WIFI_SSID,
|
||||
.password = WIFI_PASS,
|
||||
},
|
||||
};
|
||||
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, "Inicjalizacja wifi_init_sta zakończona.");
|
||||
|
||||
// Czekaj na połączenie lub błąd
|
||||
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, "Połączono z sieci AP SSID:%s", WIFI_SSID);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(TAG, "Błąd połączenia z SSID:%s", WIFI_SSID);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "NIEOCZEKIWANE ZDARZENIE");
|
||||
}
|
||||
}
|
||||
|
||||
void bmp280_test(void *pvParameters)
|
||||
{
|
||||
bmp280_params_t params;
|
||||
bmp280_init_default_params(¶ms);
|
||||
bmp280_t dev;
|
||||
memset(&dev, 0, sizeof(bmp280_t));
|
||||
|
||||
ESP_ERROR_CHECK(bmp280_init_desc(&dev, BMP280_I2C_ADDRESS_0, 0, I2C_MASTER_SDA, I2C_MASTER_SCL));
|
||||
ESP_ERROR_CHECK(bmp280_init(&dev, ¶ms));
|
||||
|
||||
bool bme280p = dev.id == BME280_CHIP_ID;
|
||||
printf("BMP280: found %s\n", bme280p ? "BME280" : "BMP280");
|
||||
|
||||
float pressure, temperature, humidity;
|
||||
|
||||
while (1)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
if (bmp280_read_float(&dev, &temperature, &pressure, &humidity) != ESP_OK)
|
||||
{
|
||||
printf("Temperature/pressure reading failed\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
/* float is used in printf(). you need non-default configuration in
|
||||
* sdkconfig for ESP8266, which is enabled by default for this
|
||||
* example. see sdkconfig.defaults.esp8266
|
||||
*/
|
||||
last_temperature = temperature;
|
||||
printf("Pressure: %.2f Pa, Temperature: %.2f C", pressure, temperature);
|
||||
if (bme280p)
|
||||
printf(", Humidity: %.2f\n", humidity);
|
||||
else
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t get_temp_handler(httpd_req_t *req)
|
||||
{
|
||||
float current_temp = last_temperature;
|
||||
char response_json[64];
|
||||
snprintf(response_json, sizeof(response_json), "{\"temperature\": %.1f}", current_temp);
|
||||
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
|
||||
httpd_resp_send(req, response_json, HTTPD_RESP_USE_STRLEN);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static const httpd_uri_t temp_uri = {
|
||||
.uri = "/temp",
|
||||
.method = HTTP_GET,
|
||||
.handler = get_temp_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
|
||||
httpd_handle_t start_webserver(void)
|
||||
{
|
||||
httpd_handle_t server = NULL;
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
|
||||
ESP_LOGI(TAG, "Uruchamianie serwera na porcie: %d", config.server_port);
|
||||
if (httpd_start(&server, &config) == ESP_OK) {
|
||||
// Rejestracja handlera dla GET /temp
|
||||
httpd_register_uri_handler(server, &temp_uri);
|
||||
return server;
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "Błąd podczas uruchamiania serwera!");
|
||||
return 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);
|
||||
wifi_init_sta();
|
||||
|
||||
start_webserver();
|
||||
|
||||
ESP_ERROR_CHECK(i2cdev_init());
|
||||
xTaskCreatePinnedToCore(bmp280_test, "bmp280_test", configMINIMAL_STACK_SIZE * 8, NULL, 5, NULL, APP_CPU_NUM);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "thermo_sensor.c" "ygg_http_routes.c" "ygg_bmp280.c" "../../common/esp32/ygg_wifi.c" "../../common/esp32/ygg_http_server.c"
|
||||
INCLUDE_DIRS "." "../../common/esp32")
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_system.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "ygg_http_server.h"
|
||||
#include "ygg_http_routes.h"
|
||||
#include "ygg_wifi.h"
|
||||
#include "ygg_http_server.h"
|
||||
#include "ygg_bmp280.h"
|
||||
|
||||
|
||||
static const char *TAG = "thermo_sensor";
|
||||
float last_temperature = 0.0f;
|
||||
bmp280_t dev_bmp_280;
|
||||
|
||||
void main_loop(void *pvParameters) {
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
ygg_bmp280_read_sensor(&dev_bmp_280);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
wifi_init_sta();
|
||||
|
||||
httpd_handle_t server = ygg_start_webserver();
|
||||
httpd_register_uri_handler(server, &ygg_temp_uri);
|
||||
|
||||
ESP_ERROR_CHECK(i2cdev_init());
|
||||
ygg_bmp280_init(&dev_bmp_280);
|
||||
xTaskCreatePinnedToCore(main_loop, "bmp280_test", configMINIMAL_STACK_SIZE * 8, NULL, 5, NULL, APP_CPU_NUM);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <bmp280.h>
|
||||
#include "ygg_bmp280.h"
|
||||
|
||||
extern float last_temperature;
|
||||
|
||||
void ygg_bmp280_init(bmp280_t *dev) {
|
||||
bmp280_params_t params;
|
||||
bmp280_init_default_params(¶ms);
|
||||
memset(dev, 0, sizeof(bmp280_t));
|
||||
|
||||
ESP_ERROR_CHECK(bmp280_init_desc(dev, BMP280_I2C_ADDRESS_0, 0, I2C_MASTER_SDA, I2C_MASTER_SCL));
|
||||
ESP_ERROR_CHECK(bmp280_init(dev, ¶ms));
|
||||
|
||||
bool bme280p = dev->id == BME280_CHIP_ID;
|
||||
printf("BMP280: found %s\n", bme280p ? "BME280" : "BMP280");
|
||||
}
|
||||
|
||||
void ygg_bmp280_read_sensor(bmp280_t *dev) {
|
||||
float pressure, temperature, humidity;
|
||||
bool bme280p = dev->id == BME280_CHIP_ID;
|
||||
|
||||
if (bmp280_read_float(dev, &temperature, &pressure, &humidity) != ESP_OK)
|
||||
printf("Temperature/pressure reading failed\n");
|
||||
|
||||
last_temperature = temperature;
|
||||
printf("Pressure: %.2f Pa, Temperature: %.2f C", pressure, temperature);
|
||||
if (bme280p)
|
||||
printf(", Humidity: %.2f\n", humidity);
|
||||
else
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef YGG_BMP280_H
|
||||
#define YGG_BMP280_H
|
||||
|
||||
#include <bmp280.h>
|
||||
|
||||
#define I2C_MASTER_SDA 19
|
||||
#define I2C_MASTER_SCL 23
|
||||
|
||||
void ygg_bmp280_init(bmp280_t *dev);
|
||||
void ygg_bmp280_read_sensor(bmp280_t *dev);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "esp_http_server.h"
|
||||
#include "ygg_http_routes.h"
|
||||
|
||||
extern float last_temperature;
|
||||
|
||||
esp_err_t ygg_get_temp_handler(httpd_req_t *req)
|
||||
{
|
||||
float current_temp = last_temperature;
|
||||
char response_json[64];
|
||||
snprintf(response_json, sizeof(response_json), "{\"temperature\": %.1f}", current_temp);
|
||||
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
|
||||
httpd_resp_send(req, response_json, HTTPD_RESP_USE_STRLEN);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
const httpd_uri_t ygg_temp_uri = {
|
||||
.uri = "/temp",
|
||||
.method = HTTP_GET,
|
||||
.handler = ygg_get_temp_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef HTTP_ROUTES_H
|
||||
#define HTTP_ROUTES_H
|
||||
|
||||
esp_err_t ygg_get_temp_handler(httpd_req_t *req);
|
||||
|
||||
extern const httpd_uri_t ygg_temp_uri;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user