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

Hello Experts,
I am zero in terms of coding, please excuse.
I have a compiling error : invalid conversion from ‘long int’ to ‘const char*’ [-fpermissive]

The idea is to read water levels in a tank using ultrasonic sensor and publish to MQTT

Error Messages:

Arduino: 1.8.1 (Windows 10), Board: “NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)”

C:\Users\hazariv\AppData\Local\Temp\ultrasonic_senzor_nodemcu_functional\ultrasonic_senzor_nodemcu_functional.ino: In function ‘void loop()’:

ultrasonic_senzor_nodemcu_functional:115: error: invalid conversion from ‘long int’ to ‘const char*’ [-fpermissive]

client.publish(“WaterLevel”,distance);

^

In file included from C:\Users\hazariv\AppData\Local\Temp\ultrasonic_senzor_nodemcu_functional\ultrasonic_senzor_nodemcu_functional.ino:4:0:

C:\Users\hazariv\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:130:12: error: initializing argument 2 of ‘boolean PubSubClient::publish(const char*, const char*)’ [-fpermissive]

boolean publish(const char* topic, const char* payload);

^

exit status 1
invalid conversion from ‘long int’ to ‘const char*’ [-fpermissive]

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

The following is the code :

#define echoPin D0 // Echo Pin
#define trigPin D1 // Trigger Pin
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "Ven Res";
const char* password = "venhazari";
const char* mqtt_server = "192.168.0.103";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
//int distance;


long duration, distance; // Duration used to calculate distance


void setup_wifi() {
 delay(10);
 // We start by connecting to a WiFi network
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

WiFi.persistent(false);
WiFi.mode(WIFI_OFF);  
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
 

 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }

 randomSeed(micros());
 Serial.println("");
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
 Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
 for (int i = 0; i < length; i++) {
   Serial.print((char)payload[i]);
 }
 Serial.println();

}

void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
   Serial.print("Attempting MQTT connection...");
   // Create a random client ID
   String clientId = "ESP8266Client-";
   clientId += String(random(0xffff), HEX);
   if (client.connect(clientId.c_str())) {
     Serial.println("connected");
     Serial.println (clientId.c_str());
   } else {
     Serial.print("failed, rc=");
     Serial.print(client.state());
     Serial.println(" try again in 5 seconds");
     delay(5000);
   }
 }
}




void setup()
{
ArduinoOTA.begin();
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
setup_wifi();
 client.setServer(mqtt_server, 1883);
 client.setCallback(callback);

Serial.println("esp-ultrasoonsensor2017.ino");
}

void loop()
{
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */

 if (!client.connected()) {
   reconnect();
 }

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
Serial.println(distance);
client.publish("WaterLevel",distance);
delay(1000);
}

Do you understand what the error message is telling you? That you can't use a 'long int' as a parameter; it wants a pointer to a character just like the first argument.

You already have a message buffer that is not used anywhere. So use that to convert the distance to test using sprintf (see below) or ltoa (long-to-ascii); the latter might be preferred.

  sprintf(msg, "%ld", distance);
  client.publish("WaterLevel", msg);

sterretje

Thanks it worked.
And yes I don't understand those error messages. I code by cutting and pasting. Sorry for that :slight_smile:

hazariv:
And yes I don't understand those error messages.

The message is telling you that you tried to pass an variable declared as 'unsigned long' to a function argument declared as 'const char *'. The compiler can't automatically convert integers to character strings so it is telling you that you have to do that.