The best tools to make your project dreams come true

Login or Signup
USD

1/9/2019 | By Maker.io Staff

How to Use Adafruit IO with an ESP8266 and the Arduino IDE

Using Python to interact with Adafruit IO is great and all, but Python is pretty much restricted to large computational devices, such as desktop computers and the Raspberry Pi. In this how-to article, we will learn how to use an ESP8266 with Adafruit IO, using an Adafruit Feather Huzzah or any generic ESP8266 board!

BOM

  • ESP8266 board
  • Micro USB B cable
  • Arduino IDE
  • Step 1: Configure Arduino IDE

    First, we need to configure the Arduino IDE so it can talk to our Huzzah board and program it. To do this, go to your Arduino IDE and click File > Preferences. In the preference window, you will need to fill in the “Additional Boards Manager URLs” textbox with the link provided below.

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    http://arduino.esp8266.com/versions/2.4.1/package_esp8266com_index.json

    Then, you will need to go to Tools > Board > Board Manager, and in the manager window that appears, you will need to search for “ESP8266”. Click the result called “esp8266 by ESP8266 Community”, and this should result in the IDE downloading the needed packages. Note that these packages are over 150MB, so be patient!

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    When the board package has downloaded, you will need to configure the IDE to use the ESP8266 board. At this point, you will need to connect your board to the computer via a micro USB cable. When connected, the computer will see the ESP8266 board as a COM port. To find out what number this is, go to Device Manager and then look for devices connected serial devices. In my list of serial devices, I also have an Arduino Uno currently connected, so my ESP8266 is COM17. COM1 is almost always an internal Windows port, so your device will never be COM1!

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    Go back to your IDE and click Tools > Board > Adafruit Huzzah ESP8266 if you are using the Adafruit board. Generic ESP8266 boards should also work, but it is best to choose one that is proven (hence the Huzzah). Once you have selected your board, you will also need to configure the baud rate that you will use for uploaded. 115200 is a safe baud rate and works 100% of the time, but you can try to go higher. Personally, I have had no problem programming at the maximum speed of 921600. To set this option go to Tools > Upload Speed. There are other options, but chances are these don’t require adjusting (such as 80MHz CPU speed and flash size of 4M (1M SPIFFS)).

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    Step 2: Get the Libraries

    Before we see which libraries are needed to get our device talking to Adafruit IO, we need to load the libraries manager. This will allow us to automatically download libraries without needing to do any work. To load this up, go to Sketch > Include Libraries > Manage Libraries.

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    The first library that we need to get is the Adafruit IO library, which can be found by typing “Adafruit IO” into the library managers search bar. Once found, click the library in the list to install it.

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    The second library we need to get is the HTTP Client library, which can be found by searching “http client”.

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    The last library we need to get is the Adafruit MQTT library, which can be found by searching “Adafruit MQTT Library”.

    How to Use Adafruit IO with an ESP8266 and the Arduino IDE

    Step 3: Create our Sketch

    Now that we have our libraries and board set up, we can go and start programming our ESP8266! But instead of learning to add code one line at a time, we will instead copy and paste the code below into a new file. Note that, in this code, you will need to change the SSID, the Wi-Fi password, your Adafruit IO username, and the Adafruit AIO key to whatever yours are.

    Copy Code
    #include <ESP8266WiFi.h>
    #include "AdafruitIO_WiFi.h"
     
    #define WIFI_SSID       "YOUR SSID HERE"
    #define WIFI_PASS       "YOUR WIFI PASSWORD HERE"
     
    #define IO_USERNAME    "YOUR USERNAME HERE"
    #define IO_KEY         "YOUR AIO KEY HERE"
     
    // Connect to Wi-Fi and Adafruit IO handel 
    AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
     
    // Create a feed object that allows us to send data to
    AdafruitIO_Feed *temperatureFeed = io.feed("Workshop temperature");
     
     
    void setup() 
    {
      // Enable the serial port so we can see updates
      Serial.begin(115200);
     
      // Connect to Adafruit IO
      io.connect();
     
      // wait for a connection
      while(io.status() < AIO_CONNECTED) 
      {
        Serial.print(".");
        delay(500);
      }
    }
     
    void loop() 
    {
      // Always keep this at the top of your main loop
      // While not confirmed, this implies that the Adafruit IO library is not event-driven
      // This means you should refrain from using infinite loops
      io.run();
     
      // Send 10 to our Workshop Temperature Feed
      temperatureFeed->save(10);
     
      // This is so we can see some response on the computer
      Serial.print("Data sent");
     
      // DONT SEND A BILLION DATA POINTS! This slows down the program so that it sends readings once every 5 seconds
      delay(5000);
    }
    

    So, let’s break down this code to get a better idea of what’s going on!

    The first section of code will be the same in most of your Adafruit IO projects and is shown below. This gets all the needed libraries loaded, your ESP8266 connected to a specified Wi-Fi network, and then connects the ESP8266 to Adafruit IO.

    Copy Code
    #include <ESP8266WiFi.h>
    #include "AdafruitIO_WiFi.h"
     
    #define WIFI_SSID       "YOUR SSID HERE"
    #define WIFI_PASS       "YOUR WIFI PASSWORD HERE"
     
    #define IO_USERNAME    "YOUR USERNAME HERE"
    #define IO_KEY         "YOUR AIO KEY HERE"
     
    // Connect to Wi-Fi and Adafruit IO handle
    AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
    

    The next piece of code is very important because it creates a feed object. As we know, feeds in Adafruit IO are used to store information such as temperature and state, and each feed has a unique name. Here, we create a variable called “temperatureFeed”, and this is connected to our “Workshop temperature” feed hosted on Adafruit IO.

    Copy Code
    // Create a feed object that allows us to send data to
    AdafruitIO_Feed *temperatureFeed = io.feed("Workshop temperature");
    

    The setup function is in the code section shown below. This will again be common to most of your Adafruit IO projects, and it gets your board connected to Adafruit IO. At the same time, it also waits for the connection to be established before continuing.

    Copy Code
      // Connect to Adafruit IO
      io.connect();
     
      // wait for a connection
      while(io.status() < AIO_CONNECTED) 
      {
        Serial.print(".");
        delay(500);
      }
    

    In your main loop, you must have io.run() at the top. This function ensures that your board is connected to Adafruit IO. This function is non-negotiable, and you must include it!

    Copy Code
     // Always keep this at the top of your main loop
      // While not confirmed, this implies that the Adafruit IO library is not event-driven
      // This means you should refrain from using infinite loops
      io.run();
    

    This line of code sends the number 10 to our temperature feed. You can see here how simple it is to upload data to Adafruit IO!

    Copy Code
      // Send 10 to our Workshop Temperature Feed
      temperatureFeed->save(10);
    

    The last line of code is very important, despite being a simple delay. Adafruit IO limits how much data you’re allowed to send, so this prevents your device from using up your bandwidth allocation too quickly!

    Copy Code
    // DONT SEND A BILLION DATA POINTS! This slows down the program so that it sends readings once every 5 seconds
      delay(5000);