1
00:00:00,720 --> 00:00:02,830
I will come back in this lecture.

2
00:00:02,850 --> 00:00:11,040
I'll show you how to do a simple request that goes out onto the Web, grabs the content of a text file

3
00:00:11,040 --> 00:00:18,030
stored in a publicly accessible Eurorail, and then presents the contents of this text file onto the

4
00:00:18,030 --> 00:00:18,350
show.

5
00:00:19,140 --> 00:00:26,170
So here's the example sketch that I've prepared to demonstrate how to do a simple request.

6
00:00:26,880 --> 00:00:34,290
This script uses the network requests and Jason modules, which I described in the previous lecture.

7
00:00:34,620 --> 00:00:41,460
On top of them, I'm also using the USIS module, which allows me to do something like this down here.

8
00:00:41,460 --> 00:00:49,500
And basically, if my ISP 32 fails to connect to my local Wi-Fi network, it will just call the exit

9
00:00:49,680 --> 00:00:58,020
method that belongs to the user's module and stop the execution of the program and then I'll get back

10
00:00:58,020 --> 00:00:58,750
to the show.

11
00:00:59,640 --> 00:01:03,000
So that's the only thing that I've added to this script.

12
00:01:03,000 --> 00:01:09,930
In addition to the modules that are described in the previous lecture before we have a look at the actual

13
00:01:09,930 --> 00:01:16,680
code, I just want to show you that I have created a simple text file, this text file here, which

14
00:01:16,680 --> 00:01:18,390
is available on my website.

15
00:01:18,920 --> 00:01:25,200
Its name is Hemorrhoidal takes the and just a couple of lines of text here that I'm going to fetch.

16
00:01:26,190 --> 00:01:30,480
I have stored this file on my Web server.

17
00:01:30,480 --> 00:01:37,860
As I said, this is its name and I'm able to use them to make modifications.

18
00:01:38,460 --> 00:01:40,530
So this is my interface.

19
00:01:40,530 --> 00:01:47,280
I'm going to make a change to this in a moment and see how that change affects what the script presents

20
00:01:47,280 --> 00:01:48,120
on the shell.

21
00:01:48,420 --> 00:01:55,440
In your case, if you don't have a Web server, you can just place this file on any publicly accessible

22
00:01:56,310 --> 00:01:57,000
location.

23
00:01:57,000 --> 00:02:04,050
Jerrell, it could be could be perhaps even a Dropbox Eurorail or a Google drive.

24
00:02:04,050 --> 00:02:12,720
Your real just tried to make the target file as small as possible because it's got too many characters

25
00:02:12,720 --> 00:02:13,230
in it.

26
00:02:13,230 --> 00:02:19,140
It will most likely exceed the memory of arem limitations of your HP thirty two.

27
00:02:19,140 --> 00:02:21,710
And that will cause the script to crash.

28
00:02:22,050 --> 00:02:26,610
So just remember that you're easiest to do, has a limited amount of ram and therefore you won't be

29
00:02:26,610 --> 00:02:37,650
able to fetch large files from the web that will exceed that limited memory space in its frame.

30
00:02:38,080 --> 00:02:42,480
Again, that will cause the E.S.P to in your script to crash.

31
00:02:43,110 --> 00:02:43,470
All right.

32
00:02:43,470 --> 00:02:45,030
So I've got my recommendation here.

33
00:02:45,030 --> 00:02:48,210
You can check it out for more details and.

34
00:02:49,700 --> 00:02:57,000
Let's have a look at what is going on now, so in line 40 to the first thing to do is to use the with

35
00:02:57,050 --> 00:02:58,630
open structure.

36
00:03:00,630 --> 00:03:06,040
To open up this Jason file, which is this Jason file right here.

37
00:03:06,070 --> 00:03:12,840
So if you never work with Jason file before, then be assured there's nothing difficult about them.

38
00:03:13,830 --> 00:03:21,560
They all start with curly brackets, top and bottom, and then you can have your value pairs in this

39
00:03:21,570 --> 00:03:22,250
notations.

40
00:03:22,260 --> 00:03:26,100
They've got in quotes the name of the value.

41
00:03:26,370 --> 00:03:32,100
And then you've got if you talk about a string, you've got, again, double quotes with the value.

42
00:03:33,150 --> 00:03:34,910
You could have a number here as well.

43
00:03:34,920 --> 00:03:42,990
If you want to store a numerical value or could be an integer or a number that contains decimals.

44
00:03:43,650 --> 00:03:50,430
And then you just need to remember to put a comma there and then you have the next value pair and so

45
00:03:50,430 --> 00:03:50,730
on.

