1
00:00:00,690 --> 00:00:07,710
In this and in the next lecture, I'm going to show you how you can work with text files on the flash

2
00:00:07,710 --> 00:00:18,840
based file system of the ESB 32, the HP 32 has about one and a half megabytes of available space on

3
00:00:18,840 --> 00:00:21,390
its flash base file system, as I said.

4
00:00:21,660 --> 00:00:30,690
And you can use that space to store programs, but also to store files in those files can contain sensor

5
00:00:30,690 --> 00:00:30,930
data.

6
00:00:30,930 --> 00:00:39,750
Perhaps he can use this capability as a sensor data logging feature, but you can also use it to store

7
00:00:39,870 --> 00:00:42,990
other text files that perhaps contain configuration.

8
00:00:43,320 --> 00:00:50,970
You have already done something like this in the past in the section on Wi-Fi where you stored the Wi-Fi

9
00:00:50,970 --> 00:00:54,630
network's credentials in a text adjacent file.

10
00:00:55,230 --> 00:01:02,550
In this lecture and in this example here, I'm going to first show you how you can create a CSP file.

11
00:01:02,550 --> 00:01:10,680
That is a text file that contains multiple rows where each wrote stores data separated by a comma.

12
00:01:10,860 --> 00:01:15,430
And then in the next lecture, I'll show you how to read out that data.

13
00:01:15,900 --> 00:01:25,260
So once you have a data file like that on your to stored on its file system, then you can use a tool

14
00:01:25,260 --> 00:01:31,800
like Thorney to export it and then be able to manipulate it.

15
00:01:31,800 --> 00:01:37,170
See with a spreadsheet perhaps of some of the text processing program on your computer.

16
00:01:37,410 --> 00:01:43,290
Or you can read out data from a text file and transmit them to an online service.

17
00:01:43,410 --> 00:01:48,900
It could be, for example, Google spreadsheet for of device storage.

18
00:01:50,010 --> 00:01:57,990
So let's begin this example by looking at the first script in which I demonstrate how you can create

19
00:01:57,990 --> 00:01:59,070
actually file.

20
00:02:00,290 --> 00:02:02,850
This script actually contains two parts.

21
00:02:02,880 --> 00:02:05,460
And I'm going to talk a little bit more about this in a moment.

22
00:02:05,910 --> 00:02:09,690
The first part has to do with statistics.

23
00:02:09,690 --> 00:02:17,040
So there's a few tools that Microplace then provides that allow you to do things such as find out how

24
00:02:17,040 --> 00:02:24,390
much space total space is available on the petition that you are able to use to steal files.

25
00:02:24,780 --> 00:02:31,440
But you can also go down to the level of the individual file and find out how many bytes it contains.

26
00:02:31,890 --> 00:02:37,530
And those tools together allow you to keep track of the size of, in this case, a text file so you

27
00:02:37,530 --> 00:02:38,630
don't run out of space.

28
00:02:39,390 --> 00:02:46,320
And this is something that you want to manage either through your script, keeping a tally of how big

29
00:02:46,320 --> 00:02:55,050
a file gets or periodically checking to see if a storage file facility file becomes too big and then

30
00:02:56,130 --> 00:03:01,860
just reset it to copy its data across the computer and to delete it from the device.

31
00:03:01,860 --> 00:03:09,000
Because remember, again, the ESB 32 flash is about to make it right in space, but is used for other

32
00:03:09,000 --> 00:03:11,250
things as well, including your programs.

33
00:03:11,610 --> 00:03:14,940
So you end up with about a bit more than a megabyte.

34
00:03:16,050 --> 00:03:17,000
So that's what happens here.

35
00:03:17,010 --> 00:03:22,290
We get a few statistics and actually I'm going to play around with these functions as well on the shell.

36
00:03:22,770 --> 00:03:30,450
And then in the second part, so line 54 onwards is where the actual writing of dummy data.

37
00:03:30,630 --> 00:03:32,340
See, I'm using the random function here.

38
00:03:32,760 --> 00:03:38,960
We are adding some dummy data onto this file in a committed, limited format.

39
00:03:40,750 --> 00:03:45,910
In terms of the things that we need to consider, I'm reporting random can generate some random data

