Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
arduino/00 Introduction.md
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
114 lines (82 sloc)
4.2 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MQTT | |
## Intro | |
MQTT - a lightweight messaging protocol. | |
Ideal for use with IOT devices. | |
Created by Andy Stanford-Clark and Arlen Nipper 1999. | |
Also used for Facebook messenger. | |
![MQTT Logo](exercises/.images/mqttorg-glow.png) | |
## Why MQTT | |
Always use existing standards when possible. | |
Custom protocols cause fragmentation. | |
Why MQTT? | |
- Low bandwidth | |
- Good when communication is high cost, i.e. power. | |
- Good when communication is unreliable, i.e. long distance. | |
- High latency | |
- Good when responses can take a long time. | |
- Free | |
- Important for low cost devices. | |
- Widespread | |
- Large ecosystem. | |
- Lots of support. | |
# Publisher/Subscriber model. | |
![MQTT Diagram](exercises/.images/mqtt.png) | |
## Topics | |
MQTT clients subscribe/publish to topics. | |
Topics can be arranged into levels. Allows grouping of related actions/information. | |
- myhouse/upstairs/bedroom/temperature | |
- myhouse/upstairs/bedroom/light | |
- myhouse/upstairs/bathroom/light | |
- myhouse/heating | |
Wildcards let you subscribe to multiple topics. | |
Single level wildcard (+), e.g. myhouse/upstairs/+/light | |
- %myhouse/upstairs/+/light | |
- %myhouse/upstairs/+/light | |
Multi-level wildcard (\#), e.g. myhouse/upstairs/\# | |
- myhouse/upstairs/bedroom/temperature | |
- myhouse/upstairs/bedroom/light | |
- myhouse/upstairs/bathroom/light | |
## Message Quality | |
Different `QOS` levels depending on message type. | |
0. Send once, it might get through. | |
- E.g. simple sensor readings. | |
1. Send at least once, will get through, might be duplicated. | |
- Keep sending until confirmed. | |
- E.g. data requests. | |
2. Send exactly once, will get through. | |
- E.g. device control. | |
Don't use `QOS` level 2 for everything. | |
- Takes longer. | |
- Needs more resources. | |
# Connecting | |
## Test brokers | |
**NOT** to be used for a real system. | |
Have **NO** security, | |
**NO** privacy. | |
But good enough to test with. | |
- iot.eclipse.org | |
- mqtt.pubnub.com | |
- test.mosquitto.org | |
- broker.mqttdashboard.com | |
Real systems need username/password or more sophisticated authentication. | |
For example the Coventry University MQTT broker is available on mqtt.coventry.ac.uk and will require you to use a username and password and a CA certificate to connect. The CA certificate is available in this repository in the `mqtt.crt` file. | |
## Mosquitto | |
Mosquitto is an open source MQTT broker implementation. Given its popularity it's a good choice if you need to run your own broker. | |
It also has a number of command line tools that can be used to publish and subscribe to MQTT topics. These are incredibly useful when checking that the MQTT portion your application is working correctly. | |
The most used commands are `mosquitto_sub` for subscribing to MQTT topics and `mosquitto_pub` for publishing to MQTT topics. | |
If you are on a linux based system and need to install these tools, they are part of the `mosquitto_clients` package. | |
### Using it | |
Use the `--help` flag to see the full range of commands but general usage will typically be some variation of: | |
`mosquitto_sub -h mqtt.coventry.ac.uk -t "mytopic/wibble" -v` and | |
`mosquitto_pub -h mqtt.coventry.ac.uk -t "mytopic/wibble" -m "42"`. | |
- `-h` is the host to connect to, in this case `mqtt.coventry.ac.uk` | |
- `-t` is the topic to publish/subscribe to, i.e. `mytopic/wibble` | |
- `-v` enables verbose mode in `mosquitto_sub`, causes additional useful information to be displayed when messages are recieved | |
- `-m` is the message that is sent to the topic, i.e. `42` | |
If you are using a secure MQTT broker (i.e. `mqtt.coventry.ac.uk`) then some or all of the following flags may also be required. | |
- `-u` the username to use | |
- `-P` the password to use | |
- `--cafile` the certificate file to use | |
- `-p` the port to connect on, normally 8883 for secure brokers | |
For example `mosquitto_sub -h mqtt.coventry.ac.uk -t "mytopic/wibble" -v -u USERNAME -P PASSWORD --cafile mqtt.crt -p 8883` | |
For security, users need to be prevented from seeing each others topics. The easiest way to achieve this and to prevent clashes when different users try to create topics with the same names, users may be limited to subscribing/publishing topics that start with their own username. This is definitely the case on the Coventry broker, your topics will have to start with your own username i.e. `USERNAME/mytopic/wibble` | |