Error Using If statement on the serial buffer

Hi there, I am trying to check whether the Serial Buffer contains a specific string, if so i would like to turn on one LED, but for some reason i can read an print out the Serial Buffer but i cannot check whether it is the sentence i want it to be. Btw, i am using an ESP32 connected to an Arduino Mega.

This is the code for Arduino Mega:
//Code for Arduino MEGA
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

uint8_t leds_cycle[] = { 5, 6, 7};
uint8_t leds_control[] = {0, 2, 3, 4};

int quant = 20;
int led_passed = 0;
int led_delay = 1000;
int current_led = 0;

void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(4,0);

Serial.begin(115200);

}

void loop() {

led_passed += quant;

if (Serial.available()){

//Serial.println("It is working in the Serial available");

String Incoming = Serial.readString();
Incoming.trim();

String Incoming1 = Serial.readString();
Incoming1.trim();

Serial.println(Incoming);
Serial.println(Incoming1);

delay(1000);

if(Incoming == "Switch 1"){
  Serial.println("It is coming in the if statement");
    if(Incoming1 == "1")
      digitalWrite(2, HIGH);

    digitalWrite(2,LOW);
}

else if(Incoming.toInt() == "3"){
  String State = Serial.readString();

  Serial.print("Setting LED ");
  Serial.print(Incoming);
  Serial.print(" to state ");
  Serial.println(State);
  
    if(State == "true" || State == "TRUE"){
      digitalWrite(3, HIGH);
    }
   digitalWrite(3, LOW);

}

else if(Incoming.toInt() == "4"){
  String State = Serial.readString();

  Serial.print("Setting LED ");
  Serial.print(Incoming);
  Serial.print(" to state ");
  Serial.println(State);

    if(State == "true" || State == "TRUE"){
      digitalWrite(4, HIGH);
    }
   digitalWrite(4, LOW);

}

else {
led_delay = Incoming.toInt();

}

}

if (led_passed > led_delay) {
digitalWrite(leds_cycle[current_led], LOW);
led_passed = 0;

current_led = current_led >= 2 ? 0 : (current_led + 1);
// Turn on next LED in a row
digitalWrite(leds_cycle[current_led], HIGH);
delay(3000);
/* Serial.print("Current LED: ");
Serial.println(current_led);
Serial.print("led_passed: ");
Serial.println(led_passed);
Serial.print("led_delay: ");
Serial.println(led_delay);
Serial.println('\n');*/

}

}

This is the code for ESP32:
//Code for ESP32

#include <WiFi.h>
#include <DHTesp.h>
#include <ThingsBoard.h>
#include <HardwareSerial.h>

#define COUNT_OF(x)((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#define WIFI_USER "YOURSSID"
#define WIFI_PASS "YOURPASS"
#define TOKEN "YOURTOKEN"
#define THINGSBOARD_SERVER "YOURTHINGSBOARDSERVER"

#define echoPin 13
#define trigPin 12

#define DHT_PIN 14

HardwareSerial SerialPort(2);

WiFiClient espClient;
ThingsBoard tb(espClient);

int status = WL_IDLE_STATUS;

uint8_t leds_cycle[] = {5, 6, 7};
uint8_t leds_control[] = {0, 2, 3, 4};

DHTesp dht;

int quant = 20;
int led_delay = 1000;
int send_delay = 2000;
int led_passed = 0;
int send_passed = 0;
int current_led = 0;

bool subscribed = false;

long duration;
int distance;

RPC_Response processDelayChange(const RPC_Data &data)
{
led_delay = data;
SerialPort.print(led_delay);

return RPC_Response(NULL, led_delay);

}

RPC_Response processGetDelay(const RPC_Data &data)
{
return RPC_Response(NULL, led_delay);
}

RPC_Response processSetSwitch1(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");

bool onoff = data;
SerialPort.println("Switch 1");
SerialPort.println(onoff);

return RPC_Response(NULL, onoff);
}

RPC_Response processSetSwitch2(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");

bool onoff = data;
SerialPort.print("Switch data: ");
SerialPort.println(onoff);

return RPC_Response(NULL, onoff);
}

