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
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
You can also learn more about configuring the Adafruit feather ESP8266 HUZZAH board at:
https://learn.adafruit.com/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/
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/
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
The code starts by importing the required libraries:
#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:
const char* ssid = "wifi-name"; const char* password = "wifi-pass";
You also need to set your Adafruit IO username and key:
#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:
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:
// 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:
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:
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.
Text editor powered by tinymce.