Esp 32 Core 1 panic'ed

Hello. I wrote this code to set up a wifi network on esp32. Sorry for the code, I'm just getting started.

My problem is, I'm getting a kernel error, most likely due to a buffer overflow. Please help me with this.

`#include <GyverPortal.h>
#include <FS.h>
#include <SPIFFS.h>
#include <ArduinoJson.h>

#define FORMAT_SPIFFS_IF_FAILED true

struct config_wifi_struct {
  char* ssid;
  char* pass;
  bool DHCP;
  char* ip;
  char* gt;
  char* mac;
  char* dns;
};

config_wifi_struct str;

//===================================================================
//spiffs functions
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
    Serial.printf("Listing directory: %s\r\n", dirname);

    File root = fs.open(dirname);
    if(!root){
        Serial.println("- failed to open directory");
        return;
    }
    if(!root.isDirectory()){
        Serial.println(" - not a directory");
        return;
    }

    File file = root.openNextFile();
    while(file){
        if(file.isDirectory()){
            Serial.print("  DIR : ");
            Serial.println(file.name());
            if(levels){
                listDir(fs, file.name(), levels -1);
            }
        } else {
            Serial.print("  FILE: ");
            Serial.print(file.name());
            Serial.print("\tSIZE: ");
            Serial.println(file.size());
        }
        file = root.openNextFile();
    }
}

String readFile(fs::FS &fs, const char * path){
    Serial.printf("Reading file: %s\r\n", path);
    String return_err = "error";
    File file = fs.open(path);
    if(!file || file.isDirectory()){
        Serial.println("- failed to open file for reading");
        return return_err;
    }
    
    String msg_string;
    while(file.available()){
        //Serial.write(file.read());
        msg_string += char(file.read());
    }
    file.close();
    return msg_string;
}

void writeFile(fs::FS &fs, const char * path, String message){
    Serial.printf("Writing file: %s\r\n", path);

    File file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("- failed to open file for writing");
        return;
    }
    if(file.print(message)){
        Serial.println("- file written");
    } else {
        Serial.println("- write failed");
    }
    file.close();
}

void appendFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Appending to file: %s\r\n", path);

    File file = fs.open(path, FILE_APPEND);
    if(!file){
        Serial.println("- failed to open file for appending");
        return;
    }
    if(file.print(message)){
        Serial.println("- message appended");
    } else {
        Serial.println("- append failed");
    }
    file.close();
}


//========================================================================
//gyver portal functions
void build_login() {
  BUILD_BEGIN();
  GP.THEME(GP_DARK);
  
  GP.FORM_BEGIN("/login");
  GP.TEXT("lg", "Login");
  GP.BREAK();
  GP.TEXT("ps", "Password", str.pass);
  GP.BREAK();
  GP.LABEL("DHCP: ");
  GP.BREAK();
  GP.CHECK("DHCP", str.DHCP);
  GP.BREAK();
  GP.TEXT("ip", "ip", str.ip);
  GP.BREAK();
  GP.TEXT("gt", "gt", str.gt);
  GP.BREAK();
  GP.TEXT("mac", "mac", str.mac);
  GP.BREAK();
  GP.TEXT("dns", "dns", str.dns);
  GP.BREAK();
  GP.SUBMIT("Submit");
  GP.FORM_END();

  BUILD_END();
}

void action_login(GyverPortal& p) {
  if (p.form("/login")) {
    p.copyStr("lg", str.ssid);
    p.copyStr("ps", str.pass);
    str.DHCP = p.getCheck("DHCP");
    p.copyStr("ip", str.ip);
    p.copyStr("gt", str.gt);
    p.copyStr("mac", str.mac);
    p.copyStr("dns", str.dns);
    WiFi.softAPdisconnect();
    load_config_wifi();
    
  }
}

void loginPortal() {
  Serial.println("Portal start");

  // запускаем точку доступа
  WiFi.mode(WIFI_AP);
  WiFi.softAP("WiFi Config");

  // запускаем портал
  Serial.println("Запуск портала");
  GyverPortal portal;
  portal.attachBuild(build_login);
  Serial.println("Запуск build_login");
  portal.start(WIFI_AP);
  Serial.println("Запуск WIFI_AP");
  portal.attach(action_login);
  Serial.println("Запуск action_login");

  // работа портала
  while (portal.tick());
}

bool read_config_wifi(String msg_json_wifi_config){
  config_wifi_struct str;
  StaticJsonDocument<300> doc;
      
  DeserializationError error = deserializeJson(doc, msg_json_wifi_config);

  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return false;
  }
  str.ssid = strdup(doc["ssid"]);
  str.pass = strdup(doc["pass"]);
  str.DHCP = strdup(doc["DHCP"]) == "1";
  str.gt = strdup(doc["gt"]);
  str.mac = strdup(doc["mac"]);
  str.dns = strdup(doc["dns"]);
  str.ip = strdup(doc["ip"]);
  Serial.println(str.ssid);
  Serial.println(str.pass);
  Serial.println(str.DHCP);
  Serial.println(str.ip);
  Serial.println(str.gt);
  Serial.println(str.mac);
  Serial.println(str.dns);
  
  return true;
}

bool load_config_wifi(){
  StaticJsonDocument<300> doc;
  
  
  doc["ssid"] = str.ssid;
  doc["pass"] = str.pass;
  doc["DHCP"] = str.DHCP;
  doc["ip"] = str.ip;
  doc["gt"] = str.gt;
  doc["mac"] = str.mac;
  doc["dns"] = str.dns;
  
  String msg_return;
  
  serializeJson(doc, msg_return);

  Serial.print(msg_return);
  writeFile(SPIFFS, "/config_wifi.json", msg_return);
  
  return true;
}

//========================================================================
//wifi functions
bool DHCP_switch (){
  if(!str.DHCP){
    IPAddress local_IP(ipaddr_addr(str.ip));
    IPAddress gateway(ipaddr_addr(str.gt));
    IPAddress subnet(ipaddr_addr(str.mac));
    IPAddress primaryDNS(ipaddr_addr(str.dns));

    if (WiFi.config(local_IP, gateway, subnet, primaryDNS) == false) {
      Serial.println("Configuration failed.");
      return false;
    }
  }
  return true;
}

void wifi_connect(){
  Serial.print("Connect to: ");
  Serial.println(str.ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(str.ssid, str.pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected! Local IP: ");

  Serial.println(WiFi.localIP());
  Serial.print("DHCP: ");
  Serial.println(str.DHCP);
}

//========================================================================
//arduino functions

void setup() {
  Serial.begin(115200);
  Serial.println();

  if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
        Serial.println("SPIFFS Mount Failed");
  }
  
  pinMode(27, INPUT_PULLUP);
  delay(500);
  Serial.println(read_config_wifi(readFile(SPIFFS, "/config_wifi.json")));
  delay(500);
  if (!digitalRead(27)){ 
    loginPortal();
  }
  DHCP_switch();
  wifi_connect();
}

void loop() {
}`
bool read_config_wifi(String msg_json_wifi_config){
  StaticJsonDocument<300> doc;
      
  DeserializationError error = deserializeJson(doc, msg_json_wifi_config);

  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return false;
  }
  
  str.ssid = strdup(doc["ssid"]);
  str.pass = strdup(doc["pass"]);
  str.DHCP = strdup(doc["DHCP"]);
  str.gt = strdup(doc["gt"]);
  str.mac = strdup(doc["mac"]);
  str.dns = strdup(doc["dns"]);
  str.ip = strdup(doc["ip"]);
  
  Serial.println(str.ssid);
  Serial.println(str.pass);
  Serial.println(str.DHCP);
  Serial.println(str.ip);
  Serial.println(str.gt);
  Serial.println(str.mac);
  Serial.println(str.dns);
  
  free (str.ssid);
  free (str.pass);
  free (str.DHCP);
  free (str.gt);
  free (str.mac);
  free (str.dns);
  free (str.ip);
  
  return true;
}

