Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store more string variables but receive back only the last #17

Open
Sentinel8000 opened this issue Jun 25, 2022 · 2 comments
Open

Store more string variables but receive back only the last #17

Sentinel8000 opened this issue Jun 25, 2022 · 2 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@Sentinel8000
Copy link

This lib will be a great choose to my wifi radio project to store/change url-s, but i have trouble to store more String. I have altered example so:

#include <Effortless_SPIFFS.h>

eSPIFFS fileSystem;
String Station1;
String Station2;

void setup(){
    Serial.begin(115200);
    Station1 = "http://retro.dancewave.online/retrodance.mp3";
    Station2 = "http://live.topfm.hu:8000/radio.mp3";
    
    // Write the data back to the SPIFFS
    if (fileSystem.saveToFile("/configuration.txt", Station1)) {
        Serial.println("Successfully wrote data to file");
    }
    if (fileSystem.saveToFile("/configuration.txt", Station2)) {
        Serial.println("Successfully wrote data to file");
    }
    
}

void loop() {
    
    if (fileSystem.openFromFile("/configuration.txt", Station1)) {
        Serial.print("Station1 url:");
        Serial.println(Station1);
    }
    delay(1000);
    if (fileSystem.openFromFile("/configuration.txt", Station2)) {
        Serial.print("Station2 url:");
        Serial.println(Station2);
    }
    delay(1000);
    
}

I wait to receive both Station 1 and Station2, but just receive only the second one:

19:43:16.441 -> Station1 url:http://live.topfm.hu:8000/radio.mp3
19:43:17.430 -> Station2 url:http://live.topfm.hu:8000/radio.mp3
19:43:18.423 -> Station1 url:http://live.topfm.hu:8000/radio.mp3
19:43:19.428 -> Station2 url:http://live.topfm.hu:8000/radio.mp3

Library allows store only one String variable?

@Sentinel8000 Sentinel8000 added the bug Something isn't working label Jun 25, 2022
@thebigpotatoe
Copy link
Owner

I think you may be a little confused how the library works and in general how file system works on the ESP's.

You are overriding all the contents in the file when you save back to configuration.txt from this line

if (fileSystem.saveToFile("/configuration.txt", Station1)) 

to this line

if (fileSystem.saveToFile("/configuration.txt", Station2)) 

which is why you are only seeing the string http://live.topfm.hu:8000/radio.mp3 when you read it back.

There are a couple of ways you can fix this, depending on your code architecture. If you really need the string to be dynamic as you may be changing them from a web interface and you would like to persist them through reboots, then simply have 2 configuration.txt files for both of your variables like so:

    if (fileSystem.saveToFile("/configuration1.txt", Station1)) {
        Serial.println("Successfully wrote data to file");
    }
    if (fileSystem.saveToFile("/configuration2.txt", Station2)) {
        Serial.println("Successfully wrote data to file");
    }

If these string remain the same regardless of what you do in your application, then I would suggest not using this library at all and just use the flash string helpers built into arduino (note this is untested code I wrote from the top of my head):

String Station1 = F("http://retro.dancewave.online/retrodance.mp3");
String Station2 = F("http://live.topfm.hu:8000/radio.mp3");

@thebigpotatoe thebigpotatoe added the question Further information is requested label Jun 26, 2022
@Sentinel8000
Copy link
Author

Sentinel8000 commented Jun 26, 2022

Sure, im not a advanced developer, just a hobby code writer.

I just think this libraray can handle more from same type variable and if im define more string will store as station 1 and station 2 ,,variable" in configuration.txt. This can be more perfect against the standard SPIFFS. My wifi radio project currently using the <Preferences.h> to store in struct the station / urls what working well, but because this was designed not store large variables fail using more than 10 channels. With SPIFFS was for me realy clear as hobby code writer, if i want handle 20 stations/url i need make 20 different txt and write/rewrite these, what maybe not a nice solution

My idea was using one big single configuration.txt with saved last played station, last volume settings:

http://retro.dancewave.online/retrodance.mp3
http://live.topfm.hu:8000/radio.mp3
http://dancewave.online/dance.ogg
http://listen.hotget.net:810
http://icast.connectmedia.hu/5201/live.mp3
http://streaming.radiodancefloor.it:80/dancefloor.mp3
https://icast.connectmedia.hu/5001/live.mp3
http://mp3.ffh.de/ffhchannels/hqeurodance.mp3
http://mp3.hitradiort1.c.nmdn.net/rt1eurodancewl/livestream.mp3
http://streaming.radiodancefloor.it:80/dancefloor.mp3
http://streaming.radiodancefloor.it:80/dancefloor.mp3
https://icast.connectmedia.hu/5001/live.mp3
http://mp3.ffh.de/ffhchannels/hqeurodance.mp3
http://mp3.hitradiort1.c.nmdn.net/rt1eurodancewl/livestream.mp3
http://streaming.radiodancefloor.it:80/dancefloor.mp3
http://streaming.radiodancefloor.it:80/dancefloor.mp3
https://icast.connectmedia.hu/5001/live.mp3
http://mp3.ffh.de/ffhchannels/hqeurodance.mp3
http://mp3.hitradiort1.c.nmdn.net/rt1eurodancewl/livestream.mp3
http://streaming.radiodancefloor.it:80/dancefloor.mp3
6 //saved played last station
3 //saved volume value

