MQTT onMessage function - char* == char

I'm unable to get any further here... I still don't seem to get the difference between a pointer and a variable, so maybe that's where I am stuck..

The callback executes this function and reads out on the serial monitor the MQTT message to which the microcontroller is subscribed. So I figured, I could throw in this if statement to make to do stuff based on the payload and topic...

#include <AsyncMqttClient.h> 

#define MQTT_SUB_GREENLED "esp32a/output/greenled"




mqttClient.onMessage(onMqttMessage);

void onMqttMessage(char* topic,char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  Serial.println("Publish received.");
  Serial.print("  topic: ");
  Serial.println(topic);
  Serial.print("  payload: ");
  Serial.println(payload);
  Serial.print("  qos: ");
  Serial.println(properties.qos);
  Serial.print("  dup: ");
  Serial.println(properties.dup);
  Serial.print("  retain: ");
  Serial.println(properties.retain);
  Serial.print("  len: ");
  Serial.println(len);
  Serial.print("  index: ");
  Serial.println(index);
  Serial.print("  total: ");
  Serial.println(total);



if (topic == MQTT_SUB_GREENLED) {
  Serial.println("~~~GreenLED triggered~~~~");
  GreenLED(payload,200);                          
}



// ***GreenLED***

const int GREENLED_PIN = 17;
int g;

void GreenLED(int GreenLEDFlashes, int GreenLEDDelay) {
  for(g = 0; g < GreenLEDFlashes; g++) {
  digitalWrite(GREENLED_PIN, HIGH);
  delay(GreenLEDDelay);
  digitalWrite(GREENLED_PIN, LOW);
  delay(GreenLEDDelay);  
  }
}

I've tried converting to a string or other data type, or using string compare but I always get errors..
Ultimately, I'd like to send more than just payload over MQTT but also other variables to plug into my function:
if (topic == MQTT_SUB_GREENLED)
{ Serial.println("~GreenLED triggered~~");
GreenLED (payload, delay, brightness, whatever.)

This shouldn't be a problem from the Broker and Node-Red side, as long as I can pull it properly using the OnMessage function.

Thank You!

if (topic == MQTT_SUB_GREENLED) {
  Serial.println("~~~GreenLED triggered~~~~");
  GreenLED(payload,200);                         
}

You can not use == with c-string character literals. strcmp() is the function used for comparison.

http://www.cplusplus.com/reference/cstring/strcmp/

Try

if(strcmp(topic, MQTT_SUB_GREENLED) == 0) //match
{
Serial.println("~~~GreenLED triggered~~~~");
GreenLED(payload,200);  
}

Brilliant. Thanks

Any idea how to make the payload do things? I guess the MQTT library coverts the payload to a char*. I figured I could use it to bring into my function but I get a conversion error.

I'd also like to send MQTT messages other than payload, ie. payload2. From what I understand any message can be connected to a topic - not just payload. In this example it would control the number of flashes and delay.

thanks

// *************BAUSTELLE********


void onMqttMessage(char* topic,char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  Serial.println("Publish received.");
  Serial.print("  topic: ");
  Serial.println(topic);
  Serial.print("  payload: ");
  Serial.println(payload);
  Serial.print("  qos: ");
  Serial.println(properties.qos);
  Serial.print("  dup: ");
  Serial.println(properties.dup);
  Serial.print("  retain: ");
  Serial.println(properties.retain);
  Serial.print("  len: ");
  Serial.println(len);
  Serial.print("  index: ");
  Serial.println(index);
  Serial.print("  total: ");
  Serial.println(total);




if (strcmp(topic, MQTT_SUB_GREENLED) == 0) 
  {
  Serial.println("~~~GreenLED triggered~~~~");
  GreenLED(payload, payload2);
}

I'm also getting this watchdog error in the serial monitor but I think this may explain itself in tonight's reading of UKHeliBob : Using millis() for timing. A beginners guide - Introductory Tutorials - Arduino Forum

E (226683) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (226683) task_wdt:  - async_tcp (CPU 0/1)
E (226683) task_wdt: Tasks currently running:
E (226683) task_wdt: CPU 0: IDLE0
E (226683) task_wdt: CPU 1: loopTask
E (226683) task_wdt: Aborting.
abort() was called at PC 0x400db387 on core 0

Backtrace: 0x4008d024:0x3ffbe170 0x4008d255:0x3ffbe190 0x400db387:0x3ffbe1b0 0x400852d1:0x3ffbe1d0 0x4014209f:0x3ffbc140 0x400dc737:0x3ffbc160 0x4008aed1:0x3ffbc180 0x400896dd:0x3ffbc1a0

Rebooting...
GreenLED(payload, payload2);

From what I understand any message can be connected to a topic

I think the payload with multiple values for a topic should be sent as comma separated values like "value1,value2".

You can use strtok() to separate the values at the comma separators. You will also use atoi() to convert the text to integers for use in the function.

Take a look at Example5 in the Serial Input Basics tutorial.