46
00:03:50,860 --> 00:03:59,340
So be using this chasten file to contain the credentials of my wi fi network to which my microcontroller

47
00:03:59,340 --> 00:04:03,630
is connecting to or perhaps an epic for a remote resource.

48
00:04:04,170 --> 00:04:11,520
And you need to store this file through the files here for my especially too.

49
00:04:11,730 --> 00:04:12,350
Right.

50
00:04:12,360 --> 00:04:21,030
So you need to make sure that this Jason file to make the bottom pane a bit bigger.

51
00:04:21,070 --> 00:04:21,830
You can see.

52
00:04:21,840 --> 00:04:22,390
There you go.

53
00:04:22,950 --> 00:04:23,310
All right.

54
00:04:23,320 --> 00:04:29,930
So you need to make sure that this file is stored in the file system of your macropod device or the

55
00:04:30,120 --> 00:04:31,220
facility in this case.

56
00:04:31,620 --> 00:04:39,930
So I'm just going to upload it over and make sure it is right here so that my script will be able to

57
00:04:39,930 --> 00:04:40,290
read.

58
00:04:40,290 --> 00:04:49,200
It can sit forward slash, which means that it's in the root of the storage python device and then it

59
00:04:49,200 --> 00:04:49,840
will open it up.

60
00:04:50,580 --> 00:04:57,440
So once the file is opened, I've got a variable or an object that is the handle to this file.

61
00:04:57,870 --> 00:05:06,420
So call it credentials and this code, Jason, and then I can use this command here, credentials and

62
00:05:06,420 --> 00:05:11,310
this code, Jason, or whatever name you've given to your file, don't read.

63
00:05:11,550 --> 00:05:15,330
And they're just going to bring in the whole file, the entire file.

64
00:05:15,960 --> 00:05:23,100
Then you're going to use Jason Load's, which is going to take this text file and convert it into a

65
00:05:23,640 --> 00:05:29,700
Jason object, in which case that is called settings and inside settings.

66
00:05:29,700 --> 00:05:38,250
I'll be able to use commands such as this settings and then in square brackets, the name of the value

67
00:05:38,250 --> 00:05:40,380
pair that I want to extract like this.

68
00:05:40,740 --> 00:05:48,240
And that will give me in this case, the value, which is the name of my Wi-Fi network, and I'm doing

69
00:05:48,240 --> 00:05:50,460
the same thing for the password.

70
00:05:50,970 --> 00:05:59,190
Therefore, he can use this construct to store any sensitive information such as login credentials outside

71
00:05:59,490 --> 00:06:07,410
of the main micro python script and will be using this method in the next few lectures as well.

72
00:06:08,430 --> 00:06:11,780
Next, I've got a variable to hold the URL.

73
00:06:12,330 --> 00:06:21,150
This is a girl that points to the simple text file a text and I've got a method here called Do Connect.

74
00:06:21,290 --> 00:06:23,730
Can probably guess what it's meant to do.

75
00:06:25,860 --> 00:06:30,540
This method is called down here, so let's jump down here first.

76
00:06:30,740 --> 00:06:42,240
I create a W land object and I'm passing the network dot SDK if underscore, if constant, which means

77
00:06:42,240 --> 00:06:46,410
that what I'm trying to do is to connect to a wi fi hotspot.

78
00:06:47,370 --> 00:06:55,140
So I'll get my object for the wireless lan, then I'll call do connect program jumps up here, a 10

79
00:06:55,140 --> 00:06:56,340
on the interface.

80
00:06:56,730 --> 00:07:01,110
And if I'm not already connected, which this is the first time that I will go through this, I won't

81
00:07:01,110 --> 00:07:08,820
be connected then in a message to the shills that I know what's going on and then try to connect to

82
00:07:08,820 --> 00:07:15,170
those that don't connect method and passing the credentials as I'm grabbing them from the adjacent file

83
00:07:15,700 --> 00:07:18,540
is a name, the Wi-Fi name and the password.

84
00:07:19,020 --> 00:07:26,850
And then if I fail to connect, then I'm just going to print out this message and exit the program by

85
00:07:26,850 --> 00:07:31,420
calling exit and passing a zero ID here.

86
00:07:32,330 --> 00:07:36,450
It doesn't make any difference whether you pass anything or not here in MacRobertson.

87
00:07:38,870 --> 00:07:47,780
Otherwise, we continue in line 62, and so long as we do have a connection and still have a connection,

88
00:07:47,780 --> 00:07:56,270
hasn't dropped out for some reason are a couple of messages out, including the IP address that the

