I'm trying to create a command interpreter. Allowed commands are stored in a JSON document inside SPIFFS. If I send an allowed command, it doesn't print the field associated with that value (end of receiveMsg function), can someone help me to figure out what the problem is? Here's the full code.

EDIT: added minimal code

EDIT 2: solved, but not entirely. I fixed readFile function, but I cannot print out the first field, it appears to be empty. Json is { "ssid"="aaa", "password":"bbb"}, password is printed out, but not ssid. Changing position, password is empty, ssid is printed out. I can resolve by adding a first empty field, but why it happens?

#include "FS.h"

DynamicJsonDocument* functions;

DynamicJsonDocument* readFile(String filename) {
  File file = SPIFFS.open(filename, "r");
  if (!file) {
    Serial.println("Failed to open the file");
  size_t size = file.size();
  if (size > 1024) {
    Serial.println("File size is too large");
  std::unique_ptr<char[]> buf(new char[size]);
  file.readBytes(buf.get(), size);
  DynamicJsonDocument* jsonBuffer = new DynamicJsonDocument(200);
  DeserializationError error = deserializeJson(*jsonBuffer, buf.get());
  if (error) {
    Serial.println("Failed to parse file");
  return jsonBuffer;

int connection() {
  DynamicJsonDocument *settings = readFile("/wifiSettings.json");
  const char* ssid = (*settings)["ssid"];
  Serial.println("Printing ssid");
  const char* psw = (*settings)["password"];
  Serial.println("Printing password");
  WiFi.begin(ssid, psw); //Connect to wifi
  Serial.println("Connecting to Wifi");
  while (WiFi.status() != WL_CONNECTED) {
  return WiFi.status();

void setup() {
  if (!SPIFFS.begin()) {
    Serial.println("Failed to mount file system");
  if (connection() != WL_CONNECTED)
    Serial.println("Not connected");
  • Could it be that you're losing all your data as you pass objects around copying them like that? A better strategy would be to have a single document as a global object, and load the data into that once at start up (after all, it's not going to change, is it?) – Majenko Aug 30 '19 at 15:51
  • add the output from Serial Monitor to the question? do you sent the command from Serial Monitor? contains the command a space? Is the command from Serial Monitor terminated with new line characters? – Juraj Aug 30 '19 at 15:54
  • I tried modifying readFile function by returning a DynamicJsonDocument pointer, but the problem still remains. The output from serial monitor is empty, even If I put a fixed key like "test" (where "test":"test") – Maray97 Aug 30 '19 at 16:13
  • so this is not a minimal sketch to reproduce the problem? – Juraj Aug 30 '19 at 16:16
  • Not properly, I changed it to minimal – Maray97 Aug 30 '19 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.