From e42f3094e5d5b76d12a08a3844267575e36ee00b Mon Sep 17 00:00:00 2001 From: Tony DiCola Date: Mon, 31 Aug 2015 14:36:06 -0700 Subject: [PATCH] Add Arduino Yun example. --- README.md | 2 +- examples/mqtt_yun/mqtt_yun.ino | 130 +++++++++++++++++++++++++++++++++ library.properties | 4 +- 3 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 examples/mqtt_yun/mqtt_yun.ino diff --git a/README.md b/README.md index 8cdbdbe..645ef82 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Adafruit MQTT Library [![Build Status](https://travis-ci.org/adafruit/Adafruit_MQTT_Library.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_MQTT_Library) Arduino library for MQTT support, including access to Adafruit IO. Works with -the Adafruit CC3000, FONA, ESP8266 Arduino platforms, and anything that supports +the Adafruit CC3000, FONA, Arduino Yun, ESP8266 Arduino platforms, and anything that supports Arduino's Client interface (like Ethernet shield). See included examples for how to use the library to access an MQTT service to diff --git a/examples/mqtt_yun/mqtt_yun.ino b/examples/mqtt_yun/mqtt_yun.ino new file mode 100644 index 0000000..b50a22f --- /dev/null +++ b/examples/mqtt_yun/mqtt_yun.ino @@ -0,0 +1,130 @@ +/*************************************************** + Adafruit MQTT Library Arduino Yun Example + + Make sure your Arduino Yun is connected to a WiFi access point which + has internet access. Also note this sketch uses the Console class + for debug output so make sure to connect to the Yun over WiFi and + open the serial monitor to see the console output. + + Works great with the Arduino Yun: + ----> https://www.adafruit.com/products/1498 + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Tony DiCola for Adafruit Industries. + MIT license, all text above must be included in any redistribution + ****************************************************/ +#include +#include +#include +#include "Adafruit_MQTT.h" +#include "Adafruit_MQTT_Client.h" + + +/************************* Adafruit.io Setup *********************************/ + +#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..." + +/************ Global State (you don't need to change this!) ******************/ + +// Create a YunClient instance to communicate using the Yun's brighe & Linux OS. +YunClient client; + +// Store the MQTT server, client ID, username, and password in flash memory. +// This is required for using the Adafruit MQTT library. +const char MQTT_SERVER[] PROGMEM = AIO_SERVER; +// Set a unique MQTT client ID using the AIO key + the date and time the sketch +// was compiled (so this should be unique across multiple devices for a user, +// alternatively you can manually set this to a GUID or other random value). +const char MQTT_CLIENTID[] PROGMEM = AIO_KEY __DATE__ __TIME__; +const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; +const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; + +// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. +Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); + +/****************************** Feeds ***************************************/ + +// Setup a feed called 'photocell' for publishing. +// Notice MQTT paths for AIO follow the form: /feeds/ +const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell"; +Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED); + +// Setup a feed called 'onoff' for subscribing to changes. +const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff"; +Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED); + +/*************************** Sketch Code ************************************/ + +void setup() { + Bridge.begin(); + Console.begin(); + Console.println(F("Adafruit MQTT demo")); + + // Setup MQTT subscription for onoff feed. + mqtt.subscribe(&onoffbutton); +} + +uint32_t x=0; + +void loop() { + // Ensure the connection to the MQTT server is alive (this will make the first + // connection and automatically reconnect when disconnected). See the MQTT_connect + // function definition further below. + MQTT_connect(); + + // Try to ping the MQTT server + /* + if (! mqtt.ping(3) ) { + // MQTT pings failed, lets reconnect + Console.println("Ping fail!"); + } + */ + + // this is our 'wait for incoming subscription packets' busy subloop + Adafruit_MQTT_Subscribe *subscription; + while ((subscription = mqtt.readSubscription(1000))) { + if (subscription == &onoffbutton) { + Console.print(F("Got: ")); + Console.println((char *)onoffbutton.lastread); + } + } + + // Now we can publish stuff! + Console.print(F("\nSending photocell val ")); + Console.print(x); + Console.print("..."); + if (! photocell.publish(x++)) { + Console.println(F("Failed")); + } else { + Console.println(F("OK!")); + } + + delay(1000); +} + +// Function to connect and reconnect as necessary to the MQTT server. +// Should be called in the loop function and it will take care if connecting. +void MQTT_connect() { + int8_t ret; + + // Stop if already connected. + if (mqtt.connected()) { + return; + } + + Console.print("Connecting to MQTT... "); + + while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected + Console.println(mqtt.connectErrorString(ret)); + Console.println("Retrying MQTT connection in 5 seconds..."); + mqtt.disconnect(); + delay(5000); // wait 5 seconds + } + Console.println("MQTT Connected!"); +} diff --git a/library.properties b/library.properties index 7656e01..e8bea1c 100644 --- a/library.properties +++ b/library.properties @@ -1,8 +1,8 @@ name=Adafruit MQTT Library -version=0.10.0 +version=0.11.0 author=Adafruit maintainer=Adafruit -sentence=MQTT library that supports the CC3000, FONA, ESP8266, and generic Arduino Client hardware. +sentence=MQTT library that supports the CC3000, FONA, ESP8266, Yun, and generic Arduino Client hardware. paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics. category=Communication url=https://github.com/adafruit/Adafruit_MQTT_Library