89
00:07:56,300 --> 00:08:09,230
Wi-Fi network has given to my ISP three to this if config method returns all of the network attributes

90
00:08:09,230 --> 00:08:11,660
that have been given to the ISP 32.

91
00:08:11,960 --> 00:08:20,510
And when I pass a zero index in this array, when I get back is the acquired IP address, I'm also printing

92
00:08:20,510 --> 00:08:23,880
the full array of information Isatabu up here as well.

93
00:08:23,900 --> 00:08:30,920
So you'll see that when I run the program for the first time, you'll see that the config method prints

94
00:08:30,920 --> 00:08:39,530
out the acquired IP address, the Gateway IP address, DNS, IP address and information such as that.

95
00:08:40,650 --> 00:08:49,800
Then going into line 65, where I use the get method, because I want to do a get request of the requests

96
00:08:49,800 --> 00:08:53,270
module and pass on the rail that I've set up up here.

97
00:08:54,240 --> 00:08:59,840
So that would actually do the get request to this where you are real.

98
00:09:00,630 --> 00:09:07,260
Then a few messages coming back, including line sixty eight, the response that I'm getting from the

99
00:09:07,260 --> 00:09:07,920
Web server.

100
00:09:07,930 --> 00:09:10,110
So this hopefully will give us.

101
00:09:11,200 --> 00:09:14,440
This text, this is two lines with a bit of text in it.

102
00:09:15,070 --> 00:09:16,640
All right, so as you can see, it's fairly simple.

103
00:09:16,660 --> 00:09:25,910
Let's try it out of looking at the version of the script that is stored on my local machine.

104
00:09:25,930 --> 00:09:28,930
I haven't uploaded yet to my HP with the.

105
00:09:29,190 --> 00:09:30,040
But that is OK.

106
00:09:30,040 --> 00:09:38,450
As long as the credentials file is here so that my script can get it, I can just click on the current

107
00:09:38,530 --> 00:09:42,580
script button and that will execute this script on the device.

108
00:09:43,120 --> 00:09:48,370
This is Connected Network can connect the network with giving credentials, history, making sure that

109
00:09:48,370 --> 00:09:49,420
this is correct.

110
00:09:51,260 --> 00:09:54,230
Right, I'm going to get rid of that tab.

111
00:09:54,790 --> 00:09:57,720
This is on the device, it looks OK.

112
00:09:58,490 --> 00:09:59,530
Let's try one more time.

113
00:10:01,240 --> 00:10:04,330
The second time it worked.

114
00:10:04,990 --> 00:10:11,050
All right, give so much space down here, so let's see what we have.

115
00:10:11,050 --> 00:10:13,300
First of all, we've got the netbook config.

116
00:10:13,360 --> 00:10:16,810
This is a it is printed from.

117
00:10:18,360 --> 00:10:19,710
This line right here.

118
00:10:21,150 --> 00:10:24,960
And you can see that this is the acquired IP address.

119
00:10:26,150 --> 00:10:26,990
The mosque.

120
00:10:28,120 --> 00:10:29,560
The gateway.

121
00:10:30,760 --> 00:10:33,220
And the DNS server.

122
00:10:35,710 --> 00:10:44,260
It's setting out contents from this location and this is the result, which is what I expected to make

123
00:10:44,260 --> 00:10:52,870
a little change to make that point, a few more numbers like that and then just save that right.

124
00:10:53,300 --> 00:10:56,850
If I refresh my vision document in my breath.

125
00:10:56,860 --> 00:11:02,650
So you see the additional numbers, let's do the same thing on with the script.

126
00:11:04,960 --> 00:11:05,480
All right.

127
00:11:05,500 --> 00:11:07,730
And you can see that it works.

128
00:11:07,750 --> 00:11:15,040
All right, so as you can see, this is a fairly simple way of getting data from a web resource.

129
00:11:15,070 --> 00:11:21,970
Now, imagine that this document, the FETCH data, was formatted, for example, as a comma delimited

130
00:11:21,970 --> 00:11:24,520
file or even an adjacent document.

131
00:11:24,790 --> 00:11:30,820
Then you'll be able to import the data from the document and decoded accordingly.

132
00:11:30,850 --> 00:11:39,040
So you could do a bit of text processing to extract data from your CSP file or use the Jason module

133
00:11:39,100 --> 00:11:42,950
to extract data from your adjacent file and so on.

134
00:11:44,140 --> 00:11:50,590
Anyway, I'm going to leave this example to this and move on to the next lecture where I'll show you

135
00:11:50,590 --> 00:11:56,830
how to send example a simulated sensor data to tweet DOD and.
