user62379: Hello I have problems with strings in my sketch. I assingn values to my string (for example tempStrReturn) in the functions setNr1, setNr2. If i pass the value 1 to the function i get "" or "application/x-www-form-urlencoded" when there should be 1. I don't understand why this is happening.
If you see the output printout from the function setNr1 i get "" then after assigning the value (nr1+"") to the tempStrReturn i still get "". After i use the tempStrReturn in the setNr2 function it asigns a mishmash value to it.
I dont use the tempStrReturn, she is just there for showing the error. When i change the int values of nr1 or nr2 that i want to assign to the string, the value mishmash in the strings changes. With the same numbers the string value has always the same mishmash.
OUTPUT:
192.168.43.140
Function setNR1............
Got: 0
NR1 int: 0
tempStrReturn before:
tempStrReturn after:
Function setNR2............
Got: 1
NR2 int: 1
tempStrReturn before:
tempStrReturn after: application/x-www-form-urlencoded
Function setID...........
String id: ID
Set msgId to: 0
Set msgPos to: 1
Function setNR1............
Got: 1
NR1 int: 1
tempStrReturn before: ID
tempStrReturn after: application/x-www-form-urlencoded
Function setNR2............
Got: 1
NR2 int: 1
tempStrReturn before: application/x-www-form-urlencoded
tempStrReturn after: application/x-www-form-urlencoded
Function setH...........
nr1: 1
nr2: 1
Created value: 11
Original value in string: 0
nrX: 0 nrPos: 1 Value i want to enter: n/x-www-form-urlencoded
Value afther assignment: n/x-www-form-urlencoded

MY CODE:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <FS.h>
#include <Wire.h>

const char* ssid = "Ti";
const char* password = "1234567891234";
// Stores LED state
String ledState;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

#define ledPin 2  //BuiltInLed is inverted
#define countMax 2500
#define multiplyMax 100 
int count = 0, multiply = 0, msgDel = 0;
int nr1=0, nr2=0,msgPos, msgId, msgVar;
String inOut[10][10];
String tempStr1, tempStr2, tempStrReturn;
bool oneShot = false;


//***************************************

/*Set value of String[] on the nrPos to val*/
void inOutSet(int nrX, int nrPos, String val)
{
  Serial.print("Original value in string: "); Serial.println(inOut[nrX][nrPos]);
  Serial.print("nrX: ");Serial.print(nrX);Serial.print(" nrPos: ");Serial.print(nrPos);
  Serial.print(" Value i want to enter: "); Serial.println(val);
  inOut[nrX][nrPos] = val;
  Serial.print("Value afther assignment: ");Serial.println(inOut[nrX][nrPos]); 
}

String inOutGet(int nrX, int nrPos)
{
  return inOut[nrX][nrPos];
}


String dataRequest(int roomNr) 
{
  tempStrReturn = "";
  Serial.println("Preparing to send data as word...");
  Serial.print("Value in TempStringReturn before: "); Serial.println(tempStrReturn);
  tempStrReturn = "g" + roomNr;
  tempStrReturn = tempStrReturn+".";
  Serial.print("Adding g and . : "); Serial.println(tempStrReturn);
  nr1 = 0; nr2 = 0;

  for (int i = 0; i < 9; i++)
  {
    Serial.print("Data Request: "); Serial.println(msgId);
    tempStrReturn = tempStrReturn+inOut[roomNr][i];
    Serial.print("Ading value from inOut: "); Serial.println(tempStrReturn);
  }
  Serial.print("Returning value: ");
  Serial.println(tempStrReturn);

  return tempStrReturn;
}

//saves value to nr1 as int
String setNr1(String var)
{
  Serial.println("Function setNR1............");
  Serial.print("Got: "); Serial.println(var);
  nr1 = var.toInt();
  Serial.print("NR1 int: "); Serial.println(nr1);
  Serial.print("tempStrReturn before: "); Serial.println(tempStrReturn);
  tempStrReturn = nr1+"";
  Serial.print("tempStrReturn after: "); Serial.println(tempStrReturn);
  return nr1+"";
}

//saves value to nr2 as int
String setNr2(String var)
{
  Serial.println("Function setNR2............");
  Serial.print("Got: "); Serial.println(var);
  nr2 = var.toInt();
  Serial.print("NR2 int: "); Serial.println(nr2);
  Serial.print("tempStrReturn before: "); Serial.println(tempStrReturn);
  tempStrReturn = nr2+"";
  Serial.print("tempStrReturn after: "); Serial.println(tempStrReturn);
  return nr2+"";
}

