Warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

Hi! I've been trying to connect a ESP8266-01 wifi module to Thinkspeak software, but I keep getting the same warning and error, despite I've tried to change it.
Please help me solve it :pray:

CODE:

#include <SoftwareSerial.h>
#define RX 2
#define TX 3
String AP = "BIOTECHNOLOGY";       // AP NAME
String PASS = "biotechnology2022"; // AP PASSWORD
String API = "XANUNQA51PTLB5KW";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
int valSensor = 1;
SoftwareSerial esp(RX,TX); 
 
  
void setup() {
  Serial.begin(9600);
  esp.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}

void loop() {
 valSensor = getSensorData();
 String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
}

int getSensorData(){
  return random(1000); // Replace with your own sensor code
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp.println(command);//at+cipsend
    if(esp.find(readReplay))//ok
    {
      found = true;
      break;
    }
  
    countTimeCommand++;
  }
  
  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
 }

WARNING AND ERROR:
C:\Users\camia\Downloads\esp4\esp4.ino: In function 'void setup()':
C:\Users\camia\Downloads\esp4\esp4.ino:20:22: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
20 | sendCommand("AT",5,"OK");
| ^~~~
C:\Users\camia\Downloads\esp4\esp4.ino:21:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
21 | sendCommand("AT+CWMODE=1",5,"OK");
| ^~~~
C:\Users\camia\Downloads\esp4\esp4.ino:22:57: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
22 | sendCommand("AT+CWJAP=""+ AP +"",""+ PASS +""",20,"OK");
| ^~~~
C:\Users\camia\Downloads\esp4\esp4.ino: In function 'void loop()':
C:\Users\camia\Downloads\esp4\esp4.ino:28:29: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
28 | sendCommand("AT+CIPMUX=1",5,"OK");
| ^~~~
C:\Users\camia\Downloads\esp4\esp4.ino:29:63: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
29 | sendCommand("AT+CIPSTART=0,"TCP",""+ HOST +"","+ PORT,15,"OK");
| ^~~~
C:\Users\camia\Downloads\esp4\esp4.ino:30:60: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
30 | sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
| ^~~
C:\Users\camia\Downloads\esp4\esp4.ino:32:32: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
32 | sendCommand("AT+CIPCLOSE=0",5,"OK");
| ^~~~
. Variables and constants in RAM (global, static), used 28900 / 80192 bytes (36%)
║ SEGMENT BYTES DESCRIPTION
╠══ DATA 1500 initialized variables
╠══ RODATA 1168 constants
╚══ BSS 26232 zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 61895 / 65536 bytes (94%)
║ SEGMENT BYTES DESCRIPTION
╠══ ICACHE 32768 reserved space for flash instruction cache
╚══ IRAM 29127 code in IRAM
. Code in flash (default, ICACHE_FLASH_ATTR), used 243528 / 1048576 bytes (23%)
║ SEGMENT BYTES DESCRIPTION
╚══ IROM 243528 code in flash
esptool.py v3.0
Serial port COM11
Connecting......................................_____
A fatal esptool.py error occurred: Failed to connect to ESP8266: Timed out waiting for packet header

I guess that some parameters are C strings (char*) and you supply a C++ String. That's not allowed because the String content could be damaged by unsafe pointer operations.

In this case it may help to declare the parameter as const. I don't know where to place the "const" in the declaration, different places have different meaning. Or you simply replace the offending char* parameters by String.

1 Like

Change that to

void sendCommand(const char *command, int maxTime, char readReplay[]) {
1 Like

I don't think that it's the command, instead the readReplay can not accept a String argument.

1 Like

The problem does appear to be that readReplay needs to be const, that is a fairly common error when using a string literal in a function call.

1 Like

I was able to fix de warning about converting string constant to 'char*', but when I connect the Arduino board to my computer I still get the "fatal esptool.py error" that appears at the end of the warning :smiling_face_with_tear:

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