Skip to content

ESP8266 Object.printTo(SPIFFS_File) -> caused empty File #514

Closed
@Pfannex

Description

@Pfannex

Hi,

I want to write back an jsonObject to my ESP8266 FFS in my own Class.
But after writing the file by using Object.printTo(SPIFFS_File) the File is Empty.

On wich Point I make my mistake?

FFS.h

#pragma once
//FileSystem
  #include <FS.h>                //this needs to be first, or it all crashes and burns...
  #include <ArduinoJson.h>
  #include <Arduino.h>
 
//===============================================================================
//  jsonFile
//===============================================================================
class FFSjsonFile{
public:  
  FFSjsonFile(String filePath, int type);
  String filePath;
  size_t size;                               //filled in readJsonString()
  int type;                                  //0=Object; 1=Array
  String root;

  void loadFile();
  String readItem(String itemName);
  
private: 
  String readJsonString();
};
 

//===============================================================================
//  FFS 
//===============================================================================
class FFS{
public:
  FFS();
  FFSjsonFile myFile;
  void mount();  
private:
};

FFScpp

#include "FFS.h"

//===============================================================================
//  FFS 
//===============================================================================
FFS::FFS():
    //jsonFiles
    myFile("/MyFile.json", 1){
}

//  FFS public
//===============================================================================

//===> mount FFS <-------------------------------------------------
void FFS::mount(){

  Serial.print("Mounting FS...");
  if (!SPIFFS.begin()) {
    Serial.println("Failed to mount file system");
  }else{
    Serial.println("OK");
    //Serial.print("formating FS...");
    //SPIFFS.format();
    //Serial.println("OK");
    
    myFile.loadFile();
    
  }


//--- TEST----------------------------------------  
  Serial.println(myFile.readItem("Field_01"));
  Serial.println(myFile.readItem("Field_02"));


  DynamicJsonBuffer JsonBuffer;
  JsonObject& rootObject = JsonBuffer.parseObject(myFile.root);  
  rootObject["Field_01"] = "Hello";
  rootObject["Field_02"] = "World!";

  File jsonFile = SPIFFS.open("/MyFile.json", "w");
  if (!jsonFile) {
    Serial.println("Failed to open config file for writing");
    //return false;
  }

  rootObject.printTo(Serial);        // this works!
  Serial.println("");

  // writes empty file?
  rootObject.printTo(jsonFile);  
  
  jsonFile.close();
}

//===============================================================================
//  FFSjsonFile
//===============================================================================

FFSjsonFile::FFSjsonFile(String filePath, int type):
    filePath       (filePath),
    type           (type){
}

//  FFSjsonFile public
//===============================================================================

//===> loadFile <-----------------------------------------------------
void FFSjsonFile::loadFile(){
  root = readJsonString();
}

//===> readItem from jsonString <-------------------------------------
String FFSjsonFile::readItem(String itemName){
  DynamicJsonBuffer JsonBuffer;
  JsonObject& rootObject = JsonBuffer.parseObject(root);  
  return rootObject[itemName].asString();
}


//===> Read json String from File <------------------------------------
String FFSjsonFile::readJsonString(){  
    
  File jsonFile;
  String jsonData = "NIL";
 
  //if (SPIFFS.begin()) {
    Serial.print("reading " + filePath + "...");
    if (SPIFFS.exists(filePath)) {
      jsonFile = SPIFFS.open(filePath, "r");
      if (jsonFile) {
        Serial.println("OK");
        size = jsonFile.size();
        std::unique_ptr<char[]> buf(new char[size]);

        jsonFile.readBytes(buf.get(), size);
        if (type == 0){
        //Array 
          DynamicJsonBuffer jsonBufferArray;
          JsonArray& jsonArray = jsonBufferArray.parseArray(buf.get());             
          if (jsonArray.success()) {
            char buffer[size];
            jsonArray.printTo(buffer, sizeof(buffer));
            jsonData = String(buffer); 
          }
        }else{
        //Object
          DynamicJsonBuffer jsonBuffer;
          JsonObject& json = jsonBuffer.parseObject(buf.get());
          if (json.success()) {         
            char buffer[size];
            json.printTo(buffer, sizeof(buffer));
            jsonData = String(buffer);
          }
        }
      }
    }
  //}
  jsonFile.close();
  return jsonData;  
}

main.ino

#include "FFS.h"
FFS myffs;

void setup() {  
  Serial.begin(115200); 
  Serial.println("");
  myffs.mount();  
}  

void loop() {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions