deprecated conversion from string constant to char*

Hello everyone!
I am new to this forum so forgive if this problem is already solved.
I am trying to control my dht11 temp. and humidity sensor using wifi module ESP8266 and sending the data to my thingspeak account for making graphs. However there is an error which i encountered and cannot solve this.
This is the line where the error deprecated conversion from string constant to char* occurs.

char *api_key="W4GD6K3M241DXGUY";

and the error which i get is:-

C:\Users\Saxena\Documents\Arduino\final\final.ino:11:15: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

 char *api_key="W4GD6K3M241DXGUY";     // Enter your Write API key from ThingSpeak

               ^

C:\Users\Saxena\Documents\Arduino\final\final.ino: In function 'void connect_wifi(String, int)':

C:\Users\Saxena\Documents\Arduino\final\final.ino:99:27: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

       if(Serial1.find("OK"))

                           ^

It would be an immense pleasure for me if anyone can provide a solution to this.
And also please tell that is this the correct way to connect to my home wifi using wifi module ESP8266:-

connect_wifi("AT",1000);
 connect_wifi("AT+CWMODE=1",1000);
 connect_wifi("AT+CWQAP",1000);  
 connect_wifi("AT+RST",5000);
  connect_wifi("AT+CWJAP=\"abhikaran\",\"18148841\"",10000);
Serial.println("Wifi Connected");

have also attached the full code if u want.

Thanks in advance. :wink:

final.ino (2.81 KB)

Try:

char api_key[] ="W4GD6K3M241DXGUY";

and the error which i get i

...is a warning.

The literal, “W4GD6K3M241DXGUY” has a type. It is const char *. You are trying to store that in a variable of a different type. You can make the warning go away by using the correct type to store the value in.

const char *api_key="W4GD6K3M241DXGUY";     // Enter your Write API key from ThingSpeak

karan96: This is the line where the error deprecated conversion from string constant to char* occurs.

char *api_key="W4GD6K3M241DXGUY";

As AWOL says, this is not an error it's only a warning. This means that it shouldn't prevent your program from compiling and running. It's basically just warning you that the way you are defining that string is not recommended.

A string defined that way cannot be altered by the program. As Paul says it's a constant. So just include const (as shown in Paul's reply) in the definition and all the compiler will be happy.

If you don't want a const string, but instead want something that can have new values copied to it throughout the program, then do what gfvalvo suggests in reply #1.

You asked about connecting to Wi-Fi. AFAIK , ‘AT’ commands are for modems not for Wi-Fi connections.
There’s a very easy-to-use library for Wi-Fi connections with the esp8266:

#include <ESP8266WiFi.h>
   .....
  .....
  WiFi.begin(ssid, abc1);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }

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

Re: deprecated conversion from string constant to char*

You asked about connecting to Wi-Fi. AFAIK , 'AT' commands are for modems not for Wi-Fi connections. There's a very easy-to-use library for Wi-Fi connections with the esp8266:

The ESP8266 CAN be controlled via its RX/TX pins using 'AT' commands just like a modem. The ESP8266-01 does not have any accessible IO pins suitable for interfacing with analogue sensors per the OPs requirements. So, one needs to use a suitable host controller, such as an arduino board and then talk to the ESP8266 via the serial interface.

The OP reported a compiler warning (per the subject of this thread) received when trying to use a standard library function. It is unfortunate that the OP indicated the wrong line, leading to irrelevant replies none which address the serial library issue causing the compiler warning message.

The issue in fact is probably due to changes made in the libraries. In another thread nickgammon said "Compilers are getting snippy these days about passing string constants (like "foo") to a function that does not declare the argument as const."

A temporary fix for this "bug" is to declare the argument separately as a variable and pass the variable instead.

So, instead of;

if (Serial1.find("OK")) {

you should use;

char workaround[] = "OK"; if (Serial1.find(workaround)) {

The proper fix is to redefine the function and that is a matter for the moderators and/or arduino developers.

Personally I've not been able to find any examples of using the ESP8266 internal functions other than quilkins example in this thread.