We are now going to configure the project we just assembled. On the software side, you will need the latest version of the Arduino IDE, that you can get from:


To be able to use the ESP8266, you will need to have the ESP8266 boards definitions installed. You can add those definitions to your Arduino IDE by following the instructions at:


You can also learn more about configuring the Adafruit feather ESP8266 HUZZAH board at:


You will also need the following Arduino libraries, that you can install from the Arduino library manager:

  • Adafruit INA219
  • Adafruit SSD1306
  • Adafruit MQTT library
  • Adafruit GFX library
  • Adafruit BusIO

To learn how to install Arduino libraries, you can also check:


You will also need to have an Adafruit IO account. You can learn more about Adafruit IO & how to create an account at:


Let's now take care of the code for this project. As the code is quite long, I will only highlight the most important parts here, but you can find the complete code at:


The code starts by importing the required libraries:

Download: file
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_INA219.h>

Then, you need to set your WiFi network name & password:

Download: file
const char* ssid     = "wifi-name";
const char* password = "wifi-pass";

You also need to set your Adafruit IO username and key:

Download: file
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "username"
#define AIO_KEY         "key"

Next, we define a feed called 'power' that will hold the measurements made by the board:

Download: file
const char POWER_FEED[] PROGMEM = AIO_USERNAME "/feeds/power";
Adafruit_MQTT_Publish power = Adafruit_MQTT_Publish(&mqtt, POWER_FEED);

Inside the loop() function, we continuously switch the LED on & off, and at the same time measure the power, send it to Adafruit IO, and display it on the OLED screen:

Download: file
  digitalWrite(14, HIGH);

  // Measure
  current_mA = measureCurrent();
  power_mW = measurePower();

  // Publish data
  if (! power.publish(power_mW)) {
  } else {

  // Display data
  displayData(current_mA, power_mW);

Note that here, we continuously switch the state of the LED, just to show the changes in the measured power on the OLED screen.

Of course, if you are using the project to measure the power flowing through a device not connected to the ESP8266, you will simply measure the power at regular intervals in the loop() function.

Here are the details of the function used to measure the power:

Download: file
float measurePower() {

  // Measure
  float shuntvoltage = ina219.getShuntVoltage_mV();
  float busvoltage = ina219.getBusVoltage_V();
  float current_mA = ina219.getCurrent_mA();
  float loadvoltage = busvoltage + (shuntvoltage / 1000);
  Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");

  // If negative, set to zero
  if (current_mA < 0) {
    current_mA = 0.0; 
  return current_mA * loadvoltage;

And here are the details of the function used to display the data on the OLED screen:

Download: file
void displayData(float current, float power) {

  // Clear

  // Power
  display.println("Power: ");
  display.println(" mW");

  // Displays

Note that due to the small size of the screen, I am not displaying the current here, but you could also use this information instead of the power.

This guide was first published on May 21, 2016. It was last updated on May 21, 2016.

This page (Configuring the Project) was last updated on Apr 08, 2021.

Text editor powered by tinymce.