hello there
this is code for controlling relay over internet but when i want to compile it show error
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>
WiFiClient espClient;
PubSubClient client(espClient);
aREST arestVar = aREST(client);
/*Enter an Unique ID to identify the device for arest.io
A Maximum of 6 characters are supported. Must be unique.*/
char* device_id = "re1403"; // Do not use this device_id. Create your own id.
/* Enter SSID and Password of your
WiFi Network*/
const char* ssid = "SSID"; // Name of WiFi Network.
const char* password = "PASSWORD"; // Password of your WiFi Network.
void callback(char* topic, byte* payload, unsigned int length);
void setup(void)
{
Serial.begin(115200);
client.setCallback(callback);
// Give name and ID to device
arestVar.set_id(device_id);
arestVar.set_name("MyESP");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi connected");
char* out_topic = arestVar.get_topic();
}
void loop()
{
arestVar.loop(client);
}
void callback(char* topic, byte* payload, unsigned int length)
{
arestVar.handle_callback(client, topic, payload, length);
}
and the error
In function 'void setup()':
test:42:39: error: cannot convert 'String' to 'char*' in initialization
42 | char* out_topic = arestVar.get_topic();
| ~~~~~~~~~~~~~~~~~~^~
| |
| String
exit status 1
cannot convert 'String' to 'char*' in initialization
".c_str()" is the 'member function' of String that returns the pointer to the String's internal character buffer so it can be treated as a C string (pointer to a null-terminated array of characters).
The line in question, even once you get it to compile, will do absolutely nothing! You create the new variable out_topic, then try to set out_topic to the value returned by arestVar.get_topic(), and then out_topic IMMEDIATELY goes out of scope and is discarded. So what is the point?
Terrible idea, you shouldn't cast away const just to make the compiler happy, and you'll be left with a dangling pointer, referencing the storage of a temporary String that will be long gone by the time you want to use it.
Well, you're going to have to explain that one. Did you not see the comment I put AFTER that line? Obviously, as I pointed out in post #6, that line, by itself is completely useless, as the variable goes out of scope immediately. But if the variable is used BEFORE it goes out of scope, it will work just fine. Also obviously, if the variable is simply defined in file or global scope, and only initialized in setup(), it should also work just fine.
You are CLEARLY not reading my posts before responding to them. I POINTED EXACTLY THAT OUT in my first post, and repeated it in my last post. And made it clear in the comment in my code ''snippet", which I also pointed out, that the value of out-topic MUST BE USED BEFORE setup() is exited. Please READ before responding.
Please read my previous post, the temporary is destroyed at the end of the expression, i.e. the end of the line. I didn't mention the end of the setup function at all.