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.

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


#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: ");

  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);

  //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) {
    Serial.println("Connecting to WiFi..");

  // Print ESP32 Local IP Address

  // 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

void loop() {
  if (count > countMax)  {    multiply++; count = 0;  }
  if (multiply > multiplyMax)
    if (!oneShot)
      oneShot = true;

    if (nr1 != 0 || nr2 != 0)
      if (msgDel > 5)
        nr1 = 0; nr2 = 0;
        msgDel = 0;


    multiply = 0;

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.

| improve this answer | |
  • My point is to add it to a String array. In the function setH i add it via inOutSet() to the two dimensional array inOut[][]. I then send a string message to my website that is completed in dataRequest(). It adds part of the array to one string that i send back to my website. I know that String is a little bit overkill for the poor processor. Any better idea? – Tomas Jun 5 at 10:20
  • Why use strings at all? You have numbers. Store them as such. You only care about them being stringified when you construct your HTTP request. – Majenko Jun 5 at 10:27
  • 80%of the values are numbers but not all. I wanted to have one inOut array with all my values. I try to make a web gui. This inOut array will store the values of Analog values, digital values or messages like "Something overfilled". I created the Website and the javascript so that i doesnt need to change the html, css or javascript. All i need to do is to program the sketch. – Tomas Jun 5 at 10:38
  • Do you really need to store the text "Something overfilled" in your array? Why not just a simple flag that says something overfilled, then when you construct your request you say "Is the something overfilled flag 1? Yes? Ok, send 'something overfilled'". – Majenko Jun 5 at 10:39
  • I use it as addresses. inOut[0][1] is the first item on the first page. It should be modular. I don't know in advance which address will be int and which string or what will be the text in it – Tomas Jun 5 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.