From e00f3a9ede1b8e46b480bd68daf48da0bb08acae Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Thu, 4 Sep 2025 01:11:11 +0200 Subject: Initial --- src/CMakeLists.txt | 6 ++++ src/main.c | 68 +++++++++++++++++++++++++++++++++++++++++++ src/mqtt.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/wifi.c | 49 +++++++++++++++++++++++++++++++ src/wifi_scan.c | 61 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 269 insertions(+) create mode 100644 src/CMakeLists.txt create mode 100644 src/main.c create mode 100644 src/mqtt.c create mode 100644 src/wifi.c create mode 100644 src/wifi_scan.c (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..483bc0c --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,6 @@ +# This file was automatically generated for projects +# without default 'CMakeLists.txt' file. + +FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) + +idf_component_register(SRCS ${app_sources}) diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..790959d --- /dev/null +++ b/src/main.c @@ -0,0 +1,68 @@ +#include "esp_event.h" +#include "esp_log.h" +#include "esp_system.h" +#include "esp_timer.h" +#include "nvs_flash.h" + +#include "mqtt.h" +#include "wifi.h" + +static const char *TAG = "app"; + +/** + * @brief Initialize NVS flash storage, handling full or incompatible pages. + * + * @return esp_err_t ESP_OK on success, error code otherwise. + */ +static esp_err_t nvs_flash_init_check(void) +{ + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || + ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_LOGW(TAG, + "NVS partition was full or incompatible, erasing..."); + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + if (ret != ESP_OK) + ESP_LOGE(TAG, "Failed to initialize NVS (%s)", + esp_err_to_name(ret)); + return ret; +} + +/** + * @brief Application entry point. + * + * Initializes NVS, network interface, event loop, + * WiFi station, and MQTT client with proper error handling and logging. + */ +void app_main(void) +{ + esp_err_t ret = nvs_flash_init_check(); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "NVS init failed, aborting app start"); + return; + } + + ret = esp_netif_init(); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize network interface (%s)", + esp_err_to_name(ret)); + return; + } + + ret = esp_event_loop_create_default(); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to create event loop (%s)", + esp_err_to_name(ret)); + return; + } + + ESP_LOGI(TAG, "Initializing WiFi in station mode"); + wifi_init_sta(); + + ESP_LOGI(TAG, "Starting MQTT client"); + mqtt_app_start(); + + ESP_LOGI(TAG, "Application setup completed"); +} diff --git a/src/mqtt.c b/src/mqtt.c new file mode 100644 index 0000000..716feec --- /dev/null +++ b/src/mqtt.c @@ -0,0 +1,85 @@ +#include "mqtt.h" +#include "config.h" +#include "esp_log.h" +#include "esp_timer.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include +#include +#include + +#ifndef BUFFER_SIZE +#define BUFFER_SIZE 64 +#endif + +#ifndef PUBLISH_INTERVAL_MS +#define PUBLISH_INTERVAL_MS 1000 +#endif + +#ifndef PUBLISHER_TASK_STACK_SIZE +#define PUBLISHER_TASK_STACK_SIZE 4096 +#endif + +#ifndef PUBLISHER_TASK_PRIORITY +#define PUBLISHER_TASK_PRIORITY 5 +#endif + +static esp_mqtt_client_handle_t client; + +static 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: + printf("MQTT connected!\n"); + esp_mqtt_client_subscribe(event->client, SUB_TOPIC, 1); + break; + + case MQTT_EVENT_DATA: { + char topic[event->topic_len + 1]; + char data[event->data_len + 1]; + memcpy(topic, event->topic, event->topic_len); + topic[event->topic_len] = '\0'; + memcpy(data, event->data, event->data_len); + data[event->data_len] = '\0'; + + printf("MQTT Msg received\nTopic: %s\nData: %s\n", topic, data); + + char *endptr; + long long sent = strtoll(data, &endptr, 10); + if (endptr != data) { + long long now = esp_timer_get_time() / 1000ULL; + long long rtt = now - sent; + printf("RTT: %lld ms\n", rtt); + } + break; + } + + default: + break; + } +} + +static void publisher_task(void *pvParameters) { + while (1) { + char buf[BUFFER_SIZE]; + snprintf(buf, BUFFER_SIZE, "%lld", + (long long)(esp_timer_get_time() / 1000ULL)); + esp_mqtt_client_publish(client, PUB_TOPIC, buf, 0, 1, 0); + vTaskDelay(pdMS_TO_TICKS(PUBLISH_INTERVAL_MS)); + } +} + +void mqtt_app_start(void) { + esp_mqtt_client_config_t mqtt_cfg = { + .broker.address.uri = MQTT_URI, + }; + client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, + NULL); + esp_mqtt_client_start(client); + + xTaskCreate(publisher_task, "publisher_task", PUBLISHER_TASK_STACK_SIZE, NULL, + PUBLISHER_TASK_PRIORITY, NULL); +} diff --git a/src/wifi.c b/src/wifi.c new file mode 100644 index 0000000..8d6b29d --- /dev/null +++ b/src/wifi.c @@ -0,0 +1,49 @@ +#include "wifi.h" +#include "config.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_wifi.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include + +#define WIFI_SCAN_DELAY_MS 3000 + +/** + * @brief Initialize WiFi in station mode. + * + * This function performs the following steps: + * - Creates the default WiFi station network interface. + * - Initializes the WiFi driver with default configuration. + * - Sets WiFi mode to station. + * - Starts WiFi. + * - Starts scanning for available WiFi networks. + * - Waits for a predefined delay to allow scanning to complete. + * - Configures the WiFi station with SSID and password. + * - Connects to the configured WiFi network. + * + * The SSID and password are defined in the configuration headers. + */ +void wifi_init_sta(void) { + esp_netif_create_default_wifi_sta(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + if (esp_wifi_init(&cfg) != ESP_OK) { + return; + } + + esp_wifi_set_mode(WIFI_MODE_STA); + esp_wifi_start(); + + wifi_scan_start(); + vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_DELAY_MS)); + + wifi_config_t wifi_config = {0}; + strncpy((char *)wifi_config.sta.ssid, WIFI_SSID, + sizeof(wifi_config.sta.ssid) - 1); + strncpy((char *)wifi_config.sta.password, WIFI_PASS, + sizeof(wifi_config.sta.password) - 1); + + esp_wifi_set_config(WIFI_IF_STA, &wifi_config); + esp_wifi_connect(); +} diff --git a/src/wifi_scan.c b/src/wifi_scan.c new file mode 100644 index 0000000..e588cfc --- /dev/null +++ b/src/wifi_scan.c @@ -0,0 +1,61 @@ +#include "esp_log.h" +#include "esp_wifi.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#define WIFI_SCAN_TASK_STACK_SIZE 4096 +#define WIFI_SCAN_TASK_PRIORITY 5 +#define WIFI_SCAN_MAX_APS 20 +#define WIFI_SCAN_POLL_INTERVAL_MS 500 +#define WIFI_SCAN_DEFAULT_CHANNEL 0 +#define WIFI_SCAN_SHOW_HIDDEN true +#define WIFI_SCAN_TASK_NAME "wifi_scan_task" + +/** + * @brief Task to perform WiFi scanning asynchronously. + * + * Starts a WiFi scan, polls for scan completion, + * retrieves and logs the found access points, then deletes itself. + * + * @param pvParameter Task parameter (unused). + */ +static void wifi_scan_task(void *pvParameter) { + uint16_t ap_count = WIFI_SCAN_MAX_APS; + static wifi_ap_record_t ap_info[WIFI_SCAN_MAX_APS]; + wifi_scan_config_t scan_config = { + .ssid = NULL, + .bssid = NULL, + .channel = WIFI_SCAN_DEFAULT_CHANNEL, + .show_hidden = WIFI_SCAN_SHOW_HIDDEN, + }; + + ESP_LOGI("wifi_scan", "Starting Wi-Fi scan..."); + ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); + + while (true) { + uint16_t finished_ap_count = 0; + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&finished_ap_count)); + if (finished_ap_count > 0) + break; + vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_POLL_INTERVAL_MS)); + } + + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); + ESP_LOGI("wifi_scan", "Found %d access points:", ap_count); + for (uint16_t ap_index = 0; ap_index < ap_count; ++ap_index) { + ESP_LOGI("wifi_scan", "%d: SSID: %s, RSSI: %d", ap_index + 1, + ap_info[ap_index].ssid, ap_info[ap_index].rssi); + } + + vTaskDelete(NULL); +} + +/** + * @brief Starts the WiFi scan task. + * + * Creates a FreeRTOS task that runs the WiFi scan asynchronously. + */ +void wifi_scan_start(void) { + xTaskCreate(wifi_scan_task, WIFI_SCAN_TASK_NAME, WIFI_SCAN_TASK_STACK_SIZE, + NULL, WIFI_SCAN_TASK_PRIORITY, NULL); +} -- cgit v1.2.3