40
00:03:45,910 --> 00:03:54,220
so we don't have to complicate the so good with sensors then importing the petition from the E.S.P three

41
00:03:54,430 --> 00:04:01,660
module so I can just demonstrate how we can call this function in particular petition info, which gives

42
00:04:01,660 --> 00:04:07,090
us a variety of information about the petition that we are using to write our file in.

43
00:04:07,600 --> 00:04:15,640
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

44
00:04:15,640 --> 00:04:25,570
size excuse me, the total available size of the 32, but also micro OS.

45
00:04:25,720 --> 00:04:33,970
So this is the micro python version of the OS module that comes with C Python, which allows us to grab

46
00:04:33,970 --> 00:04:37,090
statistical information about a specific file.

47
00:04:37,420 --> 00:04:43,000
So this is actually the function that is most useful out of all of the functions that I mentioned so

48
00:04:43,000 --> 00:04:49,840
far, because this function specifically allows me to get information about how many bytes are stored

49
00:04:49,840 --> 00:04:56,950
within the file that I'm working with this example and I'm just going to add to the extension because

50
00:04:56,950 --> 00:04:57,790
that is important.

51
00:04:58,360 --> 00:04:58,900
All right.

52
00:05:00,580 --> 00:05:07,780
Now, in terms of actually writing the data in this text file, I'm going to start with just creating

53
00:05:08,200 --> 00:05:11,110
a variable that holds the name of the file.

54
00:05:11,470 --> 00:05:15,400
And then I'm going inside a loop here.

55
00:05:15,550 --> 00:05:23,860
I just want to write ten new rows that contain dummy data or create some random numbers supposably for

56
00:05:23,860 --> 00:05:26,330
the temperature, humidity and atmospheric pressure.

57
00:05:26,680 --> 00:05:33,380
Then I'm using the open function, and that's the same a function that is integrated into micro python.

58
00:05:33,400 --> 00:05:37,680
We don't need to import a particular library at this function.

59
00:05:38,140 --> 00:05:47,260
So passing the filename across is the first parameter and the second parameter is a modifier that indicates

60
00:05:47,590 --> 00:05:50,550
what kind of access we want to have to this file.

61
00:05:51,010 --> 00:05:58,000
I'm using a here to indicate that I want to append if I use W and every time that the program runs,

62
00:05:58,000 --> 00:06:03,400
it would replace the existing content and write new content would overwrite data.

63
00:06:03,910 --> 00:06:10,910
In this example, I just want to append data and notice here I'm looking at the Micra Python device

64
00:06:10,960 --> 00:06:14,740
file system and this file does not exist yet.

65
00:06:15,490 --> 00:06:20,410
So because it doesn't exist yet, the A modifier will create it.

66
00:06:20,440 --> 00:06:26,530
And from that point onwards, it will continue to append data to the end of this file sort of operating.

67
00:06:28,710 --> 00:06:33,420
I've got information about the various types of money fires are going to blow up here, so if you go

68
00:06:33,420 --> 00:06:37,140
to the python open function.

69
00:06:38,260 --> 00:06:44,590
And at the market, python, but the normal python c python function, because that's where all the

70
00:06:44,590 --> 00:06:49,860
information is it C I believe it's this one here and these are the modifiers.

71
00:06:50,230 --> 00:06:54,910
So I'm using a Pente for this example, but now you know what the others do as well.

72
00:06:56,290 --> 00:06:56,890
All right.

73
00:06:57,910 --> 00:06:58,540
Next.

74
00:07:00,010 --> 00:07:01,880
I'm creating another variable here.

75
00:07:01,900 --> 00:07:09,640
This is just their variable contains string, and I'm using the format function that allows me to have

76
00:07:10,000 --> 00:07:17,760
in between double quotes of code and the curly bracket notation, which are simply placeholders.

77
00:07:18,520 --> 00:07:24,640
Then I can have whatever other text, the one in between those placeholders in in this case, I'm creating

78
00:07:24,640 --> 00:07:27,730
a record for my seat to be filled.

79
00:07:27,770 --> 00:07:34,360
So all of the values that I am storing this single record which use a single line, needs to be separated

80
00:07:34,360 --> 00:07:36,070
or need to be separated by commas.

