In our previous post we had seen how to setup IoT in AWS and we created the certificate and policy and downloaded the credentials(Certificate file, Private key, Public key and Root CA certificate) which are going to use it here to connect MQTT with AWS.
Overview of protocols:
The protocols are used by things (T) and servers (S) to interact and share data. Below are some of the protocols widely used in IoT:
MQTT: a protocol for collecting device data and communicating it to servers. This protocol is mainly used for the interaction between the server and the things (T<->S) which is what we are going to discuss in this blog.
XMPP: a protocol best for connecting devices to people, a special case of the (T<->S) pattern, since people are connected to the servers
DDS: a fast bus for integrating intelligent machines, mainly used for communication between the things (T<->T).
AMQP: a queuing system designed to connect servers to each other mainly used for communication between servers (S<->S).
What is MQTT and why it is most widely used?
MQTT stands for Message Queuing Telemetry Transport. It is publish-subscribe based, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.
Quality of Service(QoS):
The QoS ensures the delivery of the content to the receiver the use of the QoS levels are solely based on our need. Mostly, the QoS-1 is preferred as it handles retransmission of data for guaranteed transmission at least once to the receiver. The 3 levels of QoS:
Qos-0 – At most once delivery: In this the response is not expected and no retry semantics are defined in the protocol. A QoS-0 message can get lost if the client unexpectedly disconnects or if the server fails.
QoS-1 – At least Once Delivery: For this level of service, the MQTT client or the server would attempt to deliver the message at-least once. But there can be a duplicate message.
QoS-2 – Exactly once delivery: This is the highest level of Quality of Service. The message is delivered once and only once when QoS-2 is used.
Using MQTT in Rails with AWS:
As, we have setup everything for using MQTT with AWS now, lets use those credentials we downloaded from the generated certificate to connect to AWS IoT.
Connecting the MQTT client:
After installing the ‘mqtt’ gem initialize the MQTT::Client with the credentials (certificate file, private key file and the root CA certificate file) that we have generated in the Step 2 of AWS IoT setup that we discussed in the blog Getting started with AWS IoT.
Host is the REST API endpoint (Ex: abcedefghijkl.iot.us-west-2.amazonaws.com) of the thing we created in Step 1 of AWS IoT setup. We can copy this endpoint by clicking the IoT thing from the AWS IoT console.
Now, you can publish and subscribe to a topic to send and receive the payload.
Here, payload is nothing but the data that we are going to publish to a topic.
After connecting the client we can publish the payload to the topic with just one command as below:
To subscribe to a topic just use the “subscribe” method with topic as the argument. Separate the topics by comma(,) to subscribe to multiple topics.
For simulating this MQTT publish-subscribe we can use the MQTT.fx tool and create a connection profile in the tool with the credentials we downloaded from AWS as shown in the screen:
As we have successfully implemented a MQTT client with AWS IoT credentials and performed publish-subscribe. In the upcoming blog we will be learning how to use AWS::IoTDataPlane instead of MQTT::Client.