From 0b9f5e2997506ba70b30d4775b23176ff6281cf4b02b4602da8d2d1a634a8787 Mon Sep 17 00:00:00 2001 From: localhorst Date: Thu, 19 Dec 2024 18:58:05 +0100 Subject: [PATCH] own wifi module --- .vscode/settings.json | 5 +- README.md | 10 ++++ main/CMakeLists.txt | 4 +- main/main.c | 16 ++++--- main/metrics.c | 104 ---------------------------------------- main/metrics.h | 2 - main/sntp.c | 0 main/sntp.h | 0 main/wifi.c | 108 ++++++++++++++++++++++++++++++++++++++++++ main/wifi.h | 3 ++ 10 files changed, 137 insertions(+), 115 deletions(-) create mode 100644 main/sntp.c create mode 100644 main/sntp.h create mode 100644 main/wifi.c create mode 100644 main/wifi.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 884c37d..34ff52a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,7 +17,10 @@ "type_traits": "c", "cmath": "c", "*.tcc": "c", - "*.inc": "c" + "*.inc": "c", + "safety.h": "c", + "event_groups.h": "c", + "string.h": "c" }, "idf.openOcdConfigs": [ "board/esp32-wrover-kit-3.3v.cfg" diff --git a/README.md b/README.md index 9b900e1..f0dbad7 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,16 @@ Safety <|-- HTTP_Metrics +getSafetyState() } + + class Wifi{ + +initWifi() + } + + class SNTP{ + +initSntp() + +getSntpState() + } + class Metrics{ +initMetrics() -taskMetrics() diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index fda1709..cef8b5e 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,7 +1,9 @@ -idf_component_register(SRCS "main.c" +idf_component_register(SRCS "sntp.c" "wifi.c" "main.c" "metrics.c" "inputs.c" "outputs.c" "control.c" "safety.c" + "wifi.c" + "sntp.c" INCLUDE_DIRS ".") diff --git a/main/main.c b/main/main.c index 8751ec7..a34a9bc 100644 --- a/main/main.c +++ b/main/main.c @@ -20,6 +20,7 @@ #include "outputs.h" #include "inputs.h" #include "control.h" +#include "wifi.h" #define I2C_MASTER_SCL 19 #define I2C_MASTER_SDA 18 @@ -65,21 +66,22 @@ void app_main(void) // initOutputs(); // initInputs(); // initSafety(); + initWifi(); // initControl(); - initMetrics(); + // initMetrics(); - esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); - esp_sntp_setservername(0, "10.1.0.1"); // Set first NTP server local router + // esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); + // esp_sntp_setservername(0, "10.1.0.1"); // Set first NTP server local router - sntp_set_time_sync_notification_cb(time_sync_notification_cb); // Register the SNTP time sync callback - esp_sntp_init(); + // sntp_set_time_sync_notification_cb(time_sync_notification_cb); // Register the SNTP time sync callback + // esp_sntp_init(); while (1) { vTaskDelay(pdMS_TO_TICKS(1000)); // Print the current time - print_current_time(); + // print_current_time(); - ESP_LOGI(TAG, "SNTP Server 0 reachability: %i", esp_sntp_getreachability(0)); + // ESP_LOGI(TAG, "SNTP Server 0 reachability: %i", esp_sntp_getreachability(0)); } } \ No newline at end of file diff --git a/main/metrics.c b/main/metrics.c index 2823dbb..1a3886d 100644 --- a/main/metrics.c +++ b/main/metrics.c @@ -1,28 +1,15 @@ -#include -#include #include #include "esp_timer.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp_system.h" -#include "spi_flash_mmap.h" #include "esp_wifi.h" -#include "esp_event.h" -#include "freertos/event_groups.h" #include "esp_log.h" -#include "esp_netif.h" -#include -#include -#include -#include #include "metrics.h" #include "outputs.h" #include "inputs.h" #include "safety.h" -static EventGroupHandle_t s_wifi_event_group; - static const char *TAG = "smart-oil-heater-control-system-metrics"; char caHtmlResponse[HTML_RESPONSE_SIZE]; @@ -31,15 +18,11 @@ static sMetric aMetrics[METRIC_MAX_COUNT]; static uint16_t u16MetricCounter = 0U; void taskMetrics(void *pvParameters); -void connect_wifi(void); httpd_handle_t setup_server(void); esp_err_t get_metrics_handler(httpd_req_t *req); void initMetrics(void) { - connect_wifi(); - return; - setup_server(); BaseType_t taskCreated = xTaskCreate( @@ -227,93 +210,6 @@ void vSetMetrics(sMetric *paMetrics, uint16_t u16Size) } } -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) - { - esp_wifi_connect(); - ESP_LOGI(TAG, "Retry to connect to the AP"); - } - 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)); - xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); - } -} - -void connect_wifi(void) -{ - s_wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - - esp_netif_t *my_sta = esp_netif_create_default_wifi_sta(); - esp_netif_dhcpc_stop(my_sta); - esp_netif_ip_info_t ip_info; - ip_info.ip.addr = ipaddr_addr(CONFIG_STATIC_IP_ADDR); - ip_info.gw.addr = ipaddr_addr(CONFIG_STATIC_GATEWAY_IP_ADDR); - ip_info.netmask.addr = ipaddr_addr(CONFIG_STATIC_IP_NETMASK); - esp_netif_set_ip_info(my_sta, &ip_info); - - 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 = CONFIG_SSID, - .password = CONFIG_WIFI_PASSWORD, - .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_ERROR_CHECK(esp_wifi_set_max_tx_power(78)); // Set max power to 19.5 dBm (78 in units of 0.25 dBm) - ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM)); // Use power-saving mode - - 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", CONFIG_SSID); - } - else if (bits & WIFI_FAIL_BIT) - { - ESP_LOGI(TAG, "Failed to connect to SSID:%s", CONFIG_SSID); - } - else - { - ESP_LOGE(TAG, "Unexpected event"); - } - vEventGroupDelete(s_wifi_event_group); -} - esp_err_t get_metrics_handler(httpd_req_t *req) { if (xSemaphoreTakeRecursive(xMutexAccessMetricResponse, pdMS_TO_TICKS(5000)) == pdTRUE) diff --git a/main/metrics.h b/main/metrics.h index 7e8a161..ad7dd23 100644 --- a/main/metrics.h +++ b/main/metrics.h @@ -2,8 +2,6 @@ #include -#define WIFI_CONNECTED_BIT BIT0 -#define WIFI_FAIL_BIT BIT1 #define HTML_RESPONSE_SIZE 1024U #define METRIC_NAME_MAX_SIZE 256U #define METRIC_MAX_COUNT 64U diff --git a/main/sntp.c b/main/sntp.c new file mode 100644 index 0000000..473a0f4 diff --git a/main/sntp.h b/main/sntp.h new file mode 100644 index 0000000..473a0f4 diff --git a/main/wifi.c b/main/wifi.c new file mode 100644 index 0000000..d1fd0c1 --- /dev/null +++ b/main/wifi.c @@ -0,0 +1,108 @@ +#include +#include "esp_timer.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "freertos/event_groups.h" +#include "esp_log.h" +#include "esp_netif.h" +#include + +#include "wifi.h" + +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 + +static const char *TAG = "smart-oil-heater-control-system-wifi"; + +static EventGroupHandle_t s_wifi_event_group; +static void event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data); + +void initWifi(void) +{ + s_wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + esp_netif_t *my_sta = esp_netif_create_default_wifi_sta(); + esp_netif_dhcpc_stop(my_sta); + esp_netif_ip_info_t ip_info; + ip_info.ip.addr = ipaddr_addr(CONFIG_STATIC_IP_ADDR); + ip_info.gw.addr = ipaddr_addr(CONFIG_STATIC_GATEWAY_IP_ADDR); + ip_info.netmask.addr = ipaddr_addr(CONFIG_STATIC_IP_NETMASK); + esp_netif_set_ip_info(my_sta, &ip_info); + + 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 = CONFIG_SSID, + .password = CONFIG_WIFI_PASSWORD, + .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_ERROR_CHECK(esp_wifi_set_max_tx_power(78)); // Set max power to 19.5 dBm (78 in units of 0.25 dBm) + ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM)); // Use power-saving mode + + 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", CONFIG_SSID); + } + else if (bits & WIFI_FAIL_BIT) + { + ESP_LOGI(TAG, "Failed to connect to SSID:%s", CONFIG_SSID); + } + else + { + ESP_LOGE(TAG, "Unexpected event"); + } + vEventGroupDelete(s_wifi_event_group); +} + +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) + { + esp_wifi_connect(); + ESP_LOGI(TAG, "Retry to connect to the AP"); + } + 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)); + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + } +} \ No newline at end of file diff --git a/main/wifi.h b/main/wifi.h new file mode 100644 index 0000000..380c625 --- /dev/null +++ b/main/wifi.h @@ -0,0 +1,3 @@ +#pragma once + +void initWifi(void); \ No newline at end of file