I think it's all about this feature. Without it, the code works properly. The remaining functions were implemented using the library.

Errors in the logs when resolving this function:

ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (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:1044
load:0x40078000,len:10124
load:0x40080400,len:5856
entry 0x400806a8

Reading file: /config_wifi.json
111
111
0
255.255.255.255
255.255.255.255
255.255.255.255
255.255.255.255
1
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x401350f3  PS      : 0x00060330  A0      : 0x80135358  A1      : 0x3ffb1ea0  
A2      : 0x00000000  A3      : 0x3ffb1edc  A4      : 0x00000001  A5      : 0x00000001  
A6      : 0x00060320  A7      : 0x00000000  A8      : 0x80085450  A9      : 0x3ffb1ef0  
A10     : 0x3ff000e0  A11     : 0x00000001  A12     : 0x3ffbe958  A13     : 0x0000ff00  
A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x0000001b  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  

ELF file SHA256: 0000000000000000

Backtrace: 0x401350f3:0x3ffb1ea0 0x40135355:0x3ffb1ed0 0x400d1b33:0x3ffb1f00 0x400d368f:0x3ffb1f70 0x400dc6ca:0x3ffb1fb0 0x40089966:0x3ffb1fd0

You should decode the backtrace to find the specific line where it goes wrong, that should make it easier to debug.

Guru Meditation Error: Core  0 panic'ed (StoreProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x4000c2a3  PS      : 0x00060a33  A0      : 0x80089019  A1      : 0x3ffb3f10  
A2      : 0xbfef3fbd  A3      : 0x3ffb3f8d  A4      : 0x00000007  A5      : 0xbfef3fbd  
A6      : 0x00000028  A7      : 0x00000000  A8      : 0x80081b60  A9      : 0x3ffb3f10  
A10     : 0x3ffce628  A11     : 0x3ffce628  A12     : 0x00060620  A13     : 0x3ffcea3c  
A14     : 0x3ffcea0d  A15     : 0x3ffcea3d  SAR     : 0x00000010  EXCCAUSE: 0x0000001d  
EXCVADDR: 0xbfef3fbd  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  

ELF file SHA256: 0000000000000000

Backtrace: 0x4000c2a3:0x3ffb3f10 0x40089016:0x3ffb3f20 0x40089384:0x3ffb3f40 0x400d46df:0x3ffb3f80 0x400d4709:0x3ffb3fb0 0x4014a65d:0x3ffb3fd0 0x4014a6b1:0x3ffb3ff0 0x40089966:0x3ffb4050

PC: 0x4000c2a3
EXCVADDR: 0xbfef3fbd

Decoding stack results
0x40089016: prvCopyDataToQueue at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/queue.c line 1888
0x40089384: xQueueGenericSend at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/queue.c line 743
0x400d46df: postToSysQueue(system_prov_event_t*) at D:\���������\ArduinoData\packages\esp32\hardware\esp32\1.0.6\libraries\WiFi\src\WiFiGeneric.cpp line 57
0x400d4709: _network_event_cb(void*, system_event_t*) at D:\���������\ArduinoData\packages\esp32\hardware\esp32\1.0.6\libraries\WiFi\src\WiFiGeneric.cpp line 89
0x4014a65d: esp_event_post_to_user at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/event_loop.c line 44
0x4014a6b1: esp_event_loop_task at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/event_loop.c line 58
0x40089966: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

This error is thrown after submitting data from the web page

Does it work if you remove the usage of the config_wifi_struct type and print the info directly from the JSON?

//Either check for null
if (str.ssid != null) Serial.println(str.ssid);

//Or remove it
Serial.println(doc["ssid"]);
bool read_config_wifi(String msg_json_wifi_config){
  StaticJsonDocument<300> doc;
      
  DeserializationError error = deserializeJson(doc, msg_json_wifi_config);

  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return false;
  }
  
  str.ssid = strdup(doc["ssid"]);
  str.pass = strdup(doc["pass"]);
  str.DHCP = strdup(doc["DHCP"]);
  str.gt = strdup(doc["gt"]);
  str.mac = strdup(doc["mac"]);
  str.dns = strdup(doc["dns"]);
  str.ip = strdup(doc["ip"]);
  
  if (str.ssid != null) Serial.println(str.ssid);
  if (str.pass != null) Serial.println(str.pass);
  if (str.DHCP != null) Serial.println(str.DHCP);
  if (str.gt != null) Serial.println(str.gt);
  if (str.mac != null) Serial.println(str.mac);
  if (str.dns != null) Serial.println(str.dns);
  if (str.ip != null) Serial.println(str.ip);
  
  free (str.ssid);
  free (str.pass);
  free (str.DHCP);
  free (str.gt);
  free (str.mac);
  free (str.dns);
  free (str.ip);
  
  return true;
}