For me was clear currently the second step rewrite the txt, i just think thats was worked not ok, but if im good understand, with this library i need do same what i dont want, making 20 txt for the 20 stations if i want less pain writing code, the libraray itself not working so what im thinking about first time. The only one help, i can use all station as variable and so more easier as handle \n lines:

With the standard SPIFFS im counting (if i have single configuration.txt) the \n lines to handle the 20 line configuration.txt and read all line in a ,,buffer". But for change lookslike i dont have another way if i dont want create 20, 30 files, rebuild by any station url change the single configuration.txt:

maybe this is not part of this library section/bug, but im share the test code what im using currently:

#include "SPIFFS.h"

   int i = 0;
   char buffer[200];
   String line_1, line_2, line_3, line_4, line_5, line_6, line_7, line_8, line_9, line_10, line_11, line_12, line_13, line_14, line_15, line_16, line_17, line_18, line_19, line_20, line_21, line_22, line_23;

void setup() {
  Serial.begin(115200);
  SPIFFS.begin(true);
  File file = SPIFFS.open("/configuration.txt", "r");
  //File file = SPIFFS.open("/configuration.txt");
  while(file.available())
  {
    //Serial.write(file.read());

    int l = file.readBytesUntil('\n', buffer, sizeof(buffer));
    buffer[l] = 0;
  
   if (i == 0) {
    line_1 = buffer;
    Serial.println(line_1);
   }
   if (i == 1) {
    line_2 = buffer;
    Serial.println(line_2);
   }
   if (i == 2) {
    line_3 = buffer;
    Serial.println(line_3);
   }
      if (i == 3) {
    line_4 = buffer;
    Serial.println(line_4);
   }
   if (i == 4) {
    line_5 = buffer;
    Serial.println(line_5);
   }
   if (i == 5) {
    line_6 = buffer;
    Serial.println(line_6);
   }
      if (i == 6) {
    line_7 = buffer;
    Serial.println(line_7);
   }
   if (i == 7) {
    line_8 = buffer;
    Serial.println(line_8);
   }
   if (i == 8) {
    line_9 = buffer;
    Serial.println(line_9);
   }
   if (i == 9) {
    line_10 = buffer;
    Serial.println(line_10);
   }
    if (i == 10) {
    line_11 = buffer;
    Serial.println(line_11);
   }
   if (i == 11) {
    line_12 = buffer;
    Serial.println(line_12);
   }
   if (i == 12) {
    line_13 = buffer;
   }
      if (i == 13) {
    line_14 = buffer;
    Serial.println(line_14);
   }
   if (i == 14) {
    line_15 = buffer;
    Serial.println(line_15);
   }
   if (i == 15) {
    line_16 = buffer;
    Serial.println(line_16);
   }
      if (i == 16) {
    line_17 = buffer;
    Serial.println(line_17);
   }
   if (i == 17) {
    line_18 = buffer;
    Serial.println(line_18);
   }
   if (i == 18) {
    line_19 = buffer;
    Serial.println(line_19);
   }
   if (i == 19) {
    line_20 = buffer;
    Serial.println(line_20);
   }
   if (i == 20) {
    line_21 = buffer;
    Serial.println(line_21);
   }
   if (i == 21) {
    line_22 = buffer;
    Serial.println(line_22);
   }
     if (i == 22) {
    line_23 = buffer;
    Serial.println(line_23);
   }
   
   i++;

   if (i == 22){
    break;
   }
  }
  file.close();
delay(200);

//adding new station:
// todo: rewrite all values back to configuration.txt but with the changed station url
  File filea = SPIFFS.open("/configuration.txt", "a");
  filea.println("test_url_blabla");
  filea.close();
  
}

 void loop() {

     File file = SPIFFS.open("/configuration.txt");
     while(file.available())
  {
    Serial.write(file.read());
  }
  file.close();
   delay(2000);
 }

Thank you for the fast answer and sorry because i dont have other way in github ask question than added as bug.

Maybe i will use 20 txt as solution, more easier against handle one file with the 20+ values.

You can close this, because this is not bug, this is how working the library itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants