Data from serial comms and strcmp not working as expected

So I have a 4x4 keypad providing input to an Arduino Nano which then checks the input and passes a message to a Wemos D1 mini depending on the input. The serial communication works and when the commend 'reset' is sent from the Wemos to the Arduino, it successfully checks the received message using strcmp and calculates a new random code which it passes back. However, this same code structure (specifically "strcmp") does not work when I use it for the Wemos. As the Arduino is working, I'm focusing on the Wemos.

I declare the following variable at the start:

char buffer[64];

Then I run ReceiveChar() in the void loop:

void ReceiveChars()
{
  if (keypad.available())
  {
    while (keypad.available())
    {
      buffer[count++] = keypad.read();   // writing data into array
      if (count == 64)break;
      delay(3);
    }

    Serial.println("New Data: ");
    Serial.println(sizeof(buffer));
    Serial.write(buffer, sizeof(buffer));
    Serial.println();
    if(strcmp(buffer, "1236547890123")==0) {
      Serial.println("Correct code received");
    } else if (strcmp(buffer, "Alert!")==0) {
      Serial.println("Alert received");
    } else {
      Serial.print("New code: ");
      Serial.println(buffer);
    }  
    clearBufferArray();        // call clearBufferArray function to clear the storaged data from the array
    count = 0;                       // set counter of while loop to zero
  }
}

void clearBufferArray()              // function to clear buffer array
{
  for (int i = 0; i < 64; i++)
  {
    buffer[i] = NULL;    // clear all index of array with command NULL
  }
}

The Serial monitor spits out:

*New Data: *
64
Alert!

New code: Alert!

So

  1. The char array "Alert!" is correctly received into the buffer array
  2. The buffer array is 64 bytes long which I suspect is the problem (but I'm not sure how to fix)
  3. strcmp does not pick up that this matches "Alert!" and thinks it is a new code that was sent.

strcmp works on C strings
C strings need to be terminated.

*New Data: *
64
Alert!

New code: Alert!

do you see the epmty line? that is part of the received string.

Ok, so I added

buffer[count++] = '\0';

after the while loop but it still picks up a 64 byte char array. I really try to figure things out and find a solution before asking here but I am just stumped.

I do get that empty line yes. But I don't know why or how to get rid of it.

sizeof is the size of the buffer, not the size of data filled. print count

turn off line ending in Serial Monitor

Interesting, I included the code

Serial.println(count);

and it gives me 3 more than the number of characters, i.e. for "Alert!" (6 characters) it returns 9. The additional
buffer[count++] = '\0';
adds one but where does the other two come from?

(I switched to "No line ending" but still get the extra empty line.)

why don't you use

} else if (strncmp(buffer, "Alert!", 6)==0) {

if a match at the beginning is sufficient.

This works!

I don't understand where the additional characters come from and might never know...

But thanks, I owe you a beer!