From 6b7d7e063592f41422559b378d802a05e36a8ff6 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Mon, 5 Dec 2022 20:22:34 +0100 Subject: [PATCH] added challenge_com, JSON parsing not yet implemented, improved main workflow for challenge_app, updated CMakeLists --- ADIS_ESP32_Eclipse/main/CMakeLists.txt | 3 ++ ADIS_ESP32_Eclipse/main/challenge_app.c | 55 ++++++++++++--------- ADIS_ESP32_Eclipse/main/challenge_com.c | 66 +++++++++++++++++++++++++ ADIS_ESP32_Eclipse/main/challenge_com.h | 19 +++++++ ADIS_ESP32_Eclipse/main/myMqtt.c | 7 ++- 5 files changed, 125 insertions(+), 25 deletions(-) create mode 100644 ADIS_ESP32_Eclipse/main/challenge_com.c create mode 100644 ADIS_ESP32_Eclipse/main/challenge_com.h diff --git a/ADIS_ESP32_Eclipse/main/CMakeLists.txt b/ADIS_ESP32_Eclipse/main/CMakeLists.txt index 8696903..4717782 100644 --- a/ADIS_ESP32_Eclipse/main/CMakeLists.txt +++ b/ADIS_ESP32_Eclipse/main/CMakeLists.txt @@ -18,6 +18,9 @@ idf_component_register( "robot.c" "myMqtt.c" "application.c" + "challenge_app.c" + "splitflap_wrapper.c" + "challenge_com.c" INCLUDE_DIRS "." diff --git a/ADIS_ESP32_Eclipse/main/challenge_app.c b/ADIS_ESP32_Eclipse/main/challenge_app.c index c66dec9..d9372fa 100644 --- a/ADIS_ESP32_Eclipse/main/challenge_app.c +++ b/ADIS_ESP32_Eclipse/main/challenge_app.c @@ -12,6 +12,7 @@ #include "freertos/task.h" #include "myMqtt.h" #include "wifi.h" +#include "challenge_com.h" /* LOCAL Var */ /* identifies robot Mode for the challenge: true = robot is stationary, false = robot is moveable */ @@ -22,38 +23,44 @@ static TaskHandle_t appTaskHandle; /* task handle */ static void appTask(void *pv){ - if(pv != NULL){ + /*Task argument not used + * if(pv != NULL){ printf("task argument: %s\n", (char*)pv); - } - - printf("Application was started"); - fflush(stdout); + }*/ + bool myMqttInitSuccess = false; + ESP_LOGI("Challenge application was started. Waiting for WiFi connection...."); while(WiFi_isConnected() == false){ vTaskDelay(pdMS_TO_TICKS(1000)); } - // wifi now connected - printf("Application task detected that WiFi is connected\n"); - if(MyMqtt_Init()){ - vTaskDelay(pdMS_TO_TICKS(1000)); - printf("5s done\n"); - - MyMqtt_Subscribe("scada/status"); - - printf("Application endless task was started\n"); - fflush(stdout); - - // endless loop - for(;;){ - MyMqtt_Publish("scada/status", "Testmessage from ESP"); - - vTaskDelay(pdMS_TO_TICKS(10000)); + ESP_LOGI("Challenge Application task detected that WiFi is connected."); + // endless loop: always reconnect to MQTT when something fails + while(1){ + + ESP_LOGI("MyMqtt Initializing...."); + myMqttInitSuccess = MyMqtt_Init(); + if(myMqttInitSuccess){ + ESP_LOGI("MyMqtt Initialization successful."); + ESP_LOGI("Challenge Application started."); + + MyMqtt_Subscribe(MQTT_TOPIC_SF_INITALL); + MyMqtt_Subscribe(MQTT_TOPIC_SF_DISPLAY); + MyMqtt_Subscribe(MQTT_TOPIC_SF_CONFIG_SETUP); + MyMqtt_Subscribe("scada/status"); + + // endless loop + for(;;){ + MyMqtt_Publish("scada/status", "Testmessage from ESP"); + + vTaskDelay(pdMS_TO_TICKS(10000)); + } } - } else { - printf("Init of MyMqtt failed! Quitting application task.\n"); - } + ESP_LOGE(myMqttInitSuccess?"Something in challenge app failed. Re-initializing MQTT in 5s.":"Init of MyMqtt failed! Retrying in 5s."); + MyMqtt_Deinit(); + if(myMqttInitSuccess == false) vTaskDelay(5000); // only wait if the init is the issue + } } diff --git a/ADIS_ESP32_Eclipse/main/challenge_com.c b/ADIS_ESP32_Eclipse/main/challenge_com.c new file mode 100644 index 0000000..cbd0888 --- /dev/null +++ b/ADIS_ESP32_Eclipse/main/challenge_com.c @@ -0,0 +1,66 @@ +/* + * challenge_com.c + * + * Created on: 05.12.2022 + * Author: jonas + */ + +#include "stdbool.h" +#include "challenge_app.h" + +#define TAG "CHALLENGE_COM" /* tag for logging with ESP_LOG */ + +static void Challenge_Com_ParseMqtt(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data){ + /* INFO + * TOPIC: event->topic + * TOPIC LENGTH: event->topic_len + * DATA: event->data + * DATA LENGTH: event->data_len + * CLIENT: event->client + */ + + esp_mqtt_event_handle_t event = event_data; + bool handled = false; + + // check if event id is MQTT_EVENT_DATA => break if not + if((esp_mqtt_event_id_t)event_id != MQTT_EVENT_DATA) { + ESP_LOGE(TAG, "Wrong data received. Can only handle MQTT_EVENT_DATA (event id %i). But received event id: %i", MQTT_EVENT_DATA, (esp_mqtt_event_id_t)event_id); + return; + } + + /* check topic and call related command if allowed by robot mode */ + /* stationary robot allowed commands */ + if(Challenge_App_GetRobotMode() == true) { + if(McuUtility_strcmp((char*)event->topic, MQTT_TOPIC_SF_CONFIG_SETUP)==0){ + handled = true; + // TODO JSON PARSER + uint8_t setupId = 0; + uint8_t hwId = 1; + SplitFlap_Wrapper_SetHardwareIdentifier(setupId, hwId); + } else if(McuUtility_strcmp((char*)event->topic, MQTT_TOPIC_SF_INITALL)==0){ + handled = true; + SplitFlap_Wrapper_MoveAllToZeroPosition(); + } else if(McuUtility_strcmp((char*)event->topic, MQTT_TOPIC_SF_DISPLAY)==0){ + handled = true; + // TODO JSON PARSER + char message[] = "TST"; + SplitFlap_Wrapper_Display(message); + } + } + /* mobile robot allowed commands */ + else{ + + } + + /* both robot modes allowed commands */ + + + + if(handled == false) + { + ESP_LOGE(TAG, "Received data could not be handled. Topic was %s", event->topic); + } + + return; +} + diff --git a/ADIS_ESP32_Eclipse/main/challenge_com.h b/ADIS_ESP32_Eclipse/main/challenge_com.h new file mode 100644 index 0000000..9617f9e --- /dev/null +++ b/ADIS_ESP32_Eclipse/main/challenge_com.h @@ -0,0 +1,19 @@ +/* + * challenge_communication.h + * + * Created on: 05.12.2022 + * Author: jonas + */ + +#ifndef MAIN_CHALLENGE_COM_H_ +#define MAIN_CHALLENGE_COM_H_ + +/* TOPICS */ +const char MQTT_TOPIC_SF_DISPLAY[] = "/splitFlap/cmd/display/"; +const char MQTT_TOPIC_SF_INITALL[] = "/splitFlap/cmd/init/"; +const char MQTT_TOPIC_SF_CONFIG_SETUP[] = "/splitFlap/config/setup/"; + + + + +#endif /* MAIN_CHALLENGE_COM_H_ */ diff --git a/ADIS_ESP32_Eclipse/main/myMqtt.c b/ADIS_ESP32_Eclipse/main/myMqtt.c index 984245f..8edf14d 100644 --- a/ADIS_ESP32_Eclipse/main/myMqtt.c +++ b/ADIS_ESP32_Eclipse/main/myMqtt.c @@ -11,6 +11,7 @@ #include #include "esp_log.h" #include "mqtt_client.h" +#include "challenge_com.h" // Settings #define CONFIG_BROKER_URL "mqtt://10.180.254.80" @@ -32,13 +33,17 @@ bool MyMqtt_Init(void){ client = esp_mqtt_client_init(&mqtt_cfg); /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + esp_mqtt_client_register_event(client, MQTT_EVENT_DATA, Challenge_Com_ParseMqtt, NULL); // ToDo: maybe make this configurable via parameter esp_err_t mqtt_error = esp_mqtt_client_start(client); + // allow 1s to finish mqtt client to start + vTaskDelay(1000); + return mqtt_error == ESP_OK; } void MyMqtt_Deinit(void){ - client = NULL; + esp_mqtt_client_destroy(client); } void MyMqtt_Publish(const char *topic, const char *data){