Sorry, I didn't quite understand what to do. Like this ?

It’s a bool not a pointer I would panic too if I had to free it

Oh sorry, I already found this error and translated the DHCP variable into char

But my code also sends the same error with kernel

I’m sorry, are you saying the code you posted is not the code you’re running?

#include <GyverPortal.h>
#include <FS.h>
#include <SPIFFS.h>
#include <ArduinoJson.h>

#define FORMAT_SPIFFS_IF_FAILED true

struct config_wifi_struct {
  char* ssid;
  char* pass;
  char* DHCP;
  char* ip;
  char* gt;
  char* mac;
  char* dns;
};

config_wifi_struct str;

//===================================================================
//spiffs functions
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
    Serial.printf("Listing directory: %s\r\n", dirname);

    File root = fs.open(dirname);
    if(!root){
        Serial.println("- failed to open directory");
        return;
    }
    if(!root.isDirectory()){
        Serial.println(" - not a directory");
        return;
    }

    File file = root.openNextFile();
    while(file){
        if(file.isDirectory()){
            Serial.print("  DIR : ");
            Serial.println(file.name());
            if(levels){
                listDir(fs, file.name(), levels -1);
            }
        } else {
            Serial.print("  FILE: ");
            Serial.print(file.name());
            Serial.print("\tSIZE: ");
            Serial.println(file.size());
        }
        file = root.openNextFile();
    }
}

String readFile(fs::FS &fs, const char * path){
    Serial.printf("Reading file: %s\r\n", path);
    String return_err = "error";
    File file = fs.open(path);
    if(!file || file.isDirectory()){
        Serial.println("- failed to open file for reading");
        return return_err;
    }
    
    String msg_string;
    while(file.available()){
        //Serial.write(file.read());
        msg_string += char(file.read());
    }
    file.close();
    return msg_string;
}

void writeFile(fs::FS &fs, const char * path, String message){
    Serial.printf("Writing file: %s\r\n", path);

    File file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("- failed to open file for writing");
        return;
    }
    if(file.print(message)){
        Serial.println("- file written");
    } else {
        Serial.println("- write failed");
    }
    file.close();
}

void appendFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Appending to file: %s\r\n", path);

    File file = fs.open(path, FILE_APPEND);
    if(!file){
        Serial.println("- failed to open file for appending");
        return;
    }
    if(file.print(message)){
        Serial.println("- message appended");
    } else {
        Serial.println("- append failed");
    }
    file.close();
}


//========================================================================
//gyver portal functions
void build_login() {
  BUILD_BEGIN();
  GP.THEME(GP_DARK);
  
  GP.FORM_BEGIN("/login");
  GP.TEXT("lg", "Login");
  GP.BREAK();
  GP.TEXT("ps", "Password", str.pass);
  GP.BREAK();
  GP.LABEL("DHCP: ");
  GP.BREAK();
  GP.CHECK("DHCP", str.DHCP);
  GP.BREAK();
  GP.TEXT("ip", "ip", str.ip);
  GP.BREAK();
  GP.TEXT("gt", "gt", str.gt);
  GP.BREAK();
  GP.TEXT("mac", "mac", str.mac);
  GP.BREAK();
  GP.TEXT("dns", "dns", str.dns);
  GP.BREAK();
  GP.SUBMIT("Submit");
  GP.FORM_END();

  BUILD_END();
}

void action_login(GyverPortal& p) {
  if (p.form("/login")) {
    p.copyStr("lg", str.ssid);
    p.copyStr("ps", str.pass);
    p.copyStr("DHCP", str.DHCP);
    p.copyStr("ip", str.ip);
    p.copyStr("gt", str.gt);
    p.copyStr("mac", str.mac);
    p.copyStr("dns", str.dns);
    WiFi.softAPdisconnect();
    load_config_wifi();
    
  }
}

void loginPortal() {
  Serial.println("Portal start");

  // запускаем точку доступа
  WiFi.mode(WIFI_AP);
  WiFi.softAP("WiFi Config");

  // запускаем портал
  Serial.println("Запуск портала");
  GyverPortal portal;
  portal.attachBuild(build_login);
  Serial.println("Запуск build_login");
  portal.start(WIFI_AP);
  Serial.println("Запуск WIFI_AP");
  portal.attach(action_login);
  Serial.println("Запуск action_login");

  // работа портала
  while (portal.tick());
}

