The best tools to make your project dreams come true

Login or Signup
USD

6/17/2019 | By Maker.io Staff

How to Use Adafruit IO with Particle Photons

The Particle Photon is a very useful prototyping IoT platform, but unless you create your own IoT service to receive data, you will find that your Photon is not much more than an Arduino Uno. In this tutorial, we will learn how to use the Particle Photon with the Adafruit IO IoT service, so you can log data!

Include the Adafruit.IO library

The first step in using the Adafruit.io framework with the Particle Photon is to create a new project on the online IDE (build.particle.io), then navigate to libraries > search adafruit_io, select “Adafruit_IO_Particle”, and click “Include in project”.

How to Use Adafruit.IO with Particle Photons  

Adafruit.IO Class Functions

There are many functions to learn about, but we will just cover the essentials and some data types. (Note: AIO stands for Adafruit.IO and is easier to write in code.)

  • Adafruit_IO_Client(tcpClient, AIO_KEY) – Used to create an AIO client
  • Adafruit_IO_Feed – A data type used to interact with feeds
  • .begin() – Starts the AIO object and connects to the server
  • .getFeed(feed name as string) – Assigns a specific feed to a feed object
  • .send(value) – Sends a value to a feed object
  • .receive() – Receive a value from a feed object
  • .isValue() – Checks to see if received data is valid

When using the Adafruit IO class, you will need to create an AIO class, a TCP client class, and connect the TCP class to the AIO class. This code is often placed at the top and outside of any functions so that it is global in your code. The code below is a simple example of how it can be done.

Copy Code
#include "Adafruit_IO_Client.h"				// Adafruit IO Library
 
#define AIO_KEY "5be5de5d0e3c4d7a92c90dc7058fb91e"       // Adafruit IO AIO Key
TCPClient client;                                        // TCP Client used by Adafruit IO library
 
// Create the AIO client object
Adafruit_IO_Client  AIOClient = Adafruit_IO_Client(client, AIO_KEY);
 
// Create a feed object to send and get data
Adafruit_IO_Feed    testFeed = AIOClient.getFeed("temperature");

This example shows how a define is used to store the AIO key, which makes using the key in the code easier to do. The key and TCP client are passed to the AIO object - called AIOClient - using the Adafruit_IO_Client(client, AIO_KEY) function, and an AIO feed object is also created here.

For the AIO object to be used, it needs to be started using the function .begin(). No variables are passed in this function, and it is only called once in the setup() function of your Photon.

Copy Code
void setup() 
{
    // Start the Adafruit IO Client
    AIOClient.begin();
}

Feeds

The feed object allows us to save and retrieve data from a feed on Adafruit.IO. Feeds first need to be “assigned” to an AIO object, and this is done at the top of the code (just below the initialization of the library and AIO object). The feed object (that we use to interact with a feed) is pointed to a feed by using the .getFeed function which takes a single parameter: the feed name.

In the top of our example code (previously seen), we assign the feed object called “testFeed” with the feed found on Adafruit.IO called “temperature” using the AIO object. This way, when we access the “testFeed” object, we can interact with the temperature feed via the AIO object.

Copy Code
// Create a feed object to send and get data
Adafruit_IO_Feed    testFeed = AIOClient.getFeed("temperature");

Sending Data to a Feed

Sending data to a feed is very easy to do and uses the .send() function on a feed object. This function takes a string, so to send non-string values, they first need to be converted into a string. To convert numbers into strings, typecasting can be done. The code below shows two examples of how data can be sent (either as a string or as a number).</p>

Copy Code
void loop()
{
testFeed.send(“It is hot in here”);	// Send the string “It is hot in here”
 
testFeed.send(String(20));		// Send the number 20 as a string
 
delay(2000);				// Let’s not access AIO too quickly!	
}

Receiving Data From a Feed

Reading data from a feed is very easy to do and only requires the .receive() function and a temporary FeedData object. Before using that received data further, however, it is good practice to use the .isValid() function (which returns true if the data is valid and usable or false if there was something wrong with the data).

The FeedData object type can be directly used as a string, which makes it useful in string comparisons. The example below shows how a temporary FeedData object, latest, is used to first receive the data from AIO, test the validity, and then print the received data onto a serial port.</p>

Copy Code
void loop()
{  
    FeedData latest = testFeed.receive();	// Get the latest data from “temperature” feed
    
    if(latest.isValid())				// If data is valid then print it!
    {
        Serial.println(latest);
    }
    Else						// Else it was not so print error ?
    {
        Serial.println("error");
    }
}

A complete example

Copy Code
#include <application.h>
#include "Adafruit_IO_Client.h"
 
#define AIO_KEY "5be5de5d0e3c4d7a92c90dc7058fb91e"          // Adafruit IO AIO Key
TCPClient client;                                           // TCP Client used by Adafruit IO library
 
// Create the AIO client object
Adafruit_IO_Client  AIOClient = Adafruit_IO_Client(client, AIO_KEY);
 
// Create a feed object to send and get data
Adafruit_IO_Feed    testFeed = AIOClient.getFeed("temperature");
 
 
void setup() 
{
    // Start the Adafruit IO Client
    AIOClient.begin();
    
    // Start a serial port connection
    Serial.begin(9600);
}
 
void loop()
{
    // Send the number 20 to AIO then wait for 2 seconds
    testFeed.send(String(20));
    delay(2000);
 
    // Grab the latest data from AIO    
    FeedData latest = testFeed.receive();
    
    // Determine if the data is valid
    if(latest.isValid())
    {
        // It was so print it!
        Serial.println(latest);
    }
    else
    {
        // Whoops, something went wrong!
        Serial.println("error");
    }
    
    delay(2000);
}