Wifi.h invalid conversion from 'const char*' to 'char*' [-fpermissive]

Hello guys,

This question was asked before, but not with my specific problem i think.

First of all i was able to compile this code successfully before but suddenly it stops working.

This is the questionable code:

i have replaced all privacy related things with “(replaced)”

#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

// Replace the next variables with your SSID/Password combination
const char* ssid = "replaced";
const char* password = "replaced";

// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "Broker IP Adress";
const char* mqtt_server = "(replaced)";

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

//uncomment the following lines if you're using SPI
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/

Adafruit_BMP280 bmp; // I2C

Adafruit_BME280 bme; // I2C


//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
float temperature = 0;
float humidity = 0;
float altitude = 0;
float pressure = 0;

// LED Pin
const int ledPin = 4;

void setup() {
  Serial.begin(115200);
  // default settings
  // (you can also pass in a Wire library object like &Wire2)
  //status = bme.begin();  
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  pinMode(ledPin, OUTPUT);
}

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

  WiFi.begin(ssid, password);

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

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

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

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic esp32/output, you check if the message is either "on" or "off". 
  // Changes the output state according to the message
  if (String(topic) == "esp32/output") {
    Serial.print("Changing output to ");
    if(messageTemp == "on"){
      Serial.println("on");
      digitalWrite(ledPin, HIGH);
    }
    else if(messageTemp == "off"){
      Serial.println("off");
      digitalWrite(ledPin, LOW);
    }
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("esp32/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 5000) {
    lastMsg = now;
    
    // Temperature in Celsius
    temperature = bme.readTemperature();   
    // Uncomment the next line to set temperature in Fahrenheit 
    // (and comment the previous temperature line)
    //temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
    
    // Convert the value to a char array
    char tempString[8];
    dtostrf(temperature, 1, 2, tempString);
    Serial.print("Temperature: ");
    Serial.println(tempString);
    client.publish("(replaced)", tempString);

    humidity = bme.readHumidity();
    
    // Convert the value to a char array
    char humString[8];
    dtostrf(humidity, 1, 2, humString);
    Serial.print("Humidity: ");
    Serial.println(humString);
    client.publish("(replaced)", humString);

    pressure = bme.readPressure() / 100.0F;
    
    // Convert the value to a char array
    char presString[8];
    dtostrf(pressure, 1, 2, presString);
    Serial.print("Pressure: ");
    Serial.println(presString);
    client.publish("(replaced)", presString);

    altitude = bme.readAltitude(999.00);
    
    // Convert the value to a char array
    char altitudeString[8];
    dtostrf(altitude, 1, 2, altitudeString);
    Serial.print("Altitude: ");
    Serial.println(altitudeString);
    client.publish("(replaced)", altitudeString);
  }
}

And now the full error:

(replaced).ino: In function 'void setup_wifi()':
BME280_working_MQTT_au_en_topic:67:28: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
   WiFi.begin(ssid, password);
                            ^
In file included from (replaced).ino:1:0:
C:\Program Files (x86)\Arduino\libraries\WiFi\src/WiFi.h:79:9: error:   initializing argument 1 of 'int WiFiClass::begin(char*, const char*)' [-fpermissive]
     int begin(char* ssid, const char *passphrase);
         ^
exit status 1
invalid conversion from 'const char*' to 'char*' [-fpermissive]

What i have changed since the last successful compile with the exact same code:

  • i have only changed the board in the Boards Manager from ESP32 Board to a ESP01S Board.

As i said the exact same code was compiled successfully before.

