I will come back in this lecture.
I'll show you how to do a simple request that goes out onto the Web, grabs the content of a text file
stored in a publicly accessible Eurorail, and then presents the contents of this text file onto the
show.
So here's the example sketch that I've prepared to demonstrate how to do a simple request.
This script uses the network requests and Jason modules, which I described in the previous lecture.
On top of them, I'm also using the USIS module, which allows me to do something like this down here.
And basically, if my ISP 32 fails to connect to my local Wi-Fi network, it will just call the exit
method that belongs to the user's module and stop the execution of the program and then I'll get back
to the show.
So that's the only thing that I've added to this script.
In addition to the modules that are described in the previous lecture before we have a look at the actual
code, I just want to show you that I have created a simple text file, this text file here, which
is available on my website.
Its name is Hemorrhoidal takes the and just a couple of lines of text here that I'm going to fetch.
I have stored this file on my Web server.
As I said, this is its name and I'm able to use them to make modifications.
So this is my interface.
I'm going to make a change to this in a moment and see how that change affects what the script presents
on the shell.
In your case, if you don't have a Web server, you can just place this file on any publicly accessible
location.
Jerrell, it could be could be perhaps even a Dropbox Eurorail or a Google drive.
Your real just tried to make the target file as small as possible because it's got too many characters
in it.
It will most likely exceed the memory of arem limitations of your HP thirty two.
And that will cause the script to crash.
So just remember that you're easiest to do, has a limited amount of ram and therefore you won't be
able to fetch large files from the web that will exceed that limited memory space in its frame.
Again, that will cause the E.S.P to in your script to crash.
All right.
So I've got my recommendation here.
You can check it out for more details and.
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
open structure.
To open up this Jason file, which is this Jason file right here.
So if you never work with Jason file before, then be assured there's nothing difficult about them.
They all start with curly brackets, top and bottom, and then you can have your value pairs in this
notations.
They've got in quotes the name of the value.
And then you've got if you talk about a string, you've got, again, double quotes with the value.
You could have a number here as well.
If you want to store a numerical value or could be an integer or a number that contains decimals.
And then you just need to remember to put a comma there and then you have the next value pair and so
on.
So be using this chasten file to contain the credentials of my wi fi network to which my microcontroller
is connecting to or perhaps an epic for a remote resource.
And you need to store this file through the files here for my especially too.
Right.
So you need to make sure that this Jason file to make the bottom pane a bit bigger.
You can see.
There you go.
All right.
So you need to make sure that this file is stored in the file system of your macropod device or the
facility in this case.
So I'm just going to upload it over and make sure it is right here so that my script will be able to
read.
It can sit forward slash, which means that it's in the root of the storage python device and then it
will open it up.
So once the file is opened, I've got a variable or an object that is the handle to this file.
So call it credentials and this code, Jason, and then I can use this command here, credentials and
this code, Jason, or whatever name you've given to your file, don't read.
And they're just going to bring in the whole file, the entire file.
Then you're going to use Jason Load's, which is going to take this text file and convert it into a
Jason object, in which case that is called settings and inside settings.
I'll be able to use commands such as this settings and then in square brackets, the name of the value
pair that I want to extract like this.
And that will give me in this case, the value, which is the name of my Wi-Fi network, and I'm doing
the same thing for the password.
Therefore, he can use this construct to store any sensitive information such as login credentials outside
of the main micro python script and will be using this method in the next few lectures as well.
Next, I've got a variable to hold the URL.
This is a girl that points to the simple text file a text and I've got a method here called Do Connect.
Can probably guess what it's meant to do.
This method is called down here, so let's jump down here first.
I create a W land object and I'm passing the network dot SDK if underscore, if constant, which means
that what I'm trying to do is to connect to a wi fi hotspot.
So I'll get my object for the wireless lan, then I'll call do connect program jumps up here, a 10
on the interface.
And if I'm not already connected, which this is the first time that I will go through this, I won't
be connected then in a message to the shills that I know what's going on and then try to connect to
those that don't connect method and passing the credentials as I'm grabbing them from the adjacent file
is a name, the Wi-Fi name and the password.
And then if I fail to connect, then I'm just going to print out this message and exit the program by
calling exit and passing a zero ID here.
It doesn't make any difference whether you pass anything or not here in MacRobertson.
Otherwise, we continue in line 62, and so long as we do have a connection and still have a connection,
hasn't dropped out for some reason are a couple of messages out, including the IP address that the
Wi-Fi network has given to my ISP three to this if config method returns all of the network attributes
that have been given to the ISP 32.
And when I pass a zero index in this array, when I get back is the acquired IP address, I'm also printing
the full array of information Isatabu up here as well.
So you'll see that when I run the program for the first time, you'll see that the config method prints
out the acquired IP address, the Gateway IP address, DNS, IP address and information such as that.
Then going into line 65, where I use the get method, because I want to do a get request of the requests
module and pass on the rail that I've set up up here.
So that would actually do the get request to this where you are real.
Then a few messages coming back, including line sixty eight, the response that I'm getting from the
Web server.
So this hopefully will give us.
This text, this is two lines with a bit of text in it.
All right, so as you can see, it's fairly simple.
Let's try it out of looking at the version of the script that is stored on my local machine.
I haven't uploaded yet to my HP with the.
But that is OK.
As long as the credentials file is here so that my script can get it, I can just click on the current
script button and that will execute this script on the device.
This is Connected Network can connect the network with giving credentials, history, making sure that
this is correct.
Right, I'm going to get rid of that tab.
This is on the device, it looks OK.
Let's try one more time.
The second time it worked.
All right, give so much space down here, so let's see what we have.
First of all, we've got the netbook config.
This is a it is printed from.
This line right here.
And you can see that this is the acquired IP address.
The mosque.
The gateway.
And the DNS server.
It's setting out contents from this location and this is the result, which is what I expected to make
a little change to make that point, a few more numbers like that and then just save that right.
If I refresh my vision document in my breath.
So you see the additional numbers, let's do the same thing on with the script.
All right.
And you can see that it works.
All right, so as you can see, this is a fairly simple way of getting data from a web resource.
Now, imagine that this document, the FETCH data, was formatted, for example, as a comma delimited
file or even an adjacent document.
Then you'll be able to import the data from the document and decoded accordingly.
So you could do a bit of text processing to extract data from your CSP file or use the Jason module
to extract data from your adjacent file and so on.
Anyway, I'm going to leave this example to this and move on to the next lecture where I'll show you
how to send example a simulated sensor data to tweet DOD and.