# Build a Cloud-Connected ESP8266 Power Meter

## Introduction

Controlling the electrical consumption in your home is one of the most important thing you can do, both because of environmental concerns & to reduce the electricity bill at the end of the month.&nbsp;There are countless of electrical power meters out there, but in this guide, I’ll show you how to build your own, and to use the ESP8266 feather board&nbsp;to&nbsp;measure&nbsp;how much power a single device is using. Note that this guide&nbsp;is about measuring power for DC (Direct Current) devices only.

![](https://cdn-learn.adafruit.com/assets/assets/000/032/544/medium800thumb/sensors_ezgif.com-gif-maker.jpg?1463642571)

Here, we are going to do something different: we are going to measure the power used&nbsp;by a device, and then display it right on top of&nbsp;the ESP8266&nbsp;board, user the featherwing OLED add-on board. This way, you’ll be able to build your own power meter based on the ESP8266, that is completely independent from any external&nbsp;components. As an additional function, we'll also send the data on Adafruit IO so it can be monitored online. Let’s start!

# Build a Cloud-Connected ESP8266 Power Meter

## Hardware Configuration

Let’s first see what we need to build this project. As the center of the project, I used the Adafruit Feather HUZZAH ESP8266 board.

To measure the voltage & current of the measured device, I used an Adafruit breakout board for the INA219 sensor. To display the data, I used the Adafruit featherwing 128×32&nbsp;OLED add-on, as it is very easy to use with the ESP8266 feather board.

As a test device, I will use a simple LED, along with a 330 Ohm resistor. But this could also be any DC device you are using in your home, for example a strip of LEDs.

Of course, you will need the usual breadboard & jumper wires to make the necessary connections.

Let’s now see how to assemble the hardware for this project. Here is a schematic to help you out:

![](https://cdn-learn.adafruit.com/assets/assets/000/032/563/medium800/sensors_schematic.png?1463823887)

First, put the ESP8266 board on the breadboard, and mount the featherwing OLED add-on on it.

For the INA219 sensor, you first need to connect the power (VCC & GND) to the ESP8266&nbsp;power. For convenience, I connected the ESP8266&nbsp;VCC & GND pins to the two power rails of the breadboard. Then, you need to connect the SCL pin to the ESP8266&nbsp;SCL pin, and the SDA pin to SDA pin on the ESP8266.

Finally, for the LED, we need to make the current of the LED ‘flow’ through the INA219 sensor, so it can be measured. To do so, first connect pin 12 of the ESP8266&nbsp;board to the Vin+ pin of the sensor. Then, connect the Vin- pin of the sensor to the resistor, in series with the anode of the LED. Finally, connect the other pin of the LED to the ground.

This is the final result:

![](https://cdn-learn.adafruit.com/assets/assets/000/032/451/medium800/sensors_hw.jpg?1463051165)

# Build a Cloud-Connected ESP8266 Power Meter

## Configuring the Project

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:

[https://www.arduino.cc/en/Main/Software](https://www.arduino.cc/en/Main/Software)

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:

[https://github.com/esp8266/Arduino](https://github.com/esp8266/Arduino)

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

[https://learn.adafruit.com/adafruit-feather-huzzah-esp8266/](../../../../adafruit-feather-huzzah-esp8266/)

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:

[https://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use/](../../../../adafruit-all-about-arduino-libraries-install-use/)

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

[https://learn.adafruit.com/adafruit-io/](../../../../adafruit-io/)

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:

[https://github.com/openhomeautomation/power-meter-esp8266](https://github.com/openhomeautomation/power-meter-esp8266)

The code starts by importing the required libraries:

```auto
#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:

```auto
const char* ssid     = "wifi-name";
const char* password = "wifi-pass";
```

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

```auto
#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:

```auto
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:

```auto
// LED ON
  digitalWrite(14, HIGH);

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

  // Publish data
  if (! power.publish(power_mW)) {
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("OK!"));
  }

  // Display data
  displayData(current_mA, power_mW);
  delay(2000);
```

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:

```auto
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");
  Serial.println("");

  // 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:

```auto
void displayData(float current, float power) {

  // Clear
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);

  // Power
  display.setCursor(0,0);
  display.println("Power: ");
  display.print(power);
  display.println(" mW");

  // Displays
  display.display();
  
}
```

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.

# Build a Cloud-Connected ESP8266 Power Meter

## Testing the Project

It's now time to finally test the project! You can of course grab the complete code from the GitHub repository of the project:

[https://github.com/openhomeautomation/power-meter-esp8266&nbsp;](https://github.com/openhomeautomation/power-meter-esp8266)

Make sure to modify the code to include your own WiFi name and password, and also your Adafruit IO credentials.

You can now upload the code to the board. Make sure that you selected the correct board (Adafruit HUZZAH ESP8266) inside the Arduino IDE.

You should observe the following result on the board:

![](https://cdn-learn.adafruit.com/assets/assets/000/032/452/medium800thumb/sensors_ezgif.com-gif-maker.jpg?1463051422)

Now, remember that the project is also sending data to Adafruit IO! To visualise the incoming data sent by the board, log into Adafruit IO, and navigate to the 'feeds' menu. You should see a feed called 'power', as this is the name we set inside the code.

If you visualise the feed from Adafruit IO, it should like the following graph, as the LED is switching on & off continuously:

![](https://cdn-learn.adafruit.com/assets/assets/000/032/454/medium800/sensors_Screen_Shot_2016-05-11_at_14.43.24.png?1463051500)

Before ending this guide, I will show you how to actually display the current power consumption of the device inside a dashboard. For that, open a dashboard on Adafruit IO or create a new one, and create a text block:

![](https://cdn-learn.adafruit.com/assets/assets/000/032/455/medium800/sensors_Screen_Shot_2016-05-11_at_14.44.13.png?1463051519)

After that, link this block to the 'power' feed:

![](https://cdn-learn.adafruit.com/assets/assets/000/032/456/medium800/sensors_Screen_Shot_2016-05-11_at_14.45.46.png?1463051531)

You should now see the instant consumption of the device connected to your project. For example, you should see 0.00 when the LED is off:&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/032/457/medium800/sensors_Screen_Shot_2016-05-11_at_14.46.18.png?1463051541)

This is the same text block, showing the power&nbsp;consumption when the LED is on:

![](https://cdn-learn.adafruit.com/assets/assets/000/032/458/medium800/sensors_Screen_Shot_2016-05-11_at_14.46.23.png?1463051549)

# Build a Cloud-Connected ESP8266 Power Meter

## How to Go Further

In this guide, you learned how to build a power meter based on the ESP8266, using a featherwing OLED add-on to display the measured power. The project was also sending data directly to Adafruit IO.

You can now of course take what you learned in this article and use it in several other projects. You can for example use this project on several devices in your home, and have them all display the current power used by the device, while monitoring all the devices from an Adafruit IO dashboard.


## Featured Products

### Adafruit Feather HUZZAH with ESP8266 - Loose Headers

[Adafruit Feather HUZZAH with ESP8266 - Loose Headers](https://www.adafruit.com/product/2821)
Feather is the new development board from Adafruit, and like its namesake, it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller cores.

This is the&nbsp; **Adafruit Feather HUZZAH ESP8266** &nbsp;- our take on an...

In Stock
[Buy Now](https://www.adafruit.com/product/2821)
[Related Guides to the Product](https://learn.adafruit.com/products/2821/guides)
### Adafruit FeatherWing OLED - 128x32 OLED Add-on For Feather

[Adafruit FeatherWing OLED - 128x32 OLED Add-on For Feather](https://www.adafruit.com/product/2900)
A Feather board without ambition is a Feather board without FeatherWings! This is the **FeatherWing OLED** : it adds a 128x32 monochrome OLED plus 3 user buttons to _any_ Feather main board. Using our [Feather Stacking...](https://www.adafruit.com/products/2830)

In Stock
[Buy Now](https://www.adafruit.com/product/2900)
[Related Guides to the Product](https://learn.adafruit.com/products/2900/guides)
### INA219  High Side DC Current Sensor Breakout - 26V ±3.2A Max

[INA219  High Side DC Current Sensor Breakout - 26V ±3.2A Max](https://www.adafruit.com/product/904)
This breakout board will solve all your power-monitoring problems. Instead of struggling with two multimeters, you can just use the handy INA219 chip on this breakout to both measure both the high side voltage and DC current draw over I2C with ±1% precision.

**Please...**

Out of Stock
[Buy Now](https://www.adafruit.com/product/904)
[Related Guides to the Product](https://learn.adafruit.com/products/904/guides)
### Premium Male/Male Jumper Wires - 40 x 6" (150mm)

[Premium Male/Male Jumper Wires - 40 x 6" (150mm)](https://www.adafruit.com/product/758)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

Out of Stock
[Buy Now](https://www.adafruit.com/product/758)
[Related Guides to the Product](https://learn.adafruit.com/products/758/guides)
### Assembled Adafruit FeatherWing OLED

[Assembled Adafruit FeatherWing OLED](https://www.adafruit.com/product/3045)
A Feather board without ambition is a Feather board without FeatherWings! This is the **Assembled FeatherWing OLED** : it adds a 128x32 monochrome OLED plus 3 user buttons to _any_ Feather main board. Comes fully assembled so you can connect a FeatherWing on top of your...

In Stock
[Buy Now](https://www.adafruit.com/product/3045)
[Related Guides to the Product](https://learn.adafruit.com/products/3045/guides)

## Related Guides

- [Adafruit Feather HUZZAH ESP8266](https://learn.adafruit.com/adafruit-feather-huzzah-esp8266.md)
- [Adafruit OLED FeatherWing](https://learn.adafruit.com/adafruit-oled-featherwing.md)
- [CircuitPython Hardware: LED Backpacks & FeatherWings](https://learn.adafruit.com/micropython-hardware-led-backpacks-and-featherwings.md)
- [MicroPython Hardware: SPI Devices](https://learn.adafruit.com/micropython-hardware-spi-devices.md)
- [USB MIDI Host Messenger](https://learn.adafruit.com/usb-midi-host-messenger.md)
- [Feather Waveform Generator in CircuitPython](https://learn.adafruit.com/waveform-generator.md)
- [Pro Trinket Power Meter](https://learn.adafruit.com/pro-trinket-power-meter.md)
- [CircuitPython 2FA TOTP Authentication Friend](https://learn.adafruit.com/circuitpython-totp-otp-2fa-authy-authenticator-friend.md)
- [MicroPython Displays: Drawing Shapes](https://learn.adafruit.com/micropython-displays-drawing-shapes.md)
- [7 Segment Display Internet Clock](https://learn.adafruit.com/7-segment-display-internet-clock.md)
- [All the Internet of Things - Episode Three: Services](https://learn.adafruit.com/all-the-internet-of-things-episode-three-services.md)
- [3D Printed IoT On Air Sign for Twitch](https://learn.adafruit.com/3d-printed-iot-on-air-sign-for-twitch.md)
- [MicroPython Smart Holiday Lights](https://learn.adafruit.com/micropython-smart-holiday-lights.md)
- [CircuitPython Powered AT Hand-Raiser](https://learn.adafruit.com/at-hand-raiser.md)
- [Introducing Adafruit Feather](https://learn.adafruit.com/adafruit-feather.md)
- [MAC Address Finder](https://learn.adafruit.com/mac-address-finder.md)
- [LoRa Signal Bridge with the Feather RP2040 RFM ](https://learn.adafruit.com/lora-signal-bridge-with-the-feather-rp2040-rfm.md)
