logo elektroda
logo elektroda
X
logo elektroda

Configuring Tuya Clone Wifi Door Sensor with BK7231N Chip for External Web Reporting,

norbi771 1371 12
ADVERTISEMENT
  • #1 21212130
    norbi771
    Level 11  
    Hello,
    I have another, Tuya clone, Wifi door sensor powered 2xAAA, based on BK7231N chip. https://vi.aliexpress.com/item/1005006699671958.html
    When I opened it, components are so small that I can hardly see descriptions when I use magnifying tool from my phone. No chance I can use any electronics way approach to hack this device.

    I am newbie in the IoT devices hacking and I just bought 2 of these devices some time ago to do some tests, and didn't know the reality of compatibility issues, etc.
    For my life scenario I wanted this device to report opening door events to external web application, not the HomeAssistant nor MQTT.

    After trial and error, I successfully disconnected the device from Tuya via tuya-cloudcutter.
    Then I successfully connected device to Home Assistant via ESPHome. Unfortunately it eats the battery in a few hours in that setup and I realized that with ESPHome I will be unable to use deepsleep. I didn't want to use Home Assistant anyway, just started it for some tests.

    Then I realized I will need Openbaken and found a way to use it.
    Running ESPHome I wanted to install OpenBaken via OTA. It was not so easy as latest openbaken images for this chip are in rbl format only (why?). Finally found old openbaken uf2 image somewhere, installed it with ESPHome OTA, and then I updated it to the latest version with OpenBaken OTA which uses rbl format. Little confused but happy that I succeeded I am currently running OpenBaken version: 1.17.670.

    So far so good :-)

    Here are the problems I encountered and I'd like to kindly ask for help:

    1. I don't understand the logic of pins and channels. E.g. I found out that magnet sensor is working at pin 7 when I choose DoorSnsrWSleep for this pin. But then for some reason channel is changed to 56 when I use GPOFinder. Fine I can live with that, but I don't know how to properly assign other channels. In the debug log I found out that the reset button is assigned to PIN? 20:

    Info:GEN:20 Button_OnLongPressHoldStart
    Info:GEN:20 Button_OnInitialPressDown
    Info:GEN:20 Button_OnInitialPressDown
    Info:GEN:20 key_double_press
    Info:GEN:20 key_short_press


    How can I work with this button properly?

    2. I found out that with command like that I can add eventhandler and send requests to my web page
     addChangeHandler Channel7 != 0 SendGet http://10.10.10.5/sensor1Change 

    I didn't find information how to store that command permanently so it survives restarts.
    I read about autoexec.bat but I cannot find a way to access it.

    3. Battery level and voltage
    I copied some settings from similar chip. And I get info regarding battery level and voltage, but values are weird
    
    Battery level=0.00%, voltage=-0.58mV
    

    How can I fix that?

    4. DeepSleep settings and MQTT
    I would like to adjust DeepSleep for 10 seconds (or 5 maybe), just to have the option to configure it, I didn't find such an option.
    I would like to change DeepSleep for 300 seconds or any configured value after reset button is pressed.
    But for sure I'd like to have the option to turn off 300 seconds deepsleep when there is no MQTT connection.

    I spend a lot of time during a last few days trying to find out how to make this working for me, but there are many questions I couldn't find answers for.
    Could you answer to my questions?
    Are there any comprehensive tutorial for newbies?

    I will appreciate any help.
    Thanks.

    Dodano po 3 [minuty]:

    >>21212130
    Close-up of a circuit board with BK7231N chip and electronic components.

    Dodano po 51 [minuty]:

    >>21212130

    I found how to extract the settings from original Tuya software (via BK7231Flasher.exe), but after extraction it is not converted properly into the script in the import tab :-(

    
    {
       "bt_pin":"20",
       "status_led_pin":"26",
       "rstcnt":"3",
       "basic_pin_pin":"7",
       "module":"CB3S",
       "jv":"1.0.4",
       "bt_lv":"0",
       "net_t":"180",
       "samp_type":"1",
       "basic_st":"1",
       "basic_pin_lv":"1",
       "samp_sw_pin":"14",
       "max_V":"3000",
       "min_V":"2200",
       "samp_sw_lv":"1",
       "status_led_lv":"1",
       "samp_pin":"23",
       "crc":"45"
    }
    


    View of an interface for converting JSON settings to a script.
  • ADVERTISEMENT
  • #2 21212839
    p.kaczmarek2
    Moderator Smart Home
    You have chosen one of the hardest devices for your first flashing attempts. Battery-powered stuff is hard to configure, but we will try to help.

    1. GPIOFinder is a debug tool, it changes channel internally. You are supposed to first use GPIOFinder, and then change channel again for your final configuration
    2. LittleFS tutorial:



    3. Maybe Bat_Relay is not set or is set on wrong pin. I think this key: samp_sw_pin indicates which pin should be used for Bat_Relay.
    Battery tutorial:
    https://www.elektroda.com/rtvforum/topic3959103.html
    4. Everything depends on whether you use a premade driver (DoorSensorWithDeepSleep role, etc) or a manual, scriptable approach (PinDeepSleep command in autoexec.bat).
    If you are using premade driver, then you can find some commands there:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands-extended.md
    For example, DSTime will allow you to adjust the awake time of the device.

    You can also check our devices list:
    https://openbekeniot.github.io/webapp/devicesList.html
    Or check autoexec.bat samples:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 21212862
    norbi771
    Level 11  
    >>21212839
    Thank you for the answer.
    I've already adjusted PINs for voltage and now it seems that Voltage and Battery percentage are shown properly.
    I cannot handle the reset button properly yet, but it is the least important issue.

    Now I am rather in the need of adjusting the startup commands.
    Do I understand correctly that autoexec.bat is not required?

    Following https://www.elektroda.com/rtvforum/topic3959103.html#20540936 as the example do I simply put
    
    Battery_Setup 2000 3000 1.87 2400 4096
    Battery_cycle 20
    

    info autoexec.bat?
    or taking my setup as the example do I put this into autoexec.bat?
    
     addChangeHandler Channel7 != 0 SendGet http://10.10.10.5/sensor1Change
    


    And the question remains ... how can I disable 300 seconds deepsleep delay if MQTT is not used?
  • ADVERTISEMENT
  • #4 21212873
    p.kaczmarek2
    Moderator Smart Home
    I am not sure if you can disable wait for MQTT in that driver. If you want a non-MQTT version, you may need to script sleep yourself.
    Here are autoexec.bat samples:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    Maybe you can check the "Deep sleep usage with SHT30 sensor and data reporting via HTTP GET" sample. If you also want to support wake up on GPIO, change DeepSleep to PinDeepSleep.
    Helpful post? Buy me a coffee.
  • #5 21215156
    norbi771
    Level 11  
    I played a bit with autoexec.bat and now my device is bricked or "half-bricked" :-(
    The device switches to recovery mode I can log in into openbaken startup page, can even change wifi settings, but when I click on launch app the page does not exist and I don't know how to modify or delete autoexec. I tried to upload fresh openbaken via OTA (config page is working) but didn't succeed.
    Is there a way to fully reload openbaken somehow?

    Dodano po 39 [minuty]:

    it seems that I managed to unbrick the device by executing command:
    
    lfs_format
    
  • ADVERTISEMENT
  • #6 21217901
    norbi771
    Level 11  
    norbi771 wrote:
    but when I click on launch app the page does not exist and I don't know how to modify or delete autoexec

    Reading other articles I realized that I couldn't launch the app in recovery mode, because the device didn't have access to the Internet. If I understand it properly the app page is not stored in the device memory, but read from the Internet.
    https://www.elektroda.com/rtvforum/topic3971355.html
  • #7 21217904
    p.kaczmarek2
    Moderator Smart Home
    lfs_format is a good solution to recover from bad config.

    App requires internet connection unless you host it yourself. Still, if you have a laptop, you can connect to Internet via wire and connect to OBK via WiFi.

    Futhermore, AP mode should be used just for AP config, you can do the configuration in STA mode later.
    Helpful post? Buy me a coffee.
  • #8 21217913
    norbi771
    Level 11  
    >>21217904
    p.kaczmarek2 wrote:
    Still, if you have a laptop, you can connect to the Internet via wire and connect to OBK via WiFi

    Good point.
    I usually use my phone to find WIFI OpenBaken AP when the device is in recovery mode and my laptop/desktop when the device connects to the WiFi. I will change my development configuration a bit :-)

    Dodano po 15 [minuty]:

    Unfortunately, when I put PinDeepSleep into autoexec, the device doesn't work properly and goes into recovery.
    And it is always looking for MQTT and goes to sleep after 300 seconds from the last sensor status change.

    I think that I will try to adjust the openbaken code to suit my needs, in the way that MQTT is ignored or even better, could be completely skipped.
    In my project, I have to notify an external application, out of LAN, via http, that the door has opened. Nothing more, nothing less.
  • #9 21217932
    p.kaczmarek2
    Moderator Smart Home
    So what is your current autoexec.bat, can you share?

    You can adjust OBK that way : https://www.elektroda.com/rtvforum/topic4056286.html
    Helpful post? Buy me a coffee.
  • #10 21218040
    norbi771
    Level 11  
    >>21217932
    I tried to use autoexec.bat like that:
    
    addChangeHandler Channel7 != 0 SendGet http://10.10.10.5/sensor1Change
    setChannelLabel 26 "Dioda"
    startDriver NTP
    startDriver DoorSensor
    startDriver Battery
    startDriver SSDP
    PinDeepSleep
    

    but it didn't work and the device went into recovery

    I also tried simple:
    
    PinDeepSleep
    


    and:
    
    PinDeepSleep 100
    


    None of these attempts were successful, every time I end up with recovery mode
  • #11 21218045
    p.kaczmarek2
    Moderator Smart Home
    Well, the following autoexec.bat will never work, because it basically says "go to sleep as soon as you reboot". So, with this approach. your device will never ever connect to your network.

    You can fix it by checking out our samples here:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    But long story short, you need to add something like:
    
    // wait for wifi to become WIFI_STA_CONNECTED
    waitFor WiFiState 4
    // extra wait
    delay_s 5
    // finally , go to sleep and wake up either on GPIO event, or after 100 seconds
    PinDeepSleep 100
    



    And if I were to rewrite your script, I would do:
    
    
    // wait for wifi to become WIFI_STA_CONNECTED
    waitFor WiFiState 4
    // extra wait
    delay_s 1
    // send data
    SendGet http://10.10.10.5/sensor1.php?val=$CH7
    // extra wait after sending data
    delay_s 1
    // finally , go to sleep and wake up either on GPIO event, or after 100 seconds
    PinDeepSleep 100
    

    The following will do, after reboot:
    1. first it waits for WiFi connection
    2. then it waits a moment for WiFi to become stable
    3. then it sends channel 7 value (usually 0 or 1) to index.php as GET argument
    4. then it waits a moment for send to complete (it could be a larger delay)
    5. then it goes to sleep

    Keep in mind that's just a simplified example. You can check out better version here:
    https://www.elektroda.com/rtvforum/topic3945688-180.html#20693161

    PS: Do not start 'DoorSensor' driver if you are going to script PinDeepSleep manually. You either use "DoorSensor" driver with MQTT, or script device yourself.
    Helpful post? Buy me a coffee.
  • #12 21218047
    norbi771
    Level 11  
    >>21218045
    Great!
    Thank you for the answer, I will try to implement this tonight :-)
  • #13 21218049
    p.kaczmarek2
    Moderator Smart Home
    EDIT2:
    However, I would still sugest futher improvements on your script. The version with delays will not catch opening doors for 1 second. This is because we send current $CH7 value after WiFi connection and delay, and if someone will quickly close back the doors, it will send only "closed" value and not "open".
    I would consider trying:
    Spoiler:

    
    // save door state at reboot 
    delay_s 0.25
    // (assumes $CH7 is door state)
    setChannel 10 $CH7
    
    // wait for wifi to become WIFI_STA_CONNECTED
    waitFor WiFiState 4
    // extra wait
    delay_s 1
    // send data (door state at reboot)
    SendGet http://10.10.10.5/sensor1.php?val=$CH10
    // send data (door state now)
    SendGet http://10.10.10.5/sensor1.php?val=$CH7
    // extra wait after sending data
    delay_s 1
    // finally , go to sleep and wake up either on GPIO event, or after 100 seconds
    PinDeepSleep 100
    


    Of course, the final code depends on your use case. I also assume that you know how to parse GET arguments and that you are using PHP. To access val, you may need to use $_GET['val'] on your server.
    Helpful post? Buy me a coffee.

Topic summary

The discussion revolves around configuring a Tuya clone WiFi door sensor powered by the BK7231N chip for external web reporting. The user, a novice in IoT device hacking, faced challenges with battery consumption when connecting the device to Home Assistant via ESPHome. After disconnecting from Tuya, the user attempted to implement deep sleep functionality but encountered issues with the autoexec.bat configuration, leading to the device becoming "half-bricked." Various solutions were proposed, including using GPIOFinder for debugging, adjusting battery settings, and scripting sleep commands. The user ultimately sought to send HTTP GET requests to an external application upon door events, avoiding MQTT entirely. Suggestions included waiting for WiFi connection before entering deep sleep and ensuring the device could report door status changes effectively.
Summary generated by the language model.
ADVERTISEMENT