• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 Web server book, 3.4 Display from file

Q&A Forum › Category: ESP32 › ESP32 Web server book, 3.4 Display from file
0 Vote Up Vote Down
Dave Walmsley asked 4 years ago

Hi,
just working through this book and in the Display Sensor Readings from File (section 3.4) the section Create a data.txt file in setup() always says “File already exists”, so no file gets created.
I think the if(!file) condition should be reversed, also there’s a small typo “doens’t” should be “doesn’t”.
Dave

Question Tags: ESP32, SPIFFS.open()
8 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi
Can you clarify what is not working properly in your project?
Can you provide more details?
That’s how the project is supposed to work. It only creates the file one time; then, if it already exists, it will append data to the already existing file instead of creating a new (empty) file.
Thanks for letting me know about the typo. We’ll fix that soon.
Regards,
Sara

0 Vote Up Vote Down
Dave Walmsley answered 4 years ago

when there is no data.txt file it doesn’t create one, it says “File already exists”
Dave

0 Vote Up Vote Down
Dave Walmsley answered 4 years ago

Sara,
I’ve added some FS checking, here’s some output from the Serial Console:

Project – 3_4_WS data from file
Connecting to WiFi …192.168.0.26
SPIFFS mounted successfully
===== File system info =====
Total space: 1378241byte
Total space used: 9538byte
/favicon.png 717
/index.html 1347
/readings.json 73
/script.js 4731
/style.css 974
File already exists
File size: 0

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.
The file gets created. However, it will take 30 minutes until it writes a data point.
To test the project, you can change the delay time in the timerDelay variable. For example:

unsigned long timerDelay = 18000;

After a few seconds, you’ll see that it adds a point to the file and displays it in the chart. Tell me your results.
 
Regards,
 
Sara

0 Vote Up Vote Down
Dave Walmsley answered 4 years ago

The data.txt file does not get created until after timerDelay (which I have set to 30 seconds for testing), so no initial read of the sensors is stored.

ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
Project – 3_4_WS data from file
Connecting to WiFi …192.168.0.26
SPIFFS mounted successfully
===== File system info =====
Total space: 1378241byte
Total space used: 9538byte
/favicon.png 717
/index.html 1347
/readings.json 73
/script.js 4755
/style.css 974
File already exists <— this is where data.txt should be created
testing filesystem <– this line and the following directory listing are from code I added after the create data.txt section.
/favicon.png 717
/index.html 1347
/readings.json 73
/script.js 4755
/style.css 974
File size: 0
Appending to file: /data.txt <—– this is the update after timer Delay, which shows only one entry in the file
– message appended
Reading file: /data.txt
{“time”:”1616476592″,”Solar”:”1.02″,”Alternator”:”1.19″,”Battery”:”13.02″},

I can send the actual code if you like.

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Dave.
 
You are right, and I’ve found where the problem is.
 
Basically, to check if a file already exists, we need to use the exists() method. So, the lines of code that check if the file exists should be like this:

bool fileexists = SPIFFS.exists(dataPath);
Serial.print(fileexists);
if(!fileexists) {
  Serial.println("File doens't exist");
  Serial.println("Creating file...");
  // Prepare readings to add to the file
  String message = getSensorReadings() + ",";
  // Apend data to file to create it
  appendFile(SPIFFS, dataPath, message.c_str());
}
else {
  Serial.println("File already exists");
}

 
Thanks for letting me know about this issue. We’ll update the eBook with this correction soon.
 
Before uploading the new code, I recommend formatting SPIFFS to actually be able to check if it finds the file or not.
To format SPIFFS, I’ve used the code by techtutorialsx:https://techtutorialsx.com/2019/02/24/esp32-arduino-formatting-the-spiffs-file-system/.
 
Let me know if this solves the issue.
Regards,
Sara 
 
 

0 Vote Up Vote Down
Dave Walmsley answered 4 years ago

Thanks Sara, that’s better.

I used the ‘Delete File’ button to remove data.txt. That works as long as you reset the ESP before the timerDelay update.

I was doing a bit of reading and I think the SPIFFS.open returns a file handle which may be interpreted wrongly.
Also, before you commit the changes,  there still the ” ‘doens’t ” to fix.
One last thing, in the  “3_4_Charts_From_File.zip” there’s a ‘readings.json’ in the data directory that I don’t think is necessary.

Dave

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Dave.
Thanks for pointing that out.
That file was not supposed to be in the folder. It is not needed for the project. 
We’ll review the complete code before making an update.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.