//Creates values for the array
String setID()
{
  Serial.println("Function setID...........");
  msgId = nr1;
  msgPos = nr2;
  nr1 = 0; nr2 = 0;
  tempStrReturn = "ID";
  Serial.print("String id: "); Serial.println(tempStrReturn);
  Serial.print("Set msgId to: "); Serial.println(msgId);
  Serial.print("Set msgPos to: "); Serial.println(msgPos);
  return "ID";
}

//Creates value from nr1 and nr2
String setH()
{
  Serial.println("Function setH...........");
  msgVar = (nr1*10)+nr2;
  Serial.print("nr1: "); Serial.println(nr1);
  Serial.print("nr2: "); Serial.println(nr2);
  nr1 = 0; nr2 = 0;
  if (msgVar >= 99)  {  msgVar = 100;  }
  Serial.print("Created value: "); Serial.println(msgVar);
  inOutSet(msgId, msgPos, msgVar+"");
  return "H";  
}

String getIO()
{
  nr1 = nr1/10;
  return dataRequest(nr1);
}

String processor(const String& var){
  Serial.print("Processor in: ");  Serial.println(var);
  return var;
}

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);

  //Filling the inOut array
  for (int i = 0; i < 10; i++)
  {
    for (int x = 0; x < 10; x++)
    {
      inOut[i][x]= "0";
    }

  }


  if(!SPIFFS.begin()){Serial.println("\nSPIFFS chyba!"); return;}

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html", String(), false, processor);//change
  });

  //route to favicon
  server.on("favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/favicon.png", "image/png");
  });

  // Route to load style.css file
  server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/style.css", "text/css");
  });

  // Route to javascript
  server.on("/main.js", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/main.js", "text/javascript");
  });


// Nr1 of the msg
  server.on("/0", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("0").c_str());
  });
  server.on("/1", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("1").c_str());
  });
  server.on("/2", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("2").c_str());
  });
  server.on("/3", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("3").c_str());
  });
  server.on("/4", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("4").c_str());
  });
  server.on("/5", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("5").c_str());
  });
  server.on("/6", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("6").c_str());
  });
  server.on("/7", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("7").c_str());
  });
  server.on("/8", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("8").c_str());
  });
  server.on("/9", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr1("9").c_str());
  });
// Nr2 of the msg
  server.on("/X0", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X0").c_str());
  });
  server.on("/X1", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X1").c_str());
  });
  server.on("/X2", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X2").c_str());
  });
  server.on("/X3", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X3").c_str());
  });
  server.on("/X4", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X4").c_str());
  });
  server.on("/X5", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X5").c_str());
  });
  server.on("/X6", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X6").c_str());
  });
  server.on("/X7", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X7").c_str());
  });
  server.on("/X8", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X8").c_str());
  });
  server.on("/X9", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setNr2("X9").c_str());
  });
//end commands for use the nr values
  server.on("/ID", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setID().c_str());//Set ID nr
  });
  server.on("/H", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", setH().c_str());//Set value to id
  });
  server.on("/G", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", getIO().c_str());//Get value
  });

  // Start server
  server.begin();
}


/*-------------LOOP---------------*/
void loop() {
  count++;
  if (count > countMax)  {    multiply++; count = 0;  }
  if (multiply > multiplyMax)
  {
    if (!oneShot)
    {
      setNr1("0");
      setNr2("1");
      setID();
      setNr1("1");
      setNr2("1");
      setH();
      oneShot = true;
    }

    if (nr1 != 0 || nr2 != 0)
    {
      msgDel++;
      if (msgDel > 5)
      {
        nr1 = 0; nr2 = 0;
        Serial.println("DELETING...");
        msgDel = 0;
      }

    }

    //delay(60000);
    multiply = 0;
  }
}  


user4143: nr1+"" is not how you convert an integer to a String. All you are doing there is taking the value of nr1 and adding it to the address of a string constant that consists of just a \0. Then assigning that to a String.
The results of such an operation are undefined.
Instead you should go:
tempStrReturn = String(nr1);

which will create a new String object, populate it with the text representation of the value of nr1 then copy that content into the tempStrReturn String.
However I would recommend avoiding using String, even on a chip as capable as the ESP32. Instead use C strings (NULL terminated character arrays) which are far less wasteful of resources.

62379: <p><em>Hello</em> I have problems with strings in my sketch. I assingn values to my string (for example tempStrReturn) in the functions setNr1, setNr2. If i pass the value 1 to the function i get "" or "application/x-www-form-urlencoded" when there should be 1. I don't understand why this is happening.</p>

<p><strong>If you see the output printout from the function setNr1 i get "" then after assigning the value (nr1+"") to the tempStrReturn i still get "". After i use the tempStrReturn in the setNr2 function it asigns a mishmash value to it.</strong></p>

<p><em>I dont use the tempStrReturn, she is just there for showing the error. When i change the int values of nr1 or nr2 that i want to assign to the string, the value mishmash in the strings changes. With the same numbers the string value has always the same mishmash.</em></p>

<p><em>OUTPUT:</em></p>

<pre><code>192.168.43.140
Function setNR1............
Got: 0
NR1 int: 0
tempStrReturn before:
tempStrReturn after:
Function setNR2............
Got: 1
NR2 int: 1
tempStrReturn before:
tempStrReturn after: application/x-www-form-urlencoded
Function setID...........
String id: ID
Set msgId to: 0
Set msgPos to: 1
Function setNR1............
Got: 1
NR1 int: 1
tempStrReturn before: ID
tempStrReturn after: application/x-www-form-urlencoded
Function setNR2............
Got: 1
NR2 int: 1
tempStrReturn before: application/x-www-form-urlencoded
tempStrReturn after: application/x-www-form-urlencoded
Function setH...........
nr1: 1
nr2: 1
Created value: 11
Original value in string: 0
nrX: 0 nrPos: 1 Value i want to enter: n/x-www-form-urlencoded
Value afther assignment: n/x-www-form-urlencoded
</code></pre>

<p><em>MY CODE:</em></p>

<pre><code>#include &lt;Arduino.h&gt;
#include &lt;ESP8266WiFi.h&gt;
#include &lt;ESPAsyncTCP.h&gt;
#include &lt;ESPAsyncWebServer.h&gt;
#include &lt;FS.h&gt;
#include &lt;Wire.h&gt;

const char* ssid = "Ti";
const char* password = "1234567891234";
// Stores LED state
String ledState;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

#define ledPin 2  //BuiltInLed is inverted
#define countMax 2500
#define multiplyMax 100 
int count = 0, multiply = 0, msgDel = 0;
int nr1=0, nr2=0,msgPos, msgId, msgVar;
String inOut[10][10];
String tempStr1, tempStr2, tempStrReturn;
bool oneShot = false;


//***************************************

/*Set value of String[] on the nrPos to val*/
void inOutSet(int nrX, int nrPos, String val)
{
  Serial.print("Original value in string: "); Serial.println(inOut[nrX][nrPos]);
  Serial.print("nrX: ");Serial.print(nrX);Serial.print(" nrPos: ");Serial.print(nrPos);
  Serial.print(" Value i want to enter: "); Serial.println(val);
  inOut[nrX][nrPos] = val;
  Serial.print("Value afther assignment: ");Serial.println(inOut[nrX][nrPos]); 
}

String inOutGet(int nrX, int nrPos)
{
  return inOut[nrX][nrPos];
}


String dataRequest(int roomNr) 
{
  tempStrReturn = "";
  Serial.println("Preparing to send data as word...");
  Serial.print("Value in TempStringReturn before: "); Serial.println(tempStrReturn);
  tempStrReturn = "g" + roomNr;
  tempStrReturn = tempStrReturn+".";
  Serial.print("Adding g and . : "); Serial.println(tempStrReturn);
  nr1 = 0; nr2 = 0;

  for (int i = 0; i &lt; 9; i++)
  {
    Serial.print("Data Request: "); Serial.println(msgId);
    tempStrReturn = tempStrReturn+inOut[roomNr][i];
    Serial.print("Ading value from inOut: "); Serial.println(tempStrReturn);
  }
  Serial.print("Returning value: ");
  Serial.println(tempStrReturn);

  return tempStrReturn;
}

//saves value to nr1 as int
String setNr1(String var)
{
  Serial.println("Function setNR1............");
  Serial.print("Got: "); Serial.println(var);
  nr1 = var.toInt();
  Serial.print("NR1 int: "); Serial.println(nr1);
  Serial.print("tempStrReturn before: "); Serial.println(tempStrReturn);
  tempStrReturn = nr1+"";
  Serial.print("tempStrReturn after: "); Serial.println(tempStrReturn);
  return nr1+"";
}

//saves value to nr2 as int
String setNr2(String var)
{
  Serial.println("Function setNR2............");
  Serial.print("Got: "); Serial.println(var);
  nr2 = var.toInt();
  Serial.print("NR2 int: "); Serial.println(nr2);
  Serial.print("tempStrReturn before: "); Serial.println(tempStrReturn);
  tempStrReturn = nr2+"";
  Serial.print("tempStrReturn after: "); Serial.println(tempStrReturn);
  return nr2+"";
}

