In my startup, we have worked on some AIoT (AI + IoT) projects and we’ve done some MVPs as demo to our client. In some certain project, we have used MQTT protocol for collecting sensors data and transporting them to other smart devices. So in this post, I would like to present a simple demo for building a MQTT communication via a broker on AWS cloud.
MQTT (Message Queuing Telemetry Transport) is an open OASIS and ISO standard (ISO/IEC 20922) lightweight, publish-subscribe network protocol that transports messages between devices. The protocol usually runs over TCP/IP; however, any network protocol that provides ordered, lossless, bi-directional connections can support MQTT. It is designed for connections with remote locations where a “small code footprint” is required or the network bandwidth is limited.
Cloud side
On AWS server, I use docker to run an emqx service which play a role as “broker”.
1 | sudo docker run --rm -ti --name emqx -p 18083:18083 -p 1883:1883 -e EMQX_ADMIN_PASSWORD="myPasswd" emqx/emqx:latest |
Output of my putty terminal:
When this service is running, one can access to its administration page by visiting http://3.138.200.179:18083/#/
:
Subscriber
I have a subcriber.py file which defines a mqtt
client who connects to cloud broker with port 1883 and subscribes message named 'qiu_data'
.
1 | import paho.mqtt.client as mqtt |
Publisher
I have a publisher.py file which defines another mqtt
client who connects to cloud broker with port 1883 and publish some data in 'qiu_data'
messages.
1 | import paho.mqtt.client as mqtt |
Execution
In a terminal I type python subcriber.py
, and in a second terminal I type python publisher.py
. See the published topics have been received in the subscriber’s terminal: