esp8266 crashing

when i invoke function sendCommand() the esp crashes and reboots. I think it has something to do with (char*) "NODECM", (char*)"NODECM" im trying to send those chars but why is it causing it to crash?

#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress Client1(192, 168, 4, 100);

const char* ssid = "LCS";
const char* password = "a1b2c3d4";

WiFiUDP Udp;
unsigned int localUdpPort = 4210;  // local port to listen on
unsigned int clientPort = 4220;
char incomingPacket[800];  // buffer for incoming packets

int _pwmVal = 600;

#define OLED_RESET LED_BUILTIN  //4
Adafruit_SSD1306 display(OLED_RESET);

void sendCommand(IPAddress to, char* data, char* delimiter, char* delimiter2  ) {
  char* packetData = data;
  Udp.beginPacket(to, clientPort);
  Udp.write((const uint8_t*)delimiter, sizeof(delimiter) - 1);
  Udp.write((const char*)packetData, sizeof(packetData));
  Udp.write((const uint8_t*)delimiter2, sizeof(delimiter2) - 1);
  Udp.endPacket();
}

void setup() {
  Serial.begin(115200);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  WiFi.setOutputPower(20.5);
  WiFi.persistent(0);
  Serial.begin(115200);
  Serial.println();
  WiFi.mode(WIFI_AP);
  while (!WiFi.softAP(ssid, password, 9, false, 15)) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
}

void loop() {
  _pwmVal = 600;
  sendCommand(Client1, (char*)_pwmVal, (char*) "NODECM", (char*)"NODECM");
  //  display.clearDisplay();
  //  display.setCursor(0, 0);
  //  display.println("Sent1");
  //  display.setCursor(1, 1);
  //  display.display();
  delay(2000);
}

at first i tried,

void sendCommand(IPAddress to, char* data, char delimiter[], char delimiter2[]  ) {
  char* packetData = data;
  Udp.beginPacket(to, clientPort);
  Udp.write((const uint8_t*)delimiter, sizeof(delimiter) - 1);
  Udp.write((const char*)packetData, sizeof(packetData));
  Udp.write((const uint8_t*)delimiter2, sizeof(delimiter2) - 1);
  Udp.endPacket();
}


  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
}

void loop() {
  _pwmVal = 600;
  sendCommand(Client1, (char*)_pwmVal, "NODECM", "NODECM");

}

but i have the same results

How come when i send the data to the client like ,

void sendCommand(IPAddress to, char* data ) {
  char delimiter[] = "NODECM";
  char delimiter2[] = "CMNODE";
  //char* packetData = data;
  Udp.beginPacket(to, clientPort);
  Udp.write(delimiter);
  Serial.println(sizeof(delimiter));
  Udp.write(delimiter2);
  Udp.endPacket();
}

the client receives the data but verifyStart when printer says,
NODECM⸮Z @⸮⸮⸮?

instead of
NODECM

here is the receiving code,

  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);
    Serial.println(verifyStart);
    Serial.println(verifyEnd);
    _data[len - 12] = 0;
    unsigned long value = atol(_data);
    if (strcmp(verifyStart, "NODECM") == 0) {
      if (strcmp(verifyEnd, "CMNODE") == 0) {
        Serial.println("command_CM");
      }
    }

strncpy takes a maximum length. In this case you have said 6...

strncpy (verifyStart, (char*)incomingPacket, 6);

But the string you are parsing IS exactly six characters...
NODECM

So strncpy copies those 6 characters into verifyStart and stops. It does not append a trailing NUL string terminator.

And because verifyStart is on the stack, and not the heap, that memory is not initialised to zero. So you get a buffer overrun when printing.

verifyStart[6] = 0;

pcbbc:
strncpy takes a maximum length. In this case you have said 6...

strncpy (verifyStart, (char*)incomingPacket, 6);

But the string you are parsing IS exactly six characters...
NODECM

So strncpy copies those 6 characters into verifyStart and stops. It does not append a trailing NUL string terminator.

And because verifyStart is on the stack, and not the heap, that memory is not initialised to zero. So you get a buffer overrun when printing.

verifyStart[6] = 0;

So this is another problem because i dont have a terminating 0 at the end of the string?

That was the problem indeed. im going to have to remember that. thanks!!

is there a problem that all this "NODECM", "NODECM" business is trying to solve ?
i would just convert the data to ascii hex and send udp msg as human readable text
if you are worried about data corruption [i don't ever recall getting a corrupt udp message]
just add a crc to the message,if pkt loss is an issue add a sequence number to each message
and get the receiver to ack with sequence number, if seq wrong or no ack returned in timeout
period then resend.

racpi:
is there a problem that all this "NODECM", "NODECM" business is trying to solve ?
i would just convert the data to ascii hex and send udp msg as human readable text
if you are worried about data corruption [i don't ever recall getting a corrupt udp message]
just add a crc to the message,if pkt loss is an issue add a sequence number to each message
and get the receiver to ack with sequence number, if seq wrong or no ack returned in timeout
period then resend.

thats kind of what NODECM CMNODE is for. i just assume if the first and last bytes are intact then the rest of the data is okay. it dont have to be 100.00000% reliable.

and i can use it as a command identifier

notsolowki:
thats kind of what NODECM CMNODE is for. i just assume if the first and last bytes are intact then the rest of the data is okay. it dont have to be 100.00000% reliable.

and i use it as a command identifier

i use this to receive a single int from the room thermostat to set heater damper position
its worked for years with out error, it replies with a json sentence to the sender

int packetSize = Udp.parsePacket();
  if (packetSize) {
    remote = Udp.remoteIP();
    remotePort = Udp.remotePort();
    // read the packet into packetBufffer
    Udp.read(packetBuffer, 100);
    npos = atoi(packetBuffer);
    npos = constrain(npos, 0, 2200);
    if (abs(npos - posn) > 10)
      newmve = 1;
    char json[100] ;
    sprintf(json, "{\"posn\":%d,\"fire\":%3.1f}", posn, thermocouple->readCelsius());
    //    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(remote, Udp.remotePort());
    Udp.write(json);
    Udp.endPacket();
  }

the thermostat sends like this

void setout(int op) {
  char buff[10];
  sprintf(buff, "%d\n",op );
  Udp.beginPacket("heater", 8888);
  Udp.write(buff);
  Udp.endPacket();
}

What am i doing wrong here thats causing the esp to crash again, im pretty sure the delimiter strings are null terminated. it has something to do with delimiter. it works fine when i tested,

#include <ESP8266WiFi.h>

void function(char chars1[],char chars2[] ) {
Serial.println(chars1); //should print "Name"
Serial.println(chars2); //should print "id"
}

void setup() {
  Serial.begin(115200);
}
void loop() {
function("Name","id");
}

but when i try it in my program the esp crashes right away,

#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress Client1(192, 168, 4, 100);

const char* ssid = "LCS";
const char* password = "a1b2c3d4";

WiFiUDP Udp;
unsigned int localUdpPort = 4210;  // local port to listen on
unsigned int clientPort = 4220;
char incomingPacket[800];  // buffer for incoming packets

char _pwmVal = 600;

#define OLED_RESET LED_BUILTIN  //4
Adafruit_SSD1306 display(OLED_RESET);

void sendCommand(IPAddress to, char* data, char delimiter[], char delimiter2[]  ) {
  delimiter[6] = 0;
  delimiter2[6] = 0;
  char* packetData = data;
  Udp.beginPacket(to, clientPort);
  Udp.write((const uint8_t*)delimiter, sizeof(delimiter)  - 1);
  Udp.write((const uint8_t*)packetData, sizeof(packetData));
  Udp.write((const uint8_t*)delimiter2, sizeof(delimiter2)  - 1);
  Udp.endPacket();
}

void setup() {
  Serial.begin(115200);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  WiFi.setOutputPower(20.5);
  WiFi.persistent(0);
  Serial.begin(115200);
  Serial.println();
  WiFi.mode(WIFI_AP);
  while (!WiFi.softAP(ssid, password, 9, false, 15)) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
}

void loop() {
  sendCommand(Client1, (char*)_pwmVal, "NODECM0", "CMNODE0");
  delay(2000);
}

where am i going wrong again??

This causes crash on the receiving client,

void sendCommand(IPAddress to, char* data, char delimiter[],char delimiter2[]  ) {
  Serial.println(delimiter);
   Serial.println(delimiter2);
//char delimiter[] = "NODECM";
//char delimiter2[] = "CMNODE";
  char* packetData = data;
  Udp.beginPacket(to, clientPort);
  Udp.write((const uint8_t*)delimiter, sizeof(delimiter)  - 1);
  Udp.write((const uint8_t*)packetData, sizeof(packetData));
  Udp.write((const uint8_t*)delimiter2, sizeof(delimiter2)  - 1);
  Udp.endPacket();
}

void setup() {
  Serial.begin(115200);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  WiFi.setOutputPower(20.5);
  WiFi.persistent(0);
  Serial.begin(115200);
  Serial.println();
  WiFi.mode(WIFI_AP);
  while (!WiFi.softAP(ssid, password, 9, false, 15)) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
}

void loop() {
  sendCommand(Client1, (char*)_pwmVal,"NODECM","CMNODE");
  delay(2000);
}

This does not,

void sendCommand(IPAddress to, char* data  ) {
char delimiter[] = "NODECM";
char delimiter2[] = "CMNODE";
  char* packetData = data;
  Udp.beginPacket(to, clientPort);
  Udp.write((const uint8_t*)delimiter, sizeof(delimiter)  - 1);
  Udp.write((const uint8_t*)packetData, sizeof(packetData));
  Udp.write((const uint8_t*)delimiter2, sizeof(delimiter2)  - 1);
  Udp.endPacket();
}

void setup() {
  Serial.begin(115200);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  WiFi.setOutputPower(20.5);
  WiFi.persistent(0);
  Serial.begin(115200);
  Serial.println();
  WiFi.mode(WIFI_AP);
  while (!WiFi.softAP(ssid, password, 9, false, 15)) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
}

void loop() {
  sendCommand(Client1, (char*)_pwmVal);
  delay(2000);
}

why is this?

im still stuck here if anyone can help me

you seem to like to make things harder than you need try this

sizeof(packetData) ? i think will give you the size of a char pointer not what you are looking for

int _pwmVal = 600;


void sendCommand(IPAddress to, char * data, char delimiter[], char delimiter2[]  ) {
  Serial.println(sizeof(data));
  Udp.beginPacket(to, clientPort);
  Udp.write(delimiter);
  Udp.write(data, 4);
  Udp.write(delimiter2);
  Udp.endPacket();
}


void loop() {
  //uint8_t * ptr=(uint8_t *)  ;                                          
  sendCommand(Client1, (char *)&_pwmVal, "NODECM", "CMNODE");
  delay(2000);
}

wireshark result

Thank you that solved my issue. So if i understand correctly when i used sizeof(packetData) i was overloading the packet buffer? i still feel like im missing some points here

well apart from the incorrect use of sizeof
char* packetData = data; when its meant to be int
sendCommand(Client1, (char*)_pwmVal, when its the address of _pwmval that needed to be used recast as char type
and null terminating arrays that don't need to be then recasting them to uint8_t for i don't know why
i still see no real benefit for the entire process but its not my project

racpi:
wireshark result

@racpi or anyone why dont i receive _pwmVal's value on the receiving side. it just shows up as random gibberish

I have literally tried every possible way i can think of to decode the value on the receiving side without any luck. i cant think of a way to research my problem i have been trying for the past 20 hours. no matter how i recast/convert the bytes i cannot get the data back into an int. i tried atoi atol strol none of them seem to be doing anything i need. something just dont seem right about using strncpy to copy bytes from the packetbuffer into a string and converting it to an integer. i have no clue what to do. if someone would please help me out i would be much thankful.

A lot has been presented in this thread. Please post the current code used to send and receive.