OK it was not wise to ask question with some assumptions. Here is the detailed version.
I have 2 task, 1 for MQTT and 1 for LED. I want to read a data over MQTT and take an action to turn on an LED.
Here is my task declarations:
void taskMqtt(void *param);
xTaskHandle handleTaskMqtt;
void taskLed(void *param);
xTaskHandle handleTaskLed;
Here is the task creation part
status = xTaskCreate(taskMqtt, "taskMqtt", 4096, NULL, 2, handleTaskMqtt);
configASSERT(status == pdPASS);
ESP_LOGI(TAG, "taskMqtt CREATED");
status = xTaskCreate(taskLed, "taskLed", 4096, NULL, 2, &handleTaskLed);
configASSERT(status == pdPASS);
ESP_LOGI(TAG, "taskLed CREATED");
I created my Mqtt object like this and it works fine.
Mqtt mqtt("192.168.137.212", move(uptrWifi), handleTaskLed);
Here is the Mqtt.h file to observe class declaration
class Mqtt
{
private:
std::unique_ptr<Wifi> uptrWifi;
static xTaskHandle handlerTask;
static const char *subscribeTopic;
static const char *publishTopic;
static esp_mqtt_client_config_t _mqttCfg;
static esp_mqtt_client_handle_t _clientCfg;
static void mqttEventHandler(void *arg, esp_event_base_t eventBase, int32_t eventId, void *eventData);
public:
Mqtt(const char *brokerIp, std::unique_ptr<Wifi> uptrToWifi, xTaskHandle taskToNotify);
void init(void);
void sendDataOverMqtt(std::string title, int data);
void sendDataOverMqtt(std::string title, int data, std::string pubTopic);
static void getDataOverMqtt(char *data, int dataLen);
void setTopics(std::string subTopic, std::string pubTopic);
};
In Mqtt.cpp file I defined
xTaskHandle Mqtt::handlerTask;
and constructor
Mqtt::Mqtt(const char *brokerIp, std::unique_ptr<Wifi> uptrToWifi,
xTaskHandle taskToNotify)
{
_mqttCfg.host = brokerIp;
ESP_LOGI("MQTT_H", "MQTT BROKER IP SET\n");
uptrWifi = move(uptrToWifi);
handlerTask = taskToNotify;
}
and here is my function for read data and forward to the LED task.
void Mqtt::getDataOverMqtt(char *data, int dataLen)
{
static uint32_t valueToSend = 1;
if (strncmp(data, "Led ON", 6) == 0)
{
ESP_LOGI(TAG, "\nDATA COMES \n");
if (handlerTask != NULL)
{
xTaskNotify(handlerTask, valueToSend, eSetValueWithOverwrite);
}
else
{
printf("\nNULL pointer\n");
fflush(stdout);
}
printf("\nCMD=%.*s\n\n", dataLen, data);
}
else if (strncmp(data, "Led OFF", 7) == 0)
{
printf("\nCMD=%.*s\n\n", dataLen, data);
}
}
And here is the LED task
void taskLed(void *param)
{
uint32_t ulNotifiedValue = false;
while (1)
{
ESP_LOGI(TAG, "LED WAITING");
xTaskNotifyWait(0, ULONG_MAX, &ulNotifiedValue, portMAX_DELAY);
ESP_LOGI(TAG, "\n NOTIFICATION \n");
ESP_LOGI(TAG, "LED TASK RUNNING");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
When I run the code and sent Led ON command, I can see the logs
LED WAITING (from taskLED definition)
DATA COMES (from getDataOverMqtt definition)
Null Pointer (from getDataOverMqtt definition)
After that I was hoping to see on the logs
NOTIFICATION (from taskLED defition)
////////////////////////////////////// ORIGINAL POST BEFORE EDITS IS BELOW
I am trying to learn FreeRtos and C++.
I need to pass taskHandle to another class and want to use them for notify function. What is the best way to do that?
In taskControl.cpp file I created tasks as below.
void task1(void *param);
TaskHandle_t handleTask1;
void task2(void *param);
TaskHandle_t handleTask2;
status = xTaskCreate(task1, "task1", 4096, NULL, 2, &handleTask1);
configASSERT(status == pdPASS);
ESP_LOGI(TAG, "task1 CREATED");
status = xTaskCreate(task2, "task2", 4096, NULL, 2, &handleTask2);
configASSERT(status == pdPASS);
ESP_LOGI(TAG, "task2 CREATED");
I thought I can pass handleTask2 into another class via constructor
M m(&handleTask2);
In classM.h file, M is declared as below
class M
{
private:
static TaskHandle_t *handlerTask;
public:
M(TaskHandle_t *taskToNotify)
{
handlerTask = taskToNotify;
}
};
And I tried to use handlerTask as below in classM.cpp file.
xTaskNotify(&handlerTask, valueToSend, eSetValueWithoutOverwrite);
What am I missing? What is the correct way to do this kind of thing?
Sorry for if my question is not clear.