So far, we’ve been using our boards as clients or servers, running them on a local network. This is good fun, but still a bit limiting, as it restricts us to immediate locations, it may also be a security risk to allow these devices to just propagate online.
We’re now going to use a 3rd party service to serve our data publicly, which allows us to tie our system into more services and systems. Message Queueing Telemetry Transfer, typically referred to as MQTT, is a message protocol system that is designed for systems where a device may not always be online (like our microcontrollers! or a solar-powered system, etc). The MQTT service is a broker, and data is organized into topics, which holds messages. Devices can subscribe to a topic to publish, or to be notified when new messages arrive.
MQTT is remarkably quick when it comes to updating devices; a broker can broadcast a message to all connected clients super quickly. There are a bunch of different MQTT brokers out there. You can host your own with a program named Mosquitto, or you could use a service like Adafruit IO, shiftr.io, and MQTT Dashboard all of which are appropriate for hobbyist use, or something that’s enterprise scalable like VerneMQ.
We’ll stay on-brand today and use Adafruit IO. First off, you’ll need to sign up for an account on the site.
- Sign up for an adafruit account
- Open your Arduino software, and downgrade the version of the ESP library to 2.4.2
- From the IDE, go to “Tools > Board > Boards manager”
- in the dialogue box, filter with the term “ESP”
- Click on the esp8266 box, and in the “version” dropdown choose 2.4.2, then click “Update”. This will remove the 2.5.0 library which is incompatible with the Adafruit IO library
- Once this is complete, close the window
- Now it’s time to add the adafruit IO library and dependencies
- In the IDE again, head to “Sketch > Include Library > manage Libraries”
- search for “adafruit io arduino” and install the library
- search for “Adafruit MQTT” and install the library
- search for “ArduinoHttpClient” and install the library
- close the Library Manager once you’re finished
- Head back to your browser and open Adafruit IO. You’re going to get some information you need to use the service. Specifically, you need to generate a security key to send data back and forth.
- Click on the “View AIO Key” link
- In the popup window, click on the button that says “Regenerate IO Key” Save the key and your username somewhere locally to use in a few minutes.
- Close the popup.
- Back in the Arduino IDE, navigate to “File > Examples > Adafruit IO Arduino > adafruitio_00_publish”
- In the config.h tab, add your SSID information and replace the IO_USERNAME and IO_KEY with the information you just saved
- Upload the code to your board. In the serial monitor, you should see a counter increasing
- back to the browser in Adafruit IO, create a Feed
- click on the “Feed” link
- from the “actions” dropdown, “Create a new feed”
- name it “counter”
- after a few moments, you ought see the feed updating from the microcontroller!
- you can also get this as a JSON file. Click on the “feed info” link and then copy the API link.