How to change declared string Variable

#define RXD 16
#define TXD 17
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <Console.h>

//PROTOCOL SYNTAX
const String PREFIX_REQUEST = "#"; //Symbol(s) beginning a query
const String PREFIX_ANSWER = "~@"; //Symbol(s) beginning an answer
const String APPENDIX_QUESTION = "?"; //Symbol(s) following a query
const String APPENDIX_ANSWER = ""; //Symbol(s) following a query
const String READY_MESSAGE = "READY"; //Message sent after boot
const String ERROR_MESSAGE = "unknown command"; //Message sent for unknown query
const int WAIT_TIME = 120;  //time between answers sent //multiples of 60 work best

//SYSTEM QUERYS
const String COMMAND_TEMPERATURE = "TEMPERATURE";
const String COMMAND_VERSION = "VERSION";

//SYSTEM PARAMETERS
const String VERSION = "2.0";
//going to be changed
//PARAMETER ROOMNAME
String roomname_short = "test"; //Roomname alias //maximum 8 characters
String roomname = "test"; //Roomname full for display on UI

//QUERYS
//uppercase characters and hyphens
//no spaces
//minimum 3 characters
//phrase "INTERNAL"
//Commands using in Hercules
const String COMMAND_ROOMCONFIG = "ROOMCONFIG";
const String COMMAND_ROOMNAME = "ROOMNAME";
const String COMMAND_ROOMNAME_SHORT = "ROOMNAME-SHORT";

#ifdef __cplusplus
extern "C" {
#endif

uint8_t temprature_sens_read();

#ifdef __cplusplus
}
#endif

uint8_t temprature_sens_read();
boolean commandMode = false;
void setup() {
  Serial2.begin(9600, SERIAL_8N1, RXD, TXD);
  Serial.begin(115200);
  while(!Serial);
  Serial2.println((READY_MESSAGE));
  Serial.println(READY_MESSAGE);
  }

void loop() {
  String readString = "";
  String c = "";
  if (Serial2.available()) {
    c = Serial2.read(); //gets  one byte from serial buffer
      if(c == readString){
        c = readString;
      }
    if (topicMatching(readString, COMMAND_ROOMNAME)) {
      roomname = readString;
      sendAnswer(COMMAND_ROOMNAME_SHORT + " " + roomname);
      }
  }
  
    else {
      sendAnswer(ERROR_MESSAGE);
      
    }
    readString = "";
  }



bool topicMatching(String question, String command) {
  return question == PREFIX_REQUEST + command + APPENDIX_QUESTION;
}

void sendAnswer(String message) {
  Serial2.println(PREFIX_ANSWER + message);
  delay(WAIT_TIME);
}

void roomconfig(){
  Serial2.readString();
  sendAnswer(COMMAND_ROOMNAME_SHORT + roomname);
  sendAnswer(COMMAND_ROOMNAME_SHORT + " " + roomname_short);
}

Hey everyone I´m new here. I have written a code which works … but I want to modify it a little bit.
I use an esp32 board and a Hercules Setup utility program where I can send my commands.
So my problem is that everytime I want to change my roomname I have to change the String roomname by myself in my code. I want to change it directly when sending the commands.

this is how it works:
First after Upload to ESP32 Board I switch to Hercules there I can send to my Board Commands
##ROOMNAME sets the Roomname and
##ROOMNAME? shows me what should be set now.
but I want to set it directly
##ROOMNAME test
I hope someone can help me

