Skipping If Statement

Hello Guys,

I have been trying for hours and hours to sort out this issue but apparently I can't and for the life of me I cannot understand why and that's why I am asking for your help. Thanks in Advance.

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

//Global Variables
String adminName = "Lelz";
String realName = "Lelz";
String nick = "Lelz";
String chan = "#NesquickBotTesting";
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0xDE, 0x0B};//mac address for [my] sheild
//char server[] = "irc.chat.twitch.tv"; //can also be a byte[] with a direct IP address
char server[] = "chat.freenode.net";
//byte server[] = {54,148,142,141};
byte ip[] = {192, 168, 0, 177};
EthernetClient client;

//definitions
int connectToServer();

//let's go
void setup() {
  Serial.begin(250000);//Start the serial - useful for debugging purposes
  //Start the connection to the IRC server

  pinMode(24, OUTPUT);
  if (Ethernet.begin(mac) == 0)
  {
    Ethernet.begin(mac, ip);//if we can't get an IP address, we'll force one.
  }
  delay(2000);//Give a couple of seconds to allow the ethernet board to initialize.
  if (connectToServer() == 0)
  {
    Serial.println("Connection to server FAILED! Entering infinite loop. Please reset the Arduino and try again.");
    for (;;)
    {
      //do nothing until arduino is reset. Basically an infinite loop.
    }
  }
}

void loop() {
  bool sendRegInfo = true;
  bool newLine = false;
  String messageReceived = "";
  String messageToSend = "";
  char c;
  char d;
  String serialInput = "";






  while (client.connected())
  {
    c = client.read();
    if (Serial && Serial.available() > 0)
    {
      serialInput = Serial.readStringUntil('\n');

    }
    if (c != -1)//If we read a valid character (That is, client.read() gives us something to work with). -1 is returned when nothing is read.
    {
      messageReceived += c;
    }
    if (c == '\n')
    {

      int commaIndex = messageReceived.indexOf('!');
      int secondCommaIndex = messageReceived.indexOf('!', commaIndex + 1);
      String firstValue = messageReceived.substring(0, commaIndex);
      String secondValue = messageReceived.substring(commaIndex + 1, secondCommaIndex);
      String thirdValue = messageReceived.substring(secondCommaIndex + 1);

      Serial.println("Isto e firstValue:   " + firstValue);
      Serial.println("Isto e secondValue:   " + secondValue);
      Serial.println("Isto e thirdValue:   " + thirdValue);


      String LEDON = "lon";
      String LEDOFF = "loff";

      if (thirdValue.equals(LEDON)) {
        Serial.println("ONNNNNN");
        digitalWrite(24, HIGH);
      } else if (thirdValue.equals(LEDOFF)) {
        Serial.println("OFFFFFF");
        digitalWrite(24, LOW);
      }

      Serial.print(messageReceived); //NOT println because the carriage return already exists in the string sent from the server.
      newLine = true;

      
    }


    if (sendRegInfo)
    {
      client.println("USER " + nick + " * 0 :" + realName);
      client.println("NICK " + nick);
      client.println("JOIN " + chan);
      sendRegInfo = false;
    }
    if (newLine)//if we have a string to process...
    {

      if (messageReceived.startsWith("PING"))
      {
        messageToSend = messageReceived;
        messageToSend.replace("PING", "PONG");
      }
      if (messageReceived.indexOf("PRIVMSG " + nick + " :QUIT") > messageReceived.indexOf(":" + adminName)) // Quit in query
      {
        messageToSend = "QUIT";
      }
      messageReceived = "";
      newLine = false;
    }
    else if (serialInput != "")
    {
      messageToSend = "PRIVMSG " + chan + " :" + serialInput;
      serialInput = "";
    }
    if (messageToSend != "")
    {
      client.println(messageToSend);
      Serial.println("OUTPUT: " + messageToSend);
      messageToSend = "";
    }
  }
  Serial.println("Connection is gone. Quitting...");
  for (;;)
  {
    //end on infinite loop.
  }
}


int connectToServer()
{
  if (Serial)
  {
    Serial.println("Connecting to the IRC server...");
  }
  if (client.connect(server, 6667))
  {
    return 1;
  }
  else return 0;
}

My problem is on the IF statement that turns the LED ON/OFF. For some reason it doesn't turn on/off even though the thirdValue variable contains the right word which is "lon" "loff".
Can someone give me some sort of help?

Best Regards
Nesquick

  while (client.connected())
  {
    c = client.read();
    if (Serial && Serial.available() > 0)
    {
      serialInput = Serial.readStringUntil('\n');

Why is receiving serial data dependent on their being a client connected?

      int commaIndex = messageReceived.indexOf('!');

The index does NOT point to a comma!

even though the thirdValue variable contains the right word which is "lon" "loff".

Are you CERTAIN that the variable doesn't contain "lon\r" or "loff\r"?

Print the value better to be sure.

Serial.print("thirdValue: [");
Serial.print(thirdValue);
Serial.println("]");

Do you see

thirdValue: [lon]

or

thirdValue: [lon
]

?

Hello,

Thanks for your reply.

1- I think there is no point to receive serial data if the client is disconnected. I am right?
2- I know that the index point to an Exclamation mark, I forgot to change the variable name.
3- This is what i get, and I can see the problem. there is a /n at the end of the "lon" and thats why its not working properly. I am correct?

thirdValue: [lon
]

Best Regards
Nesquick

1- I think there is no point to receive serial data if the client is disconnected. I am right?

No. The serial data isn't coming from the client.

3- This is what i get, and I can see the problem. there is a /n at the end of the "lon" and thats why its not working properly. I am correct?

It is a line feed, since you are dealing with the carriage return. You can use String::trim() to get rid of it (or not store it in the first place).