Using strcmp for string comparison of UDP packets

This program on NodeMCU listens to the UDP port and takes the string from the incoming packet.
Using strcmp(), I compare the string in the packet. I expect the output of strcmp() to be 0 when it matches but it doesn’t. I printed the value when I sent true in the packet and it was compared with true, it showed 10 instead of 0.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "ESP8266-Access-Point";
const char* password = "123456789";

WiFiUDP Udp;

unsigned int localUdpPort = 4210;  // local port to listen on
char incomingPacket[255];  // buffer for incoming packets
char replyPacket[] = "Got message successfully!";  // a reply string to send back


void setup()  {
  Serial.begin(115200);
  Serial.println();

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");

  Udp.begin(localUdpPort);
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    // receive incoming UDP packets
    Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
    int len = Udp.read(incomingPacket, 255);
    if (len > 0)  {
      incomingPacket[len] = 0;
    }
    Serial.printf("UDP packet contents:%s\n", incomingPacket);
    
    if(strcmp(incomingPacket, "true")==10)  {
      Serial.println("on");
      digitalWrite(LED_BUILTIN, LOW); // turn led ON
    }
    else if(strcmp(incomingPacket, "false")==10) {
      Serial.println("off");
      digitalWrite(LED_BUILTIN, HIGH);  //turn led OFF
    }

    // send back a reply, to the IP address and port we got the packet from
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(replyPacket);
    Udp.endPacket();
    Serial.println("Reply sent");
  }
}

receiver.ino (1.63 KB)

Almost certainly because one string contains a trailing newline, with the other doesn't....

A trick that you can use to see if a string has a hidden trialing newline character is to print it like so:

Serial.print("<");
Serial.print(suspectString);
Serial.print(">");

If the string has a hidden trailing newline character

<suspectString
>

will be printed.

If not

<suspectString>

will be printed.

You could try using strncmp:

strncmp

int strncmp ( const char * str1, const char * str2, size_t num );

Compare characters of two strings
Compares up to num characters of the C string str1 to those of the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.

groundFungus:
A trick that you can use to see if a string has a hidden trialing newline character is to print it like so:

Serial.print("<");

Serial.print(suspectString);
Serial.print(">");




If the string has a hidden trailing newline character 


<suspectString



will be printed.

If not


```

will be printed.

Well, I checked it and yes it has a hidden trailing newline character.
strncmp() is a better choice for such issues.