Hi all. I have a code that saves settings for wifi in json and applies them. But I'm having problems with DHCP. I already have a function to implement it, but the whole problem is handling the DHCP value with JSON.
struct config_wifi_mqtt_struct {
char ssid[30];
char pass[30];
char DHCP[5];
char ip[20];
char gt[20];
char mac[20];
char dns[20];
char ip_mqtt[20];
char port_mqtt[10];
char lg_mqtt[30];
char ps_mqtt[30];
};
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(str.ssid, doc["ssid"]);
strcpy(str.pass, doc["pass"]);
strcpy(str.DHCP, doc["DHCP"]);
strcpy(str.gt, doc["gt"]);
strcpy(str.mac, doc["mac"]);
strcpy(str.dns, doc["dns"]);
strcpy(str.ip, doc["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;
}
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;
}
I think, most likely, it does not work out for me due to incorrect processing of the value for DHCP.
Thanks in advance for your reply.
If you want to see the full code:
#include <GyverPortal.h>
#include <FS.h>
#include <SPIFFS.h>
#include <ArduinoJson.h>
#define FORMAT_SPIFFS_IF_FAILED true
struct config_wifi_mqtt_struct {
char ssid[30];
char pass[30];
char DHCP[5];
char ip[20];
char gt[20];
char mac[20];
char dns[20];
char ip_mqtt[20];
char port_mqtt[10];
char lg_mqtt[30];
char ps_mqtt[30];
};
config_wifi_mqtt_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", str.ssid);
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);
bool val;
val = p.getCheck("DHCP");
str.DHCP = val.tostring();
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;
}
strcpy(str.ssid, doc["ssid"]);
strcpy(str.pass, doc["pass"]);
strcpy(str.DHCP, doc["DHCP"]);
strcpy(str.gt, doc["gt"]);
strcpy(str.mac, doc["mac"]);
strcpy(str.dns, doc["dns"]);
strcpy(str.ip, doc["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;
}
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.print(str.DHCP);
Serial.println("");
}
//========================================================================
//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() {
}
gfvalvo:
Because i need to store many settings for wifi and mqtt. Maybe in the future, I will store topics for mqtt