Upload Files to ESP8266 - Day 21 of #100DaysOfCode in IoT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to day 21 of 100 days of code in iot challenge today i'm gonna show you how to upload a file from your computer to esp8266 let's get started i've got my visual studio code opened all the required extensions already installed actually the most important one is the platform io because that allows me to upload the sketches to esp8266 and that's actually the essential extension to be able to upload files from computer to esp8266 to the devices flash memory so yeah if you would like to see any instructions how to install required extensions i recommend going to the first video of this 100 days of code in iot challenge or if you just want to do it yourself and you just need a links you can find them in the description of the video but yeah now first step of today's challenge which is uploading the file is going to be creating a new project so i'm gonna go to quick access of platform io and open it's going to open a new tab for me and then new project i'm going to call it fs upload both that we are interested in and you want to set this value to is not mcu 1.0 framework stays arduino location stays default in my case so we have our project created the platform io.ini file should be opened automatically however if it hasn't been then it's just a matter of you know clicking on the platform.e okay the first setting that i need to set is the monitor speed we're gonna use the serial monitor today so that needs to be set and i can actually open the main.cpp straight away and in a setup initialize the serial communication by calling the begin method with this value that we set for the monitor speed and also a good idea is to set a delay at the beginning for five seconds so the ide will be able to catch up with the serial communication coming from the device so we've got this done okay and now there is another setting that we need to set and this is because we're gonna be dealing with the file system but not the legacy one the spiffs which is a default one on platform io due to the compatibility backwards compatibility reasons we're going to use little fs instead and to use little fs you need to set this setting which is board build.file system equals little fs that's gonna make our board using that file system instead of spiff's the legacy one right so having this configured what i can do is to upload the file which is pretty straightforward with platform io vs code ide because there is a command built in already that i can use however first step is to create a data directory it is important that this directory needs to be called data otherwise this is not gonna work whatever you put in data is gonna be uploaded to the board so as for an example i'm just gonna create an example file i'm just gonna call it index.html and that's just gonna be a simple empty html document with the body and a heading in the body with a text hello from iot why this is html i'm gonna explain later for now let's just stick to it let's treat it as a text because that's what we're gonna see in the serial monitor when we will be reading from it okay and now because we called it index html we know the file name so i can actually i can copy paste the code to read from this file once it's uploaded but i'm not going to write it from scratch because i've already done it and that was video 18 and i have this example here already opened and i'm just going to copy paste it so this is actually to handle bigger files as well but because i'm going to be dealing with the bigger files as well in the next example i'm just gonna use this one so it can work with both examples right so that's what i'm gonna paste but two more things that i have to do one is to include little fs and another one is to define file name for index html right it doesn't upload data directory it treats data as the root directory so now we basically um accessing the root directory another thing that is worth mentioning i think is the fact that you can actually put a data directory structure in there right so it doesn't have to be single file it can be multiple files multiple folders and they can be nested and this is all gonna be reflected on the board and with little fs especially this is gonna work well right so if i put um if for example if i put here another folder let's let's call it like um a directory d1 directory or something like that then i could access it by doing something like that right but because we are in the root directory i can just do file name index.html okay um one more thing is to handle the error if the file could not be opened for reading and then if this wasn't the case then we gonna read from the file and close it so that's essentially it for reading from the file okay so we already have the code to display the file contents in the serial monitor and um you know what i'm actually gonna deploy this first so we should see there is nothing in the board memory at the moment okay so that's deployed let me connect to the serial monitor and as we can see could not could not be opened for reading actually i should put something like could not open could not open file for reading but anyway but yeah the file doesn't exist at the moment right and to upload the file we just need to go to the platform io quick access and then in the folder nodemcu v2 platform what we need to do is to press on upload file system image this is it it's going to pick up all the files all the directories in the data directory and upload it to the board and yeah basically that was quick if the file is if the file was bigger that would have been um a longer operation right depends on the size of the file the the time of uploading depends of the size of the file okay so let me connect to the serial monitor now and i'm gonna press reset and after five seconds we should see the content contents of the file okay that's it you see so yeah that works we see the contents of the file and yeah this is essentially how you deploy the file the simplest example though because there is one thing i need to mention i need to talk about in regards to the size of the files in total right because yeah it can be single file it can be multiple files but you know the point is of the total size of all the files that you put in the data directory with the default setting this can be around one megabyte i've done few tests on my own and for the default settings it's just one megabyte i don't know why to be honest i checked the documentation on on the platform i o esp8266 if you go there um i put a link in the description if you are interested and you want to check yourself for the flash mode uh sorry that's not flash mode that's for the flash size for the flash size you can actually go to the github here and there is a list of available configurations all all of those with 4m are the ones for the our model of the board which is the esp8266 the node mcu esp12e or 12f and let me show you the default one so if you don't set anything if you don't set this value bolt build ldscript in the platform io.ini that is going to be the default one and here if you have a look how the memory located the memory allocated for spiffs this is the name for the file system is zero but i was still able to upload file so i'm not sure if that memory is shared with the sketch or it's something else but yeah my my tests essentially told me that that can be it's around one megabyte if you exceed one megabyte then um there is a there is a message um i'm gonna show you in a second uh saying that you know the the the file system is full okay yeah but anyway what i'm gonna do in a second as well is to uh change the the setting to three megabytes so we'll be able to to upload bigger files okay and uh speaking of bigger files i have prepared those these two files i'm actually more interested in the two megabyte one so two megabyte file it's just a text file with um some i can show you actually it's just a text file with some numbers but you know the point is it has two megabytes and i'm just gonna drag and drop it into the data folder right and i'm going to show you with the default configuration and attempt to upload this to the board okay obviously it failed it failed because there is no more free space with this configuration okay so if you end up with bigger file than one megabyte or you have multiple files that that that you know have in total more than one megabyte and you still want to upload them again pretty straightforward is this setting in the platform io dot ini file so let me let me go back to it it's this setting board build.ld script and that needs to be set for this one set to this one which is three megabytes right with this configuration when i press upload file system image now that is going to upload this two megabyte file as well as the index.html in the meantime i'm going to modify this code to show you that the file has been uploaded successfully but we need to wait for this process to finish auntie and then i upload the sketch and we can see if the file is uploaded so let's let's let's speed it up a little bit okay so the file uploaded and now i'm gonna deploy the sketch yeah that needs to recompile because i changed the settings in the platform io.ini file and we should see a bunch of numbers from this file in a few seconds let me connect to the serial monitor and yeah that's what we're getting right it's just a bunch of numbers but essentially that that just proves that the upload is working one more thing i could do to show you that this memory is persistent is to disconnect the board from usb and connect it back and then connect to the serial monitor and we should see the contents of the file in a second again yeah so we got it let me just terminate it because my fan in the in my laptop is spinning because of that cool so yeah this is essentially how you upload the files tomorrow i'm gonna show you how to create a web server on esp8266 and save these files you know statically so that's why index.html file was one of the the files i uploaded to the board right because tomorrow we're gonna we're gonna be doing that we're gonna be doing more of this right uploading html files maybe css as well and then serve them statically using esp8266 server class if you like my content and you want to stay up to date with my channel i suggest hitting the subscribe button so you will get notified as soon as i upload the next video that is going to happen tomorrow thanks for watching and cheers bye
Info
Channel: Tomasz Tarnowski
Views: 3,447
Rating: undefined out of 5
Keywords: how-to, tutorial, DIY, iot, esp8266, arduino, arduino control, button, micro controller, electronics, breadboard, programming, nodemcu, esp8266 project, hobby, home automation, arduino project, C++, 100DaysOfCode, platformio, vscode, visual studio code, relay, internet of things, esp8266 file system, esp8266 file, esp8266 write to file, esp8266 read from file, esp8266 read files, file system, littlefs, spiffs, esp8266 littlefs, upload files, esp8266 file upload, esp8266 file system image upload
Id: 4JWY2sQJ1rA
Channel Id: undefined
Length: 15min 24sec (924 seconds)
Published: Mon Jun 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.