Conditionals in main function do not detect serial port value

I programmed something very simple on an ESP32 devkit V1 card, however my conditionals don't work. Every time I test it in my terminal I have the character string according to what I expected but it does not access within the conditional.

#include <Arduino.h>
#define RELE 12

String mensaje;

void setup() {
Serial.begin(115200);
pinMode(OUTPUT,RELE);
digitalWrite(RELE,HIGH);
}

void loop() {
if(Serial.available()){
mensaje = Serial.readString();
Serial.print(mensaje);

if(mensaje=="ON"){
  digitalWrite(RELE,HIGH);
  Serial.print(mensaje);
}

else if(mensaje=="OFF"){
  digitalWrite(RELE,LOW);
  Serial.print(mensaje);

}

mensaje ="";

}
}

The incoming data may have invisible characters with it. Like carriage return ('\r') and/or line feed ('\n'). "ON\r\n" is not equal to "ON".

Try adding this:

mensaje = Serial.readString();
// add this line to remove invisible characters
mensaje.trim();

Serial.print(mensaje);

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

1 Like

Thanks, I just tested it with the trim function and apparently it entered the conditional as expected, However my GPIO does not go into a positive state (High) I tried to change it from GPIO however I never had positive voltage, do you think it is a failure in my ESP32 board?
By the way, thank you very much for the advice to upload the code properly to the forum, it was my first time and I was a bit lost.

#include <Arduino.h>
#define RELE 14

String mensaje;

void setup() {
  Serial.begin(115200);
  pinMode(OUTPUT,RELE);
  digitalWrite(RELE,HIGH);
}

void loop() {
  if(Serial.available()){
    mensaje = Serial.readString();
    mensaje.trim();   // line to remove invisible characters such as '\n'
    Serial.println(mensaje);
    if(mensaje=="ON"){
      digitalWrite(RELE,HIGH);
      Serial.println("TURN ON RELE");
    }
    else if(mensaje=="OFF"){
      digitalWrite(RELE,LOW);
      Serial.println("TURN OFF RELE");
    }
    else{
      Serial.println("Invalid");
    }
    mensaje ="";
  }
}

The Output in the terminal is like this (It was set to 115200 baud and with both carriage return and new line):

Input: "ON"
Output: "ON"
"TURN ON RELE"

Input: "OFF"
Output: "OFF"
"TURN OFF RELE"
Input: "asdfgh"
Output: "Invalid"

Also the Board I'm using it is the ESP32 devkit V1
ESP32 Amazon

Just test it with a simpler loop() function, viz:


#include <Arduino.h>
#define RELE 14

String mensaje;

void setup() {
  Serial.begin(115200);
  pinMode(OUTPUT,RELE);
  digitalWrite(RELE,HIGH);
}

void loop() {
      digitalWrite(RELE,HIGH);
      Serial.println("TURN ON RELE");
  
      delay(333);

      digitalWrite(RELE,LOW);
      Serial.println("TURN OFF RELE");
 
      delay(300);
}

HTH

a7

1 Like

Okay, I loaded your code to test it and unfortunately it didn't put the pin high... Now I think my board is damaged then, although it seems a bit strange to me because a few days ago I had tried other programs with it and it had worked correctly .

Thanks for your time.!

PD:Do you think it could be the way I'm uploading the file to the board?
image|690x388

If the upload method worked before, then no. If you are unsure about what you did before, then you will have to find the right settings.

But I don’t think that’s your problem.

Use the same program but move the relay or LED to a different pin. Sometimes one port bit, one I/O pin that is, can be damaged, but other pins will be fine.

Doe that board have a build in LED you could try?

Are there any indications of failure to upload?

I think if there is damages to the board, they haven’t killed it completely.

HTH

a7

Actually I tested the relay in several Outputs, and none of them appears to work as expected.
And no, when I upload the code everything seems right an I don't get any errors.

The pinMode is backwards.
Should be:

pinMode(RELE, OUTPUT);

I tried it with my ESP32 DEV KIT board and it works fine with an LED connected to pin GPIO14.

1 Like

@groundFungus good catch but not in the small testing program, so.

@alfredo_feregrino can you try just an LED and series resistor? Or test your relay?

Uh oh, IC, pinMode, yikes… so, yeah good catch.

Cut and paste and errors come along for free...

a7

I tested with a ESP32 DEV KIT and LED and it worked OK after the change. I don't know the specs on their relay, whether it will work at 3.3V. That can be determined by just testing it with 3.3V.

I only caught it after uploading the code to my ESP32 and it would not work. I could not understand why until I really looked at the code. It had to be something like that.

Oh, that can explain everything...
I already checked and the last code I told you @alto777 was in the correct order as @groundFungus said: pinMode ('some name', Output) so that code worked fine, but idk what happen to make me think that It was backwards. And of course I will test it with the ESP32 (Although it will be until Monday because they already closed the laboratory and I do not have a ESP32 board of my own :frowning: I'm going to buy one anyways... )

I will inform you although I believe that everything will be solved :grin:

I ALREADY TESTED IT and it works perfectly. Thank you so much, hope one day I can help you too.

:slight_smile:
:slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.