All right, we have talked about how we can decode all of this function here, how about we print what
kind of message we have received?
That would be great.
So let's say at the beginning of the function, I'm going to say serial dot print ambiguity message
was received.
And how about we put a bracket here in order to specify what is the message that we have received?
OK, so we're going to print.
The name of the topic.
So I'm going to right here topic, it will bring the whole topic string and finally I'm going to close
the Prickett.
So I'm not using Ellen because I don't want to sit on your line and just want to put all of these as
one fine block.
Let me close the brackets.
And that's it.
Now, how about we print the actual payload?
Now, this one could be Ellen because I want to print the payload underneath it.
So I'm going to say here, Serial.
The Brent Ellen.
Payload.
Which bailout we're going to print its bailout zero, as I said it, volunteer does not matter.
We just want to bring the first message, which is bailout zero.
As I said, if I am sending multiple bytes, I could be printing bailout one, two, three, etc. But
for now, I'm sending only one bite from my friend.
All right.
Let us continue.
I think we're good.
You know, where we need to go to the set up and start connecting to include so far.
Right.
We are all connected to Wi-Fi.
Now I'm going to define a variable.
It's called the status.
And it's equal to zero.
Now I'm going to define a valuable type integer, and it's called status, this status is going to be
actually.
My client that connect to my community server.
OK, so here this client Kinect, which is actually and if you remember what is a client, the client
is the security connection.
This client, Kinect, is supposed to return a true or false if connection is succeed.
If Conexion has succeeded, then it's going to be true so that this will be true.
If this one fails, it's going to return zero and the status will be zero.
OK, but first, I need to specify where am I going to connect?
I mean, OK, I'm saying client connect.
I am saying that connecting to the server.
But where exactly?
I mean, this has billions of broker accounts.
I need a tuck in that will help me connect to my account.
Right.
To get that, we're going to go to high MPU cloud.
We're going to go to access management.
We're going to go to overview and we're going to see this.
This is the hostname.
This is the docking of the hostname you're going to use in order to access there.
So just press this small button, which is copying or if you want to copy it, make sure you copy it
with spaces.
Just copy the name of the host and then simply just paste it here.
This is how you're going to connect to your brokerage account.
OK, now I'm going to say if statis.
Is equal to one, by the way, you could write it if status equals to one of equals equals one, or
you can just say if that it's the same same syntax.
There is no difference.
But for clarity, I'm going to just leave it as equals equals one.
But if you see some statement, like if status, it means that if that is true, OK, if it's true,
what I want to do, I'm going to open two brackets like this and I'm going to do the following.
I'm going to say serial.
Franklin, what do you run a print here?
I'm going to say that it is connected.
Because this literally means that I have connected to this house and I can print a successful message,
that connection has been established.
What's next?
Well, I could create a separate function to subscribe to certain topics, but since I have only one
topic for now, I'm just going to add it here.
I'm going to say client.
Don't subscribe.
Do you remember what was the name of the topic we are publishing on let's go and check it if you go
here.
You'll see that the topic name we have chosen is internal, so I'm going to copy the topic name.
Remember, this is just the message name.
To what messaging do you want to subscribe to, what information do I want to subscribe?
So here I am subscribing to this elitist status.
As I said, if you have multiple subscriptions, you could create a separate function and you call it
subscribe and just do subscriptions there.
That's a great.
So what I'm doing is I'm subscribing to cereal.
Actually, we don't need this delay.
It's not really that important.
Subscribe to cereal, said the most of the pens Kinect print that you are connecting to a certain Wi-Fi
connection.
Then begin start the Wi-Fi check.
If the Wi-Fi is connected, then go and connect your mutty and subscribe to a certain client.
Now, why am I putting these inside an IF statement?
Because I assume that I don't have this if statement like this and assume that this connection has failed,
I could get an exception for only trying to subscribe.
My whole board would go into error and it will be and unhandled error if I tried to subscribe to a topic
for a connection that I don't even have in the first place.
This is why to be on the safe side, I am checking the status, making sure connection has succeeded.
Then I am saying, OK, start subscribing.
This is very important.
This sequencing of events is very important in order to handle errors that could come up when connection
has failed, when you have some unpredictable connection error.
This could solve it easily.
Now, the last thing we want to do is go to the loop and start writing the main program, which is actually
very short.
This program is all about client Dot Lou.
When you do that, you're going to start listening to new topics and you'll start receiving new data.
However, I just want to tell you that connection gets dropped for every message and you need to establish
it every time.
How about we add a small portion of guilt so that if for some reason the connection was interrupted,
here I go and reconnect again.
That would be great.
I would simply say, if not client connected, if I don't have a connection.
I could reconnect very simply.
How can I do that?
Well, I'm going to do exactly what I've done here.
I'm going to copy all of this.
And I'm just going to taste it here, OK?
So if Goines is not connected, I'm going to connect, check the status and say.
OK, it is connected again.
As simple as that.
Now, again, this code could be much better if we make this code, could be much better if we make
it modular and we add some functions.
But for now, all we care about is get it to work and we can take care of modulating instead of making
it more scalable after we make sure that the basic functionality is working.
This is how you program.
You just write a bunch of code, you make sure it's working.
Then you try to effectuate, you try to make it modular, or you can try to make it easier to extend
the future by just going over it again.
But the first principle here is to first write a functional code that works.
OK, next lecture.
We're going to test all of this.