Communicating on the internet utilizes a stack of protocols. The lowest level protocols interface directly with the hardware, while protocols higher up interface with those below them. This allows a variety of protocols to be developed without having to re-implement the lower levels. In terms of data, the lower level protocols handle individual bytes, slightly above these are protocols that group the bytes into packets, and higher yet are protocols that group the packets into various messages. At each layer multiple protocols with different features are available, the choice of which depends on what you are trying to accomplish. We will be using a protocol called MQTT, which is a great choice for exchanging short messages in near real-time between a number of devices.
An MQTT system consists of one or more clients and a broker which is responsible for relaying messages from one client to another. Clients can generate, consume, or both generate and consume messages. Each client (for instance, an Arduino) is assigned an ID, and a client that generates messages can do so on one or more topic (for instance, one topic for each attached sensor). Messages are identified with the client ID and the message topic.
Clients that consume messages will subscribe (via the broker) to one or more topic. When the broker receives a message, it forwards it each clients that has registered on the message topic.
The beauty of this scheme is that if a broker is on the global internet, e.g. not in your home, it can be accessed from anywhere on the internet, allowing clients that are on an internal network (your Arduino, someone’s browser) to send messages to each other.
MQTT is a protocol, not a product. You can write your own MQTT broker, you can install one of the many MQTT brokers available, or you can use a public broker already running on the internet. We will use a broker provided by the company Shiftr.io, which runs a public broker that is free to use with some restrictions. On the client side, we will create two clients: one on an Arduino MKR WiFi 1010, and one as a web page. We will host the web page on another free service provided by Glitch.com. Similar to Shiftr.io, Glitch.com provides a free web server with some restrictions.
This project is divided into four parts:
- Hardware circuit and physical construction
- A broker on Shiftr.io
- Arduino code
- A web page, including JavaScript code, on Glitch.com
Leave a Reply