ESP8266 receive problem

I have built a udp server for receiving udp messages. i tried to implement a type or homemade ack.

  int packetSize = Udp.parsePacket();
  if (packetSize) {
        char verifyStart[7];
    char verifyEnd[7];
    Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
    int len = Udp.read(incomingPacket, 800);
    strncpy (verifyStart, (char*)incomingPacket, 7 );
    strncpy (verifyEnd, (char *)incomingPacket + len - 6 , 7 );
    Serial.println(packetSize);
    if (strcmp(verifyStart, "NODECM") == 0) {
      Serial.println("NODECM");
    }
    if (strcmp(verifyEnd, "CMNODE") == 0) {
      Serial.println("CMNODE");

    }

if i send bytes NODECM it prints NODECM in the serial console as i expected also the same result if i send NODECMCMNODE both if statements print, but if i send the wrong command after a recognized byte string it still prints the if statements as if i was sending the expect NODECM or NODECMCMNODE. am i forgetting to flush the incomingPacket? how do i do this?

If i send NODECM it prints NODECM at the serial console. If i send NODEC it prints NODECM at the erial console. if i send N to the server it also prints NODECM at the serial console. this only happens if i first send NDOECM to the udp server. if i send the wrong command first it wont print anything in the console as expected. but if i send NODECM then any time i send anything that starts with N it prints NODECM at the serial console. why is this happening sorry if this is hard to understand

Udp.read () does not null terminate received pkt like Cstr functions require

either add the null yourself or use memset to clear the incomingPacket buffer

racpi:
Udp.read () does not null terminate received pkt like Cstr functions require

either add the null yourself or use memset to clear the incomingPacket buffer

How do i use memset to clear the incoming packet buffer. i think i know what you mean by null terminated "NODECM0" but either way if it wasnt null terminated and it expected a null terminated string then why does it work in the first place?

memset(incomingPacket, 0, sizeof(incomingPacket)); Seems to fix the problem

Can anyone tell me how i can get the data from between the first and last 7 bytes?

  int packetSize = Udp.parsePacket();
  if (packetSize) {
    char verifyStart[7];
    char verifyEnd[7];
    Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
    int len = Udp.read(incomingPacket, 800);
    strncpy (verifyStart, (char*)incomingPacket, 6);
    strncpy (verifyEnd, (char *)incomingPacket + len - 6 , 7 );
    if (strcmp(verifyStart, "NODECM") == 0) {
    if (strcmp(verifyEnd, "CMNODE") == 0) {
      Serial.println(data_between_bytes);
    }
    }
    memset(incomingPacket, 0, sizeof(incomingPacket));
  }

Maybe there is a better way, this is what i ended up doing. someone please chime in if you see something critically wrong here,

  int packetSize = Udp.parsePacket();
  if (packetSize) {
    char verifyStart[7];
    char verifyEnd[7];
    char _data[5];
    Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
    int len = Udp.read(incomingPacket, 800);
    strncpy (verifyStart, (char*)incomingPacket, 6);
    strncpy (verifyEnd, (char *)incomingPacket + len - 6 , 7 );
    strncpy (_data, (char*)incomingPacket + 6,len - 12);
    int value = atoi(_data);
    if (strcmp(verifyStart, "NODECM") == 0) {
    if (strcmp(verifyEnd, "CMNODE") == 0) {
      Serial.println(value);

    }
    }
    memset(incomingPacket, 0, sizeof(incomingPacket));
  }

how come when i send NODEC1100C1NODE value prints as 1000 but when i send NODEC110C1NODE value prints as 10?

  int packetSize = Udp.parsePacket();
  if (packetSize) {
    char verifyStart[7];
    char verifyEnd[7];
    char _data[10];
    Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
    int len = Udp.read(incomingPacket, 800);
    strncpy (verifyStart, (char*)incomingPacket, 6);
    strncpy (verifyEnd, (char *)incomingPacket + len - 6 , 7 );
    strncpy (_data, (char*)incomingPacket + 6, len - 12);
    unsigned long value = atol(_data);
    if (strcmp(verifyStart, "NODEC1") == 0) {
      if (strcmp(verifyEnd, "C1NODE") == 0) {
        Serial.println("command_1");
        dutycycle1 = value;
      }
    }

I suspect that you need to null terminate _data.

I changed len -12 to -11 and it seemed to have took care of the problem. i dont know why and for some reason i cant wrap my head around how to terminate the string besides adding \0 to the end of the string when i send the message? as i understand the end of a string has a trailing '0' byte

strncpy (_data, (char*)incomingPacket + 6, len - 11);

Like this:

_data[len - 12]=0;

wildbill:
Like this:

_data[len - 12]=0;

Is this for the strncpy expression or the strcmp? What i see if were inserting 0 at position len-12 'end' in data. By adding a 0 to the end of the string it will remove a 0? sorry i'm still a bit confused.

strncpy does not necessarily null terminate the target string so you need to do it manually after copying digits into _data.

If it's not null terminated, you will print what you copied, plus whatever is hanging around in memory until you get to a NULL, in your case apparently that's an extra '0'.

wildbill:
strncpy does not necessarily null terminate the target string so you need to do it manually after copying digits into _data.

If it's not null terminated, you will print what you copied, plus whatever is hanging around in memory until you get to a NULL, in your case apparently that's an extra '0'.

null terminating the string did solve the problem. Im going to try to use an ESP8266 to control multiple PWM controlled devices. now i just have to figure out how many PWM signals the ESP8266 can generate simultaneously at the same freq and different duty cycles