I`ve read somewhere here that the Wifi.h library wants a editable string as first argument.

something like that:

#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

// Replace the next variables with your SSID/Password combination
char ssid[]= "replaced";
const char* password = "replaced";

// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "Broker IP Adress";
const char* mqtt_server = "(replaced)";

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

//uncomment the following lines if you're using SPI
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/

Adafruit_BMP280 bmp; // I2C

Adafruit_BME280 bme; // I2C


//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
float temperature = 0;
float humidity = 0;
float altitude = 0;
float pressure = 0;

// LED Pin
const int ledPin = 4;

void setup() {
  Serial.begin(115200);
  // default settings
  // (you can also pass in a Wire library object like &Wire2)
  //status = bme.begin();  
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  pinMode(ledPin, OUTPUT);
}

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

  WiFi.begin(ssid, password);

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

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

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

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic esp32/output, you check if the message is either "on" or "off". 
  // Changes the output state according to the message
  if (String(topic) == "esp32/output") {
    Serial.print("Changing output to ");
    if(messageTemp == "on"){
      Serial.println("on");
      digitalWrite(ledPin, HIGH);
    }
    else if(messageTemp == "off"){
      Serial.println("off");
      digitalWrite(ledPin, LOW);
    }
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("esp32/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 5000) {
    lastMsg = now;
    
    // Temperature in Celsius
    temperature = bme.readTemperature();   
    // Uncomment the next line to set temperature in Fahrenheit 
    // (and comment the previous temperature line)
    //temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
    
    // Convert the value to a char array
    char tempString[8];
    dtostrf(temperature, 1, 2, tempString);
    Serial.print("Temperature: ");
    Serial.println(tempString);
    client.publish("(replaced)", tempString);

    humidity = bme.readHumidity();
    
    // Convert the value to a char array
    char humString[8];
    dtostrf(humidity, 1, 2, humString);
    Serial.print("Humidity: ");
    Serial.println(humString);
    client.publish("(replaced)", humString);

    pressure = bme.readPressure() / 100.0F;
    
    // Convert the value to a char array
    char presString[8];
    dtostrf(pressure, 1, 2, presString);
    Serial.print("Pressure: ");
    Serial.println(presString);
    client.publish("(replaced)", presString);

    altitude = bme.readAltitude(999.00);
    
    // Convert the value to a char array
    char altitudeString[8];
    dtostrf(altitude, 1, 2, altitudeString);
    Serial.print("Altitude: ");
    Serial.println(altitudeString);
    client.publish("(replaced)", altitudeString);
  }
}

Why did it compiled successfully with the old code before and why suddenly it stopped working?

Sorry for the extended post but it looks like the Arduino forum is also counting the characters in the “</>” code brackets and limiting to 9000 characters which is not really enough to explain a problem in detail when the forum also counts the characters in the code brackets.

Have you upgraded your IDE? It compiles for me in version 1.6.12.

It looks like it would compile with the -f flag if you can figure out where the compiler options are specified.

Given the error though, I'd try just removing const on the password declaration.

Error of: invalid conversion from 'const char*' to 'char*'

const char* ssid = "replaced";
const char* password = "replaced";

VS

int begin(char* ssid, const char *passphrase);

Notice how the SSID is declared? Notice what the begin function wants from SSID?

How?

Imagine, if you will, the developer of the library, years ago wrote one thing. You downloaded the library and use it as is. A few weeks ago the developer did an upgrade and changed thigs for the worse. Then you decide to do a library update, after all that's what the IDE bugs you about. Now, your old code is broken.

You can try to use an earlier library version to see if a library change is an issue. If so, time to alert the programmer.

nope i didn`t upgraded my IDE.

when your looking for esp8266 bme280 examples everyone is using:

const char* ssid = ""

for define this char variable.

as i said it suddenly stopped working.

now it only works with:

char* ssid = ""

very curious

But it worked before seamlessly with:

const char* ssid=""

i simply want to understand how it worked before, if its the wrong way to make it constant?

Here are some links:

@Idahowalker

The last WiFi.h library update was on 2016-03-08 so this cant be the culprit

Ultimately the problem is with the library, the function argument should be declared as const char* unless the function intends to alter the value, and if the function does alter the value the error is correct and a const char* is unacceptable.

I would suspect the problem is caused by a change in the ESP8266 core, which causes the conversion from const char* to char* to generate an error. If you compile the same sketch with the AVR core (for an UNO, for example), the compiler will only generate a warning instead of an error (although it is debatable whether this is desirable).