ESP String concaternation not working A = B+C only return B

Hi Guys,

I’m having a issue with String concatenations. My code read char from EEPROM as below

Memory location start and end position for each string stored in memory

int memmapstart[]= {0,15,35,55,75,107,157,207};
int memmapend[]= {15,35,55,75,107,157,207,257};
String savedinepprom[8];
String host, endpint_status, endpoint_read ;

setup section to loop though the memory map and call eprom read function (EEPROM,read function is used inside eprom_read )

  for (int i = 0; i < 8; ++i)
  {
    savedinepprom[i] = eprom_read(memmapstart[i],memmapend[i]);
  }

eeprom read function

String eprom_read(int addrstart, int addrend){
  String savedvalue = "";
  for (int i = addrstart; i < addrend; ++i){
    savedvalue += char(EEPROM.read(i));
  }
  return savedvalue;  
}

Then I assign those read values to String variables

host = savedinepprom[2]; // Ex: http://192.168.0.10
endpoint_status = savedinepprom[3]; // Ex: api/report_status.php
endpoint_read = savedinepprom[4]; // Ex: api/report_read.php

And In function to post data as json to the api endpoints I try to generate the full URL as below

postUrl = host + endpoint_status ;

But postUrl only contain the string in “host” variable, To check the values I added a print line as well. That shows both “host” and "endpoint_status " variable contain the correct string. What could be the reason?

Please post a complete program that illustrates the problem. Using code tags when posting code makes it easier to select and copy to an editor for examination/testing so please use them

See Read this before posting a programming question

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>   // Include the WebServer library
#include <ArduinoJson.h>
#include <EEPROM.h>

unsigned long time_now = 0;
int memmapstart[]= {0,15,35,55,75,107,157,207};
int memmapend[]= {15,35,55,75,107,157,207,257};
String savedinepprom[8];
int sensorID, period = 1000;
String postUrl, hostUrl, endpoint_status, endpoint_read,ssid, password, sensorKEY ;


void setup(void){

  Serial.begin(115200);                    //Serial connection
  EEPROM.begin(512);                       //EEPROM connection
 
  Serial.println("Reading EEPROM data");
  for (int i = 0; i < 8; ++i)
  {
    savedinepprom[i] = eprom_read(memmapstart[i],memmapend[i]);
  }
  delay(3000);
sensorID = savedinepprom[2].toInt();
sensorKEY = savedinepprom[3];
hostUrl = savedinepprom[4]; // Ex: http://192.168.0.10
endpoint_status = savedinepprom[5]; // Ex: api/report_status.php
endpoint_read = savedinepprom[6]; // Ex: api/report_read.php

for (int i = 0; i < 8; ++i)
  {
    Serial.println(savedinepprom[i]);
  }
  
  delay(100);
  
  for (int i = 0; i < 15; ++i)
  {
    ssid += char(EEPROM.read(i));
  }
  Serial.print("SSID: ");
  Serial.println(ssid);
  Serial.println("Reading EEPROM pass");
  String pass = "";
  for (int i = 15; i < 35; ++i)
  {
    password += char(EEPROM.read(i));
  }

  if ( ssid.length() > 1 ) {
    WiFi.begin(ssid.c_str(), password.c_str());
    if (connect_test()) {
      return;
    } 
  }
}
void loop(void){
  if(millis() > time_now + (period*1000)){
    time_now = millis();
    Serial.println("Sending data");
    send_ip();                             // Send sensor data to server after a set interval 
  }  
}
String eprom_read(int addrstart, int addrend){
  String savedvalue = "";
  for (int i = addrstart; i < addrend; ++i){
    savedvalue += char(EEPROM.read(i));
  }
  return savedvalue;  
}


bool connect_test(){
  int c = 0;
  Serial.println("Waiting for Wifi to connect");
  while ( c < 20 ) {
    if (WiFi.status() == WL_CONNECTED) { return true; } 
      delay(500);
      Serial.print(WiFi.status());    
      c++;
  }
  Serial.println("");
  Serial.println("Connect timed out, opening AP");  
  return false;
}


void send_ip(){
  Serial.println("Sending IP");
  StaticJsonDocument<200> postData;          //Declaring static JSON buffer
  postData["sensorID"].set(sensorID);
  postData["sensorkey"].set(sensorKEY);
  postData["IP"].set(WiFi.localIP().toString());
  char jsonOutput[200];
  serializeJson(postData, jsonOutput);
  Serial.println(jsonOutput);
 
  HTTPClient http;   

  postUrl = hostUrl + endpoint_status;                //Post url 
  Serial.println(hostUrl);
  Serial.println(endpoint_status);
  Serial.println(postUrl);

  http.begin(postUrl);                      //Specify request destination
  http.addHeader("Content-Type", "application/json");  //Specify content-type header
  int httpCode = http.POST(jsonOutput);     //Send the request
  String payLoad = http.getString();        //Get the response payload
  Serial.println(httpCode);                 //Print HTTP return code
  Serial.println(payLoad);                  //Print request response payload
  http.end();                               //Close connection
  
}

What is the output in the serial monitor? Especially these prints:

 Serial.println(hostUrl);
  Serial.println(endpoint_status);
  Serial.println(postUrl);

As I said they are correct

when I have following serial outputs

Serial.println(hostUrl);
Serial.println(endpoint_status);
Serial.println(postUrl);

Console prints
http://192.168.0.12
/api/sensor/update_sensor_data.php
http://192.168.0.12

Check for memory usage. This happens mostly likely because during concatenation, the new string fails to allocate new memory.