{"id":134008,"date":"2023-09-21T11:23:25","date_gmt":"2023-09-21T11:23:25","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=134008"},"modified":"2023-09-21T13:04:53","modified_gmt":"2023-09-21T13:04:53","slug":"esp8266-nodemcu-write-data-littlefs-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-write-data-littlefs-arduino\/","title":{"rendered":"ESP8266 NodeMCU: Write Data to a File (LittleFS) &#8211; Arduino IDE"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to write and save data permanently to a file saved on the ESP8266 filesystem (LittleFS). LittleFS is a lightweight filesystem created for microcontrollers that lets you access the flash memory like you would do in a standard file system on your computer, but simple and more limited.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-Write-Data-To-a-File-LittleFS.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU Write Data to a File LittleFS Arduino IDE\" class=\"wp-image-134009\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-Write-Data-To-a-File-LittleFS.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-Write-Data-To-a-File-LittleFS.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-Write-Data-To-a-File-LittleFS.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-Write-Data-To-a-File-LittleFS.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\">We have a similar tutorial for ESP32 boards: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-write-data-littlefs-arduino\/\">ESP32: Write Data to a File (LittleFS) \u2013 Arduino IDE<\/a>.<\/p>\n\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<p>Throughout this tutorial, we&#8217;ll cover the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#littlefs-introduction\" title=\"\">Introducing LittleFS<\/a><\/li>\n\n\n\n<li><a href=\"#esp8266-little-fs-handle-files\" title=\"\">ESP8266 with LittleFS &#8211; Handling Files and Folders<\/a><\/li>\n\n\n\n<li><a href=\"#esp8266-save-variable-littlefs\" title=\"\">ESP8266 with LittleFS &#8211; How to Save Variables&#8217; Values to a File<\/a><\/li>\n\n\n\n<li><a href=\"#check-if-file-exists-littlefs\" title=\"\">ESP8266 with LittleFS &#8211; Check if a file already exists<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>We\u2019ll program the ESP8266 board using Arduino IDE. So, make sure you have the ESP8266 add-on installed. Follow the next tutorial if you haven&#8217;t already:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\" title=\"\">Installing ESP8266 Board in Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>Additionally, make sure you&#8217;re running the latest version of the ESP8266 add-on. Go to <strong>Tools <\/strong>&gt; <strong>Board <\/strong>&gt; <strong>Boards Manager,<\/strong> search for <strong>ESP8266<\/strong>, and check that you&#8217;re running the latest version.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"littlefs-introduction\">Introducing LittleFS<\/h2>\n\n\n\n<p>LittleFS is a lightweight filesystem created for microcontrollers that lets you access the flash memory like you would do in a standard file system on your computer, but it\u2019s simpler and more limited. You can read, write, close, and delete files and folders. Using a filesystem with the ESP8266 boards is especially useful to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create configuration files with settings;<\/li>\n\n\n\n<li>Save data permanently;<\/li>\n\n\n\n<li>Create files to save small amounts of data instead of using a microSD card;<\/li>\n\n\n\n<li>Save HTML, CSS, and JavaScript files to build a web server;<\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/display-images-esp32-esp8266-web-server\/\">Save images, figures, and icons<\/a>;<\/li>\n\n\n\n<li>And much more.<\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">You may also like reading: <a href=\"https:\/\/randomnerdtutorials.com\/install-esp8266-nodemcu-littlefs-arduino\/\">Install ESP8266 NodeMCU LittleFS Filesystem Uploader in Arduino IDE<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp8266-little-fs-handle-files\">ESP8266 with LittleFS &#8211; Handling Files and Folders<\/h2>\n\n\n\n<p>Before showing you how to write data to a file on LittleFS with the ESP8266, let&#8217;s take a look at an example that shows how to do practically any task that you may need when dealing with files using LittleFS.<\/p>\n\n\n\n<p>This is the <a href=\"https:\/\/github.com\/esp8266\/Arduino\/blob\/master\/libraries\/LittleFS\/examples\/LittleFS_Timestamp\/LittleFS_Timestamp.ino\" target=\"_blank\" rel=\"noopener\" title=\"\">official example<\/a> from the ESP8266 core.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/* Example showing timestamp support in LittleFS - Released into the public domain.\n   Earle F. Philhower, III &lt;earlephilhower@yahoo.com&gt; Official example: https:\/\/github.com\/esp8266\/Arduino\/blob\/master\/libraries\/LittleFS\/examples\/LittleFS_Timestamp\/LittleFS_Timestamp.ino\n*\/\n\n\/\/ Project details: https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-write-data-littlefs-arduino\/\n\n#include &lt;FS.h&gt;\n#include &lt;LittleFS.h&gt;\n#include &lt;time.h&gt;\n#include &lt;ESP8266WiFi.h&gt;\n\n#ifndef STASSID\n#define STASSID &quot;REPLACE_WITH_YOUR_SSID&quot;\n#define STAPSK &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\n#endif\n\nconst char *ssid = STASSID;\nconst char *pass = STAPSK;\n\nlong timezone = 0;\nbyte daysavetime = 1;\n\nvoid listDir(const char *dirname) {\n  Serial.printf(&quot;Listing directory: %s\\n&quot;, dirname);\n\n  Dir root = LittleFS.openDir(dirname);\n\n  while (root.next()) {\n    File file = root.openFile(&quot;r&quot;);\n    Serial.print(&quot;  FILE: &quot;);\n    Serial.print(root.fileName());\n    Serial.print(&quot;  SIZE: &quot;);\n    Serial.print(file.size());\n    time_t cr = file.getCreationTime();\n    time_t lw = file.getLastWrite();\n    file.close();\n    struct tm *tmstruct = localtime(&amp;cr);\n    Serial.printf(&quot;    CREATION: %d-%02d-%02d %02d:%02d:%02d\\n&quot;, (tmstruct-&gt;tm_year) + 1900, (tmstruct-&gt;tm_mon) + 1, tmstruct-&gt;tm_mday, tmstruct-&gt;tm_hour, tmstruct-&gt;tm_min, tmstruct-&gt;tm_sec);\n    tmstruct = localtime(&amp;lw);\n    Serial.printf(&quot;  LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\\n&quot;, (tmstruct-&gt;tm_year) + 1900, (tmstruct-&gt;tm_mon) + 1, tmstruct-&gt;tm_mday, tmstruct-&gt;tm_hour, tmstruct-&gt;tm_min, tmstruct-&gt;tm_sec);\n  }\n}\n\nvoid readFile(const char *path) {\n  Serial.printf(&quot;Reading file: %s\\n&quot;, path);\n\n  File file = LittleFS.open(path, &quot;r&quot;);\n  if (!file) {\n    Serial.println(&quot;Failed to open file for reading&quot;);\n    return;\n  }\n\n  Serial.print(&quot;Read from file: &quot;);\n  while (file.available()) { Serial.write(file.read()); }\n  file.close();\n}\n\nvoid writeFile(const char *path, const char *message) {\n  Serial.printf(&quot;Writing file: %s\\n&quot;, path);\n\n  File file = LittleFS.open(path, &quot;w&quot;);\n  if (!file) {\n    Serial.println(&quot;Failed to open file for writing&quot;);\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(&quot;File written&quot;);\n  } else {\n    Serial.println(&quot;Write failed&quot;);\n  }\n  delay(2000);  \/\/ Make sure the CREATE and LASTWRITE times are different\n  file.close();\n}\n\nvoid appendFile(const char *path, const char *message) {\n  Serial.printf(&quot;Appending to file: %s\\n&quot;, path);\n\n  File file = LittleFS.open(path, &quot;a&quot;);\n  if (!file) {\n    Serial.println(&quot;Failed to open file for appending&quot;);\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(&quot;Message appended&quot;);\n  } else {\n    Serial.println(&quot;Append failed&quot;);\n  }\n  file.close();\n}\n\nvoid renameFile(const char *path1, const char *path2) {\n  Serial.printf(&quot;Renaming file %s to %s\\n&quot;, path1, path2);\n  if (LittleFS.rename(path1, path2)) {\n    Serial.println(&quot;File renamed&quot;);\n  } else {\n    Serial.println(&quot;Rename failed&quot;);\n  }\n}\n\nvoid deleteFile(const char *path) {\n  Serial.printf(&quot;Deleting file: %s\\n&quot;, path);\n  if (LittleFS.remove(path)) {\n    Serial.println(&quot;File deleted&quot;);\n  } else {\n    Serial.println(&quot;Delete failed&quot;);\n  }\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  \/\/ We start by connecting to a WiFi network\n  Serial.println();\n  Serial.println();\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n\n  WiFi.begin(ssid, pass);\n\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println(&quot;WiFi connected&quot;);\n  Serial.println(&quot;IP address: &quot;);\n  Serial.println(WiFi.localIP());\n  Serial.println(&quot;Contacting Time Server&quot;);\n  configTime(3600 * timezone, daysavetime * 3600, &quot;time.nist.gov&quot;, &quot;0.pool.ntp.org&quot;, &quot;1.pool.ntp.org&quot;);\n  struct tm tmstruct;\n  delay(2000);\n  tmstruct.tm_year = 0;\n  getLocalTime(&amp;tmstruct, 5000);\n  Serial.printf(&quot;\\nNow is : %d-%02d-%02d %02d:%02d:%02d\\n&quot;, (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);\n  Serial.println(&quot;&quot;);\n  Serial.println(&quot;Formatting LittleFS filesystem&quot;);\n  LittleFS.format();\n  Serial.println(&quot;Mount LittleFS&quot;);\n  if (!LittleFS.begin()) {\n    Serial.println(&quot;LittleFS mount failed&quot;);\n    return;\n  }\n  listDir(&quot;\/&quot;);\n  deleteFile(&quot;\/hello.txt&quot;);\n  writeFile(&quot;\/hello.txt&quot;, &quot;Hello &quot;);\n  appendFile(&quot;\/hello.txt&quot;, &quot;World!\\n&quot;);\n  listDir(&quot;\/&quot;);\n\n  Serial.println(&quot;The timestamp should be valid above&quot;);\n\n  Serial.println(&quot;Now unmount and remount and perform the same operation.&quot;);\n  Serial.println(&quot;Timestamp should be valid, data should be good.&quot;);\n  LittleFS.end();\n  Serial.println(&quot;Now mount it&quot;);\n  if (!LittleFS.begin()) {\n    Serial.println(&quot;LittleFS mount failed&quot;);\n    return;\n  }\n  readFile(&quot;\/hello.txt&quot;);\n  listDir(&quot;\/&quot;);\n}\n\nvoid loop() {\n\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_Testing_LittleFS.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This code covers the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#listDir\" title=\"\">List a directory;<\/a><\/li>\n\n\n\n<li><a href=\"#readFile\" title=\"\">Read a file&#8217;s content;<\/a><\/li>\n\n\n\n<li><a href=\"#writeFile\" title=\"\">Write content to a file;<\/a><\/li>\n\n\n\n<li><a href=\"#appendFile\" title=\"\">Append content to a file;<\/a><\/li>\n\n\n\n<li><a href=\"#renameFile\" title=\"\">Rename a file;<\/a><\/li>\n\n\n\n<li><a href=\"#deleteFile\" title=\"\">Delete a file;<\/a><\/li>\n\n\n\n<li><a href=\"#testing-llittlefs\" title=\"\">Initialize the LittleFS filesystem.<\/a><\/li>\n<\/ul>\n\n\n\n<p>This particular example also displays when a file was created and what was the last time it was written. So, you&#8217;ll need to insert your network credentials so that we can get the current time from the internet.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>First, you need to include the following libraries: <span class=\"rnthl rntliteral\">FS.h<\/span> to handle files, <span class=\"rnthl rntliteral\">LittleFS.h<\/span> to create and access the filesystem, the <span class=\"rnthl rntliteral\">time.h<\/span> to deal with time functions and the <span class=\"rnthl rntliteral\">ESP8266WiFi.h<\/span> so that we can connect to the internet to retrieve the current time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;FS.h&gt;\n#include &lt;LittleFS.h&gt;\n#include &lt;time.h&gt;\n#include &lt;ESP8266WiFi.h&gt;<\/code><\/pre>\n\n\n\n<p>You need to insert your network credentials on the following lines:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define STASSID \"REPLACE_WITH_YOUR_SSID\"\n#define STAPSK \"REPLACE_WITH_YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<p>Adjust your timezone in the following parameters. In the timezone variable insert a number accordingly to your timezone in relation to GMT. Additionally, add if your timezone uses daylight saving time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>long timezone = 0;\nbyte daysavetime = 1;<\/code><\/pre>\n\n\n\n<p>The example provides several functions to handle files on the LittleFS filesystem. Let&#8217;s take a look at them.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"listDir\">List a directory<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">listDir()<\/span> function lists the directories on the filesystem. This function accepts as arguments the directory&#8217;s file path. This will output all files and folders inside that path and the date of creation plus the last time they were modified.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void listDir(const char *dirname) {\n  Serial.printf(\"Listing directory: %s\\n\", dirname);\n\n  Dir root = LittleFS.openDir(dirname);\n\n  while (root.next()) {\n    File file = root.openFile(\"r\");\n    Serial.print(\"  FILE: \");\n    Serial.print(root.fileName());\n    Serial.print(\"  SIZE: \");\n    Serial.print(file.size());\n    time_t cr = file.getCreationTime();\n    time_t lw = file.getLastWrite();\n    file.close();\n    struct tm *tmstruct = localtime(&amp;cr);\n    Serial.printf(\"    CREATION: %d-%02d-%02d %02d:%02d:%02d\\n\", (tmstruct-&gt;tm_year) + 1900, (tmstruct-&gt;tm_mon) + 1, tmstruct-&gt;tm_mday, tmstruct-&gt;tm_hour, tmstruct-&gt;tm_min, tmstruct-&gt;tm_sec);\n    tmstruct = localtime(&amp;lw);\n    Serial.printf(\"  LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\\n\", (tmstruct-&gt;tm_year) + 1900, (tmstruct-&gt;tm_mon) + 1, tmstruct-&gt;tm_mday, tmstruct-&gt;tm_hour, tmstruct-&gt;tm_min, tmstruct-&gt;tm_sec);\n  }\n}<\/code><\/pre>\n\n\n\n<p>Here&#8217;s an example of how to call this function. The <span class=\"rnthl rntliteral\">\/<\/span> corresponds to the root directory. The following command will list all the directories and files in the root directory.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code> listDir(\"\/\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"readFile\">Read File Content<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">readFile()<\/span> function reads the content of a file and prints the content in the Serial Monitor. Pass as an argument the file path.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void readFile(const char *path) {\n  Serial.printf(\"Reading file: %s\\n\", path);\n\n  File file = LittleFS.open(path, \"r\");\n  if (!file) {\n    Serial.println(\"Failed to open file for reading\");\n    return;\n  }\n\n  Serial.print(\"Read from file: \");\n  while (file.available()) { Serial.write(file.read()); }\n  file.close();\n}<\/code><\/pre>\n\n\n\n<p>For example, the following line reads the content of the <span class=\"rnthl rntliteral\">hello.txt<\/span> file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>readFile(\"\/hello.txt\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"writeFile\">Write Content to a File<\/h4>\n\n\n\n<p>To write content to a file, you can use the <span class=\"rnthl rntliteral\">writeFile()<\/span> function. Pass as an argument: the file path, and the message\/data (as a const char variable). <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void writeFile(const char *path, const char *message) {\n  Serial.printf(\"Writing file: %s\\n\", path);\n\n  File file = LittleFS.open(path, \"w\");\n  if (!file) {\n    Serial.println(\"Failed to open file for writing\");\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(\"File written\");\n  } else {\n    Serial.println(\"Write failed\");\n  }\n  delay(2000);  \/\/ Make sure the CREATE and LASTWRITE times are different\n  file.close();\n}<\/code><\/pre>\n\n\n\n<p>The following line writes <span class=\"rnthl rntliteral\">Hello <\/span> in the <span class=\"rnthl rntliteral\">hello.txt<\/span> file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>writeFile(\"\/hello.txt\", \"Hello \");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"appendFile\">Append Content to a File<\/h4>\n\n\n\n<p>Similarly, you can append content to a file (without overwriting previous content) using the <span class=\"rnthl rntliteral\">appendFile()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void appendFile(const char *path, const char *message) {\n  Serial.printf(\"Appending to file: %s\\n\", path);\n\n  File file = LittleFS.open(path, \"a\");\n  if (!file) {\n    Serial.println(\"Failed to open file for appending\");\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(\"Message appended\");\n  } else {\n    Serial.println(\"Append failed\");\n  }\n  file.close();\n}<\/code><\/pre>\n\n\n\n<p>The following line appends the message <span class=\"rnthl rntliteral\">World!\\r\\n<\/span> in the <span class=\"rnthl rntliteral\">hello.txt<\/span> file. The <span class=\"rnthl rntliteral\">\\n<\/span> means that the next time you write something to the file, it will be written in a new line.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>appendFile(\"\/hello.txt\", \"World!\\n\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"renameFile\">Rename a File<\/h4>\n\n\n\n<p>You can rename a file using the <span class=\"rnthl rntliteral\">renameFile()<\/span> function. Pass as arguments the <span class=\"rnthl rntliteral\">LittleFS<\/span> filesystem, the original filename, and the new filename.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void renameFile(const char *path1, const char *path2) {\n  Serial.printf(\"Renaming file %s to %s\\n\", path1, path2);\n  if (LittleFS.rename(path1, path2)) {\n    Serial.println(\"File renamed\");\n  } else {\n    Serial.println(\"Rename failed\");\n  }\n}<\/code><\/pre>\n\n\n\n<p>The following line renames the <span class=\"rnthl rntliteral\">hello.txt<\/span> file to <span class=\"rnthl rntliteral\">foo.txt<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>renameFile(\"\/hello.txt\", \"\/foo.txt\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"deleteFile\">Delete a File<\/h4>\n\n\n\n<p>Use the <span class=\"rnthl rntliteral\">deleteFile()<\/span> function to delete a file. Pass as an argument the file path of the file you want to delete.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void deleteFile(const char *path) {\n  Serial.printf(\"Deleting file: %s\\n\", path);\n  if (LittleFS.remove(path)) {\n    Serial.println(\"File deleted\");\n  } else {\n    Serial.println(\"Delete failed\");\n  }\n}<\/code><\/pre>\n\n\n\n<p>The following line deletes the <span class=\"rnthl rntliteral\">hello.txt<\/span> file from the filesystem.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>deleteFile(\"\/hello.txt\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initialize Wi-Fi and Time<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we start by initializing Wi-Fi.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.begin(ssid, pass);\n\nwhile (WiFi.status() != WL_CONNECTED) {\n  delay(500);\n  Serial.print(\".\");\n}\nSerial.println(\"WiFi connected\");\nSerial.println(\"IP address: \");\nSerial.println(WiFi.localIP());<\/code><\/pre>\n\n\n\n<p>After initializing Wi-Fi, we can set up the time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.println(\"Contacting Time Server\");\nconfigTime(3600 * timezone, daysavetime * 3600, \"time.nist.gov\", \"0.pool.ntp.org\", \"1.pool.ntp.org\");\nstruct tm tmstruct;\ndelay(2000);\ntmstruct.tm_year = 0;\ngetLocalTime(&amp;tmstruct, 5000);\nSerial.printf(\"\\nNow is : %d-%02d-%02d %02d:%02d:%02d\\n\", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);\nSerial.println(\"\");<\/code><\/pre>\n\n\n\n<p>To learn more about time functions with the ESP8266, check the following tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-date-time-ntp-client-server-arduino\/\">ESP8266 NodeMCU NTP Client-Server: Get Date and Time (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/epoch-unix-time-esp8266-nodemcu-arduino\/\">Get Epoch\/Unix Time with the ESP8266 NodeMCU (Arduino)<\/a><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"init-littlefs\">Initialize the Filesystem<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, the following lines initialize the LittleFS filesystem.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)){\n  Serial.println(\"LittleFS Mount Failed\");\n  return;\n}<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">LittleFS.begin()<\/span> function returns <span class=\"rnthl rntliteral\">true<\/span> if the filesystem is initialized successfully or <span class=\"rnthl rntliteral\">false<\/span> if it isn&#8217;t. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"testing-llittlefs\">Testing the Filesystem<\/h4>\n\n\n\n<p>The following lines call all the functions we&#8217;ve seen previously and mount and unmount the filesystem for testing purposes<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>listDir(\"\/\");\ndeleteFile(\"\/hello.txt\");\nwriteFile(\"\/hello.txt\", \"Hello \");\nappendFile(\"\/hello.txt\", \"World!\\n\");\nlistDir(\"\/\");\n\nSerial.println(\"The timestamp should be valid above\");\n\nSerial.println(\"Now unmount and remount and perform the same operation.\");\nSerial.println(\"Timestamp should be valid, data should be good.\");\nLittleFS.end();\nSerial.println(\"Now mount it\");\nif (!LittleFS.begin()) {\n  Serial.println(\"LittleFS mount failed\");\n  return;\n}\nreadFile(\"\/hello.txt\");\nlistDir(\"\/\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Upload the previous sketch to your ESP8266 board. After that, open the Serial Monitor and press and reset your board. If the initialization succeeds, you&#8217;ll get similar messages on the Serial Monitor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"601\" height=\"631\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/testing-LittleFS-ESP8266.png?resize=601%2C631&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Testing LittleFS Examples\" class=\"wp-image-134010\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/testing-LittleFS-ESP8266.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/testing-LittleFS-ESP8266.png?resize=286%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 286w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<p>As you can see, when listing files, it shows the time of creation and the last time it was written.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp8266-save-variable-littlefs\">ESP8266 with LittleFS &#8211; How to Save Variables&#8217; Values to a File<\/h2>\n\n\n\n<p>The previous example illustrated almost all the operations you might need to do when dealing with files on the filesystem. In this section, we&#8217;ll take a look at a more simple and specific example: how to save the content of a variable to the filesystem.<\/p>\n\n\n\n<p>Let&#8217;s take a look at the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/* Example showing timestamp support in LittleFS - Released into the public domain.\n   Earle F. Philhower, III &lt;earlephilhower@yahoo.com&gt; Official example: https:\/\/github.com\/esp8266\/Arduino\/blob\/master\/libraries\/LittleFS\/examples\/LittleFS_Timestamp\/LittleFS_Timestamp.ino\n*\/\n\n\/\/ Project details: https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-write-data-littlefs-arduino\/\n\n#include &lt;FS.h&gt;\n#include &lt;LittleFS.h&gt;\n\nint mydata;\n\nvoid readFile(const char *path) {\n  Serial.printf(&quot;Reading file: %s\\n&quot;, path);\n\n  File file = LittleFS.open(path, &quot;r&quot;);\n  if (!file) {\n    Serial.println(&quot;Failed to open file for reading&quot;);\n    return;\n  }\n\n  Serial.print(&quot;Read from file: &quot;);\n  while (file.available()) { Serial.write(file.read()); }\n  file.close();\n}\n\nvoid writeFile(const char *path, const char *message) {\n  Serial.printf(&quot;Writing file: %s\\n&quot;, path);\n\n  File file = LittleFS.open(path, &quot;w&quot;);\n  if (!file) {\n    Serial.println(&quot;Failed to open file for writing&quot;);\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(&quot;File written&quot;);\n  } else {\n    Serial.println(&quot;Write failed&quot;);\n  }\n  delay(2000);  \/\/ Make sure the CREATE and LASTWRITE times are different\n  file.close();\n}\n\nvoid appendFile(const char *path, const char *message) {\n  Serial.printf(&quot;Appending to file: %s\\n&quot;, path);\n\n  File file = LittleFS.open(path, &quot;a&quot;);\n  if (!file) {\n    Serial.println(&quot;Failed to open file for appending&quot;);\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(&quot;Message appended&quot;);\n  } else {\n    Serial.println(&quot;Append failed&quot;);\n  }\n  file.close();\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  \n  Serial.println(&quot;Mount LittleFS&quot;);\n  if (!LittleFS.begin()) {\n    Serial.println(&quot;LittleFS mount failed&quot;);\n    return;\n  }\n  else{\n    Serial.println(&quot;Little FS Mounted Successfully&quot;);\n  }\n  writeFile(&quot;\/data.txt&quot;, &quot;MY ESP8266 DATA \\r\\n&quot;);\n}\n\nvoid loop() {\n  mydata = random (0, 1000);\n  appendFile(&quot;\/data.txt&quot;, (String(mydata)+ &quot;\\r\\n&quot;).c_str()); \/\/Append data to the file\n  readFile(&quot;\/data.txt&quot;); \/\/ Read the contents of the file\n  delay(30000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_Write_File_LittleFS.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>For this example, we&#8217;ll continuously save the value of a variable to the filesystem. As an example, we&#8217;ll save a random number, but this can be easily adjusted to save sensor readings, for example.<\/p>\n\n\n\n<p>We start by creating a variable that will hold the random number called <span class=\"rnthl rntliteral\">mydata<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int mydata;<\/code><\/pre>\n\n\n\n<p>For this particular example, we just need to use the <span class=\"rnthl rntliteral\">writeFile()<\/span>, <span class=\"rnthl rntliteral\">appendFile()<\/span>, and <span class=\"rnthl rntliteral\">readFile()<\/span> functions. So, we have those functions defined before the <span class=\"rnthl rntliteral\">setup()<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void readFile(const char *path) {\n  Serial.printf(\"Reading file: %s\\n\", path);\n\n  File file = LittleFS.open(path, \"r\");\n  if (!file) {\n    Serial.println(\"Failed to open file for reading\");\n    return;\n  }\n\n  Serial.print(\"Read from file: \");\n  while (file.available()) { Serial.write(file.read()); }\n  file.close();\n}\n\nvoid writeFile(const char *path, const char *message) {\n  Serial.printf(\"Writing file: %s\\n\", path);\n\n  File file = LittleFS.open(path, \"w\");\n  if (!file) {\n    Serial.println(\"Failed to open file for writing\");\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(\"File written\");\n  } else {\n    Serial.println(\"Write failed\");\n  }\n  delay(2000);  \/\/ Make sure the CREATE and LASTWRITE times are different\n  file.close();\n}\n\nvoid appendFile(const char *path, const char *message) {\n  Serial.printf(\"Appending to file: %s\\n\", path);\n\n  File file = LittleFS.open(path, \"a\");\n  if (!file) {\n    Serial.println(\"Failed to open file for appending\");\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(\"Message appended\");\n  } else {\n    Serial.println(\"Append failed\");\n  }\n  file.close();\n}<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we initialize the Serial Monitor for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>And we initialize the LittleFS filesystem:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.println(\"Mount LittleFS\");\nif (!LittleFS.begin()) {\n  Serial.println(\"LittleFS mount failed\");\n  return;\n}\nelse{\n  Serial.println(\"Little FS Mounted Successfully\");\n}<\/code><\/pre>\n\n\n\n<p>Then, we create a file called <span class=\"rnthl rntliteral\">data.txt<\/span> with the following text inside <span class=\"rnthl rntliteral\">MY ESP8266 DATA<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>writeFile(\"\/data.txt\", \"MY ESP8266 DATA \\r\\n\");<\/code><\/pre>\n\n\n\n<p>Something to notice about the <span class=\"rnthl rntliteral\">writeFile()<\/span> function: it creates a file (if it doesn&#8217;t exist) called <span class=\"rnthl rntliteral\">data.txt<\/span> with the text we define inside. <\/p>\n\n\n\n<p>If that file already exists, the <span class=\"rnthl rntliteral\">writeFile()<\/span> function will overwrite any existing contents inside that file. So, if you want to continuously add new data without replacing it, you should use the <span class=\"rnthl rntliteral\">appendFile()<\/span> function after creating the file. If you want to replace the content of the file, you should use the <span class=\"rnthl rntliteral\">writeFile()<\/span>.<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we start by attributing a random value between 0 and 1000 to the <span class=\"rnthl rntliteral\">mydata<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mydata = random (0, 1000);<\/code><\/pre>\n\n\n\n<p>Then, we append data to the file by calling the <span class=\"rnthl rntliteral\">appendFile()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>appendFile(\"\/data.txt\", (String(mydata)+ \"\\r\\n\").c_str()); \/\/ Append data to the file<\/code><\/pre>\n\n\n\n<p>Notice that we concatenate the <span class=\"rnthl rntliteral\">mydata<\/span> variable with <span class=\"rnthl rntliteral\">&#8220;\\r\\n&#8221;<\/span> so that subsequent data is written on the next line. Because our variable is of <strong><em>int <\/em><\/strong>type, we need to convert it to a String before concatenating.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String(mydata)<\/code><\/pre>\n\n\n\n<p>Additionally, then, we need to convert it to a const char using the <span class=\"rnthl rntliteral\">c_str()<\/span> method:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String(mydata)+ \"\\r\\n\").c_str()<\/code><\/pre>\n\n\n\n<p>After appending data to the file, we&#8217;ll read its content by calling the <span class=\"rnthl rntliteral\">readFile()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>readFile(\"\/data.txt\"); \/\/ Read the contents of the file<\/code><\/pre>\n\n\n\n<p>New random values are generated and added to the file every 30 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(30000);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to the ESP8266 board. Open the Serial Monitor at a baud rate of 115200. <\/p>\n\n\n\n<p>It should initialize the filesystem, create the file and start adding a new random number to the file every 30 seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"601\" height=\"674\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Write-data-to-file-and-append-littlefs-esp8266.png?resize=601%2C674&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Save Data to File LittleFS Example Serial Monitor\" class=\"wp-image-134013\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Write-data-to-file-and-append-littlefs-esp8266.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Write-data-to-file-and-append-littlefs-esp8266.png?resize=268%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 268w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<p>Notice that if you restart your board, you&#8217;ll lose all your previous data. Why is that happening?<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"507\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Write-data-to-file-and-append-littlefs-esp8266-overwrite.png?resize=601%2C507&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Save Data to File LittleFS Example Serial Monitor\" class=\"wp-image-134014\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Write-data-to-file-and-append-littlefs-esp8266-overwrite.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/Write-data-to-file-and-append-littlefs-esp8266-overwrite.png?resize=300%2C253&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<p>That happens because we&#8217;re calling the <span class=\"rnthl rntliteral\">writeFile()<\/span> function in the <span class=\"rnthl rntliteral\">setup()<\/span>. As we&#8217;ve explained previously, it will create a new file if it doesn&#8217;t exist, or overwrite an already existing file with the same name. To prevent that, we can add some lines to the <span class=\"rnthl rntliteral\">setup()<\/span> to check whether the file already exists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"check-if-file-exists-littlefs\">ESP8266 with LittleFS &#8211; Check if a file already exists<\/h3>\n\n\n\n<p>To check if a file already exists in the filesystem, we can use the <span class=\"rnthl rntliteral\">exists()<\/span> method and pass as an argument the file path. You can add the following lines to the <span class=\"rnthl rntliteral\">setup()<\/span> to prevent overwriting when the ESP8266 restarts:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Check if the file already exists to prevent overwritting existing data\nbool fileexists = LittleFS.exists(\"\/data.txt\");\nSerial.print(fileexists);\nif(!fileexists) {\n  Serial.println(\"File doesn\u2019t exist\");\n   Serial.println(\"Creating file...\");\n  \/\/ Create File and add header\n   writeFile(\"\/data.txt\", \"MY ESP8266 DATA \\r\\n\");\n}\nelse {\n  Serial.println(\"File already exists\");\n}<\/code><\/pre>\n\n\n\n<p>It uses the <span class=\"rnthl rntliteral\">exists()<\/span> method to check if the file already exists:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>bool fileexists = LittleFS.exists(\"\/data.txt\");<\/code><\/pre>\n\n\n\n<p>It will return <span class=\"rnthl rntliteral\">true<\/span> if the file already exists or <span class=\"rnthl rntliteral\">false<\/span> if it doesn&#8217;t.<\/p>\n\n\n\n<p>If it doesn&#8217;t exist, it will create the file with the content with define.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(!fileexists) {\n  Serial.println(\"File doesn\u2019t exist\");\n  Serial.println(\"Creating file...\");\n  \/\/ Create File and add header\n  writeFile(\"\/data.txt\", \"MY ESP8266 DATA \\r\\n\");\n}<\/code><\/pre>\n\n\n\n<p>If it already exists, it simply writes <span class=\"rnthl rntliteral\">File already exists<\/span> in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>else {\n  Serial.println(\"File already exists\");\n}<\/code><\/pre>\n\n\n\n<p>Here&#8217;s the complete example that checks if the file already exists.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/* Example showing timestamp support in LittleFS - Released into the public domain.\n   Earle F. Philhower, III &lt;earlephilhower@yahoo.com&gt; Official example: https:\/\/github.com\/esp8266\/Arduino\/blob\/master\/libraries\/LittleFS\/examples\/LittleFS_Timestamp\/LittleFS_Timestamp.ino\n*\/\n\n\/\/ Project details: https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-write-data-littlefs-arduino\/\n\n#include &lt;FS.h&gt;\n#include &lt;LittleFS.h&gt;\n\nint mydata;\n\nvoid readFile(const char *path) {\n  Serial.printf(&quot;Reading file: %s\\n&quot;, path);\n\n  File file = LittleFS.open(path, &quot;r&quot;);\n  if (!file) {\n    Serial.println(&quot;Failed to open file for reading&quot;);\n    return;\n  }\n\n  Serial.print(&quot;Read from file: &quot;);\n  while (file.available()) { Serial.write(file.read()); }\n  file.close();\n}\n\nvoid writeFile(const char *path, const char *message) {\n  Serial.printf(&quot;Writing file: %s\\n&quot;, path);\n\n  File file = LittleFS.open(path, &quot;w&quot;);\n  if (!file) {\n    Serial.println(&quot;Failed to open file for writing&quot;);\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(&quot;File written&quot;);\n  } else {\n    Serial.println(&quot;Write failed&quot;);\n  }\n  delay(2000);  \/\/ Make sure the CREATE and LASTWRITE times are different\n  file.close();\n}\n\nvoid appendFile(const char *path, const char *message) {\n  Serial.printf(&quot;Appending to file: %s\\n&quot;, path);\n\n  File file = LittleFS.open(path, &quot;a&quot;);\n  if (!file) {\n    Serial.println(&quot;Failed to open file for appending&quot;);\n    return;\n  }\n  if (file.print(message)) {\n    Serial.println(&quot;Message appended&quot;);\n  } else {\n    Serial.println(&quot;Append failed&quot;);\n  }\n  file.close();\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  \n  Serial.println(&quot;Mount LittleFS&quot;);\n  if (!LittleFS.begin()) {\n    Serial.println(&quot;LittleFS mount failed&quot;);\n    return;\n  }\n  else{\n    Serial.println(&quot;Little FS Mounted Successfully&quot;);\n  }\n   \/\/ Check if the file already exists to prevent overwritting existing data\n   bool fileexists = LittleFS.exists(&quot;\/data.txt&quot;);\n   Serial.print(fileexists);\n   if(!fileexists) {\n       Serial.println(&quot;File doesn\u2019t exist&quot;);\n       Serial.println(&quot;Creating file...&quot;);\n       \/\/ Create File and add header\n       writeFile(&quot;\/data.txt&quot;, &quot;MY ESP8266 DATA \\r\\n&quot;);\n   }\n   else {\n       Serial.println(&quot;File already exists&quot;);\n   }\n}\n\nvoid loop() {\n  mydata = random (0, 1000);\n  appendFile(&quot;\/data.txt&quot;, (String(mydata)+ &quot;\\r\\n&quot;).c_str()); \/\/Append data to the file\n  readFile(&quot;\/data.txt&quot;); \/\/ Read the contents of the file\n  delay(30000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_Write_File_LittleFS_Prevent_Overwrite.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>If you test this example, you&#8217;ll see that the file keeps all data even after a restart.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"597\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-write-data-file-prevent-overwrite.png?resize=601%2C597&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Save Data to File LittleFS Example Serial Monitor - prevent overwrite\" class=\"wp-image-134015\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-write-data-file-prevent-overwrite.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-write-data-file-prevent-overwrite.png?resize=300%2C298&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-write-data-file-prevent-overwrite.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>With this tutorial, you learned how to save data permanently on a file in the ESP8266 LittleFS filesystem. You learn how to create a file, append data, and read the contents of a file.<\/p>\n\n\n\n<p>If you have a file with content that you want to save to the ESP8266, and you don&#8217;t need to add data during runtime, you may want to use the LittleFS plugin instead. It allows you to save files that you have on your sketch folder directly to the ESP8266 filesystem: <a href=\"https:\/\/randomnerdtutorials.com\/install-esp8266-nodemcu-littlefs-arduino\/\">Install ESP8266 NodeMCU LittleFS Filesystem Uploader in Arduino IDE<\/a><\/p>\n\n\n\n<p>We hope you found this tutorial useful. Learn more about the ESP8266 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\">Firebase Web App with ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\"><strong>Free ESP8266 Projects and Tutorials<\/strong><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to write and save data permanently to a file saved on the ESP8266 filesystem (LittleFS). LittleFS is a lightweight filesystem created for microcontrollers that &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 NodeMCU: Write Data to a File (LittleFS) &#8211; Arduino IDE\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-write-data-littlefs-arduino\/#more-134008\" aria-label=\"Read more about ESP8266 NodeMCU: Write Data to a File (LittleFS) &#8211; Arduino IDE\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":134009,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[265,214,246,300,240,264],"tags":[],"class_list":["post-134008","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266-project","category-esp8266","category-esp8266-arduino-ide","category-0-esp8266","category-esp8266-projects","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/07\/ESP8266-Write-Data-To-a-File-LittleFS.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/134008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=134008"}],"version-history":[{"count":11,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/134008\/revisions"}],"predecessor-version":[{"id":136730,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/134008\/revisions\/136730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/134009"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=134008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=134008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=134008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}