I am using ESP8266 for years and while sending GET requests to a server, I had zero problems. Now, I have a project where some of the URL address needs to be in an EEPROM. What could possibly go wrong? Well, everything.
When I form an URL address from the EEPROM data, glue a line or two to it, on Serial I get the address I want, but it just doesn't work. When I tried to copy that address from the Serial, I get only part of it. Not the whole address. So it is whole here printed, but I can not copy it. In the example here I provide, I can copy the first part or the second one. But not whole.
Say I have something like this:
www somesite com/index.php
(dots are removed for not making a link)
in EEPROM, which I read as a String (i expect sh*t storm now, please spare me).
String servicePath ;
for (int r = addr_espPathService; r < addr_espPathService + 150; ++r){
servicePath += char(EEPROM.read(r));
}
Now, when using:
String serverPathService = servicePath+"?upit=ssid&id=2";
Serial.println(serverPathService);
if (http.begin(client, serverPathService.c_str())) {
On Serial I get the whole string, which I can not copy as a whole. But that string obviously passes only this servicePath, and not the rest. The part I can copy.
I know, it is about the data type.
I know, String...
A String acts weird when it contains null characters. I suspect the String has a bunch of null characters after the string contents. To fix that, store the length of the string in EEPROM and read that first. Then only read 'n' characters into your String instead of reading all 150.
I have never messed up with this you suggest. Can you give me an example or a link?
If I get you correctly, I should store in EEPROM a length and while reading it later to use that as an end of reading so not read nullls?
String servicePath ;
char readChar;
for (int r = addr_espPathService; r < addr_espPathService + 150; ++r){
readChar = char(EEPROM.read(r));
servicePath += readChar;
// stop after first if you use zero terminated string
if (readChar == 0x00 ) break;
}
and make sure the EEPROM has a trailing null char added when you saved the String.
if you need to store multiple data, this might become an issue if the String length varies..
using a struct with a fixed length buffer for a cString would make your life easy
That works if the char string read from EEPROM contains a NULL before reaching the end of they 150 bytes in the 'for' loop. scrcat() will use that NULL as the starting point for concatenation. If that's the case, why read all 150 bytes at all? 'break' the loop after the NULL is read and stored in 'servicePath'.
char servicePath[150];
servicePath[149] ='\0'; // just to be on the "safe" side
for (size_t i = 0; i < 149; i++) {
servicePath[i] = EEPROM.read(addr_espPathService + i);
if (servicePath[i] == '\0') break;
}
strlcat(servicePath, "?upit=ssid&id=2",150);
Serial.println(servicePath);
if (http.begin(client, servicePath)) {
}
You don't want to add the null char to the String...
String servicePath ;
char readChar;
for (int r = addr_espPathService; r < addr_espPathService + 150; ++r){
readChar = char(EEPROM.read(r));
// stop after first if you use zero terminated string
if (readChar == 0x00 ) break;
else servicePath += readChar;
}
use reserve to avoid constantly growing your String in the heap and copy operations
String servicePath ;
char readChar;
servicePath.reserve(150);
for (uint16_t r = addr_espPathService; r < addr_espPathService + 150; ++r){
readChar = EEPROM.read(r);
// stop after first if you use zero terminated string
if (readChar == 0x00 ) break;
else servicePath += readChar;
}
I used this last example, although all work as well. Tried all of this here.
One last thing.
There is an additional problem that came across. I can collect the data from the server and while replacing some of them in the EEPROM, others are erased.
The structure of the EEPROM I made in this case is:
int addr_espID = 0;
int addr_espSSID = 50;
int addr_espPASS = 100;
int addr_espPATH = 200;
int addr_espPathService = 350;
So, when I write this SSID, it somehow erases everything from the addr_espSSID till the end.
for (int y = addr_espSSID; y < 99; y++ ){
EEPROM.write(y, 0);
EEPROM.commit();
}
for (int i = 0; i < payload.length(); i++) {
EEPROM.write(addr_espSSID + i, payload[i]); //addr_espSSID
EEPROM.commit();
}
This should erase only from 50 to 99, and write only from 50 until the payload.length(). In my case, it is 8. So it should write from 50 to 57. What am I doing wrong?
prove to yourself that payload.length() is actually 8 by printing it
what's the payload type ?
(posting snippets does not help provide good advice β Snippets R Us!)
You cannot actually erase the values in the EEPROM, only write a value of your choice to a range of locations of your choice
So, use a for loop whose value is from the first to last address of your choice and write a value of your choice to each EEPROM location in that range. From your use of EEPROM.commit() I assume that you are not using an AVR based Arduino board