On an Arduino there are two different libraries you can use to access Adafruit IO. One library is based on the REST API, and the other library is based on the MQTT API. The difference between these libraries is that MQTT keeps a connection to the service open so it can quickly respond to feed changes. The REST API only connects to the service when a request is made so it's a more appropriate choice for projects that sleep for a period of time (to reduce power usage) and wake up only to send/receive data. If you aren't sure which library to use, try starting with the Adafruit MQTT library below.
Adafruit MQTT Client Library
To use Adafruit IO with the MQTT protocol on an Arduino you can use the Adafruit MQTT Arduino library. This is a general-purpose MQTT library for Arduino that's built to use as few resources as possible so that it can work with platforms like the Arduino Uno. Unfortunately platforms like the Trinket 3.3V or 5V (based on the ATtiny85) have too little program memory to use the library--stick with a Pro Trinket or better!
The Adafruit MQTT library currently supports the following networking hardware/platforms:
- Adafruit CC3000
- Adafruit FONA
- ESP8266 Arduino
- Generic Arduino Client Interface (including Ethernet shield and similar network hardware)
To install the library you can use the Arduino library manager or download the library from GitHub and manually install it.
On some platforms the Adafruit MQTT library uses the hardware watchdog to help ensure sketches run reliably. You'll need to install the Adafruit SleepyDog sleep and watchdog library, again using either the Arduino library manager or with a manual install.
Finally make sure you have any required libraries for your network hardware installed, such as the CC3000 library or FONA library.
Once the library is installed open or restart the Arduino IDE and check out the example code included with the library. These examples show the basic usage of the library, like how to connect to Adafruit IO, subscribe to receive changes to a feed, and how to send values to a feed.
PubSubClient MQTT Library
Another popular MQTT library for the Arduino is the PubSubClient MQTT library and it works great to access Adafruit IO. Note that the library only works with networking libraries that support the Arduino Client interface. This means the library will work with the Ethernet shield, CC3000 or even ESP8266 Arduino, but not the FONA platform because it does not have a Client interface.
Below is a small example that shows using the PubSubClient library with the CC3000. To use this you will need the Adafruit CC3000 library and PubSubClient library installed in Arduino.
Note that you'll need to change the following #define configuration lines at the top to fill in your wireless AP connection details and Adafruit IO username, key, and feed name:
#define WLAN_SSID "... your WiFi SSID..." #define WLAN_PASS "... your WiFi password..." #define ADAFRUIT_USERNAME "... your Adafruit username (see accounts.adafruit.com)..." #define AIO_KEY "... your Adafruit IO key..." #define FEED_PATH ADAFRUIT_USERNAME "/feeds/feed-name/"
The FEED_PATH is the path to publish or subscribe to for interacting with a feed. Notice that the path starts with the Adafruit account name and is followed by "/feeds/feed-name" (where "feed-name" is the name of the feed).
For example if your account name was Mosfet and you were accessing a feed called Photocell the full path would look like "Mosfet/feeds/Photocell".
Below is the full example source:
//Example modified from the pubsubclient library linked above #include <Adafruit_CC3000.h> #include <ccspi.h> #include <SPI.h> #include <PubSubClient.h> #define aref_voltage 3.3 // These are the interrupt and control pins #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! // These can be any two pins #define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_CS 10 // Use hardware SPI for the remaining pins // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER); #define WLAN_SSID "... your WiFi SSID..." #define WLAN_PASS "... your WiFi password..." // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 #define WLAN_SECURITY WLAN_SEC_WPA2 #define ADAFRUIT_USERNAME "... your Adafruit username (see accounts.adafruit.com)..." #define AIO_KEY "... your Adafruit IO key..." #define FEED_PATH ADAFRUIT_USERNAME "/feeds/feed-name/" Adafruit_CC3000_Client client = Adafruit_CC3000_Client(); PubSubClient mqttclient("io.adafruit.com", 1883, callback, client); void callback (char* topic, byte* payload, unsigned int length) { Serial.println(topic); Serial.write(payload, length); Serial.println(""); } void setup(void) { Serial.begin(115200); Serial.println(F("Hello, CC3000!\n")); // If you want to set the aref to something other than 5v analogReference(EXTERNAL); Serial.println(F("\nInitialising the CC3000 ...")); if (!cc3000.begin()) { Serial.println(F("Unable to initialise the CC3000! Check your wiring?")); for(;;); } uint16_t firmware = checkFirmwareVersion(); if (firmware < 0x113) { Serial.println(F("Wrong firmware version!")); for(;;); } displayMACAddress(); Serial.println(F("\nDeleting old connection profiles")); if (!cc3000.deleteProfiles()) { Serial.println(F("Failed!")); while(1); } /* Attempt to connect to an access point */ char *ssid = WLAN_SSID; /* Max 32 chars */ Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid); /* NOTE: Secure connections are not available in 'Tiny' mode! */ if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { Serial.println(F("Failed!")); while(1); } Serial.println(F("Connected!")); /* Wait for DHCP to complete */ Serial.println(F("Request DHCP")); while (!cc3000.checkDHCP()) { delay(100); // ToDo: Insert a DHCP timeout! } /* Display the IP address DNS, Gateway, etc. */ while (!displayConnectionDetails()) { delay(1000); } // connect to the broker, and subscribe to a path if (mqttclient.connect(ADAFRUIT_USERNAME, AIO_KEY, "")) { Serial.println(F("MQTT Connected")); mqttclient.subscribe(FEED_PATH); } } void loop(void) { // are we still connected? if (!mqttclient.connected()) { mqttclient.subscribe(FEED_PATH); mqttclient.publish(FEED_PATH, "11"); } else { mqttclient.publish(FEED_PATH, "11"); } mqttclient.loop(); delay(250); } /**************************************************************************/ /*! @brief Tries to read the CC3000's internal firmware patch ID */ /**************************************************************************/ uint16_t checkFirmwareVersion(void) { uint8_t major, minor; uint16_t version; #ifndef CC3000_TINY_DRIVER if(!cc3000.getFirmwareVersion(&major, &minor)) { Serial.println(F("Unable to retrieve the firmware version!\r\n")); version = 0; } else { Serial.print(F("Firmware V. : ")); Serial.print(major); Serial.print(F(".")); Serial.println(minor); version = major; version <<= 8; version |= minor; } #endif return version; } /**************************************************************************/ /*! @brief Tries to read the 6-byte MAC address of the CC3000 module */ /**************************************************************************/ void displayMACAddress(void) { uint8_t macAddress[6]; if(!cc3000.getMacAddress(macAddress)) { Serial.println(F("Unable to retrieve MAC Address!\r\n")); } else { Serial.print(F("MAC Address : ")); cc3000.printHex((byte*)&macAddress, 6); } } /**************************************************************************/ /*! @brief Tries to read the IP address and other connection details */ /**************************************************************************/ bool displayConnectionDetails(void) { uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) { Serial.println(F("Unable to retrieve the IP Address!\r\n")); return false; } else { Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); Serial.println(); return true; } }
Adafruit IO REST Client Library
The Adafruit IO Arduino library is a simple library for sending and receiving the latest value for a feed. This library uses the send and last Adafruit IO REST API calls and takes care of all the work to use the Adafruit IO REST API.
The REST client library supports the following networking platforms/hardware:
- Adafruit CC3000
- Adafruit FONA
- ESP8266 Arduino
- Generic Arduino Client Interface (including Ethernet shield and similar network hardware)
To install the library you can use the Arduino library manager or download the library from GitHub and manually install it.
Finally make sure you have any required libraries for your network hardware installed, such as the CC3000 library or FONA library.
Once the library is installed open or restart the Arduino IDE and check out the example code included with the library. These examples show the basic usage of the library, like how to connect send or receive the latest value for a feed.
Text editor powered by tinymce.