If you've found this guide, you may already know the IoT stands for Internet of Things. Which is a general concept of devices and services that are connected in ways that enable automation. This guide will teach you how to connect the latest Arduino Uno R4 WiFi to an IoT protocol called MQTT that enables communication with other devices and applications.
For example, you could connect your Arduino to another device running Home Assistant with MQTT integration and make a custom home automation device or home sensor.
MQTT, or Message Queuing Telemetry Protocol, is an open standard for allowing devices or applications to publish and subscribe to messages using named topics as routes (i.e. /sensors/temperature). It has built-in features like QoS (Quality of Service) that let you specify how you want to prioritize or guarantee message delivery. Its a favorite of IoT hobbyists and industrial automation alike.
EMQX is an open-source MQTT broker (the server that devices publish and subscribe through) with well-rounded security, efficiency, and scale. It can be run locally, in the cloud, and even on a Raspberry Pi.
Running EMQX Locally and Creating a Client UserAs it stands today the libraries that exist for connecting Arduino devices to MQTT are not compatible with creating an SSL connection on the Arduino Uno R4 WiFi. So while we cannot connect to it to EMQX Serverless, we can do so using EMQX open-source which allows connections without having to provide a Certificate.
EMQX can be installed on a local machine or cloud instance in a number of ways. The easiest is to follow the instructions for your operating system here https://www.emqx.io/downloads
All these instructions are for running in the terminal of your operating system. You can also build the project from source via GitHub. https://github.com/emqx/emqx
Once installed and running, the EMQX admin dashboard can be found at http://127.0.0.1:18083/
You'll need to create user credentials for a client, in this case the Arduino, to connect as. Use the following instructions.
- Open the EMQX Dashboard and login with the default username "admin" and password "public".
- From the dashboard navigate to the Authentication page.
- Click on + Create.
- Select Password-Based and click Next.
- Select Built-in Database and click Next.
- Leave all the default settings and click Create.
- From the Authentication page, under the Actions column for the Password-based authenticator, click Users.
- Click the plus icon to create a new user with a username/password.
- Install the latest Arduino IDE.
- Install the ArduinoMqttClient library. You can do so easily from the Library Manger.
- Install the Arduino Uno R4 WiFi Board by searching it from the Boards Manager.
The Arduino sketch creates a basic program that connects to your WiFi network, connects to the broker as the user created on your EMQX broker, subscribes to messages on a topic and publishes every 3 seconds to a different topic. Its a basic boiler plate that can be used to build more complex projects.
- Create a blank sketch in the Arduino IDE and copy this sketch in.
- At the top of the sketch configure your WiFi credentials, the user credentials from your EMQX broker, the IP address of where your broker is running, and the topics you would like to use.
- Plug the Arduino Uno R4 WiFi to your computer using a USB Type-C cable.
- In Tools > Board select Arduino Uno R4 WiFi.
- In Tools > Port select the corresponding port for your Arduino.
- Click the Upload arrow.
- Once uploaded open the Serial Monitor from the Tools menu in order to monitor the device logs and view messages received.
MQTTX is a free tool that helps you test client connections to an MQTT broker. It also lets you publish and subscribe to topics. We'll use it to connect to the EMQX broker running using the user credentials created earlier.
- Download, install, and open the MQTTX application.
- Click on the plus icon to create a New Connection.
- Name your connection, supply the username/password, and set the hostname to match the one configured on your Arduino Uno R4 WiFi.
- Click Connect.
Topics are named routes for knowing where a message is going to or coming from. They use a slash notation. For example, sensors/temp or sensors/humidity.
To publish messages to a topic, connect a device, construct a payload in the visible text box, and use the send button to publish it. The following image shows how to send a simple JSON message to testtopic/1.
To subscribe to a message, click on New subscription and supply the topic you'd like to subscribe to. Any message published to that topic will appear in the window for that connection. Try this out by creating a new user, a new connection, and subscribe to a topic that the other connection is publishing to.
You can also subscribe to all sub-topics under a topic using the # pound sign. For example, the topic sensors/# would yield any message published under the sensors/ topic.
Try publishing and subscribing to the topics set on your Arduino Uno R4 WiFi!
ArduinoJSONThe example sketch we used earlier shows how to publish and subscribe to raw messages on topics. What if you need to publish or receive messages in JSON format? JSON stand for Javascript Object Notation, its a pattern of using keys and values such as { "some_arbitrary_key_name": "value" }
This example sketch is configured exactly the same as before, but uses the ArduinoJSON library to add a JSON parser.
Where do I go from here?Now that you know how to deploy an EMQX MQTT broker, connect an Arduino Uno R4 WiFi, publish/subscribe to topics, and how to use another MQTT client such as MQTTX you have all the tools necessary to create awesome IoT projects!
MQTT is popular protocol and there is a client library for every language and platform. You could try connecting it to Home Assistant using the built in MQTT integration and the EMQX Add On or an existing EMQX broker. Or use this guide by Alex Glow on how to use the Arduino R4 built in LED Matrix and use another MQTT client, such as a webpage, to control the animations.
Be sure to publish and share any cool projects on Hackster.io or...
Join Our Community!We have an amazing community of open-source contributors and users of the EMQX project and cloud service. Join in on the discussions, share your projects, or feel free to ask any questions! We'd love to hear from you!
Comments
Please log in or sign up to comment.