In this and in the next lecture, I'm going to show you how you can work with text files on the flash
based file system of the ESB 32, the HP 32 has about one and a half megabytes of available space on
its flash base file system, as I said.
And you can use that space to store programs, but also to store files in those files can contain sensor
data.
Perhaps he can use this capability as a sensor data logging feature, but you can also use it to store
other text files that perhaps contain configuration.
You have already done something like this in the past in the section on Wi-Fi where you stored the Wi-Fi
network's credentials in a text adjacent file.
In this lecture and in this example here, I'm going to first show you how you can create a CSP file.
That is a text file that contains multiple rows where each wrote stores data separated by a comma.
And then in the next lecture, I'll show you how to read out that data.
So once you have a data file like that on your to stored on its file system, then you can use a tool
like Thorney to export it and then be able to manipulate it.
See with a spreadsheet perhaps of some of the text processing program on your computer.
Or you can read out data from a text file and transmit them to an online service.
It could be, for example, Google spreadsheet for of device storage.
So let's begin this example by looking at the first script in which I demonstrate how you can create
actually file.
This script actually contains two parts.
And I'm going to talk a little bit more about this in a moment.
The first part has to do with statistics.
So there's a few tools that Microplace then provides that allow you to do things such as find out how
much space total space is available on the petition that you are able to use to steal files.
But you can also go down to the level of the individual file and find out how many bytes it contains.
And those tools together allow you to keep track of the size of, in this case, a text file so you
don't run out of space.
And this is something that you want to manage either through your script, keeping a tally of how big
a file gets or periodically checking to see if a storage file facility file becomes too big and then
just reset it to copy its data across the computer and to delete it from the device.
Because remember, again, the ESB 32 flash is about to make it right in space, but is used for other
things as well, including your programs.
So you end up with about a bit more than a megabyte.
So that's what happens here.
We get a few statistics and actually I'm going to play around with these functions as well on the shell.
And then in the second part, so line 54 onwards is where the actual writing of dummy data.
See, I'm using the random function here.
We are adding some dummy data onto this file in a committed, limited format.
In terms of the things that we need to consider, I'm reporting random can generate some random data
so we don't have to complicate the so good with sensors then importing the petition from the E.S.P three
module so I can just demonstrate how we can call this function in particular petition info, which gives
us a variety of information about the petition that we are using to write our file in.
And I'm also using the E.S.P module so that I can demonstrate how to use functions such as E.S.P flash
size excuse me, the total available size of the 32, but also micro OS.
So this is the micro python version of the OS module that comes with C Python, which allows us to grab
statistical information about a specific file.
So this is actually the function that is most useful out of all of the functions that I mentioned so
far, because this function specifically allows me to get information about how many bytes are stored
within the file that I'm working with this example and I'm just going to add to the extension because
that is important.
All right.
Now, in terms of actually writing the data in this text file, I'm going to start with just creating
a variable that holds the name of the file.
And then I'm going inside a loop here.
I just want to write ten new rows that contain dummy data or create some random numbers supposably for
the temperature, humidity and atmospheric pressure.
Then I'm using the open function, and that's the same a function that is integrated into micro python.
We don't need to import a particular library at this function.
So passing the filename across is the first parameter and the second parameter is a modifier that indicates
what kind of access we want to have to this file.
I'm using a here to indicate that I want to append if I use W and every time that the program runs,
it would replace the existing content and write new content would overwrite data.
In this example, I just want to append data and notice here I'm looking at the Micra Python device
file system and this file does not exist yet.
So because it doesn't exist yet, the A modifier will create it.
And from that point onwards, it will continue to append data to the end of this file sort of operating.
I've got information about the various types of money fires are going to blow up here, so if you go
to the python open function.
And at the market, python, but the normal python c python function, because that's where all the
information is it C I believe it's this one here and these are the modifiers.
So I'm using a Pente for this example, but now you know what the others do as well.
All right.
Next.
I'm creating another variable here.
This is just their variable contains string, and I'm using the format function that allows me to have
in between double quotes of code and the curly bracket notation, which are simply placeholders.
Then I can have whatever other text, the one in between those placeholders in in this case, I'm creating
a record for my seat to be filled.
So all of the values that I am storing this single record which use a single line, needs to be separated
or need to be separated by commas.
And in the end, I've got the new line special character so that I've got a new line in the end of this
record.
And then as parameters for the format function, I've got the three random numbers, a temperature and
humidity and the pressure.
So as you can probably guess, this individual numerical values will be placed inside those placeholders.
And you see this in action in a moment.
I also have that string that I want to store in this new text file.
I can use the right function to write the new line at the end or pended in the end of the text file.
And every time that I finish and you write a much closer file to make sure that it's not damaged in
any way.
So open the file.
Create the thing that I want to write the text string and write the text at the end of the file and
then close the file.
And in the end, I'm also grabbing the statistics for the file.
Again, I'm calling again the stat command or the stat function in the micro OS module.
Partly filename is filename appear.
I get back a table.
This table contains a number of statistics and one of the statistics say in index position six is the
number of bytes that are now contained in the file.
So I'm doing the same thing up here.
I'm getting the total number of bytes stored in the file before I do the right and the right.
And then they take the same statistics again to see what the effect on the size of the file has been.
Again, to know more about the stat function.
You can have a look at these locations, documentation there.
All right, I'm going to go ahead and run this file and notice that here we will be seeing a new text
file up here and we'll have a look at the statistics have come out.
So the script could have dug perhaps in line 50.
Let's see what's happening here.
So I'll see what happened.
I tried to get statistics for a file that does not exist.
So what I'll do the first time that I run this script, I will just come to the Senate.
I can write a bit of code here to check that the file exists before I get its statistics.
But for the sake of this example, I'm just going to come and down these lines for now.
Play again.
Very cool.
All right.
Now let's see.
New file should appear.
Need to refresh.
They go to his text file and it looks like he's I've got 10 rows of sample data through dummy data.
Now let's run this program again.
But before I do so, I will uncommented the stats lines.
And just remember that we are going to spend so can have more data here, another 10 Roshe would appear.
To play.
All right, so size before the new ride was one hundred and four bytes, after it was 206 six points
and can close this file and reopen it.
So there there's the twenty nine in total.
So let's have a look at a couple of examples here.
Again, I'm going to import Mike Rowe is the model and I'm going to import this line here for the file
name.
And.
Can you grab the statistics from here and have a look at the full stats available so there's a couple
there's a bunch of things that also appear here, but the one that I'm interested in is this.
This is the only one that makes sense for the purposes of this example.
This how big is the file?
And that is in bytes.
This is an opposition index six so zero one, two, three, four, five, six.
And I can use a.
Command like this to get troops, to get the number that I need out so you can imagine if you are interested
in keeping track of how big this file is, then you could have a bit of logic here to let you know if
perhaps the file grows to be on a couple of thousand bytes.
And that would be an indication that you need to empty the file, copy the data across to your computer
and clear the contents of the text file.
All right.
Now, other things import the E.S.P module.
And to have a look at this function, flash size, and this gives you the total flash size, a bit about
four megabytes, but of course, you're not able to use this full amount to store programs or text files
instead.
He micropayment confines you to the running partition, so I've just imported a partition module and
then I can create a partition object.
Some passing the petition, but running constant here to the petition constructor.
So let's have a closer look at the documentation for the petition module.
So according to the micro python documentation, I'm now looking into the SB eight to six six and SB
two functions.
And down here you'll see that we've the constants and the four types of petitions that are available
on running type app and type data.
And the one that I'm looking at here is running.
This is where we can actually store data is managed by our program.
So the size of the running petition, we can print it out.
By calling the info function and that printout out another couple and you can see that its size, this
number here indicates that a megabyte and and a half of the available space in the running petition
anyway, all those numbers are not really useful.
The one that I think is mostly useful for us.
Are file specific statistics that are available through the micro OS module.
All right, so now we've got our text file and we've got a comma delimited there are stored in our text
file.
So let's move on to the next lecture where I'll show you how to read it.