How to change declared string Variable

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

I don´t really understand your question .. :confused:

drmpf:
May be you need a 'real' level shifter 3v3 to 5v, Sparkfun has some
SparkFun Level Translator Breakout - PCA9306 - BOB-15439 - SparkFun Electronics

What does this do .. ?

arduinotester01:
What does this do .. ?

it's a bidirectional voltage-level translator : you set a reference voltage on each side (typically 5V on the HIGH side and 3.3V on the LOW side) and when a signal at 5V arrives on the HIGH side, the corresponding pin on the LOW side emits 3.3V. (GND stays GND). The opposite is true as well, when 3.3V arrives on the low side, the corresponding pin on the other side shows 5V.

--> you basically translate the signals between 5V and 3.3V, hence the level shifter name.

(the pins are labeled for I2C, but it does not really matter you can use that for Serial)

J-M-L:
it's a bidirectional voltage-level translator : you set a reference voltage on each side (typically 5V on the HIGH side and 3.3V on the LOW side) and when a signal at 5V arrives on the HIGH side, the corresponding pin on the LOW side emits 3.3V. (GND stays GND). The opposite is true as well, when 3.3V arrives on the low side, the corresponding pin on the other side shows 5V.

--> you basically translate the signals between 5V and 3.3V, hence the level shifter name.

(the pins are labeled for I2C, but it does not really matter you can use that for Serial)

what has this to do with my program that I cannot send the command via Hercules?

based on the @drmpf comment

You said your Hercules is a 5V device and your ESP32 is a 3.3V device.

When you send a bit of 1 from Hercules to the ESP, the Hercules Tx pin is set to 5V and that might damage the Rx pin of your ESP which expects 3.3V

the opposite is not an issue, your ESP sends a 1 as 3.3V, which is received as 3.3V on the 5V device, but 3.3V is high enough to be considered a 1 by the receiver.

so ESP -> Hercules should be OK but the opposite direction might have damaged your ESP.

(PLEASE DON'T QUOTE FULL TEXT, JUST ANSWER. MY TEXT IS ALREADY AVAILABLE FOR READING)

I took a look at mine, the shifter is already installed, which is below my circuit board. That means everything is regulated with the current

arduinotester01:
I don´t really understand your question .. :confused:

C strings or character arrays are normally NULL terminated. The serial.print function will print the contents of the array until it finds a NULL. If you have not NULL terminated your character array then the function will read beyond the text you expect and printout whatever follows (whether in your character array or the memory that follows) until it finds a NULL
If you are using the String (note upper case S) class then you don't need to NULL terminate the String.
You don't appear to have posted the code you are currently using, so we cannot check, but failing to terminate a character array is a common fault.

countrypaul:
C strings or character arrays are normally NULL terminated. The serial.print function will print the contents of the array until it finds a NULL. If you have not NULL terminated your character array then the function will read beyond the text you expect and printout whatever follows (whether in your character array or the memory that follows) until it finds a NULL
If you are using the String (note upper case S) class then you don't need to NULL terminate the String.
You don't appear to have posted the code you are currently using, so we cannot check, but failing to terminate a character array is a common fault.

#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>
#define Serial2 Serial

// test inputs
// ##ROOMNAME <cr> sets the Roomname and
// ##ROOMNAME? <cr> shows me what should be set now.
//but I want to set it directly
// ##ROOMNAME test <cr>

String readString = "";
const size_t readString_RESERVE = 100;
String roomname;
const size_t roomname_RESERVE = 50;
String cmdValue;
const size_t cmdValue_RESERVE = 50;


//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 APPENDIX_VALUE_SPACE = " "; //Symbol(s) following a cmd befor value
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";
const String EMPTY_STRING = "";

//SYSTEM PARAMETERS
const String VERSION = "2.0";
//going to be changed
//PARAMETER ROOMNAME
const 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);
  for (int i = 10; i > 0; i--) {
    Serial.print(i); Serial.print(' ');
    delay(500);
  }
  Serial2.println((READY_MESSAGE));
  Serial.println(READY_MESSAGE);
  readString.reserve(readString_RESERVE); // allow for 100 char lines
  roomname.reserve(roomname_RESERVE);
  cmdValue.reserve(cmdValue_RESERVE);
  roomname = "test";
}

// read into line until to '\n' or '\r'returns true when have a line
bool readLine() {
  if (Serial2.available()) {
    char c = Serial2.read(); //gets  one byte from serial buffer
    if ((c == '\n') || (c == '\r')) {
      return true; // found line
    }
    // else
    readString += c;
    if (readString.length() >= readString_RESERVE) {
      return true; // a long line
    }
    // else
  }
  return false;
}

void roomconfig() {
  Serial2.readString();
  sendAnswer(COMMAND_ROOMNAME_SHORT, roomname);
  sendAnswer(COMMAND_ROOMNAME_SHORT, roomname_short);
  Serial.println(roomname_short);
  Serial.println(roomname);
}


void sendAnswer(const String& title, const String& message) {
  Serial2.print(PREFIX_ANSWER);
  Serial2.print(title);
  Serial2.print(APPENDIX_ANSWER);
  Serial2.println(message); // do not use +
}

bool topicMatching(const String& question, const String& command) {
  String fullCmd = PREFIX_REQUEST;
  fullCmd += command;
  fullCmd += APPENDIX_QUESTION;
  Serial2.println(F("comparing topic to ")); Serial2.println(fullCmd);
  return question == fullCmd;
}

