1
votes

I am trying to connect to my wifi using my M5StickCPlus. I use the platformIO framework. I am getting this error : error: invalid conversion from 'const char*' to 'char*' at compilation.

Here is the code:

#include "M5StickCPlus.h"
#include <WiFi.h>

const char* WIFI_SSID =  "router";
const char* WIFI_PASS = "pass";

void setup() {
   M5.begin(true, true, true);
   Serial.printf("Connecting to %s ", WIFI_SSID);
   WiFi.begin(WIFI_SSID, WIFI_PASS);
   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
   }
  Serial.println("CONNECTED");
}

I also tried to declare the SSID and the password using #define. But, I am getting another error : addApbChangeCallback(): duplicate func=400EAB44 arg=3FFBF008 during execution.

What am I missing ?

1

1 Answers

1
votes

The WiFi.begin() function overload that takes two arguments (an SSID and a password) requires the first argument to be a non-const char* pointer (i.e. a pointer to a potentially modifiable char array, even if the function doesn't actually change it).

A simple way around this (similar to the example given in the documentation) is to redeclare your WIFI_SSID variable as such an array and initialize it with a copy of the string literal. (You can also do the same for the WIFI_PASS argument, but that is not required, as the second argument to begin() is a const char*.):

char WIFI_SSID[] = "router";    // Change to a non-const char array
const char* WIFI_PASS = "pass"; // You can leave this one as-is

NOTE: The declaration of that begin() overload, as in this GitHub repo, is:

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

An implicit conversion from a const pointer to a non-const pointer is not allowed in C++; you could use a const_cast<char*>(WIFI_SSID) on your current definition but, IMHO, that is a less desirable approach – the argument will have been defined as non-const for a reason, even if that reason is not obvious!