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

Como puedo convertir el resultado de este codigo a integer

#include "Adafruit_DHT_Particle.h"
#define DHTPIN TX   
#define DHTTYPE DHT11		

String sendData;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  dht.begin();
}

void loop() {
  delay(10000);
  float temperatura = dht.getTempCelcius();
  sendData = String::format("%4.2f C", temperatura);
  Particle.publish("Temp", sendData);
}
  int x = temperatura;        // truncate result
  int y = temperatura + 0.5;  // "round" to next integer

thanks so much, but you refer a this example:

#include "Adafruit_DHT_Particle.h"
#define DHTPIN TX   
#define DHTTYPE DHT11		

String sendData;
DHT dht(DHTPIN, DHTTYPE);

void setup() { 
  dht.begin();
}

void loop() {  
  delay(10000);
  float temperatura = dht.getTempCelcius();
  sendData = String::format("%4.2f C", temperatura);

  int x = temperatura;        // truncate result
  int y = temperatura + 0.5;  // "round" to next integer
  Particle.publish("Temp %d", y);
}

// I keep getting the same error
// dht-test.ino:30:31: invalid conversion from 'int' to 'const char*' [-fpermissive]

If I translated this correctly, your question was how to convert the result to int but what you now appear to actually be looking for is how to convert an integer to const char* or string.

If the question were asked that way this would have been the answer

  int y = temperatura + 0.5;
  sendData = String::format("%d C", y);
  Particle.publish("Temp", sendData, PRIVATE);

However, then I wonder why you want to do that at all.
If you stick with float but don’t want any decimal places you’d just do this

  float temperatura = dht.getTempCelcius();
  // since I prefer char[] over String
  char sendData[16];
  snprintf(sendData, sizeof(sendData), "%4.0f C", temperatura); 
  sendData = String::format("%4.0f C", temperatura);
  Particle.publish("Temp", sendData, PRIVATE);
2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.