81
00:07:36,370 --> 00:07:44,530
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

82
00:07:44,530 --> 00:07:45,010
record.

83
00:07:45,580 --> 00:07:52,870
And then as parameters for the format function, I've got the three random numbers, a temperature and

84
00:07:52,870 --> 00:07:55,550
humidity and the pressure.

85
00:07:56,020 --> 00:08:04,480
So as you can probably guess, this individual numerical values will be placed inside those placeholders.

86
00:08:05,140 --> 00:08:06,940
And you see this in action in a moment.

87
00:08:07,450 --> 00:08:10,930
I also have that string that I want to store in this new text file.

88
00:08:11,950 --> 00:08:19,290
I can use the right function to write the new line at the end or pended in the end of the text file.

89
00:08:19,960 --> 00:08:25,900
And every time that I finish and you write a much closer file to make sure that it's not damaged in

90
00:08:25,900 --> 00:08:26,310
any way.

91
00:08:26,320 --> 00:08:28,090
So open the file.

92
00:08:29,490 --> 00:08:35,460
Create the thing that I want to write the text string and write the text at the end of the file and

93
00:08:35,460 --> 00:08:36,510
then close the file.

94
00:08:37,230 --> 00:08:40,910
And in the end, I'm also grabbing the statistics for the file.

95
00:08:40,920 --> 00:08:48,780
Again, I'm calling again the stat command or the stat function in the micro OS module.

96
00:08:49,080 --> 00:08:51,630
Partly filename is filename appear.

97
00:08:52,110 --> 00:08:54,420
I get back a table.

98
00:08:54,740 --> 00:09:02,940
This table contains a number of statistics and one of the statistics say in index position six is the

99
00:09:02,940 --> 00:09:05,880
number of bytes that are now contained in the file.

100
00:09:06,240 --> 00:09:08,120
So I'm doing the same thing up here.

101
00:09:08,280 --> 00:09:13,380
I'm getting the total number of bytes stored in the file before I do the right and the right.

102
00:09:13,380 --> 00:09:19,770
And then they take the same statistics again to see what the effect on the size of the file has been.

103
00:09:20,700 --> 00:09:26,070
Again, to know more about the stat function.

104
00:09:26,550 --> 00:09:31,980
You can have a look at these locations, documentation there.

105
00:09:34,160 --> 00:09:43,670
All right, I'm going to go ahead and run this file and notice that here we will be seeing a new text

106
00:09:43,670 --> 00:09:46,880
file up here and we'll have a look at the statistics have come out.

107
00:09:48,200 --> 00:09:52,530
So the script could have dug perhaps in line 50.

108
00:09:52,550 --> 00:09:53,960
Let's see what's happening here.

109
00:09:54,000 --> 00:09:57,260
So I'll see what happened.

110
00:09:58,020 --> 00:10:01,120
I tried to get statistics for a file that does not exist.

111
00:10:01,790 --> 00:10:10,250
So what I'll do the first time that I run this script, I will just come to the Senate.

112
00:10:10,970 --> 00:10:18,650
I can write a bit of code here to check that the file exists before I get its statistics.

113
00:10:19,340 --> 00:10:22,940
But for the sake of this example, I'm just going to come and down these lines for now.

114
00:10:23,600 --> 00:10:24,260
Play again.

115
00:10:24,710 --> 00:10:25,340
Very cool.

116
00:10:26,090 --> 00:10:26,510
All right.

117
00:10:26,510 --> 00:10:28,690
Now let's see.

118
00:10:28,850 --> 00:10:30,470
New file should appear.

119
00:10:32,850 --> 00:10:34,080
Need to refresh.

120
00:10:35,030 --> 00:10:43,670
They go to his text file and it looks like he's I've got 10 rows of sample data through dummy data.

121
00:10:44,720 --> 00:10:47,870
Now let's run this program again.

122
00:10:47,880 --> 00:10:53,990
But before I do so, I will uncommented the stats lines.

123
00:10:57,280 --> 00:11:03,640
And just remember that we are going to spend so can have more data here, another 10 Roshe would appear.

124
00:11:04,740 --> 00:11:05,400
To play.

