esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
422 stars 27 forks source link

Add component for BL0910 energy IC #2618

Open MarcoCITA opened 9 months ago

MarcoCITA commented 9 months ago

Describe the problem you have/What new integration you would like

Add support for the BL0910 integrated circuit Please describe your use case for this integration and alternatives you've tried:

Component for IC BL0910 not present. Additional context

latonita commented 9 months ago

are there any devices or breadboards easily available ?

MarcoCITA commented 9 months ago

I don't tink. I only see the expensive and not available kincony M30 (code and schematic on the bottom of the page).

JustNoot commented 8 months ago

I am developing a board for current measurement with this IC. ESPHome would be nice, but I am unfamiliar with component development. If there are some guides I can probably write something usable.

JustNoot commented 8 months ago

I have ordered the PCBs and looked around for some custom components. Looks simple enough, but I will update in a week. Here is the board: image

MarcoCITA commented 8 months ago

PCB Here is mine, hope it works :)

tarontop commented 8 months ago

Athom is currently producing such a product and will release it soon

yiannosi commented 6 months ago

I have ordered the PCBs and looked around for some custom components. Looks simple enough, but I will update in a week. Here is the board: image

@latonita Looks real professional! How did it go? Did it work? By the way.. care to share the schematic?

JustNoot commented 6 months ago

@yiannosi https://github.com/JustNoot/10CH-Energy-Meter The schematic is available. The boards arrived but we decided that this will be better as a Modbus device. For context I am creating a company with a friend and we essentially make smart homes for people. The places we did so far presented us with some challenges that could have been solved easier with a bus system instead of WiFi. So our products will be mainly Modbus and CAN based with an ESP32 controller for smart home integration if needed, this way it could be used in industrial settings as a standalone device with standardized communication. I will open source the schematic and code after testing is done. image image

yiannosi commented 6 months ago

@yiannosi https://github.com/JustNoot/10CH-Energy-Meter The schematic is available. The boards arrived but we decided that this will be better as a Modbus device. For context I am creating a company with a friend and we essentially make smart homes for people.

Thanks for the link. I am also using an RS485 link for most of my home automation devices. This requires wiring when the house was built and I have used cat5e which also carries DC power for the devices (24V). However for those cases that there is no provision for wiring, I use wifi.

jimmyw commented 3 months ago

Hi, i got a branch with a working bl-0910 implementation.

Plan to clean it up, and send a PR. Please help me test it.

https://github.com/jimmyw/esphome/tree/bl0910/esphome/components/bl0910

Example here: https://github.com/jimmyw/esphome/blob/bl0910/bl-0910/bl-0910.yml

jimmyw commented 3 months ago

Here is my board, is designed to be a 4 unit din rail, and they have angled pin headers, that connects up to 6 modules in serial. It was cheaper to produce pcb this way, than produce one that had 30 channels in one.

Got a 3d-printed din rail mount case also ready :)

https://github.com/jimmyw/energymeter/

image

https://github.com/jimmyw/energymeter/blob/main/energymeter.pdf

Less than 100EUR fully populated from JLCPCB for 5 modules.

MarcoCITA commented 3 months ago

@jimmyw what is "MAGIC1 = 12875.0f;" in your code and how can i set the CT rateo in esphome .yaml file?

jimmyw commented 3 months ago

The magics are from the app node, got a google translated version here: https://github.com/jimmyw/energymeter/blob/main/datasheets/BL0910%20APP%20Note_V1.02-eng.pdf And the chineese: https://www.belling.com.cn/media/file_object/bel_product/BL0910/datasheet/BL0910%20APP%20Note_V1.02.pdf

Putting them side by side, makes you able to get the formulas out.

I am currently working on to set the CT-Ratio directly in the yml file per clamp. You also need to specify the burden resistor value, as these some times are included in the CT-Clamp and some times on the pcb.

yiannosi commented 3 months ago

Nice design. i made a similar one based for a 3phase energy meter. It supports Operation of BL0910 on 3U6I mode. It would be nice to be able to configure this mode on esphome as well.

jimmyw commented 3 months ago

Contributions is always welcome!

jimmyw commented 3 months ago

@MarcoCITA Have another look now in my repo, added so you can adjust reference values in the yml file, and also described the formula with examples from 3 different schematics.

jimmyw commented 3 months ago

@yiannosi I removed all assumptions that we are running in 1U10I mode, so now by setting the correct register BL0910_REG_MODE in setup(), this should work with no issues.

I can help you get it working, but then you need to send me a meter to test on :)

jimmyw commented 3 months ago

FYI. Opened up a draft PR https://github.com/esphome/esphome/pull/7402 Needs final testing, and documentation before ready to merge.

jesserockz commented 3 months ago

@jimmyw Please make sure you have seen my comment on your PR https://github.com/esphome/esphome/pull/7402#issuecomment-2325789347

