Sorry in advance, I'm a Noob when it comes to Arduino programming. From what I can see, const char* is for a read-only string.
I want to declare a string with an initial value, but I am writing code to retrieve a value from a file saved to an ESP8266., and if any value exists, to overwrite the defined value.
So I have FOO = file.readString();, but it didn't like my initial declaration of const char* FOO = "192.168.1.xx";
I hope this makes sense. Any assistance greatly received
The const means constant which makes it not writable. This can help prevent errors when you really intend it to not change. It also allows the compiler to potentially do optimization when it knows the value will not change.
Rather than share what isn't working for me following your advice, I ask you write a small sketch that shows what you mean, as I fear I have misunderstood.
So I did the following
char* MQTT_SERVER = "192.168.1.xx";
I think hit the next error at the following point
File file = SPIFFS.open("/mqtt_server.txt", "r");
MQTT_SERVER = file.readString();
with the error
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino:12:21: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
12 | char* MQTT_SERVER = "192.168.1.xx";
| ^~~~~~~~~~~~~~
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino: In function 'void mqttReconnect()':
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino:586:34: error: cannot convert 'String' to 'char*' in assignment
586 | MQTT_SERVER = file.readString();
| ~~~~~~~~~~~~~~~^~
| |
| String
I get
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino: In function 'void mqttReconnect()':
C:\Users\corma\Downloads\Sofar2mqtt\Sofar2mqtt.ino:586:17: error: incompatible types in assignment of 'String' to 'char [13]'
586 | MQTT_SERVER = file.readString();
| ^~~~~~~
You are repeating yourself, and I told you what you did wrong there. But, so is the compiler. It's telling you what the problem is...
The String class, and char arrays, are different types in C, and behave completely differently. You need to look up the String class function that converts a String object to a C string. It's in the reference section here.
What are you really up to? Are you merging two separate sketches that you don't understand very well?
I am using the script with the WifiManager.h include. I have a custom field to get the user to add in the IP address of the MQTT Server. The "hope" was that I had the prompt initially saying 192.168.1.xx and the user then puts in the correct IP
From there, once the ESP8266 restarts with the WifiManager details stored, it will retrieve the MQTT_SERVER address saved to the file, and use it in
if(mqtt.connect(MQTT_SERVER, MQTT_USERNAME, MQTT_PASSWORD))
{
Serial.println(" connected");
delay(1000);
updateOLED("NULL", "NULL", "NULL", "CONNECTED");
delay(1000);
etc.....
There are no Arduino "scripts". But your plan sounds plausible. Good luck with your project. You're not feeding back even when I do footwork for you... at least not on the relevant issue which is the type conversion...