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() {
}`