AdafruitAIOFeed is an optional helper class based on AdafruitMQTTTopic. It aims to make working with feeds in Adafruit IO a bit easier, with the goal of implementing specialised classes that correspond to AIO feed types in the future.

Be sure to look at the documentation for AdafruitMQTTTopic as well, since this class is a specialized version of that aimed at Adafruit IO.

Constructor

AdafruitAIOFeed uses the following constructor:

AdafruitAIOFeed(AdafruitAIO* aio, 
                const char* feed, 
                uint8_t qos = MQTT_QOS_AT_MOST_ONCE, 
                bool retain = true)

Parameters:

  • aio: A reference to the AdafruitAIO class instance, which will be used when sending and receiving data to the AIO server.
  • feed: A string containing the name of the AIO feed to work with, minus the 'username/feeds/' text which will be automatically added by this class.  For example, to work with 'testuser/feeds/status' you would provide 'status' to the feed parameter.
  • qos: An optional quality of server (QoS) level to use when publishing.  If left empty, this argument will default to 'At Most Once', meaning it will try to publish the data but if the operation fails it won't persist the attempt and retry again later.
  • retain: Sets the 'retain' bit to indicate if any messages published to the MQTT broker should be retained on the broker for the next client(s) that access that topic. By default this will be set to 'true' for AIO feeds.

Functions

The following functions are defined as part of AdafruitAIOFeed, but you also have access to the public functions that are defined in AdafruitMQTTTopic since AdafruitAIOFeed class inherits from it.

bool follow   ( feedHandler_t fp )
bool unfollow ( void )
bool followed ( void )
  
virtual size_t write ( const uint8_t *buf, size_t len )
virtual size_t write ( uint8_t ch )

bool follow (feedHandler_t fp)

Enables you to 'follow' this feed, meaning that you subscribe to any changes that are published to this feed on the AIO server.  To follow the feed, you simple set the callback handler, which is the function that will be called when this feed changes in AIO.

Parameters:

  • fp: The callback handler function that will be fired when the feed changes on the AIO server.  This function should have the following signature:
The name of the callback handler function can be set to anything you like, although the parameters and return type must be identical.
void feed_callback(UTF8String message)
{
  Serial.println(message);  
}

Returns: 'True' (1) if the operation was successful, otherwise 'false' (0).

bool unfollow (void)

Calling this function will stop the follow callback and unsubscribe from the feed, meaning any changes will no longer be received by this class.

Parameters: None

Returns: 'True' (1) if the operation was successful, otherwise 'false' (0).

bool followed (void)

Checks whether 'follow' is currently enabled or not (indicate whether or not we are subscribed to the AIO feed).

Parameters: None

Returns: 'True' (1) if the operation was successful, otherwise 'false' (0).

Example

For more examples of working with AdafruitIO and AdafruitIOFeed see the /AIO folder in /examples in the WICED Feather board support package.

/*********************************************************************
 This is an example for our Feather WIFI modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

#include <adafruit_feather.h>
#include <adafruit_mqtt.h>
#include <adafruit_aio.h>

/* This sketch connects to the Adafruit IO server at io.adafruit.com
 * and updates a 'PHOTOCELL_FEED' every 5 seconds.
 *
 * It also follow 'ONOFF_FEED' to receive updates from the AIO server via
 * the built-in follow/subscribe callback handler.
 *
 * To run this demo
 * 1. Change WLAN_SSID/WLAN_PASS
 * 2. Decide whether you want to use TLS/SSL or not (USE_TLS)
 * 3. Change AIO_USERNAME, AIO_KEY to match your own account details
 * 4. If you want, change PHOTOCELL_FEED and ONOFF_FEED to use different feeds
 * 5. Compile and run
 * 6. Optionally log into the AIO webserver to see any changes in data, etc.
 */

#define WLAN_SSID         "yourSSID"
#define WLAN_PASS         "yourPass"

#define AIO_USERNAME      "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY           "...your AIO key..."

// AdafruitAIO will auto append the "username/feeds/" prefix to your feed(s)
#define PHOTOCELL_FEED     "photocell"
#define ONOFF_FEED         "onoff"

// Connect using TLS/SSL or not
#define USE_TLS             0

// Uncomment to set your own ClientID, otherwise a random ClientID is used
//#define CLIENTID          "Adafruit Feather"

AdafruitAIO       aio(AIO_USERNAME, AIO_KEY);
AdafruitAIOFeed   photocell (&aio, PHOTOCELL_FEED);
AdafruitAIOFeed   onoff     (&aio, ONOFF_FEED);

int value = 0;

/**************************************************************************/
/*!
    @brief  The setup function runs once when the board comes out of reset
*/
/**************************************************************************/
void setup()
{
  Serial.begin(115200);

  // Wait for the USB serial port to connect. Needed for native USB port only
  while (!Serial) delay(1);

  Serial.println("AIO Test Example\r\n");

  // Print all software versions
  Feather.printVersions();

  while ( !connectAP() )
  {
    delay(500); // delay between each attempt
  }

  // Connected: Print network info
  Feather.printNetwork();

  // Tell the MQTT client to auto print error codes and halt on errors
  aio.err_actions(true, true);

  // Set ClientID if defined
  #ifdef CLIENTID
  aio.clientID(CLIENTID);
  #endif

  Serial.print("Connecting to io.adafruit.com ... ");
  if ( USE_TLS )
  {
    aio.connectSSL(); // Will halted if an error occurs
  }else
  {
    aio.connect(); // Will halted if an error occurs
  }
  Serial.println("OK");

  // 'Follow' the onoff feed to capture any state changes
  onoff.follow(feed_callback);
}

/**************************************************************************/
/*!
    @brief  This loop function runs over and over again
*/
/**************************************************************************/
void loop()
{
  value = (value+1) % 100;

  Serial.print("Updating feed " PHOTOCELL_FEED " : ");
  Serial.print(value);
  photocell.print(value);
  Serial.println(" ... OK");

  delay(5000);
}

/**************************************************************************/
/*!
    @brief  'follow' event callback handler

    @param  message    The new value associated with this feed

    @note   'message' is a UTF8String (byte array), which means
            it is not null-terminated like C-style strings. You can
            access its data and len using .data & .len, although there is
            also a Serial.print override to handle UTF8String data types.
*/
/**************************************************************************/
void feed_callback(UTF8String message)
{
  // Print message
  Serial.print("[ONOFF Feed] : ");
  Serial.println(message);
}

/**************************************************************************/
/*!
    @brief  Connect to defined Access Point
*/
/**************************************************************************/
bool connectAP(void)
{
  // Attempt to connect to an AP
  Serial.print("Please wait while connecting to: '" WLAN_SSID "' ... ");

  if ( Feather.connect(WLAN_SSID, WLAN_PASS) )
  {
    Serial.println("Connected!");
  }
  else
  {
    Serial.printf("Failed! %s (%d)", Feather.errstr(), Feather.errno());
    Serial.println();
  }
  Serial.println();

  return Feather.connected();
}

This guide was first published on Mar 23, 2016. It was last updated on Mar 26, 2024.

This page (AdafruitAIOFeed) was last updated on Mar 07, 2016.

Text editor powered by tinymce.