Esp-32 invalid conversion from 'const char*' to 'int' [-fpermissive]

Hello I have a problem with some variable conversions and a problem is that when try to compile the code a get this error message. Why couldn't it convert?

I put the wtrtemp as a String tried changing it to int and float and const char same problem. the Mqtt just prints out a number from a slider. which is sent from node red

invalid conversion from 'const char*' to 'int' [-fpermissive]

//MQTT incoming
#define mqttFloodInterval "greenHouse/floodInt"
#define mqttFloodDuration "greenHouse/floodDur"
#define mqttLightsOnDuration "greenHouse/lightsOnDur"
#define mqttLightsOffDuration "greenHouse/lightsOffDur"
//MQTT Setup End
int wtrtemp;
void topicsSubscribe(){
   client.subscribe(mqttFloodInterval);
   client.subscribe(mqttFloodDuration);
   client.subscribe(mqttLightsOnDuration);
   client.subscribe(mqttLightsOffDuration);
}
  Serial.print("MQTT message received on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  messageTemp.remove(0);
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();
  Serial.println(messageTemp);
  if (String(topic) == mqttFloodDuration) {
    wtrtemp = mqttFloodDuration; // The problem is here 
    Serial.print("*** (Flood Interval Received)");
  }
  if(wtrtemp == 23){
 digitalWrite(15, HIGH); // turn on pump 5 seconds
     delay(5000);
  } 
     else {
     
       digitalWrite(15, LOW);  // turn off pump 5 seconds
      delay(5000);
  }
}

Upvotes: 0

Views: 2288

Answers (2)

CoffeDev
CoffeDev

Reputation: 269

I don't have the environment to test your case, but i highly suggest if your'e trying to convert a string to int that you use atol() since the esp32 framework supports it.

int x = (int)atol("550");

So in your case: wtrtemp = (int)atol(mqttFloodDuration); // The problem is here

If that doesn't solve your case(can't 100% remember if atol used parameter took a const char* or char*) so incase it insists on char* try use this instead: wtrtemp = (int)atol((char*)mqttFloodDuration); // The problem is here

If you wish to go on the dangerous but easy road of using String class, then you can very easily set a String by

String xx = "hello world";
int xx_int = xx.toInt();

But underhood that function also does the atol() function referred to above so keep that in mind if you are trying to be efficient in your memory allocation and usage of the esp32 onboard ram.

Upvotes: 2

John Bollinger
John Bollinger

Reputation: 181104

Your mqttFloodDuration is a macro that expands to the string literal "greenHouse/floodDur". The error message is telling you, correctly, that this is not correctly type matched with a variable of type int, such as your wtrtemp.

Moreover, you do seem to expect wrtemp to take a genuine integer value because you later compare it to the integer constant 23, but it is unclear from the code presented how the string "greenHouse/floodDur" corresponds to an integer. Possibly there is some kind of lookup function you could use to get a corresponding value, but as far as I can tell, that would be specific to your project.

Upvotes: 2

Related Questions