#include "FS.h"
#include <LittleFS.h>
#include <ArduinoJson.h>
/* You only need to format LittleFS the first time you run a
test or else use the LITTLEFS plugin to create a partition
https://github.com/lorol/arduino-esp32littlefs-plugin
If you test two partitions, you need to use a custom
partition.csv file, see in the sketch folder */
#define FORMAT_LITTLEFS_IF_FAILED true
#define radio_stage "/Radia.json"
String kStationURLs[30];
int max_stations;
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.path(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print("\tSIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
void readFile(fs::FS &fs, const char * path) {
Serial.printf("\nReading file: %s\r\n", path);
File file = fs.open(path);
if (!file || file.isDirectory()) {
Serial.println("- failed to open file for reading");
return;
}
Serial.println("- read from file:");
while (file.available()) {
Serial.write(file.read());
}
file.close();
}
void readFileKey(fs::FS &fs, const char * path) {
Serial.printf("\nReading file: %s\r\n", path);
File file = fs.open(path);
if (!file || file.isDirectory()) {
Serial.println("- failed to open file for reading");
return;
}
/*
Serial.println("- read from file:");
while (file.available()) {
Serial.write(file.read());
}
*/
Serial.println("Čtení klíčových key hodnot");
size_t size = file.size();
if (size > 1024) {
Serial.println("Config file size is too large");
}
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, file);
if (error)
Serial.println(F("Failed to read file, using default configuration"));
JsonObject documentRoot = doc.as<JsonObject>();
//zobrazni radio stanic
for (JsonPair keyValue : documentRoot) {
Serial.println(keyValue.key().c_str()); //vycet klicu
Serial.println(keyValue.value().as<const char*>()); //vycet hodnot
}
//počet radio stanic
Serial.println(documentRoot.size());
//vyhledaní stanice dle indexu
int index = 2;
JsonObject::iterator it = doc.as<JsonObject>().begin();
it += index;
Serial.println(it->value().as<const char*>());
file.close();
}
void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("\nWriting 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("\nAppending 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();
}
void serializeData() {
// allocate the memory for the document
StaticJsonDocument<256> data;
//sprintf(body,"[{\"id\":\"%s\",\"sb\":\"%s\",\"sc\":%d,\"oa\":%d,\"ob\":%d,\"oc\":%u,\"od\":%d,\"oe\":%d,\"of\":%d,\"og\":%d,\"oh\":%u,\"oi\":%u,\"oj\":%u,\"ok\":%u,\"ol\":%d,\"om\":%d,\"on\":%d,\"oo\":%d,\"op\":%d,\"oq\":%d,\"or\":%d,\"os\":%d,\"ot\":%d,\"ou\":%d,\"pa\":\"%s\",\"ax\":%d,\"ay\":%d,\"az\":%d,\"am\":%d,\"at\":%d,\"ga\":%0.6f,\"gb\":%0.6f,\"gc\":%d,\"gd\":%d,\"ge\":%d,\"gf\":%d,\"ha\":%d,\"hb\":%d,\"hc\":%d,\"hd\":%d,\"e1\":\"%s\",\"e2\":%d,\"e3\":%d,\"result\":\"%s\"}]",
// imei,sim_iccid_arr,int(gsmRssi),int(fuelStatus),int(engineload*100),int(coolantTemp),int(sf1*100),int(lf1*100),int(sf2*100),int(lf2*100),gasPressure,manifold_press,rpm,vehicle_speed,int(timing_adv),int(intakeTemp),maf,int(throttle*100),int(fuelT),int(hybridBattRem),int(odometer),int(hybrid_Bvolt),int(fuel_percentage*100),int(veh_bat*100),vin_arr,int(angleX*100),int(angleY*100),int(angleZ*100),int(mag*100),int(tempT*100),GPSlatitude,GPSlongitude,int(GPSspeed*10),int(GPSaltitude*10),int(accuracy*10),int(usat),int(carTrip),int(device_plug),int(device_volt*100),int(vMCheck1),cantype,int(ver),int(otaError),rxResult);
// add some values
data["id"] = "123456789";
data["sb"] = "test string";
data["sc"] = "123456789";
data["oa"] = 111111111;
data["ob"] = 222222222;
// .......
File file = LittleFS.open("/myData.json", FILE_WRITE);
if (!file) {
Serial.println("- failed to open file for writing");
return;
}
// serialize the array and send the result to Serial
//serializeJsonPretty(data, Serial);
// serialize the array and save to file
serializeJson(data, file);
file.close();
}
void loadStage(const char *filename) {
log_d("Načtení rádii");
Serial.println(filename);
//vymazaní seznamu radií
//lv_obj_clean(ui_List1);
log_d("Vymazán seznam rádií");
// Otevření souboru pro čtení
File file = LittleFS.open(filename);
if (!file) {
Serial.println("Soubor nenalezen bude vytvořen novej");
StaticJsonDocument<256> data;
data["Capitol"] = "http://vis.media-ice.musicradio.com/CapitalMP3";
data["Urban"] = "http://hydra.cdnstream.com/1536_128";
data["Krokodyl"] = "http://icecast4.play.cz/krokodyl128.mp3";
data["RockZone"] = "http://icecast2.play.cz/rockzone128.mp3";
data["Evropa 2"] = "http://ice.actve.net/fm-evropa2-128";
data["HipHop Vibes"] = "http://ice4.abradio.cz/hiphopvibes128.mp3";
data["Známka Punku"] = "http://ice.abradio.cz/znamkapunku128.mp3";
data["Radio Spin"] = "http://icecast4.play.cz/spin128.mp3";
data["Radio Beat"] = "http://icecast5.play.cz/radiobeat128.mp3";
File file = LittleFS.open(filename, FILE_WRITE);
if (!file) {
Serial.println(F("Failed to create file"));
return;
}
// Serialize JSON to file
if (serializeJson(data, file) == 0) {
Serial.println(F("Failed to write to file"));
}
// Close the file
file.close();
}
//alokace paměťi
StaticJsonDocument<256> doc;
// Deserializace JSON documentu
DeserializationError error = deserializeJson(doc, file);
if (error)
Serial.println(F("Selhalo čtení souboru, nelze spustit rádio bez stanic"));
JsonObject documentRoot = doc.as<JsonObject>();
//celkový pocet stanic
max_stations = documentRoot.size();
Serial.println("Pocet stanic" + documentRoot.size());
/*for (int i = 0; i < max_stations; ++i) {
lv_obj_t *btn = lv_list_add_btn(ui_List1, NULL, (kStationURLs[i]).c_str());
lv_obj_add_event_cb(btn, list_event_handler, LV_EVENT_CLICKED, NULL);
}
*/
int i=0;
for (JsonPair keyValue : documentRoot) {
Serial.println(keyValue.key().c_str()); //vycet klicu
Serial.println(keyValue.value().as<const char*>()); //vycet hodnot
kStationURLs[i]=(keyValue.value().as<const char*>());
i++;
}
file.close();
}
void setup() {
Serial.begin(115200);
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
Serial.println("LittleFS Mount Failed");
return;
}
// listDir(LittleFS, "/", 0);
// writeFile(LittleFS, "/hello2.txt", "Hello2");
// listDir(LittleFS, "/", 0);
// writeFile(LittleFS, "/hello.txt", "Hello ");
// appendFile(LittleFS, "/hello.txt", "World!\r\n");
// readFile(LittleFS, "/hello.txt");
// listDir(LittleFS, "/", 0);
serializeData();
//cteni celého dokumentu
//readFile(LittleFS, "/myData.json");
//čtení pouze klíčových hodnot
//readFileKey(LittleFS, "/myData.json");
loadStage(radio_stage);
}
void loop() {
}