Taillieu.Info

More Than a Hobby..

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /customers/3/2/5/taillieu.info/httpd.www/templates/taillieuinfo_joomla3_rev1/functions.php on line 194

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...

You can also use your computer to play around with feeds! Check out this MQTT.fx tutorial for desktop client usage

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

First Test

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.

https://cdn-learn.adafruit.com/assets/assets/000/028/200/medium640/adafruit_io_1491-00.jpg?1445559174 640w, https://cdn-learn.adafruit.com/assets/assets/000/028/200/medium800/adafruit_io_1491-00.jpg?1445559174 800w, https://cdn-learn.adafruit.com/assets/assets/000/028/200/large1024/adafruit_io_1491-00.jpg?1445559174 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/028/200/medium800/adafruit_io_1491-00.jpg?1445559174" alt="adafruit_io_1491-00.jpg" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">
https://cdn-learn.adafruit.com/assets/assets/000/029/628/medium640/adafruit_io_2821-08.jpg?1452396511 640w, https://cdn-learn.adafruit.com/assets/assets/000/029/628/medium800/adafruit_io_2821-08.jpg?1452396511 800w, https://cdn-learn.adafruit.com/assets/assets/000/029/628/large1024/adafruit_io_2821-08.jpg?1452396511 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/029/628/medium800/adafruit_io_2821-08.jpg?1452396511" alt="adafruit_io_2821-08.jpg" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">

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

https://cdn-learn.adafruit.com/assets/assets/000/028/202/medium640/adafruit_io_demo.png?1445559333 640w, https://cdn-learn.adafruit.com/assets/assets/000/028/202/medium800/adafruit_io_demo.png?1445559333 800w, https://cdn-learn.adafruit.com/assets/assets/000/028/202/large1024/adafruit_io_demo.png?1445559333 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/028/202/medium800/adafruit_io_demo.png?1445559333" alt="adafruit_io_demo.png" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">

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!

  1. /*************************** CC3000 Pins ***********************************/
  2. #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
  3. #define ADAFRUIT_CC3000_VBAT 5 // VBAT & CS can be any digital pins.
  4. #define ADAFRUIT_CC3000_CS 10
  5. // Use hardware SPI for the remaining pins
  6. // 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:

  1. /************************* WiFi Access Point *********************************/
  2. #define WLAN_SSID "...your SSID..." // can't be longer than 32 characters!
  3. #define WLAN_PASS "...your password..."

and for CC3000, also the security method

  1. #define WLAN_SECURITY WLAN_SEC_WPA2 // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
  2. // 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

  1. #define AIO_SERVER "io.adafruit.com"
  2. #define AIO_SERVERPORT 1883
  3. #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
  4. #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)

https://cdn-learn.adafruit.com/assets/assets/000/028/203/medium640/adafruit_io_espphoto.png?1445561346 640w, https://cdn-learn.adafruit.com/assets/assets/000/028/203/medium800/adafruit_io_espphoto.png?1445561346 800w, https://cdn-learn.adafruit.com/assets/assets/000/028/203/large1024/adafruit_io_espphoto.png?1445561346 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/028/203/medium800/adafruit_io_espphoto.png?1445561346" alt="adafruit_io_espphoto.png" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">

Now click back to your adafruit.io dashboard, the one you made before. You'll see the photocellgauge clicking upwards

https://cdn-learn.adafruit.com/assets/assets/000/028/204/medium640/adafruit_io_photocellincrease.png?1445561529 640w, https://cdn-learn.adafruit.com/assets/assets/000/028/204/medium800/adafruit_io_photocellincrease.png?1445561529 800w, https://cdn-learn.adafruit.com/assets/assets/000/028/204/large1024/adafruit_io_photocellincrease.png?1445561529 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/028/204/medium800/adafruit_io_photocellincrease.png?1445561529" alt="adafruit_io_photocellincrease.png" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">

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!

https://cdn-learn.adafruit.com/assets/assets/000/028/205/medium640/adafruit_io_lastupdated.png?1445561625 640w, https://cdn-learn.adafruit.com/assets/assets/000/028/205/medium800/adafruit_io_lastupdated.png?1445561625 800w, https://cdn-learn.adafruit.com/assets/assets/000/028/205/large1024/adafruit_io_lastupdated.png?1445561625 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/028/205/medium800/adafruit_io_lastupdated.png?1445561625" alt="adafruit_io_lastupdated.png" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">

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

https://cdn-learn.adafruit.com/assets/assets/000/028/206/medium640/adafruit_io_feedupate.png?1445561744 640w, https://cdn-learn.adafruit.com/assets/assets/000/028/206/medium800/adafruit_io_feedupate.png?1445561744 800w, https://cdn-learn.adafruit.com/assets/assets/000/028/206/large1024/adafruit_io_feedupate.png?1445561744 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/028/206/medium800/adafruit_io_feedupate.png?1445561744" alt="adafruit_io_feedupate.png" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">

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

https://cdn-learn.adafruit.com/assets/assets/000/028/207/medium640/adafruit_io_on.png?1445562751 640w, https://cdn-learn.adafruit.com/assets/assets/000/028/207/medium800/adafruit_io_on.png?1445562751 800w, https://cdn-learn.adafruit.com/assets/assets/000/028/207/large1024/adafruit_io_on.png?1445562751 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/028/207/medium800/adafruit_io_on.png?1445562751" alt="adafruit_io_on.png" data-pin-nopin="true" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">
https://cdn-learn.adafruit.com/assets/assets/000/028/208/medium640/adafruit_io_off.png?1445563123 640w, https://cdn-learn.adafruit.com/assets/assets/000/028/208/medium800/adafruit_io_off.png?1445563123 800w, https://cdn-learn.adafruit.com/assets/assets/000/028/208/large1024/adafruit_io_off.png?1445563123 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/028/208/medium800/adafruit_io_off.png?1445563123" alt="adafruit_io_off.png" data-pin-nopin="true" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">

In the serial console, you'll see those messages are received:

https://cdn-learn.adafruit.com/assets/assets/000/028/209/medium640/adafruit_io_received.png?1445563239 640w, https://cdn-learn.adafruit.com/assets/assets/000/028/209/medium800/adafruit_io_received.png?1445563239 800w, https://cdn-learn.adafruit.com/assets/assets/000/028/209/large1024/adafruit_io_received.png?1445563239 1024w" sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, (max-width: 1365px) 47vw, 750px" src="https://cdn-learn.adafruit.com/assets/assets/000/028/209/medium800/adafruit_io_received.png?1445563239" alt="adafruit_io_received.png" style="box-sizing: border-box; vertical-align: middle; display: block; max-width: 100%; height: auto; margin: 0px auto; text-align: center;">

the updates from button flip to message appearing should be under 1 second, showing the speed of MQTT!