bool read_config_wifi(String msg_json_wifi_config){
  StaticJsonDocument<300> doc;
      
  DeserializationError error = deserializeJson(doc, msg_json_wifi_config);

  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return false;
  }
  
  str.ssid = strdup(doc["ssid"]);
  str.pass = strdup(doc["pass"]);
  str.DHCP = strdup(doc["DHCP"]);
  str.gt = strdup(doc["gt"]);
  str.mac = strdup(doc["mac"]);
  str.dns = strdup(doc["dns"]);
  str.ip = strdup(doc["ip"]);
  
  if (str.ssid != null) Serial.println(str.ssid);
  if (str.pass != null) Serial.println(str.pass);
  if (str.DHCP != null) Serial.println(str.DHCP);
  if (str.gt != null) Serial.println(str.gt);
  if (str.mac != null) Serial.println(str.mac);
  if (str.dns != null) Serial.println(str.dns);
  if (str.ip != null) Serial.println(str.ip);
  
  free (str.ssid);
  free (str.pass);
  free (str.DHCP);
  free (str.gt);
  free (str.mac);
  free (str.dns);
  free (str.ip);
  
  return true;
}

bool load_config_wifi(){
  StaticJsonDocument<300> doc;
  
  
  doc["ssid"] = str.ssid;
  doc["pass"] = str.pass;
  doc["DHCP"] = str.DHCP;
  doc["ip"] = str.ip;
  doc["gt"] = str.gt;
  doc["mac"] = str.mac;
  doc["dns"] = str.dns;
  
  String msg_return;
  
  serializeJson(doc, msg_return);

  Serial.print(msg_return);
  writeFile(SPIFFS, "/config_wifi.json", msg_return);
  
  return true;
}

//========================================================================
//wifi functions
bool DHCP_switch (){
  if(str.DHCP == "0"){
    IPAddress local_IP(ipaddr_addr(str.ip));
    IPAddress gateway(ipaddr_addr(str.gt));
    IPAddress subnet(ipaddr_addr(str.mac));
    IPAddress primaryDNS(ipaddr_addr(str.dns));

    if (WiFi.config(local_IP, gateway, subnet, primaryDNS) == false) {
      Serial.println("Configuration failed.");
      return false;
    }
  }
  return true;
}

