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<ESP8266WiFi.h>
#include<ArduinoJson.h>
#include<stdio.h>
#include<string.h>
#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");
Serial.println(ssid);
const char* psw = (*settings)["password"];
Serial.println("Printing password");
Serial.println(psw);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, psw); //Connect to wifi
Serial.println("Connecting to Wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
return WiFi.status();
}
void setup() {
Serial.begin(115200);
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
if (connection() != WL_CONNECTED)
Serial.println("Not connected");
else
Serial.println("Connected");
}