Simple String comparison not working


I have an encj28 eth shield with an UNO board.
Everything is working, using the UDP example sketch from the Eth library.
From an example for string comparisons, found here, I tried an if statement to light LED 13.

The led should be turned on with an UDP packet.


#include <EthernetENC.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
IPAddress ip(192, 168, 0, 200);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // start the Ethernet
  Ethernet.begin(mac, ip);

  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");

  // start UDP

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Received packet of size ");
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i=0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
    Serial.print(", port ");

    // read the packet into packetBufffer, UDP_TX_PACKET_MAX_SIZE);
    String results = packetBuffer;
    if (results == "A1=ON") {
  //for the LED
      Serial.println("Turning LED ON");
      digitalWrite(13, HIGH);


   // results = (ReplyBuffer);

    // send a reply to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

    digitalWrite(13, HIGH);


With Serial debugger you can see, the string gets loaded from the UDP packet into the string "results"
It prints in the serial command line. yet in the code lines below it, the simple string comparator if statement ( if (results == "A1=ON") ) never does what it supposed to be doing... turning the led on.

What am I missing here? Must be something really simple, but I cannot find it...

You are missing the linefeed
"A1=ON" != "A1=ON\n"

Serial.print( "packetBuffer = '");

will illuminate the issue

You should not use the "String" class on an AVR based MCU's like the UNO because it may cause unwanted behavior which can be hard to debug. If you really have to, you could use the trim() function to remove blank characters:

String result = packet;
result.trim(); //Remove blank characters
if (result == "whatever") {
  //Do something