logo elektroda
logo elektroda
X
logo elektroda

Smart cube with power metering, 2 phase with clamps, 2x BL0942, CBU module, BK7231N

XJ_ 3399 118
ADVERTISEMENT
  • #91 21422210
    p.kaczmarek2
    Moderator Smart Home
    Retry later and if ok, then I can merge. But didn't @insmod include a TCP to UART bridge in his PR? I would suspect that this bridge will also require update for new UART handling?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #92 21422237
    XJ_
    Level 11  
    >>21422210
    my changes to uart are backwards compatible. I only changed UART on the Beken HAL platform, and only those use the new routines with uartindex.
    The others still using old functions.

    Added after 9 [minutes]:

    >>21422210
    What I mean is that the behavior of uart.h is unchanged, and outwardly it works the same as before. The only change in uart.h that affected the rest was

    extern int g_uart_init_counter;

    was changed to
    int get_g_uart_init_counter();


    Added after 11 [minutes]:

    p.kaczmarek2 wrote:
    Retry later and if ok, then I can merge.

    recheck successful
  • #93 21424868
    XJ_
    Level 11  
    >>21422210
    p.kaczmarek2 wrote:
    Retry later and if ok, then I can merge


    @p.kaczmarek2

    Hi, can you merge this? It's still fine, even in version 1.18.34 from today.

    If not, let me know and I'll cancel the PR.

    It's been 3 months and third PR. I'm tired of checking, editing and waiting for someone to make a change so I can merge the 3 month old changes back into the current version.

    Without this change, it's not possible to do twin bl0942 support.

    Thanks.
  • #94 21424915
    p.kaczmarek2
    Moderator Smart Home
    Ok I just hope it won't break anything as I didn't manage to do full checks, especially:

    Code snippet showing UART log redirection to UART1 instead of UART2 on Beken with UART command line support.
    Helpful post? Buy me a coffee.
  • #95 21424948
    XJ_
    Level 11  
    >>21424915
    I couldn't test everything. If there's a problem, I'll fix it.
  • ADVERTISEMENT
  • Helpful post
    #96 21432573
    XJ_
    Level 11  
    Finally, I added support for measuring devices with two BL0942 connected to UART1 and UART2 (Beken).

    PR: https://github.com/openshwprojects/OpenBK7231T_App/pull/1531

    Currently, energy is always counted only from the first BL0942, the second one is informatives and values are not counted in main energy. In the future, the option of counting energy from UART1 and UART2 together could be added.
    For HASS (MQTT), the second one reports with the suffix _b.

    To activate, there is a new command BL0942opts [value]

    0 - default (as before this modification), one BL0942 on the port according to flag 26 (as the main one, with energy)
    3 - two BL0942 on the UART1 and UART2 ports, UART1 is taken as the main one (with energy calculation) and the second as informative (without energy)

    When working with both ports, I recommend disabling logging on UART.

    To activate the dual BL mode, you need to add the following to autoexec.bat

    logtype none
    BL0942opts 3
    startDriver BL0942
    
  • ADVERTISEMENT
  • #99 21451211
    p.kaczmarek2
    Moderator Smart Home
    I see, I think I can merge it, but can you also check how much it made the binary file grow?

    @divadiow do you have BL0942 device or BL0937 device to double-check if OBK behaviour is still the same after this PR?
    Helpful post? Buy me a coffee.
  • #100 21451391
    XJ_
    Level 11  
    >>21451211
    BK7231N +1079 bytes (mainly due to double datasets for two sensors and names for mqtt)
    OpenBK7231N_App-twmod_UG_1.18.43-twinbl0942mod.bin 500504 bytes
    OpenBK7231N_App_UG_1.18.43.bin 499425 bytes
  • #101 21451599
    p.kaczmarek2
    Moderator Smart Home
    That's a bit much, especially considering that there are just few people who can use this feature. Would it be possible to introduce a #define to obk_config.h so we can have it disabled by default in main release?

    We have a serious problem with OTA file growing and removing LFS on update on T platform periodically.
    Helpful post? Buy me a coffee.
  • #102 21451722
    XJ_
    Level 11  
    >>21451599
    by adding ENABLE_BL_TWIN I can get 499848 - that's +423 bytes with ENABLE_BL_TWIN mode disabled.

    Is that enough? Shortening it more will probably require a lot more work.

    I tested it with ENABLE_BL_TWIN turned off and on with two BL0942 on BK7231N.
    Both variants work as they should
    I edited the branch and updated PR.
  • Helpful post
    #103 21452680
    divadiow
    Level 34  
    p.kaczmarek2 wrote:
    @divadiow do you have BL0942 device or BL0937 device to double-check if OBK behaviour is still the same after this PR?

    on the face of it, seems OK for BL0937..

    1.18.23
    Screenshot of electrical parameter measurements.
    1531_merge_964b96547bd1
    Screenshot of electrical energy consumption data.

    Screenshot showing data on electrical energy consumption and electrical parameters.
    HA OK before and after

    Added after 15 [minutes]:

    do these code changes only affect BK7231N? I have this for T/BL0942... https://www.elektroda.com/rtvforum/topic4101872.html
  • #104 21452969
    XJ_
    Level 11  
    divadiow wrote:
    do these code changes only affect BK7231N? I have this for T/BL0942

    Internally, the code was changed for all platforms. However, the functionality of the original functions was preserved.

    The possibility of using two bl0942 is currently only available for the Beken platform (Bk7231N / BK7231T), which has implemented support for two UARTs
  • #105 21454665
    divadiow
    Level 34  
    BK7231T / BL0942
    1.18.45
    Screenshot displaying electrical parameters such as frequency, voltage, and energy consumption. Screenshot showing energy data of a device.

    merge_a25ae12c60c3
    Screenshot displaying electrical parameters such as frequency, voltage, and energy consumption. Screenshot showing electrical energy consumption data.
  • #106 21454692
    XJ_
    Level 11  
    >>21454665
    Super, Thank You

    Added after 6 [minutes]:

    @p.kaczmarek2 what do you think, friend?
  • #107 21454909
    p.kaczmarek2
    Moderator Smart Home
    +423 is still a bit much, at least as long as we have just like 2 or 3 known such devices.... would it be possible to optimize it just a little bit futher before merge?

    Which parts have grown so much in flash usage? Maybe if we can break the DRY principle (don't repeat yourself) and introduce extra functions for HA json creation, instead of adding argument that's always 0, we could save more bytes? Or maybe that's not the main reason of size growth... I didn't investigate that much.

    Alternatively, I can merge it when I'll see there are more such devices....

    To anyone reading this topic - let me know if you have dual BL0942 device! How many of you are there?
    Helpful post? Buy me a coffee.
  • #108 21454974
    XJ_
    Level 11  
    p.kaczmarek2 wrote:
    Which parts have grown so much in flash usage?

    well, I don't quite understand it either, maybe because I used typedef struct instead of static array. I didn't add that much new code. I'll try to investigate.

    How many extra bytes are accepted from your side, so I know if I should bother with it?

    Added after 52 [seconds]:

    p.kaczmarek2 wrote:
    To anyone reading this topic - let me know if you have dual BL0942 device! How many of you are there?


    I have four ;-)
  • #109 21455205
    p.kaczmarek2
    Moderator Smart Home
    100 bytes? 200 bytes, maybe, if some people show up with this device? 300 if more people request it?
    Helpful post? Buy me a coffee.
  • Helpful post
    #110 21455536
    XJ_
    Level 11  
    >>21455205
    I changed the code so that with ENABLE_BL_TWIN disabled the UG bin on BK7231N is +110 bytes.
    The change is made by adding the ENABLE_BL_TWIN condition in many more places.
    If it is ok from your side, I will double check the behavior in both modes on HW, update branch and PR. OK?

    Added after 6 [minutes]:

    I just want to point out that there are also a few fixes and improvements included. Additionally, the bl_shared.c sensors dataset with multiple channels could be used elsewhere (e.g. TuyaMCU) and it would add more compatibility with HASS.
  • ADVERTISEMENT
  • #111 21455546
    p.kaczmarek2
    Moderator Smart Home
    So it's just +110 bytes now? Also on RBL? Then I think I could merge it.

    TuyaMCU has separate power mechanisms right now, sadly... they don't go through bl_shared
    Helpful post? Buy me a coffee.
  • Helpful post
    #112 21455568
    XJ_
    Level 11  
    Have you thought about shortening the log dtexts? And use long texts only e.g. #ifdef LOG_TEXT_EXT?
    For example, bl_shared has cca 200 bytes of log texts

    Consumption History enabled
    Sample Count: %d
    Sample Interval: %d
    Consumption History disabled
    ConsumptionThreshold: %1.1f
    Read ENERGYMETER status values. sizeof(ENERGY_METERING_DATA)=%d\n


    especially
    
    Read ENERGYMETER status values. sizeof(ENERGY_METERING_DATA)
    


    in bl0942 200 bytes:

    Consumed %i unwanted non-header byte in BL0942 buffer\n
    Skipping packet with bad checksum %02X wanted %02X
    Failed to read reg %02X: bad checksum %02X wanted %02X
    Failed to write reg %02X val %02X: read %02X
    


    What about setting rules for adding texts to the log?

    Added after 1 [minutes]:

    p.kaczmarek2 wrote:
    TuyaMCU has separate power mechanisms right now, sadly... they don't go through bl_shared

    p.kaczmarek2 wrote:
    Also on RBL?

    RBL is +96

    Added after 2 [minutes]:

    p.kaczmarek2 wrote:
    TuyaMCU has separate power mechanisms right now, sadly... they don't go through bl_shared

    well, I also have a Tuya version and I had it modified via bl_shared. But then I bought new cubes, and there were already 2x BL0942 in all of them. So I canceled the changes for Tuya.

    Added after 2 [minutes]:

    p.kaczmarek2 wrote:
    So it's just +110 bytes now?

    OK, I'll check it today evening and let you know.

    Added after 5 [hours] 29 [minutes]:

    @p.kaczmarek2

    Done. UG.bin +90 bytes, RBL +96 bytes in compare to main 45
    tested both variants on real HW with BK7213N

    ENABLE_BL_TWIN disabled (default)
    - flag 26 = 0 - one BL on UART1
    - flag 26 = 1 - one BL on UART2

    ENABLE_BL_TWIN enabled (rbl + 1090 in compare to main 45)
    - BL942opts 0
    - flag 26 = 0 - one BL on UART1
    - flag 26 = 1 - one BL on UART2
    - BL942opts 3, flag 26 = 0
    two BL on UART1 + UART2

    Everything works as it should

    PR -all checks OK
  • #113 21456787
    p.kaczmarek2
    Moderator Smart Home
    Ok, you've convinced me, I've merged this PR. Good job, you've managed to solve all the issues well.

    So we finally are able to get two BL0942 working together, but this topic is now 4 pages long, can you consider making a separate teardown/guide topic so people don't have to read through all 4 pages?
    Photos of 2xBL0942 device, configuration details, flashing details, etc, etc. Then we could feature it in Devices section (which is for complete guides, not for WIP topics):
    https://www.elektroda.com/rtvforum/forum507.html
    Then we coudl also link to your topic from OBK readme (I'll add double BL0942 to features list).
    Helpful post? Buy me a coffee.
  • #115 21486675
    maxim2002
    Level 13  
    Thanks for great driver guys!
    Got it working on "Built on Mar 6 2025 20:10:04 version 1.18.56" with TONGOU TO-Q-SY1-JWT.
    Setup was super easy.
    Everything worked out of the box.
    Just a feature request: Can we also have frequency published to mqtt.
    Screenshot with data on electricity consumption.
    Screenshot of energy sensor data.

    Many thanks.
  • #116 21486900
    XJ_
    Level 11  
    maxim2002 wrote:
    Can we also have frequency published to mqtt.

    I'm currently testing an mod to calculate the energy of both channels. I'll see what adding frequency would entail.

    Added after 5 [hours] 23 [minutes]:

    maxim2002 wrote:
    Just a feature request: Can we also have frequency published to mqtt.


    Added frequency. If you want to test it:

    Changes only in TWIN_BL_ENABLED mode:
    - ENERGY_TOTAL and ENERGY_TODAY are now calculated also for the second device. I used ConsumptionHistory[2] to store the values (in normal mode they are used for energy 2 and 3 days ago).

    Changes in standard and TWIN_BL_ENABLED modes:
    - Bug fix in src/cmnds/cmd_main.c - CMD_UARTConsole_Init()
    - Added HASS MQTT frequency, value from device 1, used new static channel SPECIAL_CHANNEL_OBK_FREQUENCY 138

    https://github.com/xjikka/OpenBK7231T_App/tree/twinblenergy
    https://github.com/xjikka/OpenBK7231T_App/actions/runs/13958325510

    To view the frequency in HASS, you will need to rerun the discovery after the update.
  • #117 21487451
    maxim2002
    Level 13  
    XJ_ wrote:
    Added frequency.

    That was awesome XJ_, just grabbed your build and OTA'ed on already configured device. All configs where preserved. After reboot and HA discovery I already have frequency available. Very nice job. Thanks.
  • #118 21494249
    maxim2002
    Level 13  
    Did some testing. Any clues why I'm getting 0 at power even if others are not indicating that?
    I'm on version twinblenergy_1b511cda2cfb. Didn't calibrate it yet.
    Screenshot of electrical energy data.

    Added after 16 [minutes]:

    Oddly but if toggled Off it shows some residual Power.
    It's same on official release "Built on Mar 23 2025 08:26:16 version 1.18.65".
    Did I mess around something with cables?

    Screen displaying electric energy consumption data with numerical values for frequency, voltage, current, and energy.

    Added after 22 [minutes]:

    Enabling "Flag 25 - [POWER] Allow negative current/power for power measurement (all chips, BL0937, BL0942, etc)" resolves issue, but power is negative.
    Pretty sure I've connected it as indicated on case. Shunt molex is other way around inside the case ?

    Screen displaying electrical energy consumption data with various values.

    "Flag 48 - [PWR] Invert AC dir" has no effect.
  • #119 21500108
    XJ_
    Level 11  
    >>21494249
    Try to change the polarity of the clamp

    Added after 9 [minutes]:

    maxim2002 wrote:
    Did I mess around something with cables?

    Change the polarity of clamp red<>black, calibrate it and try again.

    Added after 6 [minutes]:

    maxim2002 wrote:
    "Flag 48 - [PWR] Invert AC dir" has no effect.


    @p.kaczmarek2
    I didn't find any place in the code where this flag (OBK_FLAG_POWER_INVERT_AC) is used.

Topic summary

The discussion focuses on integrating and troubleshooting a smart energy power meter device featuring two BL0942 power metering chips connected via UART1 and UART2 on a Beken BK7231N module (CBU). The primary issue was that the BL0942 on UART1 functioned correctly, while UART2 returned zero values with flag 26 enabled. Investigation revealed that UART2 receive interrupts were broken at the SDK level, requiring a workaround by polling UART2 RX manually. A patch was developed and tested, enabling stable UART2 communication and simultaneous operation of both BL0942 chips. Firmware modifications introduced a new command, BL0942opts, to support dual BL0942 operation with energy calculation from UART1 and informative data from UART2, using MQTT suffix "_b" for the second sensor. Home Assistant (HASS) MQTT discovery was extended to support dual sensors while maintaining backward compatibility. The patch increased firmware size, leading to the introduction of a compile-time flag (ENABLE_BL_TWIN) to enable or disable dual BL0942 support. Testing confirmed stable operation on BK7231N and BK7231T platforms. Additional features such as frequency measurement publication and moving average filtering were added. Users reported calibration and polarity issues with clamps, resolved by reversing clamp polarity and enabling specific flags. The final solution includes firmware branches and pull requests on GitHub, with detailed configuration instructions and community-shared binaries. The discussion also covers logging behavior on UART ports, flashing procedures without desoldering, and troubleshooting steps for consistent sensor data reading.
Summary generated by the language model.
ADVERTISEMENT