0

I have something in my code output I don't understand. My guess is the issue is due to [me of course] ... the usage of both "struct"+"arduinoJson" to store configuration values.

I have made this simple code that only connect to Wifi;

  • either using hardcoded settings

    loadConfigHarcoded(); // Serial print of values: OK, and "WiFi Success!"

  • either using deserializeJson

    loadConfigFromJson(); // Serial print of values: OK, but "WiFi Connect Failed!"

Is there anything I missed ?

The full code :

#include <ESP8266WiFi.h>
#include <ArduinoJson.h>

const char* ssid = "my_ssid";
const char* password = "my_password";
String confJson = "{\"ssid\": \""+String(ssid)+"\", \"password\": \""+String(password)+"\"}";

struct sConfig {
  const char* ssid;
  const char* password;
  const char* host;
};
sConfig config;

void loadConfigHarcoded() {
  config.ssid = ssid;
  config.password = password;
}
void loadConfigFromJson() {
  DynamicJsonDocument docConfig(1024);
  DeserializationError err = deserializeJson(docConfig, confJson);
  if (!err) {
    config.ssid     = docConfig["ssid"];
    config.password = docConfig["password"];
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  
  loadConfigHarcoded(); // Serial print of values are OK ; and "WiFi Success!"
  //loadConfigFromJson(); // Serial print of values are OK ; but "WiFi Connect Failed!"
    
  Serial.print("SSID=");
    Serial.println(config.ssid);
  Serial.print("PSWD=");
    Serial.println(config.password);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(config.ssid, config.password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Connect Failed!");
  } else {
    Serial.println("WiFi Success!");
  }
}

void loop() {
  
}
  • What do you want actually and expected output? – NAGA RAJ S Jun 28 '20 at 14:05
  • Hi, When I'm using "loadConfigFromJson()", It should connect to Wifi as the harcoded function do. It seems the Serial output is correct on both function. So the ssid/passwd are corrects. but when I'm getting the value from json, it does not works (Serial output the correct value) but wifi connection failed. – Paul Ernond Jun 28 '20 at 14:28
  • 1
    What is the point of loading a big dependency like `ArduinoJon`, dynamically allocate 1000 byte of buffer for a String `confJson` that is less than 50 bytes which is basically a hardcoded string converted from hardcoded `const char *ssid` and `const char *password`, deserialized it back to be pass in `WiFi.begin()`, while it could be simply done with `WiFi.begin(ssid, password)` where both of the parameters are already hardcoded and defined right on top of the sketch? You need to understand that you are dealing with MCU and compiler where the only storage is the flash memory and hardcoded. – hcheung Jun 30 '20 at 01:24
  • 1
    BTW, your problem will go away if you move the codes within the `loadConfigFromJson()` function to the `loop()` and uses it directly. The reason is that `config.ssid` is a pointer, when you dynamically allocate something within a function, the memory is free-up when you leave that function, you might be lucky to print the data and have a false impression that the content is there, but it isn't safe to use it. – hcheung Jun 30 '20 at 01:35

0 Answers0