[ESP32] How to reset based upon command from serial

I'm trying to reset the board based upon command send from the serial monitor. For example if I type reset in the monitor, then the board should reset. But I'm not able to do so.

void setup()

void loop()
	if (Serial.available()) {
		String temp = Serial.readString();
		if (temp.equals("reset"))

It's not working, only printing the input text and not resetting. I've even tried temp == "reset".

    if (temp.equals("reset\r"))

depending on what you have set in the monitor... (\r = Carriage return)

Nope, not working. Tried a \r, \n, \r\n and \n\r.

try with

if(temp.indexOf("reset") > -1)

Then something else is wrong...

This method is working.

I'm wondering too what's wrong. The logic is pretty simple and it not working doesn't make any sense. Seeing your screenshot, I'm missing something.


Here take a look at this

Try this to see what you are actually receiving...

void setup()

void loop()
    byte temp = Serial.read();
    Serial.print(temp, HEX);
    Serial.print(' ');

Got this using your sketch: 72 65 73 65 74 A. Seems fine, even used a hex to string converter to double check.


The A shouldn't be there. Was adding a newline. Working now. Had to use reset\n for comparison.

0A is a newline.


Yes you're correct. Fixed now.

Thank you @red_car and @cotestatnt

ur welcome.

use temp.trim() to strip any leading and trailing whitespaces

Thanks @Juraj, your suggestion works too.

This use of variable-type String is inside loop (which is never left like more sub-ordered functions.
This includes the danger of filling up all RAM with repeated assigning values to the String-type variable.

You should not use variable-type String in that way. Of course an ESP32 has more RAM than an Arduino-Uno. But it is just a matter time until the RAM-overwrite caused through repeated assigning new content to the string-variable happends.

You should use a library that is based on c_strings (which are arrays of char) like the SafeString-library. The SafeString-library offers (most of the comfort if variable-type String but is safe to use.

@the purists: If you want newcomers to use c_string directly without an additional library provide an easy to understand tutorial with direct coded functions that give all the comfort of SafeString.
Too lazy to provide this tutorial? Well I guess you have to accept the shift towards SafeString

#include <SafeString.h>
createSafeString(myDemo_SS, 32);
createSafeString(mySecondDemo_SS, 32);

unsigned long myCounter;

// if program starts printout the source-code filename etc. to the serial monitor
void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.print( F("  compiled ") );
  Serial.print( F(" ") );

//useful function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  else return false;            // not expired

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long
const byte    OnBoard_LED = 13; // Arduino-Uno Onboard-LED is IO-pin 13

// make onboard-LED blink to show: "program is running"
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 

void setup() {
  Serial.println( F("Setup-Start") );
  myCounter = 0;
  myDemo_SS = "Hello world!";

void loop() {
  // loop is running very fast counting up very fast
  // but only once every 1234 milliseconds print 
  if ( TimePeriodIsOver(MyTestTimer,1234) ) {
    mySecondDemo_SS = myDemo_SS;  // assigning a SafeString to another SafeString
    mySecondDemo_SS += " ";       // append a SPACE
    mySecondDemo_SS += myCounter; // append integer-number


best regards Stefan

Hmm. Next time use pencil and paper to keep track of you experiments.


Unless, of course, one properly uses the ESP32's built in OS, freeRTOS. freeRTOS will clean up those pesky String caused memory holes.

I'm unsure what "properly uses the ESP32's built in OS" means.

A: Programming with the Arduino-IDE ?

B: or not programming with the Arduino-IDE but instead using the esspressif-SDK ?

C: Something inbetween by using calls to OS-functions that do the cleanup ?
(if C is possible: why is it not implemented in the ESP32-arduino-code?)

best regards Stefan

my 2 cents:

Most of the posts in the Arduino Forum are related to problems due to the abuse of delay(), another big chunk about of "strange things" done in an attempt not to use String...

I'm not a fan of String, but I'm quite sure this sketch will never have problems, especially considering that the variable is declared local as it should be and considering the 520 KB of SRAM of ESP32.
The necessary memory space will be allocated in the heap contiguously because there are no other statements that can cause fragmentation in between and, when there are no more characters in the serial buffer, temp is destroyed and memory released.

The following is meant as a real question:

The declaring of the String is inside "void loop" which is never left
Does this mean that a variable that is declared inside a function
In this case it is function "loop"
that even if this function is never left the variable gets destroyed and re-allocated new with each iteration of loop?

void loop()
  if (Serial.available()) {
    String temp = Serial.readString();  //<<== the declaration does  destroy **and** allocate new?

best regards Stefan