RPC_Response processSetSwitch3(const RPC_Data &data)
{
Serial.println("Received the set GPIO RPC method");

bool onoff = data;
SerialPort.print("Switch data: ");
SerialPort.println(onoff);

return RPC_Response(NULL, onoff);
}

RPC_Callback callbacks[] = {
{ "setValue", processDelayChange },
{ "getValue", processGetDelay },
{ "setSwitch1", processSetSwitch1 },
{ "setSwitch2", processSetSwitch2 },
{ "setSwitch3", processSetSwitch3 },
};

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_USER, WIFI_PASS);
Serial.begin(115200);

SerialPort.begin(115200, SERIAL_8N1, 16, 17);
InitiateWiFi();
dht.setup(DHT_PIN, DHTesp::DHT22);

}

void loop() {
delay(quant);

led_passed += quant;
send_passed += quant;

// Reconnect to WiFi, if needed
if (WiFi.status() != WL_CONNECTED) {
reconnect();
return;
}

if (!tb.connected()) {
subscribed = false;

if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
  return;
}

}

// Subscribe for RPC, if needed
if (!subscribed) {

if (!tb.RPC_Subscribe(callbacks, COUNT_OF(callbacks))) {
  return;
}

subscribed = true;

}

// Check if it is a time to send DHT22 temperature and humidity
if (send_passed > send_delay) {

// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor

TempAndHumidity lastValues = dht.getTempAndHumidity();    
if (isnan(lastValues.humidity) || isnan(lastValues.temperature)) {
  
} else {
  tb.sendTelemetryFloat("temperature", lastValues.temperature);
  tb.sendTelemetryFloat("humidity", lastValues.humidity);
}

tb.sendTelemetryFloat("Distance", distance);
send_passed = 0;

}

// Process messages
tb.loop();

}

void InitiateWiFi(){

while (WiFi.status() != WL_CONNECTED){
delay(500);

}

}

void reconnect(){
status = WiFi.status();

if (status != WL_CONNECTED){
WiFi.begin(WIFI_USER, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED){
delay(500);

}

}
}

You can't compare an integer to a character string. Perhaps you meant:
else if(Incoming == "3"){
or
else if(Incoming.toInt() == 3){

It is unlikely that "Incoming1" will contain any characters since the input buffer has been empty for at least a second after reading "Incoming".

Hello, thanks for the reply. Actually i noticed that, I've just been testing on one of the if else statement actually, which this line:

if(Incoming == "Switch 1"){
Serial.println("It is coming in the if statement");
if(Incoming1 == "1")
digitalWrite(2, HIGH);

digitalWrite(2,LOW);

}

Perhaps you could help me check whether i got any error with the first if else statement, that would be great.

Actually, Incoming1 contains data as I used Serial.println(Incoming1); in the following line to check what was i receiving.

You might miss some brackets {} and an else.
If Incoming1 is equal to "1", DO2 will be set high and immediately thereafter it will be set low...

Please use code tags! And align your code properly (use CTRL-T).

According to Arduino Language Reference "The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement."
Additionally, I purposely added the statement Serial.println("It is coming in the if statement") to check whether it is going into the if statement.

Thanks for the alignment recommendation tho.

It's right, but ...
Are you don't clue, that this combination of the statements is senseless?

Sorry, I don't quite get your point. How is it senseless? Because I am sending "Switch 1" and "1" on the ESP32 to the Arduino Mega, therefore, the Arduino Mega should receive "Switch 1" and "1" and store it in Incoming and Incoming1 respectively. With that, I am using the If statement to check whether Incoming contains "Switch 1" and the following If statement to check whether Incoming1 contains "1". If both of the conditions are satisfied, the LED should be set, however, if only the first If statement is satisfied, the LED would be set LOW or turned off. May you elaborate further if I misunderstood your point. Thanks :smiley:.

Why do you think that pin 2 won't be set low immediately after it is set high when the condition is met.

This was presented to you in post #5. You chose to focus on the brackets and not the else.

Thanks for pointing it out there, I get what you mean. I am missing an else statement there, therefore, the LED is set on and set off immediately.