This section of code does not make sense and I believe does not do what you require:

 if (Serial2.available()) {[color=#222222][/color]
    c = Serial2.read(); //gets  one byte from serial buffer[color=#222222][/color]
      if(c == readString){[color=#222222][/color]
        c = readString;

You check if C equals readString then if it does set it to readstring ?
At this point you only appear to have read one character so you won't have a new name yet.
I think you need to loop round until you have read al the characters from the serial buffer.

I would suggest to study Serial Input Basics to handle this

The serial input basics tutorial may be of interest.

(Great minds think alike)

Check out my Text I/O for the Real World tutorial The section on reading user commands will help you.
It is non-blocking and picks up words from the input, delimited by space etc. The SafeString library it uses provides a rich array of methods for reading and parsing text data.

However Strings is OK also (PREFIX_REQUEST is missing a #)
Try the attached code which uses String and is a modification of your code
stringcommands.ino

In either case avoid using low-level c-string methods as they are very prone to coding errors.
see “Secure Coding in C and C++”, chapter 5 and chapter 2

Here are the guidelines for using Strings

  1. Declare long lived Strings as globals and reserve space in setup()
  2. If you have created Strings in the loop() method, they are long lived, move them to Globals and do step 1.
  3. Pass all Strings arguments to methods, as const String &. For results pass a String &result, that the method can update with the result. i.e. void strProcessing(const String &input1, const String &input2, , String &result) {…}
  4. Do string processing in small compact methods. In these methods use local Strings in preference to local char. These methods will completely recover all the heap and stack used by their local Strings and other variables on exit.
    5 )Set the IDE File → Preferences Compiler Warning to ALL and watch for warning: passing ‘const String’ as ‘this’ argument discards qualifiers [-fpermissive] messages in the compile window. This says you are trying to modify a const String& arg.
  5. Do not use the String + operator at all and avoid using the String(…) constructors as these create short lived temporary Strings. Use = and += operators or concat( ), as in result += “str”; result += 5; result += number; etc

stringcommands.ino (4.99 KB)

countrypaul:
This section of code does not make sense and I believe does not do what you require:

 if (Serial2.available()) {[color=#222222][/color]

c = Serial2.read(); //gets  one byte from serial buffer
     if(c == readString){
       c = readString;




You check if C equals readString then if it does set it to readstring ? 
At this point you only appear to have read one character so you won't have a new name yet.
I think you need to loop round until you have read al the characters from the serial buffer.

So this means I have to put in a while() ?

drmpf:
Check out my Text I/O for the Real World tutorial The section on reading user commands will help you.
It is non-blocking and picks up words from the input, delimited by space etc. The SafeString library it uses provides a rich array of methods for reading and parsing text data.

However Strings is OK also (PREFIX_REQUEST is missing a #)
Try the attached code which uses String and is a modification of your code
stringcommands.ino

In either case avoid using low-level c-string methods as they are very prone to coding errors.
see “Secure Coding in C and C++”, chapter 5 and chapter 2

Here are the guidelines for using Strings

  1. Declare long lived Strings as globals and reserve space in setup()
  2. If you have created Strings in the loop() method, they are long lived, move them to Globals and do step 1.
  3. Pass all Strings arguments to methods, as const String &. For results pass a String &result, that the method can update with the result. i.e. void strProcessing(const String &input1, const String &input2, , String &result) {…}
  4. Do string processing in small compact methods. In these methods use local Strings in preference to local char. These methods will completely recover all the heap and stack used by their local Strings and other variables on exit.
    5 )Set the IDE File → Preferences Compiler Warning to ALL and watch for warning: passing ‘const String’ as ‘this’ argument discards qualifiers [-fpermissive] messages in the compile window. This says you are trying to modify a const String& arg.
  5. Do not use the String + operator at all and avoid using the String(…) constructors as these create short lived temporary Strings. Use = and += operators or concat( ), as in result += “str”; result += 5; result += number; etc

Thanks first of all… I think it doesn´t works how I want. I tried to send a command like this ##ROOMNAME test but it doesn´t send me an answer … or try to send this ##ROOMNAME? and the server isn´t answering. How does your program works?

Works for me

17:29:45.582 -> input is '##ROOMNAME'
17:29:45.582 -> comparing topic to 
17:29:45.582 -> ##ROOMNAME?
17:29:45.582 -> comparing cmd to 
17:29:45.582 -> ##ROOMNAME 
17:29:45.582 -> ~@unknown command  ##ROOMNAME
17:29:57.282 -> input is '##ROOMNAME?'
17:29:57.282 -> comparing topic to 
17:29:57.282 -> ##ROOMNAME?
17:29:57.282 -> ~@ROOMNAME-SHORT test
17:30:05.450 -> input is '##ROOMNAME test'
17:30:05.484 -> comparing topic to 
17:30:05.484 -> ##ROOMNAME?
17:30:05.484 -> comparing cmd to 
17:30:05.484 -> ##ROOMNAME 
17:30:05.484 -> found cmd
17:30:05.484 -> rest of cmd 'test'
17:30:14.607 -> input is '##ROOMNAME test33'
17:30:14.607 -> comparing topic to 
17:30:14.607 -> ##ROOMNAME?
17:30:14.607 -> comparing cmd to 
17:30:14.607 -> ##ROOMNAME 
17:30:14.607 -> found cmd
17:30:14.607 -> rest of cmd 'test33'
17:30:23.182 -> input is '##ROOMNAME?'
17:30:23.182 -> comparing topic to 
17:30:23.182 -> ##ROOMNAME?
17:30:23.182 -> ~@ROOMNAME-SHORT test33

with these code changes to read from and write to Serial

#define Serial2 Serial

void setup() {
//  Serial2.begin(9600, SERIAL_8N1, RXD, TXD);
  Serial.begin(115200);
  for (int i = 10; i > 0; i--) {
    Serial.print(i); Serial.print(' ');
    delay(500);
  }
//  Serial2.println((READY_MESSAGE));
  Serial.println(READY_MESSAGE);
. . .
}

Try the attached code to test input from Serial instead of Serial2

stringcommandsSerial.ino (5.02 KB)

drmpf:
Works for me

17:29:45.582 -> input is '##ROOMNAME'

17:29:45.582 -> comparing topic to
17:29:45.582 -> ##ROOMNAME?
17:29:45.582 -> comparing cmd to
17:29:45.582 -> ##ROOMNAME
17:29:45.582 -> ~@unknown command  ##ROOMNAME
17:29:57.282 -> input is '##ROOMNAME?'
17:29:57.282 -> comparing topic to
17:29:57.282 -> ##ROOMNAME?
17:29:57.282 -> ~@ROOMNAME-SHORT test
17:30:05.450 -> input is '##ROOMNAME test'
17:30:05.484 -> comparing topic to
17:30:05.484 -> ##ROOMNAME?
17:30:05.484 -> comparing cmd to
17:30:05.484 -> ##ROOMNAME
17:30:05.484 -> found cmd
17:30:05.484 -> rest of cmd 'test'
17:30:14.607 -> input is '##ROOMNAME test33'
17:30:14.607 -> comparing topic to
17:30:14.607 -> ##ROOMNAME?
17:30:14.607 -> comparing cmd to
17:30:14.607 -> ##ROOMNAME
17:30:14.607 -> found cmd
17:30:14.607 -> rest of cmd 'test33'
17:30:23.182 -> input is '##ROOMNAME?'
17:30:23.182 -> comparing topic to
17:30:23.182 -> ##ROOMNAME?
17:30:23.182 -> ~@ROOMNAME-SHORT test33




with these code changes to read from and write to Serial


#define Serial2 Serial

void setup() {
//  Serial2.begin(9600, SERIAL_8N1, RXD, TXD);
 Serial.begin(115200);
 for (int i = 10; i > 0; i--) {
   Serial.print(i); Serial.print(' ');
   delay(500);
 }
//  Serial2.println((READY_MESSAGE));
 Serial.println(READY_MESSAGE);
. . .
}




Try the attached code to test input from Serial instead of Serial2

It´s so frustrated why isn´t anything happening in Serial Monitor or Hercules SETUP utility ...
In my Serial Monitor from the IDE is nothing if I type ##ROOMNAME test nothing happens on Hercules .. what should be fixed now ..

drmpf:
Works for me

17:29:45.582 -> input is '##ROOMNAME'

17:29:45.582 → comparing topic to
17:29:45.582 → ##ROOMNAME?
17:29:45.582 → comparing cmd to
17:29:45.582 → ##ROOMNAME
17:29:45.582 → ~@unknown command  ##ROOMNAME
17:29:57.282 → input is ‘##ROOMNAME?’
17:29:57.282 → comparing topic to
17:29:57.282 → ##ROOMNAME?
17:29:57.282 → ~@ROOMNAME-SHORT test
17:30:05.450 → input is ‘##ROOMNAME test’
17:30:05.484 → comparing topic to
17:30:05.484 → ##ROOMNAME?
17:30:05.484 → comparing cmd to
17:30:05.484 → ##ROOMNAME
17:30:05.484 → found cmd
17:30:05.484 → rest of cmd ‘test’
17:30:14.607 → input is ‘##ROOMNAME test33’
17:30:14.607 → comparing topic to
17:30:14.607 → ##ROOMNAME?
17:30:14.607 → comparing cmd to
17:30:14.607 → ##ROOMNAME
17:30:14.607 → found cmd
17:30:14.607 → rest of cmd ‘test33’
17:30:23.182 → input is ‘##ROOMNAME?’
17:30:23.182 → comparing topic to
17:30:23.182 → ##ROOMNAME?
17:30:23.182 → ~@ROOMNAME-SHORT test33




with these code changes to read from and write to Serial


#define Serial2 Serial

void setup() {
//  Serial2.begin(9600, SERIAL_8N1, RXD, TXD);
 Serial.begin(115200);
 for (int i = 10; i > 0; i–) {
   Serial.print(i); Serial.print(’ ');
   delay(500);
 }
//  Serial2.println((READY_MESSAGE));
 Serial.println(READY_MESSAGE);
. . .
}




Try the attached code to test input from Serial instead of Serial2

Ok… it works on Serial Monitor but doesn´t work on the ESP32 I think … (Hercules)…

Input on Hercules :
##ROOMNAME test1232 <cr> - > Send

Output 
#ROOMNAME test1232

Go back to basics and just echo from Serial2 to Serial and visa versa.
Then add the code to build the inputLine, but keep the echo going.
Add bits of code at a time, until it breaks, then take out the last bit and fix it.

drmpf:
Go back to basics and just echo from Serial2 to Serial and visa versa.
Then add the code to build the inputLine, but keep the echo going.
Add bits of code at a time, until it breaks, then take out the last bit and fix it.

output:

üÿÿþþ
NVT: FF FE FE#ROOMNAME test45 #ROOMNAME?  (NB€ „$ k 2Áþÿ
NVT: FF FE FE
ÿþ{FF}#ROOMNAME? #ROOMNAME test #ROOMNAME test ¡Cb@ù AŒ

I get some code like this on Hercules .. I changed everything to Serial2 .. can´t type in Hercules but from Serial Monitor it works.

Those odd chars are a worry. Noise? bad earth or no earth connection. You need TX, RX and a GND connection.
A floating input? Check RX has a stable voltage when nothing being sent.
Bad wiring connection, bad solder joint?
Is the TX from Hercules an open collector. I.e. does it need a pullup resistor?
Voltage miss-match. ESP32 is 3v3 board, what is the TX volts from Hercules?
Does Hercules have a floating RTS or CTS pin that needs to be connected?

drmpf:
Those odd chars are a worry. Noise? bad earth or no earth connection. You need TX, RX and a GND connection.
A floating input? Check RX has a stable voltage when nothing being sent.
Bad wiring connection, bad solder joint?
Is the TX from Hercules an open collector. I.e. does it need a pullup resistor?
Voltage miss-match. ESP32 is 3v3 board, what is the TX volts from Hercules?
Does Hercules have a floating RTS or CTS pin that needs to be connected?

it´ set up this way

and the cables go to this Server

What is the voltage on TX and RX Hercules cables when they are not connected?

drmpf:
What is the voltage on TX and RX Hercules cables when they are not connected?

5 V

Well that's one problem. ESP32 is 3V3 and does not like 5V on the inputs.
So add the interface components shown in this project
https://www.forward.com.au/pfod/CheapWifiShield/ESP2866_01_WiFi_Shield/index.html

drmpf:
Well that's one problem. ESP32 is 3V3 and does not like 5V on the inputs.
So add the interface components shown in this project
ESP8266-01 Wifi Shield, Rev 14 for Arduino and other micros

Alright.. this will solve one Problem that there are no symbols like these anymore. The next unsolved problem is still that I can´t write over Hercules ..

May be you need a 'real' level shifter 3v3 to 5v, Sparkfun has some

Are you printing a String or a character array, if the latter have you NULL terminated it after constructing it?