125
00:11:06,900 --> 00:11:14,010
All right, so size before the new ride was one hundred and four bytes, after it was 206 six points

126
00:11:14,580 --> 00:11:17,270
and can close this file and reopen it.

127
00:11:18,060 --> 00:11:22,500
So there there's the twenty nine in total.

128
00:11:25,360 --> 00:11:28,870
So let's have a look at a couple of examples here.

129
00:11:28,900 --> 00:11:37,630
Again, I'm going to import Mike Rowe is the model and I'm going to import this line here for the file

130
00:11:37,630 --> 00:11:38,080
name.

131
00:11:39,410 --> 00:11:40,250
And.

132
00:11:44,020 --> 00:11:54,910
Can you grab the statistics from here and have a look at the full stats available so there's a couple

133
00:11:55,240 --> 00:12:00,790
there's a bunch of things that also appear here, but the one that I'm interested in is this.

134
00:12:00,790 --> 00:12:04,650
This is the only one that makes sense for the purposes of this example.

135
00:12:04,690 --> 00:12:06,400
This how big is the file?

136
00:12:06,400 --> 00:12:07,790
And that is in bytes.

137
00:12:08,020 --> 00:12:14,380
This is an opposition index six so zero one, two, three, four, five, six.

138
00:12:14,830 --> 00:12:17,350
And I can use a.

139
00:12:18,910 --> 00:12:28,480
Command like this to get troops, to get the number that I need out so you can imagine if you are interested

140
00:12:28,480 --> 00:12:35,510
in keeping track of how big this file is, then you could have a bit of logic here to let you know if

141
00:12:35,530 --> 00:12:38,950
perhaps the file grows to be on a couple of thousand bytes.

142
00:12:39,110 --> 00:12:45,940
And that would be an indication that you need to empty the file, copy the data across to your computer

143
00:12:45,940 --> 00:12:49,820
and clear the contents of the text file.

144
00:12:51,190 --> 00:12:51,690
All right.

145
00:12:51,790 --> 00:12:55,930
Now, other things import the E.S.P module.

146
00:12:59,050 --> 00:13:10,930
And to have a look at this function, flash size, and this gives you the total flash size, a bit about

147
00:13:11,710 --> 00:13:18,810
four megabytes, but of course, you're not able to use this full amount to store programs or text files

148
00:13:19,480 --> 00:13:20,290
instead.

149
00:13:22,950 --> 00:13:30,110
He micropayment confines you to the running partition, so I've just imported a partition module and

150
00:13:30,110 --> 00:13:33,290
then I can create a partition object.

151
00:13:34,930 --> 00:13:42,410
Some passing the petition, but running constant here to the petition constructor.

152
00:13:43,000 --> 00:13:46,850
So let's have a closer look at the documentation for the petition module.

153
00:13:47,800 --> 00:13:55,000
So according to the micro python documentation, I'm now looking into the SB eight to six six and SB

154
00:13:55,330 --> 00:13:57,370
two functions.

155
00:13:57,760 --> 00:14:04,390
And down here you'll see that we've the constants and the four types of petitions that are available

156
00:14:04,390 --> 00:14:07,180
on running type app and type data.

157
00:14:07,540 --> 00:14:10,450
And the one that I'm looking at here is running.

158
00:14:10,660 --> 00:14:16,140
This is where we can actually store data is managed by our program.

159
00:14:17,170 --> 00:14:22,150
So the size of the running petition, we can print it out.

160
00:14:23,880 --> 00:14:31,800
By calling the info function and that printout out another couple and you can see that its size, this

161
00:14:31,800 --> 00:14:40,800
number here indicates that a megabyte and and a half of the available space in the running petition

162
00:14:41,820 --> 00:14:43,750
anyway, all those numbers are not really useful.

163
00:14:43,770 --> 00:14:46,710
The one that I think is mostly useful for us.

164
00:14:47,790 --> 00:14:53,990
Are file specific statistics that are available through the micro OS module.

165
00:14:56,200 --> 00:15:03,610
All right, so now we've got our text file and we've got a comma delimited there are stored in our text

166
00:15:03,610 --> 00:15:04,090
file.

167
00:15:04,480 --> 00:15:08,560
So let's move on to the next lecture where I'll show you how to read it.
