OK now that your online MQTT broker stuff is all set up, you can get your electronics ready
If you don't have any electronics...
Install Adafruit_MQTT
In order to 'talk' MQTT, we'll use the Adafruit MQTT library. It works with any MQTT broker and frankly we think its the best low-footprint library out there. The library's code is stored here and you can download it via the Arduino library manager.
Open up the Arduino library manager
Search for the Adafruit MQTT library and install it
We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
We'll be using an Adafruit Feather Huzzah ESP8266 devboard for this demo. You can also use a HUZZAH ESP8266 + FTDI cable.
To start with, we won't be connecting any sensors or anything.
If you're using an ESP8266 (Feather or breakout), check out our tutorial and make sure you can get it compiling and programmed with the Arduino IDE and connected to your WiFi router.
Load up example
OK depending on which one you picked, load up the Arduino IDE and select the matching example. For ESP8266 pick mqtt_esp8266
before uploading, you need to set up a few things.
Connection pinouts
If you're using the ATWINC1500 or whatever, check to make sure these pins are correct!
/************************* ATWINC1500 Pins *****************************/ #define WINC_CS 8 #define WINC_IRQ 7 #define WINC_RST 4 #define WINC_EN 2 // or, tie EN to VCC
Set up WiFi credentials
Dont forget you need to tell the Arduino how to connect to your local network, so set up the WiFi credentials:
/************************* WiFi Access Point *********************************/ #define WLAN_SSID "...your SSID..." // can't be longer than 32 characters! #define WLAN_PASS "...your password..."
Finally, set your adafruit.io username (hey you remember that from the last chapter right?) and adafruit.io key
#define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." #define AIO_KEY "...your AIO key..."
NOW you can upload the sketch to your Arduino or ESP8266.
Publication test
Open up the serial console as soon as the sketch is done uploading. You'll see something like this (I'm using an ESP8266 here)
Now click back to your Adafruit IO dashboard, the one you made before. You'll see the photocell gauge clicking upwards
You can mouseover the gauge to get the last updated timestamp. And at the top of the page you'll see what looks like a bunch of blue dots. Those dots tell you that you've had data transferred in or out of your feeds, handy to get a quick sense of whether new data is streaming in!
And, if you go back to your feeds page, you can see each value as it comes in, as well as download a spreadsheet if you like
Subscription Test
OK you have data going from your device to adafruit.io but wouldn't it be nice if you could have signals going back as well? No problem! Lets use our onoff feed, we're already subscribed to it.
While keeping your serial console open, click on the slider button in your dashboard
In the serial console, you'll see those messages are received:
the updates from button flip to message appearing should be under 1 second, showing the speed of MQTT!
Text editor powered by tinymce.