Arduino Code

You will need the Adafruit IO Arduino library installed to compile the example sketch. The easiest way to install the library is by using the Arduino IDE v.1.6.4+ Library Manager, and searching for Adafruit IO Arduino.

You will also need to have the ESP8266 Arduino board package installed. For more info about installing the ESP8266 package, visit the HUZZAH ESP8266 setup guide.

Use the package index URL above to get the latest version of the ESP8266 package.

You must have your account set up first before you try to connect!

Arduino Sketch

The Arduino sketch for this project is fairly straight forward. If you haven't downloaded or cloned the Adafruit IO Basics GitHub repo, you should do that first. We will be using the trigger sketch located in the ESP8266 folder.

The first thing you will need to do is to edit the WiFi connection info at the top of the ESP8266 trigger sketch. You can use the same connection info you used when you tested your WiFi connection in the HUZZAH ESP8266 setup guide.

#define WLAN_SSID       "...your SSID..."
#define WLAN_PASS       "...your password..."

You will need to replace the Adafruit IO key placeholder in the sketch with the Adafruit IO key that you retrieved in the Adafruit IO Setup section of this guide.

#define AIO_KEY         "...your AIO key..."

By default, the sketch sends the battery level to Adafruit IO once every 5 minutes, and checks the state of the door once every 3 seconds. You can modify these intervals by changing the BATTERY_INTERVAL and SLEEP_LENGTH constants. Setting these constants to lower intervals will result in reduced battery life.

// how often to report battery level to adafruit IO (in minutes)

// how long to sleep between checking the door state (in seconds)
#define SLEEP_LENGTH 3

The bulk of the work for this example happens in the setup() function. The sketch restarts after it wakes from sleep, so the main loop() never runs.

void setup() {
  Serial.println("HUZZAH Trigger Basic");

  // get the current count position from eeprom
  byte battery_count =;

  // we only need this to happen once every X minutes,
  // so we use eeprom to track the count between resets.
  if(battery_count >= ((BATTERY_INTERVAL * 60) / SLEEP_LENGTH)) {
    // reset counter
    battery_count = 0;
    // report battery level to Adafruit IO
  } else {
    // increment counter

  // save the current count
  EEPROM.write(0, battery_count);

  // if door isn't open, we don't need to send anything
  if(digitalRead(DOOR) == LOW) {
    Serial.println("Door closed");
    // we don't do anything
  } else {
    // the door is open if we have reached here,
    // so we should send a value to Adafruit IO.
    Serial.println("Door is open!");

  // we are done here. go back to sleep.
  ESP.deepSleep(SLEEP_LENGTH * 1000000, WAKE_RF_DISABLED);

The code that sends the state of the door to Adafruit IO can be found in the door_open() function.

void door_open() {

  // turn on wifi if we aren't connected
  if(WiFi.status() != WL_CONNECTED) {
  // grab the door feed
  Adafruit_IO_Feed door = aio.getFeed("door");

  Serial.println("Sending to");
  // send door open value to AIO


The battery level is also sent to Adafruit IO once every 5 minutes. You can increase this interval using the BATTERY_INTERVAL constant, but it will reduce your battery life.

void battery_level() {

  // read the battery level from the ESP8266 analog in pin.
  // analog read level is 10 bit 0-1023 (0V-1V).
  // our 1M & 220K voltage divider takes the max
  // lipo value of 4.2V and drops it to 0.758V max.
  // this means our min analog read value should be 580 (3.14V)
  // and the max analog read value should be 774 (4.2V).
  int level = analogRead(A0);

  // convert battery level to percent
  level = map(level, 580, 774, 0, 100);
  Serial.print("Battery level: "); Serial.print(level); Serial.println("%");
  // turn on wifi if we aren't connected
  if(WiFi.status() != WL_CONNECTED)

  // grab the battery feed
  Adafruit_IO_Feed battery = aio.getFeed("battery");

  // send battery level to AIO


Upload and Test!

OK now that you have everything wired up, and your account set up, upload the above sketch to your HUZZAH ESP8266 board via the FTDI cable or other serial connection.

After uploading, open up the serial port, you should see a long stream of text that looks like this:

What you're seeing is the HUZZAH reading the sensor (Door closed and Door is open!) and then going to sleep (the zzz text indicates its about to go into sleep mode. When the ESP8266 goes to sleep, it resets the board, and so the weird text after the 'zzz' is the reset debug string. Its normal but some terminal programs print it out a little differently.

This program only pushes data to the feed when the door is open, since that's a rare event. So when the door is closed, nothing 'happens'. When you remove the magnet half from the sensor half, you'll get that text that says the door is open, and it will connect to WiFi and update your feed

Log into your account and look at the door feed to see the messages as they are logged!

After 5 minutes, you'll also see the board measure the battery voltage and upload that to the seperate battery feed.

Last updated on 2015-10-19 at 05.36.13 PM Published on 2015-10-19 at 05.38.44 PM