added challenge_com,

JSON parsing not yet implemented,
improved main workflow for challenge_app,
updated CMakeLists
main
Jonas Arnold 4 years ago
parent e38ca71a99
commit 6b7d7e0635
  1. 3
      ADIS_ESP32_Eclipse/main/CMakeLists.txt
  2. 39
      ADIS_ESP32_Eclipse/main/challenge_app.c
  3. 66
      ADIS_ESP32_Eclipse/main/challenge_com.c
  4. 19
      ADIS_ESP32_Eclipse/main/challenge_com.h
  5. 7
      ADIS_ESP32_Eclipse/main/myMqtt.c

@ -18,6 +18,9 @@ idf_component_register(
"robot.c" "robot.c"
"myMqtt.c" "myMqtt.c"
"application.c" "application.c"
"challenge_app.c"
"splitflap_wrapper.c"
"challenge_com.c"
INCLUDE_DIRS INCLUDE_DIRS
"." "."

@ -12,6 +12,7 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "myMqtt.h" #include "myMqtt.h"
#include "wifi.h" #include "wifi.h"
#include "challenge_com.h"
/* LOCAL Var */ /* LOCAL Var */
/* identifies robot Mode for the challenge: true = robot is stationary, false = robot is moveable */ /* 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){ static void appTask(void *pv){
if(pv != NULL){ /*Task argument not used
* if(pv != NULL){
printf("task argument: %s\n", (char*)pv); 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){ while(WiFi_isConnected() == false){
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
} }
// wifi now connected // wifi now connected
printf("Application task detected that WiFi is connected\n"); ESP_LOGI("Challenge Application task detected that WiFi is connected.");
if(MyMqtt_Init()){ // endless loop: always reconnect to MQTT when something fails
vTaskDelay(pdMS_TO_TICKS(1000)); while(1){
printf("5s done\n");
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"); MyMqtt_Subscribe("scada/status");
printf("Application endless task was started\n");
fflush(stdout);
// endless loop // endless loop
for(;;){ for(;;){
MyMqtt_Publish("scada/status", "Testmessage from ESP"); MyMqtt_Publish("scada/status", "Testmessage from ESP");
vTaskDelay(pdMS_TO_TICKS(10000)); 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
}
} }

@ -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;
}

@ -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_ */

@ -11,6 +11,7 @@
#include <stdint.h> #include <stdint.h>
#include "esp_log.h" #include "esp_log.h"
#include "mqtt_client.h" #include "mqtt_client.h"
#include "challenge_com.h"
// Settings // Settings
#define CONFIG_BROKER_URL "mqtt://10.180.254.80" #define CONFIG_BROKER_URL "mqtt://10.180.254.80"
@ -32,13 +33,17 @@ bool MyMqtt_Init(void){
client = esp_mqtt_client_init(&mqtt_cfg); 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 */ /* 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, 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); 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; return mqtt_error == ESP_OK;
} }
void MyMqtt_Deinit(void){ void MyMqtt_Deinit(void){
client = NULL; esp_mqtt_client_destroy(client);
} }
void MyMqtt_Publish(const char *topic, const char *data){ void MyMqtt_Publish(const char *topic, const char *data){

Loading…
Cancel
Save