ESP32 Save Data To SPIFFS Flash Memory w/ Arduino (Mac OSX and Windows) | File System Storage

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm cisco and in today's episode we're going to talk about how to write and read data to and from the spi flash file system of an esp32 specifically i'm going to go over all the steps that are needed for reading and writing data on the flash storage of an esp32 that's formatted in json alright let's do this [Music] this video is sponsored by pcb way they are a pcb manufacturing and assembly house and one thing that sets them apart is that they're not a broker instead they're a fully feature pcb manufacturing and assembly house offering a wide variety of services those include advanced pcbs like flex and rigid flex and also assembly and design of your printed circuit boards with friendly staff and great facilities i highly recommend pcb wave for the design manufacturing and assembly of your printed circuit boards you might be wondering what's so useful about this spi flash file system whether we're working with sensors and storing data over long periods of time or trying to store pieces of information like our wi-fi password it's quite common for us to need to read or write data that's able to survive even in the absence of power to the microcontroller or system on chip so in the case of the esp socs in particular the esp-32 they ship with flash storage precisely for this purpose we can access the flash file system in a few different ways the most common one is to add an option to the arduino ide that allows us to select it and load specific files to it the process is fairly straightforward we need to go to a github repository for downloading the plugin note that the repository is actually maintained by the folks behind the esp32 the espressif company and the plugin itself can be found if we click on the right hand side of the page where it says releases i'll go ahead and download the latest one then i'll unzip it and in order to determine where i need to place this folder i'll go ahead and open the arduino ide open up the preferences and make a note of the sketchbook location i'll need to access that location in my operating system create a directory if it doesn't exist that's called tools and place the entire downloaded folder in there [Music] once that's done i can go ahead and restart the arduino ide and see that there's a new option available under the tools menu it should read esp32 sketch data upload with the plugin installed i'll now create a new sketch that'll name esp32 underscore spi ffs the first thing i'll do is include the spi ffs library which comes installed with the esp32 packages i'll then go ahead and call its begin method with true as a parameter and check that the communication between the esp32 and the flash memory chip initializes correctly i'll print out a message if that's not the case and idle indefinitely so i can solve the problem that also reminds me that i need to initialize the serial communication which i'll do at 115 200 baud rate the first task that i'll set to do is to list all the existing files in the flash file system i'll simply iterate over all the contents of the flash file system and print them out over serial i'll finish by closing the access to the files using the close method of the file objects i can take this code for a test drive so i'll use the tools menu to select the correct board and port and i'll hit upload notice that this will only upload the code up to the esp32 as i haven't used the flash file system of this chip before the contents should be empty with the upload finish i can double check that this is the case by hitting the reset button and seeing that nothing is printed on the serial monitor so now in order to store files in that flash file system i'll need to create a directory that's called data inside the folder where i'm storing my sketch inside the data folder i'll just create an empty file that i'll name file.txt then go back to the arduino ide and use the option that's enabled by us installing the plugin one thing to note is that i don't need to re-upload the firmware which is already running on the esp32 i can confirm this by resetting the board once again i'm seeing in the serial monitor that the contents of the flash file system now include this empty file i'll take a moment to note that a common gotcha for using the flash file system is leaving the serial monitor open while trying to use that option so make sure it's closed and you will have no problem so now that we know how to list files from the flash file system let's go ahead and try to read data from it i'll go ahead and move the code for listing files to a user defined function that i'll name list all files the next thing i'll demonstrate is how to read data that's already stored in the flash file system for this i'll go ahead and create a new file that'll name file.json i'll use the spi ffs library to open it in the arduino side and go to the operating system to create the file and the contents will be fairly straightforward a json formatted string with a key data and the value hello spi ffs i'll use the plugin to upload it to the spi flash attached to the esp32 and to work with json formatted data as i've done in other videos i'll install the arduino json library [Music] to deserialize the data i'll follow the common procedure which includes creating a static json document calling the serialize json method with the document and the file and then parsing the data as we wrote the file we know that it's going to return a string and the key name is data lastly to make sure that everything went correctly i'll print it out to the serial monitor and i'll finish up by closing the file remembering to include the arduino json library i can go ahead and upload the code one thing to notice that when we upload the new firmware it doesn't delete the files that were already in the flash file system which is similar to when we uploaded those new files it didn't affect the firmware that was already on the esp32 so if everything goes according to plan when the new firmware is uploaded we should see in the serial monitor the contents of the new file printed out similar to what i did with the file listing code i'll go ahead and move the data reading code to its own user defined function i'll call it read data from file the last thing i want to go over is how to create a file and store it in the flash file system within the code itself as we often want to organize the data that we're storing i'll go ahead and use the same arduino json formatting that i used before to create the file i'll need to call the open method of the spi ffs library using the parameter w for write i'll then create another static json document and use the serialized json format for easily storing the json formatted data inside the file as always i'll remember to close access to the file and in this case it's a little more important so that the contents are successfully written to the file for now i'll simply check the debug message to see if the contents were written or not once i verified that that's the case i can go ahead and combine it with the previous method for reading data from the file i'll just make a few adjustments so that i can pass the name of the file that i want to read and also match what's being read to the actual contents of the file particularly the key that we're using in the new data once those changes are made i can go ahead and upload the code and if i don't have any typos i should see the file being written by the code itself then the contents of the flash file system being printed out to the serial monitor followed by the contents of that specific file also being read and printed out to the serial monitor as i did before i'll tidy things up by moving this block of code to its own user defined function i'll name it write data to file and it'll take a parameter which is the name of the file that i want to create so there you have it we've learned how to use the spi flash file system to access the spi flash that's attached to most esp32 boards this is especially important in a number of applications some of which you can see in the other videos on my channel if you like my videos and want to see more i invite you to my patreon page where you can chip in a bunker too that really helps me putting more time into the videos and release them quicker but whatever you do don't forget to like subscribe or leave me a comment you can find me also on social media on twitter on instagram or even the community page of this channel thank you so much for watching my videos and i will see you next time
Info
Channel: datasith
Views: 18,496
Rating: undefined out of 5
Keywords: ESP32, ESP8266, Arduino, IDE, NodeMcu, Tutorials, Makers, Learn, Guide, IoT, Internet Of Things, Raspberry Pi, Wireless, Communication, Weather, Creative, Sensor, Python, Programming, C++, Beginner, Electronics, DIY, MicroPython, 3d Printing, CAD, Fusion 360, Education, Tutorial, Intro, Introduction, Web, Developer, HTML, Datalogger, Project, Sensors, Data, Visualization, Webserver, Server, Client, App, WebApp, Wemos, Robot, Robotics, Robots, Easy, Adafruit, Sparkfun, OS X, Windows, Linux, Embedded, Kickstarter, RC, Fun, Build, Engineering
Id: 6KABU-I2sGM
Channel Id: undefined
Length: 9min 36sec (576 seconds)
Published: Sun Aug 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.