jimmyw commented 3 months ago

@jesserockz, @athom-tech, @tarontop Bad timing, i forked dev and started working on this just this weekend and did not know about the bl0906. (I just searched for bl0910)

They are similar chips, but all the conversions and register values are different, so some kind of abstraction is needed to support both in the same sensor. My sensor was much more basic written. Also the 0906 sensor is UART based and not SPI, even if the chip supports both.

Another thing with the 0906 sensor is all the constants are compiled in assuming burden resistor values, and turns on the CT clamp, i designed mine so you calculate the values and insert them in the yml.

To be honest, i just got my little project up running and working now, i`m not sure i have the time/energy to try to merge this two sensors into one, so i think i need some help from you in that case.

Was hoping just to document and wrap it up, and share in case someone else was interested.

If someone likes to pick this up, or at least help to get started?

jesserockz commented 3 months ago

No, of course. I only wrote that comment in haste to make sure you were aware of it. Since they are quite different it seems then they can stay separate. But please use a similar config structure and update the sensors in a similar way (1 channel per loop())

Ill try leave some inline comments/suggestions on the PR

Please make sure you have read and applied all of the codebase standards for ESPHome too as I can see a few of them not followed =)

jimmyw commented 3 months ago

@jesserockz Please give me some feedback on the docs as well:

https://deploy-preview-4221--esphome.netlify.app/components/sensor/bl0910.html

https://github.com/esphome/esphome-docs/pull/4221/files/4764ac97530e7b1a3906c4c7840696f3062677a0..213bbbe4d701e6f3a6dfb5f5b944d3cae704ec7a

jimmyw commented 3 months ago

@jesserockz I found so many different kind of schematics using this chip, its also supports multiple modes, measuring 1 X Voltage and 10 X Current, or 3 x Votlage, and 6X Current.

Also all the surrounding component choices, and the type of current clamps makes all values differ. I decided to just add a multiplier ref field to every sensor, documenting on how to calculate the values clearly in the documentation.

The other sensor (bl0906) seems to be written with a specific product in mind. Not sure how esphome handle this kind of situations, but i think it would be great with some kind of inheritance system when you can declare a specific product implementation, or just the raw sensor.

Also, kind of great if you could do the constants calculation in the python layer, so that you only assign const values to the code generating the project.

MarcoCITA commented 3 months ago

@jimmyw Tried running the component with the RP2040 but i get [bl0910:341]: Checksum error calculated: 68 != read: 0 for every measure. Log

This is the the code with the ref values for my board.

esphome:
  name: misuratore-energia
  friendly_name: misuratore-energia

rp2040:
  board: rpipicow
  framework:
    platform_version: https://github.com/maxgerhardt/platform-raspberrypi.git

external_components:
  - source: github://pr#7402
    components: [ bl0910 ]

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: ""

ota:
  - platform: esphome
    id: my_ota
    password: ""

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 
    gateway: 
    subnet: 
  power_save_mode: none

spi:
  clk_pin: GPIO18
  mosi_pin: GPIO19
  miso_pin: GPIO16

# BURDEN1 = 3.3 ohm (R2)
# BURDEN2 = 100.0 ohm (R33)
# BURDEN3 = 110.0 kohm (R36 + R37 + R38 + R39 + R40 + R41)
# CT_TURNS = 1000.0

# IREF = (12875.0 * 3.3) / (1.097 * 1000 / 1000.0) = 38730,628988149
# UREF = (13162.0 * 100.0) / (1.097 * 110000 ) = 10,9074334963
# PREF = (40.4125 * 3.3 * 100.0 * 1000.0 / 1000) / (1.097 * 1.097 * 110000) = 0,1007450501
# EREF = (3600000.0 * 16.0 * 0,1007450501) / (4194304.0 * 0.032768 * 16.0) = 2,6388601717

sensor:
  - platform: bl0910
    update_interval: 1s
    cs_pin: GPIO17

    # In mode 0, channel 11 is the voltage reference
    voltage_11:
      name: BL0910 RMS Voltage
      uref: 10.9074334963
    current_1:
      name: BL0910 Current 1
      iref: 38730.628988149
    current_2:
      name: BL0910 Current 2
      iref: 38730.628988149
    current_3:
      name: BL0910 Current 3
      iref: 38730.628988149
    current_4:
      name: BL0910 Current 4
      iref: 38730.628988149
    current_5:
      name: BL0910 Current 5
      iref: 38730.628988149
    current_6:
      name: BL0910 Current 6
      iref: 38730.628988149
    current_7:
      name: BL0910 Current 7
      iref: 38730.628988149
    current_8:
      name: BL0910 Current 8
      iref: 38730.628988149
    current_9:
      name: BL0910 Current 9
      iref: 38730.628988149
    current_10:
      name: BL0910 Current 10
      iref: 38730.628988149

    active_power_1:
      name: BL0910 Active Power 1
      pref: 0.1007450501
    active_power_2:
      name: BL0910 Active Power 2
      pref: 0.1007450501
    active_power_3:
      name: BL0910 Active Power 3
      pref: 0.1007450501
    active_power_4:
      name: BL0910 Active Power 4
      pref: 0.1007450501
    active_power_5:
      name: BL0910 Active Power 5
      pref: 0.1007450501
    active_power_6:
      name: BL0910 Active Power 6
      pref: 0.1007450501
    active_power_7:
      name: BL0910 Active Power 7
      pref: 0.1007450501
    active_power_8:
      name: BL0910 Active Power 8
      pref: 0.1007450501
    active_power_9:
      name: BL0910 Active Power 9
      pref: 0.1007450501
    active_power_10:
      name: BL0910 Active Power 10
      pref: 0.1007450501

    energy_1:
      name: BL0910 Energy 1
      eref: 2.6388601717
    energy_2:
      name: BL0910 Energy 2
      eref: 2.6388601717
    energy_3:
      name: BL0910 Energy 3
      eref: 2.6388601717
    energy_4:
      name: BL0910 Energy 4
      eref: 2.6388601717
    energy_5:
      name: BL0910 Energy 5
      eref: 2.6388601717
    energy_6:
      name: BL0910 Energy 6
      eref: 2.6388601717
    energy_7:
      name: BL0910 Energy 7
      eref: 2.6388601717
    energy_8:
      name: BL0910 Energy 8
      eref: 2.6388601717
    energy_9:
      name: BL0910 Energy 9
      eref: 2.6388601717
    energy_10:
      name: BL0910 Energy 10
      eref: 2.6388601717

    power_factor_1:
      name: BL0910 PowerFactor 1
    power_factor_2:
      name: BL0910 PowerFactor 2
    power_factor_3:
      name: BL0910 PowerFactor 3
    power_factor_4:
      name: BL0910 PowerFactor 4
    power_factor_5:
      name: BL0910 PowerFactor 5
    power_factor_6:
      name: BL0910 PowerFactor 6
    power_factor_7:
      name: BL0910 PowerFactor 7
    power_factor_8:
      name: BL0910 PowerFactor 8
    power_factor_9:
      name: BL0910 PowerFactor 9
    power_factor_10:
      name: BL0910 PowerFactor 10

    frequency:
      name: BL0910 Frequency
    temperature:
      name: BL0910 Temperature
jimmyw commented 3 months ago

Looks like spi error, try lowering the speed and see if that helps

MarcoCITA commented 3 months ago

Looks like spi error, try lowering the speed and see if that helps

Ok, problem solved. I wired incorrectly the PCB. One more question, i wired a 60W light bulb to N.1 but i get 60.000W(60Kw). Current measured seems ok but with wrong power and voltage values i get wrong energy and power factor values. Active power 2,3...10 are empty but they don't show 0 Misuratore

jimmyw commented 3 months ago

I think your IREF is wrong, Maybe you entered kohms instead of ohms somewhere? Try divide your iref value by 10k

I think power of ct 2 and 3, should means 3W and 5W, and that is probably within the error margin for this chip.

I documented the formulas here: https://deploy-preview-4221--esphome.netlify.app/components/sensor/bl0910.html

yiannosi commented 2 months ago

Contributions is always welcome! I am developing code for arduno on ESP32. Have no experience for development for ESPHome However i have an issue for 3U6I mode of operation: The frequency doesnt get reported properly! I suspect the reason is that the chip's Vp,Vn inputs are not used in this mode (as opposed to single phase 1U10I) since the phase voltages are measured in inputs 8,9,10 instead.

image

Anyone has any other ideas?

jimmyw commented 2 months ago

You need to configure the correct mode (3U6I) using the MODE register in the init function. Else it will think it meassures current on the pins instead of voltage i guess.

yiannosi commented 2 months ago

You need to configure the correct mode (3U6I) using the MODE register in the init function. Else it will think it meassures current on the pins instead of voltage i guess.

Yes I already did that. bl0910_write_data(BL0910_REG_MODE, 0x08, 0x00, 0x00); // set 3U6I mode , bit [19] =1 I can measure correct voltage on the voltage pins according to 3U6I . The only problem is the frequency which appears either zero or some other wrong value.

MarcoCITA commented 1 month ago

Component seems to be working. Only one "problem", when rebooting the microcontroller the energy values to home assistant return to 0kwh. It's possible to implement something like "restore_mode: RESTORE_LAST_VALUE” so values don't get to 0?

jimmyw commented 1 month ago

There is no flash state on the chip, so it will reset. I think that homeassistant will detect an handle this case counting up energy.

Not sure how other devices handle it.