logo elektroda
logo elektroda
X
logo elektroda

[BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX

gramais 10287 61
ADVERTISEMENT
  • #1 20486194
    gramais
    Level 4  
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX


    Hi there,

    first and most important: thank you VERY MUCH for all your work you did! :)

    Then I have a question concerning this

    https://www.aliexpress.com/item/1005004699218...o.order_list.order_list_main.5.48335c5fWLFsjO

    TUYA door and window sensor. It comes with a BK7231N (pic1) soldered onto a board like this (pic2). Now, as you can see, I can't easily access the backside with RX and TX as you did. And as I plan to use 22 such sensors all around the house, desoldering and resoldering each one would take me YEARS as I am a really newbie - willing but with not much soldering experience.

    Now I hope you either have any other idea how to access the BK´s RX/TX PiNs .... ooooor maybe the shown contacts on the right side of the board (pic2) could be helpful? They are (marked in red) on the downside marked (as you can see on the pic)

    RST, SWS, V, G, T, R

    Any idea if maybe the BK´s RX / TX are directed to any of these?


    Your help would be really appreciated!


    Gramais
  • ADVERTISEMENT
  • #2 20486243
    p.kaczmarek2
    Moderator Smart Home
    Hello, I will try to help you... hmm... why do you think this device is problematic?
    It's not WB2L/WB2L_M1/CB2L.. it's CBU. It's very easy to flash.
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
    Pin numberSymbolI/O typeFunction
    1P14I/OCommon GPIO, which can be reused as SPI_SCK (Correspond to Pin 11 of the IC)
    2P16I/OCommon GPIO, which can be reused as SPI_MOSI (Correspond to Pin 12 of the IC)
    3P20I/OCommon GPIO (Correspond to Pin 20 of the IC)
    4P22I/OCommon GPIO (Correspond to Pin 18 of the IC)
    5ADCI/OADC, which corresponds to P23 on the internal IC (Correspond to Pin 17 of the IC)
    6RX2I/OUART_RX2, which corresponds to P1 on the internal IC. (Correspond to Pin 28 of the IC)
    7TX2I/OUART_TX2, which is used for outputting logs and corresponds to P0 of the internal IC (Correspond to Pin 29 of the IC)
    8P8I/OSupport hardware PWM (Correspond to Pin 24 of the IC)
    9P7I/OSupport hardware PWM (Correspond to Pin 23 of the IC)
    10P6I/OSupport hardware PWM (Correspond to Pin 22 of the IC)
    11P26I/OSupport hardware PWM (Correspond to Pin 15 of the IC)
    12P24I/OSupport hardware PWM (Correspond to Pin 16 of the IC)
    13GNDPPower supply reference ground
    143V3PPower supply 3V3
    15TX1I/OUART_TX1, which is used for transmitting user data and corresponds to Pin 27 of the IC. For the MCU solution, please refer to CBx Module.
    16RX1I/OUART_RX1, which is used for receiving user data and corresponds to Pin 26 of the IC. For the MCU solution, please refer to CBx Module.
    17P28I/OCommon GPIO (Correspond to Pin 10 of the IC)
    18CENI/OReset pin, low active (internally pulled high), compatible with other modules (Correspond to Pin 21 of the IC)
    19P9I/OCommon GPIO (Correspond to Pin 25 of the IC)
    20P17I/OCommon GPIO, which can be reused as SPI_MISO (Correspond to Pin 14 of the IC)
    21P15I/OCommon GPIO, which can be reused as SPI_CS (Correspond to Pin 13 of the IC)
    Test pointCSNI/OMode selection pin. If it is connected to the ground before being powered on, enter the firmware test mode. If it is not connected or connected to VCC before being powered on, enter the firmware application mode. Correspond to Pin 19 on the internal IC.


    As you can see, TX1 and RX1 are easily accessible.

    By the way, please check where the mentioned pads for goldpins connect, and post information here. It might be useful for some people, but I'd already try guessing that R stands for RX and T for TX. Then G is Ground, V is Voltage...

    Then you can most likely configure it like here:
    Door/window sensor without TuyaMCU - deep sleep and energy saving, OpenBeken

    Btw, if you are flashing them, please also provide us a 2MB Tuya firmware backup from them
    Helpful post? Buy me a coffee.
  • #4 20486461
    p.kaczmarek2
    Moderator Smart Home
    Your command is not correct. It lacks unprotect and read size. Please watch our video to get the correct hid_download_py commands for flashing:
    https://www.youtube.com/watch?v=2e1SUQNMrgY&ab_channel=Elektrodacom

    If you want something simpler, here is new flasher (but I had few reports about issues with it):
    https://github.com/openshwprojects/BK7231GUIFlashTool
    Here is hid_download_py (old flasher):
    https://github.com/OpenBekenIOT/hid_download_py
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #5 20486564
    gramais
    Level 4  
    Okay,

    thanks again. I ran

    python3 uartprogram firmware_backup.bin -d tty.usbserial-1410 -r -s 0x0 -l 0x200000


    Sadly quits with:

    ImportError: cannot import name 'Timeout' from 'serial' (/Users/username/Library/Python/3.9/lib/python/site-packages/serial/__init__.py)


    Although this file exists and contains:

    
    """
    `serial` is an object serialization/deserialization library intended to facilitate authoring of API models which are
    readable and introspective, and to expedite code and data validation and testing. `serial` supports JSON, YAML, and
    XML.
    """
    
    from __future__ import nested_scopes, generators, division, absolute_import, with_statement, 
       print_function, unicode_literals
    
    from . import utilities, abc, model, marshal, errors, properties, meta, hooks, test, request
    ~                                                                                                                                                                                                                          
    ~                                                                                                                                                                                                                          
    ~      
    



    Any idea: should I add a line to this file?
  • ADVERTISEMENT
  • #7 20486613
    gramais
    Level 4  
    The BACKUP:

    Got it working using a lower baud rate:

    python3 uartprogram firmware_backup.bin -d /dev/tty.usbserial-1410 -r -s 0x0 -l 0x200000 -b 115200


    Worked with these rates 115200, 230400 and 460800 (max.).

    At least I hope, because after backup I got:

    CRC should be d5b462cf
    CRC is d4232b4b
    CRC check failed
    Wrote 200000 bytes to firmware_backup.bin


    This CRC check comes repeatedly with exact same CRC values: so it is no coincidence, isn't it?

    Can you please check the file if it's okay due to this CRC fail?

    (See attached bin-file)

    2) FLASHING:

    Tried to flash using:

    python3 uartprogram OpenBK7231N_QIO_1.15.591.bin -u -d /dev/tty.usbserial-1410 -w -s 0x0 -b 115200


    and I seem to get a bus connection, but a write error too:

    UartDownloader....
    programm....
    WriteSector 1 Failed: |                                              |[    ?k/s]


    Tried several times with no success: any idea?

    Help would really be appreciated - thank you!
  • #8 20488556
    gramais
    Level 4  
    ... interesting:

    Although reading worked as I got the backup I posted here before, writing does not.

    But,

    when I set the baud rate to 460800 (and only at this rate) I get at least a starting, but then immediately aborting flash process:

    UartDownloader....
    programm....
    WriteSector 1 Failed: |1                                             |[ 3.31s/k]
  • #9 20488604
    p.kaczmarek2
    Moderator Smart Home
    First of all, the CRC code is not updated for N so the warning is normal. It's okay.

    And regarding WriteSector failed.... hm, how do you reset? I had this issue while I was resetting too quickly... the reset should be really, just 0.25s short on CEN.

    btw how did you solve that strange Python problem from the several posts above?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #10 20488685
    gramais
    Level 4  
    Take a look at this:
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX


    Welcome to the world Newby-Born-OpenBEKEN!
    ;)

    So YES - YIPPPEEE, it worked! :)

    Followed this post...

    https://www.elektroda.com/rtvforum/topic3874289-180.html#20252165

    ... in which there was described a similar situation:

    - reading (backup) possible
    - baudrate issues same
    - writing not possible

    ALL as described above and in that post.

    Aaaand this post even became identical as I read he used a MacBook Pro (as me). So the ONLY thing I changed was connecting the USB-UART-Dongle to my RASPBPI. Result:

    user@raspberrypi:~/hid_download_py $ python3 uartprogram OpenBK7231N.bin -u -d /dev/ttyUSB0 -w -s 0x0 -b 460800
    UartDownloader....
    programm....
    Write Successful: |##################################################|[ 9.14k/s]


    ;)

    So this similarity is surely no conincidtance and clearly a macOS issue. At least users should be warned or GitHub code should be updated. If I can help to finde out the reason: let me know what I can do!
  • #11 20488738
    p.kaczmarek2
    Moderator Smart Home
    Well done, I don't have much experience with macOS so I was not aware about it.

    If you want, you can do the honors and submit a pull request with readme update here:
    https://github.com/OpenBekenIOT/hid_download_py
    so your Github account will show up as a contributor - it's always good to be remembered as a person who helped with testing the tool.

    So now, it seems your next steps are here:
    Door/window sensor without TuyaMCU - deep sleep and energy saving, OpenBeken
    Helpful post? Buy me a coffee.
  • #12 20488864
    gramais
    Level 4  
    Okay, tried to set it up as described:

    P24 (PWM4) Btn => 1
    P26 (PWM5) DoorSensorWithDeepSleep => 0


    Saved and rebooted, but how can I check before adding to HA if it works?
    How can I check if I used the correct PINs?
    Can I see sensor state results in the web frontend?

    And there is actually one issue: it loses WiFi connection. No ping or web request wakes it up....
    :(

    Needs there anything else to be configured?

    Added after 36 [minutes]:

    I am asking this because the sensor publishes a state correctly via MQTT that is displayed in HA but does not change: so I suppose that I have maybe chosen the wrong PINs? This suits the fact that the device falls asleep, loses WiFi connection and does not come back even if physically triggered: so there is no wake up command from a recognized sensor state change, right?

    When looking at the board picture (in my very first post) the magnetic contact sensor is marked as "H1" - is that helpful?

    Any suggestions?

    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
  • #13 20489197
    p.kaczmarek2
    Moderator Smart Home
    Device has to fall asleep in order to prolong battery life. Otherwise it would discharge battery in a day. While asleep, the website has to be offline, otherwise it wouldn't sleep. The only thing that can wake up correctly configured device is a button or sensor event.

    The problem seems that you have selected wrong pin for button and the sensor. Now you have to put out batteries, wait several minutes or more for capacitors to discharge, because they can still hold current, and then finally put batteries again and quickly disable DoorSensor pin and driver. Then reboot.

    Once you are sure that device is not running door sensor anymore, carefully investigate which GPIOs are really used for button and for sensor.

    You can try setting a dInput role for each pin and checking which one reacts to door magnet.

    The correctly configured device should wake up on both door sensor and button events.
    Helpful post? Buy me a coffee.
  • #14 20489265
    gramais
    Level 4  
    Okay, but how do I "investigate" - step by step? Does it involve the use of multimeter - and if yes: how?

    Thanks again!
  • #15 20491282
    dheenhasty
    Level 13  
    2 choice :

    - With multimeter in diode you check which pin of the hall sendor is conected to cbu

    - thru webapp, as p.k say assign each pwm pin to dinput with different channel and check wich one pass from 1 to 0 when you close or open the door :)
  • #16 20491519
    p.kaczmarek2
    Moderator Smart Home
    Maybe it would be a good idea to create a Youtube video about that, for our Elektroda channel?
    https://www.youtube.com/@elektrodacom

    Btw it looks like @gramais has cleared RF partition and lost MAC?
    Helpful post? Buy me a coffee.
  • #17 20491673
    gramais
    Level 4  
    Why that: why do you think I cleared RF and lost MAC?
  • #18 20491691
    p.kaczmarek2
    Moderator Smart Home
    @gramais please read our FAQ:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/faq.md
    Those two questions:
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
    Helpful post? Buy me a coffee.
  • #19 20491734
    gramais
    Level 4  
    Ahhh - I see: thank you!

    Still no windows around: What would be the Linux command for the command-line tool?
  • #20 20491748
    p.kaczmarek2
    Moderator Smart Home
    What do you need command line tool for? RF restore is done from within flashed firmware level.
    Helpful post? Buy me a coffee.
  • #21 20491757
    gramais
    Level 4  
    Sorry, my fault: I was mistaken the web app with GUI flash tool - thank you for your patience!
  • #22 20610210
    khafif
    Level 5  
    >>20486194

    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX

    I do not know if I am late, but if you work it out you can see what's in the back just by lifting it a bit.

    Added after 3 [minutes]:

    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
  • #23 20611036
    khafif
    Level 5  
    I have same BK7231N/CBU Tuya door sensor, I’ve been trying to find the firmware, can you please link it, also can you advice how I can connect it to mqtt/home assistant (in detail please). Thank you.
  • #24 20638363
    simovicsava
    Level 7  

    Guys, I found the correct settings

    1. Sensory → Configure module:

    P16 DoorSnsrWSleep_nPup
    P20 Btn
    P23 BAT_ADC
    P26 WifiLED

    1. Flagi → Configure general/flags:

    X 10 [MQTT] Broadcast self state on MQTT connect
    X 35 [HASS] Deactivate avty_t flag for sensor when publishing to HASS (permit to keep value). You must restart HASS discovery for change to take effect.
    X 37 [WiFi] Quick connect to WiFi on reboot (TODO: check if it works for you and report on github)

    mqtt:
      binary_sensor:
      - unique_id: "long name sensor"
        name: "short name sensor 0"
        state_topic: "short name sensor/0/get"
        qos: 1
        payload_on: 1
        payload_off: 0

  • #25 20638508
    p.kaczmarek2
    Moderator Smart Home
    Very good, I would only add that automatic HASS Discovery should be already working for that kind of devices, so you don't really need to write YAML manually.

    But you have exactly the same device as we spoke about, right? If not, please post the picture.
    Helpful post? Buy me a coffee.
  • #26 20639800
    simovicsava
    Level 7  
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX


    Yes

    Added after 9 [minutes]:

    It already works automatically adding to HA, but it shows the reverse status. When the sensor is open it shows that it is closed, and when the sensor is closed it shows that it is open
  • #27 20639813
    p.kaczmarek2
    Moderator Smart Home

    Hmm, so you basically want to invert the reading of DoorSnsrWSleep? I can add a flag for that.
    Helpful post? Buy me a coffee.
  • #29 20643451
    khafif
    Level 5  

    >>20638363
    Great work. Still learning from Elektroda and from you, thank you.

    Can you please advise how to reduce/change sleeping time. [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
  • #30 20643558
    p.kaczmarek2
    Moderator Smart Home

    Hello, do you want to reduce the emergency sleep time when there is no MQTT, or the generic sleep delay that happens when [b]MQTT is connected?
    Helpful post? Buy me a coffee.

Topic summary

The discussion revolves around connecting and configuring a Tuya door and window sensor equipped with a BK7231N chip without using TuyaMCU. Users seek guidance on accessing the RX/TX pins for flashing firmware and configuring the device for MQTT/Home Assistant integration. Various solutions are proposed, including using specific pin configurations for the door sensor and button, adjusting baud rates for flashing, and troubleshooting issues related to firmware and battery drain. The conversation also covers the importance of deep sleep mode for battery conservation and the need for correct pin assignments to ensure proper sensor functionality. Users share their experiences with different configurations, including inverting sensor states and adjusting sleep settings to optimize performance.
Summary generated by the language model.
ADVERTISEMENT