//Creates values for the array
String setID()
{
  Serial.println("Function setID...........");
  msgId = nr1;
  msgPos = nr2;
  nr1 = 0; nr2 = 0;
  tempStrReturn = "ID";
  Serial.print("String id: "); Serial.println(tempStrReturn);
  Serial.print("Set msgId to: "); Serial.println(msgId);
  Serial.print("Set msgPos to: "); Serial.println(msgPos);
  return "ID";
}

//Creates value from nr1 and nr2
String setH()
{
  Serial.println("Function setH...........");
  msgVar = (nr1*10)+nr2;
  Serial.print("nr1: "); Serial.println(nr1);
  Serial.print("nr2: "); Serial.println(nr2);
  nr1 = 0; nr2 = 0;
  if (msgVar &gt;= 99)  {  msgVar = 100;  }
  Serial.print("Created value: "); Serial.println(msgVar);
  inOutSet(msgId, msgPos, msgVar+"");
  return "H";  
}

String getIO()
{
  nr1 = nr1/10;
  return dataRequest(nr1);
}

String processor(const String&amp; var){
  Serial.print("Processor in: ");  Serial.println(var);
  return var;
}

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);

  //Filling the inOut array
  for (int i = 0; i &lt; 10; i++)
  {
    for (int x = 0; x &lt; 10; x++)
    {
      inOut[i][x]= "0";
    }

  }


  if(!SPIFFS.begin()){Serial.println("\nSPIFFS chyba!"); return;}

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send(SPIFFS, "/index.html", String(), false, processor);//change
  });

  //route to favicon
  server.on("favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send(SPIFFS, "/favicon.png", "image/png");
  });

  // Route to load style.css file
  server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send(SPIFFS, "/style.css", "text/css");
  });

  // Route to javascript
  server.on("/main.js", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send(SPIFFS, "/main.js", "text/javascript");
  });


// Nr1 of the msg
  server.on("/0", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("0").c_str());
  });
  server.on("/1", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("1").c_str());
  });
  server.on("/2", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("2").c_str());
  });
  server.on("/3", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("3").c_str());
  });
  server.on("/4", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("4").c_str());
  });
  server.on("/5", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("5").c_str());
  });
  server.on("/6", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("6").c_str());
  });
  server.on("/7", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("7").c_str());
  });
  server.on("/8", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("8").c_str());
  });
  server.on("/9", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr1("9").c_str());
  });
// Nr2 of the msg
  server.on("/X0", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X0").c_str());
  });
  server.on("/X1", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X1").c_str());
  });
  server.on("/X2", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X2").c_str());
  });
  server.on("/X3", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X3").c_str());
  });
  server.on("/X4", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X4").c_str());
  });
  server.on("/X5", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X5").c_str());
  });
  server.on("/X6", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X6").c_str());
  });
  server.on("/X7", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X7").c_str());
  });
  server.on("/X8", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X8").c_str());
  });
  server.on("/X9", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setNr2("X9").c_str());
  });
//end commands for use the nr values
  server.on("/ID", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setID().c_str());//Set ID nr
  });
  server.on("/H", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", setH().c_str());//Set value to id
  });
  server.on("/G", HTTP_GET, [](AsyncWebServerRequest *request){
    request-&gt;send_P(200, "text/plain", getIO().c_str());//Get value
  });

  // Start server
  server.begin();
}


/*-------------LOOP---------------*/
void loop() {
  count++;
  if (count &gt; countMax)  {    multiply++; count = 0;  }
  if (multiply &gt; multiplyMax)
  {
    if (!oneShot)
    {
      setNr1("0");
      setNr2("1");
      setID();
      setNr1("1");
      setNr2("1");
      setH();
      oneShot = true;
    }

    if (nr1 != 0 || nr2 != 0)
    {
      msgDel++;
      if (msgDel &gt; 5)
      {
        nr1 = 0; nr2 = 0;
        Serial.println("DELETING...");
        msgDel = 0;
      }

    }

    //delay(60000);
    multiply = 0;
  }
}  
</code></pre>
 4143: <p><code>nr1+""</code> is <em>not</em> how you convert an integer to a String. All you are doing there is taking the value of <code>nr1</code> and adding it to the address of a string constant that consists of just a <code>\0</code>. Then assigning that to a String.</p>

<p>The results of such an operation are undefined.</p>

<p>Instead you should go:</p>

<pre><code>tempStrReturn = String(nr1);
</code></pre>

<p>which will create a new <code>String</code> object, populate it with the text representation of the value of <code>nr1</code> then copy that content into the <code>tempStrReturn</code> String.</p>

<p>However I would recommend avoiding using <code>String</code>, even on a chip as capable as the ESP32. Instead use C strings (NULL terminated character arrays) which are far less wasteful of resources.</p>