bool cmdMatching(const String& cmd, const String& command, String& value) {
  String input = cmd; // will modify input but not cmd
  String fullCmd = PREFIX_REQUEST;
  fullCmd += command;
  fullCmd += APPENDIX_VALUE_SPACE;
  Serial2.println(F("comparing cmd to ")); Serial2.println(fullCmd);
  if (input.startsWith(fullCmd)) {
    Serial2.println("found cmd");
    // remove the cmd
    {
      String remainder = input.substring(fullCmd.length());
      input = remainder; // Strings don't line assigning substring to them selves
    }
    Serial2.print("rest of cmd '"); Serial2.print(input); Serial2.println("'");
    input.trim();
    if (input.length() != 0) {
      value = input;
      return true;
    } // else
    return true;
  }
  return false;
}

void processInput(String &inputString) { // this method modifies inputString
  String newValue;
  inputString.trim();
  if (inputString.length() == 0) {
    return; // ignore empty lines
  }
  Serial2.print(F("input is '")); Serial2.print(inputString); Serial2.println("'"); // echo input
  if (topicMatching(inputString, COMMAND_ROOMNAME)) {
    sendAnswer(COMMAND_ROOMNAME_SHORT, roomname);
  } else if (cmdMatching(inputString, COMMAND_ROOMNAME, cmdValue)) {
    // have newname
    roomname = cmdValue;
  }
  else {
    sendAnswer(ERROR_MESSAGE, inputString);
  }
}

void loop() {
  if (readLine()) {
    //process input string
    processInput(readString);
    readString = ""; // clear for nex time
  }
}

this is the status of the code already ... if you can show me where you think it should be modified it would be very great.

Your first posting of your on this thread showed you using a char but not reading the whole passed in text, so I could not tell if you had used a character array of String to add the characters back together, looking at your latest code it appears you have used a String and therefore my comments probably don't apply - so I don't believe you need to alter the code.

Okey.. but I still have the problem .. that I can´t send Commands over a tcp client to my board. It only works in Serial Monitor but not in Hercules :confused:

Ok ignoring tcp for the moment. You can send/receive via Serial and you can receive from Hercules and see it on Serial, but when you try to send from Serial, Hercules does not see it.

1)So put an oscilliscope on the ESP32 TX line and make sure it is bouncing up and down when you send to Hercules. You can get very cheap, small, oscilliscopes. (Or you could try the Hz setting on your multimeter if it has one)

  1. change the ESP32 Serial to a different one that has not had 5V on it, OR better yet use a new ESP32 board.

  2. double check you are sending the commands to Hercules in the format it expects. Is it '\n' or '\r' or '\r''\n' terminated.
    Test this from some other 5v terminal program.

  3. Try using an UNO instead for testing, which is 5V. Or a Mega2560 which has extra hardware serials.

drmpf:
Ok ignoring tcp for the moment. You can send/receive via Serial and you can receive from Hercules and see it on Serial, but when you try to send from Serial, Hercules does not see it.

1)So put an oscilliscope on the ESP32 TX line and make sure it is bouncing up and down when you send to Hercules. You can get very cheap, small, oscilliscopes. (Or you could try the Hz setting on your multimeter if it has one)

  1. change the ESP32 Serial to a different one that has not had 5V on it, OR better yet use a new ESP32 board.

  2. double check you are sending the commands to Hercules in the format it expects. Is it '\n' or '\r' or '\r''\n' terminated.
    Test this from some other 5v terminal program.

  3. Try using an UNO instead for testing, which is 5V. Or a Mega2560 which has extra hardware serials.

What do you mean at Point 3) ..

I think we lost along the way what's your Hercules and how things are wired.

I assume you are using Hercules SETUP utility (a piece of software)

Please describe better the physical wiring of everything; for example how do you connect your ESP to this software and where is the software running ? Do you have something like a PortStore5 somewhere in the setup ?

I´m using this configuration now

If I type in Serial Monitor this here:

Then in Hercules appears something like that:

I showed everything in my pictures. I hope it´s clear

  1. Try looking at the Hercules commands in HEX
    Arduino is sending CR NL when it does println(), Your example had

I think we lost along the way what's your Hercules and how things are wired.

I agree, I am a bit lost also as to what the end to end system looks like.
Is it
Hercules (software) -> via TCP -> ESP32 -> serial2 -> some device
or something else

drmpf:
I agree, I am a bit lost also as to what the end to end system looks like.
Is it
Hercules (software) -> via TCP -> ESP32 -> serial2 -> some device
or something else

Yes it´s happen that way. the Device is a Master /Room Controller at Port 1 from Kramer (MegaTools)

OK so what works at the moment
Can you do
Serial -> ESP32 -> serial2 -> Master / Room Controller ?

If you can do that
then work on
Hercules (software) -> via TCP -> ESP32 -> Serial (NOTE IDE serial)

When that works, do
Hercules (software) -> via TCP -> ESP32 -> serial2 -> Master / Room Controller

with the Serial monitoring what is going and coming on serial2

drmpf:
OK so what works at the moment
Can you do
Serial -> ESP32 -> serial2 -> Master / Room Controller ?

If you can do that
then work on
Hercules (software) -> via TCP -> ESP32 -> Serial (NOTE IDE serial)

When that works, do
Hercules (software) -> via TCP -> ESP32 -> serial2 -> Master / Room Controller

with the Serial monitoring what is going and coming on serial2

At the moment I can work on Arduino IDE with Serial Monitor
And if I want to work on Hercules (software) it isn´t working.
I can´t do the same work on Hercules (software) as on IDE .