Printing HEX from byte array in ESP32

Hi, I am using ESP32 Dev Module to receive image data of size 75kb from a raspberry pi through a tcp connection. I store the data received in a byte array and print the HEX data. The issue the ESP resets whenever I try to print the data. I have tried to print the data while receiving the data and the it print the whole data received. I need help understanding the problem.

#include <WiFi.h>

WiFiClient client;

int ledPin = 5;
String recvString = "";

const int port = 8888;  //Port number
const IPAddress server();

//Server connect to WiFi Network
const char *ssid = "MW40VD_19E7";  //Enter your wifi SSID
const char *password = "";  //Enter your wifi Password

void writeString(String stringData) {
        for (int i = 0; i < stringData.length(); i++) {
                client.write(stringData[i]);
        }
}


uint8_t recvWithStartEndMarker() {
  boolean endData = false;
  const byte numChars = 32;
  char receivedChars[numChars];
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;
      while(client.available()>0){
        rc = client.read();
        if (rc == startMarker) {
          recvInProgress = true;
         }
        else if (recvInProgress == true) {
          if (rc != endMarker) {
              receivedChars[ndx] = rc;
              ndx++;
          
              if (ndx >= numChars) {
              ndx = numChars - 1;
              }
          }
          else {
            ndx = 0;
            recvInProgress = false;
            uint8_t dataRecv = atoi(receivedChars);
            return dataRecv;
          }
          
        }
      }
}

void imgbyteArray(){
  uint8_t receivedByte[96000];
  uint32_t starttime = millis();
  int i = 0;
  long calc_data = 0;
  int a = 0;
  int b = 0;
  int c = 0;
//  while(i < 4) {
//    if (client.available()) {
//      uint8_t imgData = recvWithStartEndMarker();
//      calc_data = (imgData * pow(256, i)) + calc_data;
//      i++;
//    }
//  }
//  Serial.println(String(calc_data));
  while(i < 96000 && (millis() - starttime < 22000)) {
    if (client.available()) {
      uint8_t imgData = recvWithStartEndMarker();
      receivedByte[i++] = imgData;
    }
  }
  Serial.println(i);
  for (int j = 0; j < i; j++) {
    printHex(receivedByte[j], 2);
  }
  
}

void printHex(int num, int precision) {
    char tmp[16];
    char format[128];
 
    sprintf(format, "%%.%dX", precision);
 
    sprintf(tmp, format, num);
    Serial.print(tmp);
}


void setup() 
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.println();

  WiFi.begin(ssid, password); //Connect to wifi
 
  // Wait for connection  
  Serial.println("Connecting to Wifi");
  while (WiFi.status() != WL_CONNECTED) {   
    delay(500);
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); 

}

void loop() {
  char imgchr;
    if (client.connect(server, port)) {
    Serial.println("Client Connected");
    digitalWrite(ledPin, HIGH);
  }
  else {
    Serial.println("No Connection");
    delay(500);
    return;
  }
   while(client.connected()){ 
       while(client.available()>0){
              imgbyteArray();
       }
      while(Serial.available()>0)
      {
         client.write(Serial.read());
      }
   }
    client.stop();
    Serial.println("Client disconnected");   
    digitalWrite(ledPin, LOW); 
  
}

the Error

ets Jun  8 2016 00:22:57

rst:0x8 (TG1WDT_SYS_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8

Connecting to Wifi
.

uint8_t receivedByte[96000]; You might want to review if you need an array of 96,000 bytes. If you do need an array of 96K bytes of size you should initialize the array and empty the array memory area at initilazition.

uint8_t receivedByte[96000] = {};

The ESP32 does not automatically clear an initialized arrays memory address space.

Still, I think you should re-evaluate such a large array size.

Do you really need a long integer, the ESP32 is a 32bit device?

The ESp32 is a multicore device by using delay(500);, you are bringing the ESP32 to a stop. Why not use the ESP32's OS, freeRTOS, vTaskDelay which is a non blocking delay? Also, freeRTOS has buffers that would handle such a large data thingy as an image.

What kind of memory do you have left after compiling your code?

  Serial.println();??????????

You, also, declare a number of variables that you do not use.

uint8_t recvWithStartEndMarker() Why not void when you are not returning data.

Really you need to re-evaluate your code.

Idahowalker:
The ESp32 is a multicore device by using delay(500);, you are bringing the ESP32 to a stop. Why not use the ESP32's OS, freeRTOS, vTaskDelay which is a non blocking delay?

Because...

void delay(uint32_t ms)
{
    vTaskDelay(ms / portTICK_PERIOD_MS);
}

Thanks for the reply. I have the changed the declaration of the array and its brings up a Guru Meditation Error. So I have tried in a smaller array size like 512 and it worked but I don't receive the whole data :frowning: . So what is the best way on receiving such long byte of data. And thanks for the advice about the delay(500).

lupine37:
Thanks for the reply. I have the changed the declaration of the array and its brings up a Guru Meditation Error. So I have tried in a smaller array size like 512 and it worked but I don't receive the whole data :frowning: . So what is the best way on receiving such long byte of data. And thanks for the advice about the delay(500).

When I load a program to an ESP32 with the Arduino Ide I get a message, part of the message reads {quote]Sketch uses 249956 bytes (11%) of program storage space. Maximum is 2097152 bytes.
Global variables use 14172 bytes (4%) of dynamic memory, leaving 313508 bytes for local variables. Maximum is 327680 bytes.[/quote].

When you upload your sketch to the ESP32 what does your report indicate.

Why?

Well, for me what to do to solve your issue some information is needed and one of those pieces of information is to know what's left.

A Guru Meditation error is a 'good' error to get, it provides a whole lot of information that can be used to troubleshoot a ESP32 issue. In the future, when you get a Guru meditation error, posting that error, if you want help, would be very helpful to those whom you ask help of.


There are memory regions that you are not using that can be used to hold data. The RTC memory has 3 regions 2 regions for RTC memory, Each region is 8K of usable RAM, and you can use that memory.

If your ESP32 has PSRAM, typically 4Mb, you can store the entire received item into PSRAM.

I would, also, recommend you read up on 'ring buffer' , to see if you want to use one for this process; I would.
https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/freertos_additions.html
https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/uart.html

Hi, for more detail about my code. I am receiving fingerprint data image from the raspberry pi and sending it to the fingerprint sensor through.
the Guru error I was getting was:

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x4000bea8  PS      : 0x00060430  A0      : 0x800815e8  A1      : 0x3ffb55b0  
A2      : 0x0000069c  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x3ffb56c8  
A6      : 0x00000019  A7      : 0x0167e930  A8      : 0x800886a4  A9      : 0x3ffb5570  
A10     : 0x00000001  A11     : 0x00000000  A12     : 0x3ffb41d4  A13     : 0x00000000  
A14     : 0x00000000  A15     : 0x3ffbe150  SAR     : 0x00000019  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000004  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  

Backtrace: 0x4000bea8:0x3ffb55b0 0x400815e5:0x3ffb55d0 0x4013064f:0x3ffb55f0 0x4013ad2b:0x3ffb5630 0x4008f5f5:0x3ffb56b0 0x40088b7d:0x3ffb56f0

How does one fix this error.

The compiling details ate the same as yours. Thanks for the suggestion, learnt new things from that but have tried all and non has changed except from the one for ring buffer. I am stuck with receiving data from the ring buffer.