Arduino MQTT
Arduino+Library Setup
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 the zip of it by clicking below
Rename the uncompressed folder Adafruit_MQTT and check that the Adafruit_MQTT folder containsAdafruit_MQTT.cpp and Adafruit_MQTT.h
Place the Adafruit_MQTT library folder in your arduinosketchfolder/libraries/ folder.
You may need to create the libraries subfolder if its your first library. Restart the IDE.
We also have a great tutorial on Arduino library installation at:
" href="http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use" style="box-sizing: border-box; background: transparent; color: rgb(0, 167, 233); text-decoration: none;">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 useArduino UNO + Adafruit CC3000 shield or CC3000 breakout for this demo. You can also use a HUZZAH ESP8266 + FTDI cable.
To start with, we won't be connecting any sensors or anything. So if you're using the CC3000 check out our tutorial and make sure you have that all working.
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 CC3000 pick mqtt_cc3k for ESP8266 pick mqtt_esp8266
before uploading, you need to set up a few things.
Connection pinouts
If you're using the CC3000, or ATWINC1500 or whatever, check to make sure these pins are correct!
- /*************************** CC3000 Pins ***********************************/
- #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
- #define ADAFRUIT_CC3000_VBAT 5 // VBAT & CS can be any digital pins.
- #define ADAFRUIT_CC3000_CS 10
- // Use hardware SPI for the remaining pins
- // On an UNO, SCK = 13, MISO = 12, and MOSI = 11
cuz if you can't connect to the CC3000 module, nothing else is going to work!
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..."
and for CC3000, also the security method
- #define WLAN_SECURITY WLAN_SEC_WPA2 // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
- // WLAN_SEC_WPA or WLAN_SEC_WPA2
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, the CC3000 will look a tad different)
Now click back to your adafruit.io dashboard, the one you made before. You'll see the photocellgauge 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!