ESP8266 crashes when sending UDP packet

Hey Guys, I working on a project where I'm sending UDP packets.

But for some reason, the ESP crashes when it needs to sends a second packet, in a different part of the code, it craches at:
sprintf(buffer, "OKE"); UDP.write(buffer);

#include <Arduino.h>
#include "wifiHandler.h"
#include <eepromHandler.h>
WiFiUDP UDP;

byte macRecieved[6];
uint8_t sizeMac = 17;
uint8_t sizeResponse = 2;
char lastTwoChars[2];
const char* SSID  = "networkName";
const char* PSW = "networkPassword";

unsigned int localUdpPort = 4210;  // local port to listen on
char  incomingPacket[255];  // buffer for incoming packets
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

void setup() {
  Serial.begin(115200);
  wifiHandler hotspot;
  hotspot.setupSoftAp();
  eepromHandler eeprom;

  eeprom.saveData(SSID, PSW, PARAMETER_SIZE_IN_BYTES);
  eeprom.dumbData();
  UDP.begin(localUdpPort);
  Serial.print("Local port: ");
  Serial.println(UDP.localPort());
}

void loop() {
  int packetSize = UDP.parsePacket();
  for (uint8_t i = 0; i < packetSize; i++) {
    macRecieved[i] = 0xFF;
  }
  if (packetSize)
  {
    int len = UDP.read(incomingPacket, 255);
    Serial.printf("Length is %d\r\n", len);
    if (len > 0)
    {
      incomingPacket[len] = 0;
    }
    if (len == sizeMac){
      sscanf(incomingPacket, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", 
            &macRecieved[0], &macRecieved[1], &macRecieved[2], &macRecieved[3], &macRecieved[4], &macRecieved[5]);
      
      if(macRecieved[0] < 0xFF && macRecieved[1] < 0xFF && macRecieved[2] < 0xFF && macRecieved[3] < 0xFF && macRecieved[4] < 0xFF && macRecieved[5] < 0xFF){
        Serial.printf("Robot with address: %02hhX%02hhX asking for data\n", macRecieved[4], macRecieved[5]);
        // send back a reply, to the IP address and port we got the packet from
        UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
        char buffer[PARAMETER_SIZE_IN_BYTES * 2 + 3];
        sprintf(buffer, "%s / %s", SSID, PSW);
        UDP.write(buffer);
        UDP.endPacket();
        for (uint8_t i  = 0; i < sizeof(macRecieved) - 1; i ++){
          macRecieved[i] = 0xFF;
        }
      }
    } else if (len == sizeResponse){
      if (incomingPacket[0] == SSID[strlen(SSID) - 1] && incomingPacket[1] == PSW[strlen(PSW) - 1]){
        Serial.printf("ssid last char: %c psw last char: %c \r\n", SSID[strlen(SSID) - 1], PSW[strlen(PSW) - 1]);
        Serial.printf("IP: %s port: %d", UDP.remoteIP().toString().c_str(), UDP.remotePort());
        UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
        char buffer[4];
        sprintf(buffer, "OKE");
        UDP.write(buffer);
        UDP.endPacket();
      }
    }
  }
}

Serial port:

Length is 17
Robot with address: 1EE7 asking for data
Length is 2
ssid last char: e psw last char: d 
IP: 192.168.1.100 port: 4210
--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (9):
epc1=0x40205eca epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000003 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffd60 end: 3fffffc0 offset: 0190
3ffffef0:  3ffee750 00000020 3ffefe14 401008da  
3fffff00:  3ffffe74 3ffefe14 3fffff30 3ffee8ac  
3fffff10:  3ffee750 3ffefe14 3ffee858 40201303  
3fffff20:  3ffee853 3ffee854 000003fb 4020ef04  
3fffff30:  40206518 6401a8c0 00000000 656e202f  
3fffff40:  72657774 6361576b 6f777468 0064726f  
3fffff50:  4020216c 3ffe8887 3ffee8ac 402026a4  
3fffff60:  007a1200 87b34f01 3ffee800 40202854  
3fffff70:  00000000 40206518 6401a8c0 402029c8  
3fffff80:  0000006d 00000000 00000001 40100164  
3fffff90:  3fffdad0 00000000 3ffee924 3ffee938  
3fffffa0:  3fffdad0 00000000 3ffee924 4020315c  
3fffffb0:  feefeffe feefeffe 3ffe85e8 40100c61  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v00046080
~ld

I did some reading, and they say it has to do with memory, but I don't see how because I'm sending, and not receiving data.

what's the value of PARAMETER_SIZE_IN_BYTES ?

why do you think this is where it crashed?

note that instead of

        char buffer[4];
        sprintf(buffer, "OKE");
        UDP.write(buffer);
        UDP.endPacket();

you can do

        char buffer[] = "OKE";
        UDP.write(buffer);
        UDP.endPacket();

use the exception decoder tool to get more info about the crash: GitHub - me-no-dev/EspExceptionDecoder: Exception Stack Trace Decoder for ESP8266 and ESP32

what's the value of PARAMETER_SIZE_IN_BYTES ?

that is: 32

I found something new. The idea is this:

  1. connect to the servers hotspot
  2. Send your MAC address to the hotspot
  3. Receive the network name and password
  4. Send the last chars of both the network name and password
  5. If a match with data from the hotspot, send back "OKE"

But when I use a program called Packet Sender to send the data.
You can see that is works. But after I get the network name and password
and I send the last chars of both, the ESP crashes.

But if I just send the last chars of both, without doing the others steps, it works fine, see pic below:

you don't have much safeguard in your code for array overflow.

this feels very weird fo example

I don't see why packetSize is related to the macRecieved array...

if you want to empty the macRecieved[] array, just do a

memset(macRecieved, 0xFF, sizeof macRecieved); // https://www.cplusplus.com/reference/cstring/memset/
1 Like

Thank you for pointing out this simple problem!!!

memset(macRecieved, 0xFF, sizeof (macRecieved));

:wink:

Glad it helped

A good practice is that every time you are doing something with an array make sure the index is in range a

1 Like