{"id":6827,"date":"2020-11-16T11:21:35","date_gmt":"2020-11-16T19:21:35","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6827"},"modified":"2020-11-16T11:21:35","modified_gmt":"2020-11-16T19:21:35","slug":"accessing-spiffs-partitions-from-esp32-arduino-projects","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arduino\/esp32\/spiffs\/","title":{"rendered":"Accessing SPIFFS Partitions from ESP32 Arduino Projects"},"content":{"rendered":"<p>This tutorial shows how to add an SPIFFS data partition to an ESP32 Arduino project in order to store various resources. We will show how to add 2 sample files to the partition, program it into the FLASH memory and use the SPIFFS API to read the file contents programmatically.<\/p>\n<p>Before you begin, install VisualGDB 5.5 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open VisualGDB Arduino Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-prj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6828\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-prj.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Enter the name and location of the project you would like to create:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-name-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6829\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-name-3.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Although the ESP32 Arduino platform comes with its own SPIFFS sample that can be cloned via the VisualGDB Arduino wizard, we will instead show how to add SPIFFS support from scratch, so select &#8220;<strong>Create a new &#8216;Blinking LED&#8217; project<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-new.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6830\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-new.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>On the next page pick your ESP32 board. It is also recommended to select the COM port where the target prints the output:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6843\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/target.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Finally, select debugging settings that work with your target and press &#8220;Finish&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6844\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/debug.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>VisualGDB will create a basic <strong>LEDBlink<\/strong> sketch. Erase the contents of <strong>setup()<\/strong> and <strong>loop()<\/strong> functions and include the <strong>&lt;SPIFFS.h&gt;<\/strong> file:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-spiffs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6834\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-spiffs.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Locate the <strong>SPIFFS Partition<\/strong> node in Solution Explorer. Right-click on it and select &#8220;<strong>Add-&gt;New Item<\/strong>&#8220;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-add1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6845\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-add1-1.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Enter &#8220;file1.txt&#8221; as the file name: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-file1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6836\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-file1.png\" alt=\"\" width=\"366\" height=\"142\" \/><\/a><\/li>\n<li>VisualGDB will create a new file placing it in the &#8220;<strong>&lt;Project Directory&gt;\\data<\/strong>&#8221; folder. Open it via Solution Explorer and add a few lines to it:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-lines.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6837\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-lines.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Similarly, create a <strong>file2.txt<\/strong> file containing different lines: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-lines2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6838\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-lines2.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Now we will show how to read the SPIFFS contents programmatically. First of all, make sure that the COM port is set correctly on the <strong>Arduino Project<\/strong> page of VisualGDB Project Properties:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-port.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6839\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-port.png\" alt=\"\" width=\"1078\" height=\"753\" \/><\/a><\/li>\n<li>Then enable the Arduino Serial Terminal as shown below: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-term.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6840\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-term.png\" alt=\"\" width=\"1078\" height=\"733\" \/><\/a><\/li>\n<li>Finally, add the following code inside the <strong>setup()<\/strong> function:\n<pre class=\"\">    Serial.begin(115200);\r\n    if (!SPIFFS.begin(false))\r\n    {\r\n        Serial.println(\"SPIFFS Mount Failed\");\r\n        return;\r\n    }\r\n\r\n    File rootDir = SPIFFS.open(\"\/\");\r\n    if (rootDir)\r\n    {\r\n        for (File file = rootDir.openNextFile(); file; file = rootDir.openNextFile())\r\n        {\r\n            Serial.printf(\"[%s (%d bytes)]\\r\\n\", file.name(), file.size());\r\n            while (file.available())\r\n            {\r\n                String str = file.readStringUntil('\\n');\r\n                Serial.printf(\"\\t%s\\r\\n\", str.c_str());\r\n            }\r\n        }\r\n    }<\/pre>\n<p>The code mounts the SPIFFS partition and then iterates over all files, printing their contents line-by-line.<\/li>\n<li>Set a breakpoint on the <strong>file.available()<\/strong> call and press F5 to start debugging. The breakpoint will trigger on the first file:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6841\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-bkpt.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>You can step through the sketch, or let it run till the end to observe the complete listing of all files:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-complete.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6842\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-complete.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Now we will show how to program the SPIFFS partition separately from the program itself. Change some of the lines in <strong>file2.txt<\/strong> so that we could observe the differences: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/edited.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6846\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/edited.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Right-click on the <strong>SPIFFS Partition<\/strong> in Solution Explorer and select &#8220;<strong>Program FLASH Memory<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/program-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6847\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/program-1.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>VisualGDB will automatically update and program the SPIFFS image, leaving the program itself unchanged:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/progress.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6848\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/progress.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Open the serial terminal without starting another debug session via a context menu command on the project node:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-openterm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6849\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-openterm.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Although we have not rebuilt or reprogrammed the program itself, the output will now show the second version of the file that was successfully programmed into the FLASH memory:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-term.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6850\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-term.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to add an SPIFFS data partition to an ESP32 Arduino project in order to store various<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[170,138,225],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6827"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=6827"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6827\/revisions"}],"predecessor-version":[{"id":6851,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6827\/revisions\/6851"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}