@Control ,
I have it done with a other code from Emonlib.
Using the Custom Sensor Component — ESPHome
I get nice readings inside ESPHome.
(I only connect a voltage not current)
You need to put in de EMonlib.h and Emonlib.ccp inside ESPHome Folder,
Then creat the file like “powersensor.h” and put there the code created from the Arduino code, but then translated into custom code language.
#include "esphome.h"
#include "EmonLib.h"
class MyPowerSensor : public PollingComponent, public Sensor {
public:
EnergyMonitor emon1;
Sensor *realpower_sensor = new Sensor();
Sensor *apparentpower_sensor = new Sensor();
Sensor *powerfactor_sensor = new Sensor();
Sensor *supplyvoltage_sensor = new Sensor();
MyPowerSensor() : PollingComponent(1000) { }
void setup() override {
emon1.voltage(A0, 380.5, 1.7);
}
void update() override {
emon1.calcVI(20,2000);
float realPower = emon1.realPower;
realpower_sensor->publish_state(realPower);
float apparentPower = emon1.apparentPower;
apparentpower_sensor->publish_state(apparentPower);
float powerFactor = emon1.powerFactor;
powerfactor_sensor->publish_state(powerFactor);
float supplyVoltage = emon1.Vrms;
supplyvoltage_sensor->publish_state(supplyVoltage);
}
};
//
//
THE 380.5 IS THE CALIBRATION FACTOR!
USE YOUR OWN VALUE
//
//
Then create a yaml file, like power.yaml
esphome:
name: power
platform: your Platform
board: your board
includes:
- powersensor.h
- EmonLib.h
- EmonLib.cpp
wifi:
ssid: "SSID"
password: "secretpassword"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Power Fallback Hotspot"
password: "FCV8QTAaFy1K"
captive_portal:
# Enable logging
logger:
# Enable Home Assistant API
api:
password: ""
ota:
password: ""
sensor:
- platform: custom
lambda: |-
auto my_sensor = new MyPowerSensor();
App.register_component(my_sensor);
return {my_sensor->realpower_sensor, my_sensor->apparentpower_sensor, my_sensor->powerfactor_sensor, my_sensor->supplyvoltage_sensor};
sensors:
- name: "RealPowerSensor"
unit_of_measurement: W
accuracy_decimals: 2
- name: "ApparentPowerSensor"
unit_of_measurement: VA
accuracy_decimals: 2
- name: "PowerFactor"
accuracy_decimals: 2
- name: "supplyVoltageSensor"
unit_of_measurement: VA
accuracy_decimals: 2
I’m now busy with a other code, better calibration and better readings.
I used a esp8266 but the esp32 is better! (you need to adjust the PIN from A0 → 34)
Also reminder that you better use a Logic Level Shifter
When you are using the esp8266
And also better to use a power suply directly on the zmpt101b/ESP8266 (or ESP32)
for better stable and acurate readings.
Litle update, I noticed that when I shut short the Voltage, the unit goes into a strange error…
Need to fix.