Hey guys! Welcome back to my channel. I'm Eric here. I'll show you how to install one of the MQTT
brokers, Mosquitto on AWS EC2 instance. You can see here that the MQTT broker is currently
working and the ESP32 is sending and receiving data
through the MQTT broker with the specific topics. MQTT is a lightweight, publish-subscribe network
protocol that transports messages between devices in
the constrained bandwidth communication environments such as M2M (Machine to Machine) and Internet
of Things. Also, the reason why the MQTT broker is called
a broker, not the MQTT server is that the broker works as the bridge that Publisher
and Subscriber can send and receive messages from. In terms of topic, it's to decide where to
receive data when sending and receiving each data. Each Publisher specifies a topic, and the
Subscriber who subscribes to it receives the data. Conversely, Subscribers who do not subscribe
to the topic do not receive data. I'll tell you more about MQTT in the next
video when I make a service using it. It's gonna be interesting. Ok! Let's prepare the MQTT broker first. This is the AWS management console. And the regional setting is set to us-west-1
in my case. It doesn't matter what region you have. Just please remember the region you set. We need to put the region later on. There is nothing on the EC2 dashboard because
I created a new account. Before making a new EC2 instance, we need
to create a Role of AWS IoT Config Access in the IAM. Later, we'll generate the AWS IoT credentials
from the console. Click on Roles on the left menu. Then, click on Create role. Select EC2 and click on Next Permissions Search with the keyword, iot.
then you can see there is AWSIoTConfigAccess. Select it. click on Next Tags You can add a tag for this role. but it's not necessary for me. I just skip it. click on Next Review Enter AWS_IoT_Config_Access as the Role name. click on Create role You can see there is a role we just made. It's time to make an EC2 instance. Go to EC2. Use the search bar on top of the screen. It takes you where you want. Click on the instance on the left menu. There is no instance, of course. I need to make one. Click on the launch instances. There are a bunch of Amazon machine images. Select the free tier only. then you can see the ubuntu servers. I select Ubuntu Server 20.04 LTS which is
x86. The MQTT broker requires very low resources. You can play everything with the free tier
instance. Select the t2.micro which is free tier eligible and click on Next: Configure Instance Details. In the IAM Role dropdown, select AWS_IoT_Config_Access
you made. If you missed it, please make it first before
creating an instance. Click on Next Add Storage. we don't need to add extra storage so just
skip it. For the tag, you can put the name on this
instance. For key, name,
For value, MQTT BROKER. You don't need to set them. it's optional. Click on Next Configure Security Group. This is a default setup for security access. For now, there is no problem as long as you
can access it via ssh. Click on review and launch. To access the server via console, we need
to have a key pair. Let's make a new key pair. Put the key pair name you want
and download it. After that, you can launch the instance. This was all about creating an EC2 instance
with the IoT config role. It takes few minutes to up. Time to grab a cup of coffee. It's been about 10 minutes. Let me click the instance id for more information
about my instance. The state of the instance shows that it's
currently running. It's good to go. Time to open your terminal. You can see the key pair you downloaded before
starting the instance. Change the permission of the key pair to 400. This means that it cannot be written or executed. Use ssh to connect to the server. You can find the public server address from
the instance information page. Just copy and paste it. We have connected to the server. From now on, the task is to install the Mosquitto
MQTT broker. The AWS blog explains everything we need to
install. Of course, I followed this and did the same
thing. There is a link in the description below. Please refer to it. This is the surest way to install it. We have installed the Mosquitto broker onto
our EC2 instance. Now, we need to configure the bridge so that
the Mosquitto broker can create a bi-directional connection to AWS IoT Core. Leave them blank. but you need to put your region here. mine is us-west-1. leave the rest blank. This is to create an IAM policy for the bridge. Done. Go to the Mosquitto certificate directory
and download the Amazon Root CA certificate. Root CA certificate downloaded. This time, create certificates and keys. It's necessary to change the region at the
end of the parameter. In my case us-west-1. A public certificate and a private key have
been created. In order to attach the IoT policy to your
certificate, replace the ARN of the certificate returned above from the command line. The string I've already copied was the ARN
of the policy. It's wrong. You need to have the ARN of the certificate. Now, it's successfully attached the IoT policy
to the certificate. And add read permissions to the private key
and the public certificate. A total of three certificates are required
to access the MQTT broker via AWS IoT on ESP32. I'll tell you again after the broker works
properly. We now have a client certificate for our bridge,
this certificate is associated with an IoT policy. This IoT policy will give permissions to the
bridge to connect to AWS IoT Core and publish and subscribe to any topic. The last step is to create the configuration
file with our specific configuration for the Mosquitto
Broker bridge. I just got the AWS IoT Core ATS endpoint. This will be the endpoint where ESP32 tries
to access. Please keep this. It's time to configure for the Mosquitto broker. Copy and paste the configuration from the
blog and replace the address with your endpoint address. Now we can restart the Mosquitto broker to
load this new configuration. We have confirmed that the broker is working
in the process. Okay.
It's time to have a simple test. AWS IOT provides a test client page for testing
MQTT servers. Thanks to this, we can easily have a test. The MQTT test page address is also in the
description below. You need to select the correct region your
server is in. I'll subscribe to the topic as it is on the
blog. Since I have subscribed to the specific topic,
I can see all the data that came in here. Let me publish a local message to the Mosquitto
broker running on the EC2 instance from the terminal. I can see the data I sent
on the test page. You can also publish messages from the Test
page. I will send a message through this. Through MQTT broker, the message was published
and I was able to see it again from the subscribed topic. It works seamlessly and now it's time to set
up the ESP32. The AWS IoT blog also presents a way to deliver
messages from ESP32 to MQTT BROKER. I will complete the ESP32 code based on the
post. First, I will make a header file called secrets. I copied and pasted what I need here. I'll fill it up one by one. The thing name is MyESP32. I put the SSID and password of my wireless
router. Paste the previously saved AWS IOT ENDPOINT
address here. Each certificate must be pasted. It's all created on the server, so you can
find it and paste it in. Let me show you how to do it. First, open the root CA certificate. Copy and paste all strings, including the
begin and end. It's done. let's move to the next one. The next one is the device certificate. Copy and paste the same way. The last one is the private key. All done. This completes the header file. Now I will complete the main part of the source
code. After accessing the AWS IoT, this code works
on two topics through the MQTT broker. It's sending not only the timestamp but also
the analog value connected to the GPIO every second via the topic, esp32 slash pub. It also subscribes to the topic,
esp32 slash sub, to receive data that corresponds to this topic. The ArduinoJson library and the MQTT library
are required for this code to work properly. There are lots of MQTT libraries, so please
check the author. It's using MQTT by Joel and the version is
2.5.0. I will also register all topics in the MQTT
test client to check if the data is delivered properly. Let me change the serial speed. Lastly, I will send random data rather than
the analog value on the publishing message because I don't connect anything to GPIO. This is all. All looks good and doesn't have any problems. Let's see the result. If you look at the serial monitor, it says
the AWS IoT is connected. On the right test page, you can see the message
icon on esp32/pub. This is an indication that you have received
a message. You can see the message coming in every one
second as intended. This time, I will send a message on the topic,
esp32/sub. As soon as I send it, you can see that the
message is printed on the serial monitor. Awesome. It works smoothly. That's all for today's video. We've built a system for both the Mosquitto
broker on EC2 instance and ESP32 connection for AWS IoT. I hope it would be a great start for many
interesting projects. The next project will also cover the detailed
features of MQTT. Thanks for watching. See you soon again.