diff --git a/ESP32_ESP-IDF/CMakeLists.txt b/ESP32_ESP-IDF/CMakeLists.txt index 36aae95..43b12ca 100644 --- a/ESP32_ESP-IDF/CMakeLists.txt +++ b/ESP32_ESP-IDF/CMakeLists.txt @@ -1,8 +1,5 @@ -# The following lines of boilerplate have to be in your project's -# CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.22) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -# "Trim" the build. Include the minimal set of components, main, and anything it depends on. -idf_build_set_property(MINIMAL_BUILD ON) -project(ESP32_ESP-IDF) + +project(ESP32_ESP_IDF) \ No newline at end of file diff --git a/ESP32_ESP-IDF/main/CMakeLists.txt b/ESP32_ESP-IDF/main/CMakeLists.txt index 6cba8cc..6d715a1 100644 --- a/ESP32_ESP-IDF/main/CMakeLists.txt +++ b/ESP32_ESP-IDF/main/CMakeLists.txt @@ -1,3 +1,22 @@ -idf_component_register(SRCS "main.cpp" - PRIV_REQUIRES spi_flash - INCLUDE_DIRS "") +idf_component_register( + SRCS + "main.cpp" + "can_task.cpp" + "can/can_receiver.cpp" + "mqtt/mqtt_client.cpp" + "wifi/wifi_manager.cpp" + + INCLUDE_DIRS + "." + "can" + "mqtt" + "wifi" + + REQUIRES + driver + esp_wifi + esp_event + esp_netif + mqtt + nvs_flash +) \ No newline at end of file diff --git a/ESP32_ESP-IDF/main/can_task.cpp b/ESP32_ESP-IDF/main/can_task.cpp index 09ff96b..7c214bf 100644 --- a/ESP32_ESP-IDF/main/can_task.cpp +++ b/ESP32_ESP-IDF/main/can_task.cpp @@ -1,6 +1,9 @@ #include "can_receiver.hpp" #include "mqtt_client.hpp" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + void CANTask(void*) { uint16_t ppm; @@ -11,5 +14,7 @@ void CANTask(void*) { MQTTClient::PublishCO2(ppm); } + + vTaskDelay(pdMS_TO_TICKS(10)); } } \ No newline at end of file diff --git a/ESP32_ESP-IDF/main/main.cpp b/ESP32_ESP-IDF/main/main.cpp index 4429393..cbdfda4 100644 --- a/ESP32_ESP-IDF/main/main.cpp +++ b/ESP32_ESP-IDF/main/main.cpp @@ -2,6 +2,11 @@ #include "mqtt_client.hpp" #include "can_receiver.hpp" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +extern void CANTask(void*); + extern "C" void app_main() { WifiManager::Init(); @@ -12,12 +17,5 @@ extern "C" void app_main() CANReceiver::Init(); - xTaskCreate( - CANTask, - "CAN", - 4096, - nullptr, - 5, - nullptr - ); + xTaskCreate(CANTask, "CANTask", 4096, nullptr, 5, nullptr); } \ No newline at end of file diff --git a/ESP32_ESP-IDF/main/mqtt/mqtt_client.cpp b/ESP32_ESP-IDF/main/mqtt/mqtt_client.cpp index 2b4abee..7848acf 100644 --- a/ESP32_ESP-IDF/main/mqtt/mqtt_client.cpp +++ b/ESP32_ESP-IDF/main/mqtt/mqtt_client.cpp @@ -1,12 +1,31 @@ #include "mqtt_client.hpp" #include "mqtt_client.h" +#include "esp_log.h" static esp_mqtt_client_handle_t mqttClient; +static void mqtt_event_handler(void*, esp_event_base_t, int32_t event_id, void*) +{ + switch(event_id) + { + case MQTT_EVENT_CONNECTED: + ESP_LOGI("MQTT", "Connected"); + break; + + case MQTT_EVENT_DISCONNECTED: + ESP_LOGW("MQTT", "Disconnected"); + break; + + default: + break; + } +} + void MQTTClient::Init() { - esp_mqtt_client_config_t cfg = { + esp_mqtt_client_config_t cfg = + { .broker = { .address = @@ -16,7 +35,10 @@ void MQTTClient::Init() } }; - mqttClient = esp_mqtt_client_init(&cfg); + mqttClient = + esp_mqtt_client_init(&cfg); + + esp_mqtt_client_register_event(mqttClient, MQTT_EVENT_ANY, mqtt_event_handler, nullptr); esp_mqtt_client_start(mqttClient); } @@ -25,14 +47,7 @@ void MQTTClient::PublishCO2(uint16_t ppm) { char msg[32]; - sprintf(msg,"%u",ppm); + sprintf(msg, "%u", ppm); - esp_mqtt_client_publish( - mqttClient, - "student/co2", - msg, - 0, - 1, - 0 - ); + esp_mqtt_client_publish(mqttClient, "student/co2", msg, 0, 1, 0); } \ No newline at end of file diff --git a/ESP32_ESP-IDF/main/wifi/wifi_manager.cpp b/ESP32_ESP-IDF/main/wifi/wifi_manager.cpp index ca3cfdd..bd2e422 100644 --- a/ESP32_ESP-IDF/main/wifi/wifi_manager.cpp +++ b/ESP32_ESP-IDF/main/wifi/wifi_manager.cpp @@ -1,5 +1,6 @@ +#include #include "wifi_manager.hpp" - +#include "esp_netif.h" #include "esp_wifi.h" #include "esp_event.h" #include "nvs_flash.h"