void wifi_connect(){
  Serial.print("Connect to: ");
  Serial.println(str.ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(str.ssid, str.pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected! Local IP: ");

  Serial.println(WiFi.localIP());
  Serial.print("DHCP: ");
  Serial.println(str.DHCP);
}

//========================================================================
//arduino functions

void setup() {
  Serial.begin(115200);
  Serial.println();

  if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
        Serial.println("SPIFFS Mount Failed");
  }
  
  pinMode(27, INPUT_PULLUP);
  delay(500);
  Serial.println(read_config_wifi(readFile(SPIFFS, "/config_wifi.json")));
  delay(500);
  if (!digitalRead(27)){ 
    loginPortal();
  }
  DHCP_switch();
  wifi_connect();
}

void loop() {
}

Yes. I changed it a little, like you said.
Currently the DHCP pointer is not a bool value

and the error?

I posted the error message in the fourth post
https://forum.arduino.cc/t/esp-32-core-1-paniced/1024655/4?u=skipinkd

str.ssid = strdup(doc["ssid"]);

How is memory for str.ssid allocated here?
Is it a local allocation (and thus lost when the function is left...)?

Can something that seems a C string be freed like this?

free (str.ssid);

How can free know the size it is supposed to free?

If you strdup and then free before you leave the function you lose all data. Then later in your code you try to use that non existing data to connect. Move free to elsewhere or remove them completely (I havent studied your code to see if it can be removed completely safely)

1 Like

I need to pass a value to a struct. The JSON function gives me them in const char*, and I need to store them in a structure with char variables. On the Internet, I found the strdup function for this, but I do not understand at all how to clean it up properly. If you have any suggestions on how to do this, please write

Maybe just make the struct elements char arrays of sufficient size rather than char pointers. Then strncpy() from the const char * returned by the JSON access function to the char array. That way you don't need dynamic allocation.

First of all do this to your struct:

struct config_wifi_struct 
{
  char* ssid = nullptr;
  char* pass = nullptr;
  char* DHCP = nullptr;
  char* ip = nullptr;
  char* gt = nullptr;
  char* mac = nullptr;
  char* dns = nullptr;
};

Then move free before strdup so if you call this function again you wont have a memory leak

bool read_config_wifi(String msg_json_wifi_config){
  StaticJsonDocument<300> doc;
      
  DeserializationError error = deserializeJson(doc, msg_json_wifi_config);

  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return false;
  }
  if (str.ssid) free(str.ssid);
  if (str.pass) free(str.pass);
  if (str.DHCP) free(str.DHCP);
  if (str.gt) free(str.gt);
  if (str.mac) free(str.mac);
  if (str.dns) free(str.dns);
  if (str.ip) free(str.ip);

  str.ssid = strdup(doc["ssid"]);
  str.pass = strdup(doc["pass"]);
  str.DHCP = strdup(doc["DHCP"]);
  str.gt = strdup(doc["gt"]);
  str.mac = strdup(doc["mac"]);
  str.dns = strdup(doc["dns"]);
  str.ip = strdup(doc["ip"]);
  
  if (str.ssid) Serial.println(str.ssid);
  if (str.pass) Serial.println(str.pass);
  if (str.DHCP) Serial.println(str.DHCP);
  if (str.gt) Serial.println(str.gt);
  if (str.mac) Serial.println(str.mac);
  if (str.dns) Serial.println(str.dns);
  if (str.ip) Serial.println(str.ip);

  return true;
}
struct config_wifi_struct {
  char ssid[30];
  char pass[30];
  char DHCP[5];
  char ip[20];
  char gt[20];
  char mac[20];
  char dns[20];
};
bool read_config_wifi(String msg_json_wifi_config){
  StaticJsonDocument<300> doc;
      
  DeserializationError error = deserializeJson(doc, msg_json_wifi_config);

  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return false;
  }

  strcpy(doc["ssid"], str.ssid);
  strcpy(doc["pass"], str.pass);
  strcpy(doc["DHCP"], str.DHCP);
  strcpy(doc["gt"], str.gt);
  strcpy(doc["mac"], str.mac);
  strcpy(doc["dns"], str.dns);
  strcpy(doc["ip"], str.ip);
  
  Serial.println(str.ssid);
  Serial.println(str.pass);
  Serial.println(str.DHCP);
  Serial.println(str.gt);
  Serial.println(str.mac);
  Serial.println(str.dns);
  Serial.println(str.ip);
  
  return true;
}

If I do this I get this error

In file included from C:\Users\user\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Array/ArrayIterator.hpp:8:0,
                 from C:\Users\user\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Array/ArrayRef.hpp:8,
                 from C:\Users\user\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:24,
                 from C:\Users\user\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:9,
                 from C:\Users\user\Documents\Arduino\wifiLogin\wifiLogin.ino:4:
C:\Users\user\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Variant/VariantRef.hpp: In instantiation of 'ArduinoJson6192_F1::VariantRef::operator T() const [with T = char*]':
C:\Users\user\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Object/MemberProxy.hpp:85:30:   required from 'ArduinoJson6192_F1::MemberProxy<TParent, TStringRef>::operator T() const [with T = char*; TObject = ArduinoJson6192_F1::JsonDocument&; TStringRef = const char*]'
C:\Users\user\Documents\Arduino\wifiLogin\wifiLogin.ino:180:31:   required from here
C:\Users\user\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Variant/VariantRef.hpp:150:17: error: invalid conversion from 'ArduinoJson6192_F1::enable_if<true, const char*>::type {aka const char*}' to 'char*' [-fpermissive]
     return as<T>();

Sorry, I'm a little confused. What would be better to implement in my case: strdup() or strncpy(). I just need to write from const char* to char*. But it is also important for me that this method would work out in 100% of the case and use few resources. Thank you so much for your replies!

Sorry to post here, but the forum gave me a limit on the number of replies.

As for JSON, I decided to use it because I need to store a lot of parameters in esp32. In addition to the settings for wifi, I need to store the settings for connecting to the mqtt server and a few topics

About your code in post 18. I'm sorry, but now I don't have the opportunity to check it (my wire was chewed by my cat ..). As I wrote earlier, perhaps with the strdup () function I only complicate my life and maybe there is another way to implement the solution to my problem