Elektroda.com
Elektroda.com
X

[BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

p.kaczmarek2 114909 1234
This content has been translated flag-pl » flag-en View the original version here.
  • #32
    pepesuriano
    Level 9  
    p.kaczmarek2 wrote:

    (...)


    Hi! Could you take a peek on the new photos of my last post? I suppose that with all this replies perhaps you missed it so it's just a reminder 😂

    Cheers
  • #33
    p.kaczmarek2
    Moderator Smart Home
    Hey, I'm sorry @pepesuriano , I didn't skip your post, I just assumed you'll know what to do later.
    Also, please do not quote whole message blocks (only parts) because it makes discussion less reliable (I have moderated your post).
    Anyway:
    pepesuriano wrote:

    And the other one was an AMS1117 indeed!

    AMS1117
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    You connect 5V to the input pin of AMS1117.

    PS: if you're a beginner, please also remember to disconnect every 5V, UART converter, flasher, etc, from the module before plugging it to mains (230V or 120V etc etc), otherwise serious damage may occur
    PS2: You have a big electrolytic capacitor on 5V rail, I'd suggest you using "connect CEN to ground" procedure to RESET chip instead of "power off and power on", because such a big capacitance connected directly to 5V might reset bus temporarily (and such reset will cause BKwriter to fail because it will lose UART port for a second or two). In the past I have removed such capacitors from my WB3S device boards but I think it's easier to just reset by using CEN pin

    Pins on WB2S module:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    I assume you have WB2S

    Second UART is only for debugging, you might not need to solder that, but remember to make a backup of your firmware with BKwriter before burning openbk7231T

    Feel free to ask any futher questions.
  • #34
    pepesuriano
    Level 9  
    Thank you! Your advice is very useful because I just handle the basics on electronics, so my apologies in advance if I ask anything kind of obvious. Software programming is where I do best (and where I can make any contributions after having openbk running).

    So, to wrap up the flashing process (and avoid any mistakes or bad interpretations for me that could break anything) I should:

    1- Take a PL2303 for flashing that I have around, (it does not have a VCC pin to bridge with 3.3 or 5v, should I just connect to 5v from the PL2303?)
    2- Solder 1TX and 1RX to WB2S module, and solder 5V in AMS1117 input pin
    3- Connect CEN pin of WB2S to ground (the big ground metal piece of the power connector is fine?) (should I keep CEN to ground all the time?)
    4- Connect the smart plug to mains and power it (with the soldered wires disconnected for the moment)
    5- Connect 1TX, 1RX and 5V to the PL2303
    5- Run bkwriter, take a backup of original firmware
    6- Flash openbk
    7- Open a beer

    Cheers
  • #35
    kuba2k2
    Level 12  
    I'll add something from myself.

    https://github.com/kuba2k2/bk7231_cmake_sdk

    Repo based on tuya-iotos-embeded-sdk-wifi-ble-bk7231t, but free from the start of Tuya firmware and precompiled libraries (contains nothing but the beken378 directory and a few utilities). Additionally, instead of the original (not very elegant) Makefiles, I added scripts to CMake.

    Thanks to this, it can be built in CLion or another IDE that supports CMake and we get ready firmware to be uploaded via UART (or probably SPI) and code prompting and navigation in the IDE.

    As the SDK is completely free from Tuya, it does not include their API, as well as some things like MQTT. If I can find the time I will try to add them with e.g. bdk_freertos.

    I am sending it here because it may be useful to someone. Tested on WB2L, because I have one. In the attachment there is a screenshot of the compilation in CLion and an example firmware hello world.

    PS. bdk_freertos can be compiled and run after replacing bk7231.ld in the build directory with the one in the Tuya repo (bk7231_ota.ld). The boot address doesn't match and the bootloader doesn't know what to boot.
  • #36
    ferbulous
    Level 16  
    Hi, I'm trying to install by testing on a removed WB3S which I replaced with esp, and I keep getting flash failed (15s...failed).
    Connected vcc, gn, tx1, rx1 and did a restart.
    I also tried using test pins with 3d jig (for the twe3s) on 4 gang switch board with wb3s and that also failed.
    Is there an additional driver I need to install?
  • #37
    boozeman
    Level 11  
    Hi,

    No luck for me today with Nedis Smart Plug and Beken Writer V1.60

    Connected +3.3VDC, GND, TX and RX to my usb-serial adapter (RX to TX1 and TX to RX1). When CEN connected to ground, power led light very dim. Not able to read or write anything. Init FAILED. Seems like I can not get this to the boot mode?
  • #38
    pepesuriano
    Level 9  
    pepesuriano wrote:
    Thank you! Your advice is very useful because I just handle the basics on electronics, so my apologies in advance if I ask anything kind of obvious. Software programming is where I do best (and where I can make any contributions after having openbk running).

    So, to wrap up the flashing process (and avoid any mistakes or bad interpretations for me that could break anything) I should:

    1- Take a PL2303 for flashing that I have around, (it does not have a VCC pin to bridge with 3.3 or 5v, should I just connect to 5v from the PL2303?)
    2- Solder 1TX and 1RX to WB2S module, and solder 5V in AMS1117 input pin
    3- Connect CEN pin of WB2S to ground (the big ground metal piece of the power connector is fine?) (should I keep CEN to ground all the time?)
    4- Connect the smart plug to mains and power it (with the soldered wires disconnected for the moment)
    5- Connect 1TX, 1RX and 5V to the PL2303
    5- Run bkwriter, take a backup of original firmware
    6- Flash openbk
    7- Open a beer

    Cheers


    Sorry, I forgot to mention that it was actually a question if that procedure is ok, just to be sure to not set the house on fire 😂

    P.D.: I should keep the module powered from mains in all the process after connecting it (step 4)?
  • #39
    btsimonh
    Level 11  
    pepesuriano wrote:
    pepesuriano wrote:
    Connect the smart plug to mains and power it (with the soldered wires disconnected for the moment)

    Please no....

    Never power the plug from the mains whilst flashing, either you will die or the serial adaptor/computer will die. Such devices are rarely sufficiently isolated to protect you. In one of my sonoff lights, GND of the chip was connected directly to the LIVE of the mains.

    As stated a few posts above, connect a reliable (>500ma, probably NOT the 5v from the serial adaptor) power source to the 5v input of the regulator, OR 3.3v to the 3.3v in the plug.
    Connect the serial adaptor with GND, TX & RX only, and make these connections *before* powering on the 5V/3.3V
    With some sort of terminal running 115200 baud against the serial, you should see some logs at power on. If you don't re-check connections/power. Or replace the torched serial adaptor...

    Only once you have got this far, close the terminal program, and start BK_writer or use uartprogram (the python thing mentioned above).
    You can either:
    1/ start to program, and then turn on the 5v/3.3v whilst it's waiting to connect.
    2/ have the device by 5v/3.3v, start to program, and then short CEN to GND momentarily to reset the chip.

    good luck and please don't play with mains!
  • #40
    p.kaczmarek2
    Moderator Smart Home
    Great news! PWM from Home Assistant works!
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Code: xml
    Log in, to see the code



    @ferbulous @boozeman please post photos of your setup. Have you tried multiple baud rates?
    Are you using the same UART to USB converter as mine? I tried once to flash XR809 by using MCP2221 based one, and, to my suprise, it had random errors all the time, despite the baud rate was supposedly supported by MCP2221.

    @pepesuriano See modified quote :
    Quote:

    1- Take a trusted USB to UART converter with 3.3V logic levels (for example hw 597 ) for flashing
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    2 select 3.3v logic levels by jumper between 3.3V and VCC on (for example hw 597 ), break out 5V from USB directly (white wire)
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    (the "5V and 3.3V pins won't work for my")
    3- Solder 1TX and 1RX to WB2S module, and solder 5V directly from USB (not from pins on HW) in AMS1117 input pin

    4. Run bkwriter,
    5. Select COM port (if not present, install drivers for USB to UART dongle you're using)
    6. start "Read" in bkwriter
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    7. while BK is trying to read, connect CEN pin of WB2S to ground for a short while and disconnect it (try to do this really once, if you do twice, then you'll get error)
    8. wait for read to finish. If it fails, try different baud rates

    EXPLANATION: BKwriter is waiting to detect WB2S (or other) module booting on RX/TX pins. You can start WB2S boot by powering it on after it was off for a while (disconnect power method) or by resetting it (CEN aka RESET pin method). BKwriter must be already started with read/write command before resetting, so it can "catch" the boot and proceed)

    OPTIONAL MODIFICATION:
    - if you're having problems with CEN, you can try replacing point 7 with repowering whole WB2S module (disconnect power connected to WB2S, start read in BKwriter, connect power again). It will work as long as you don't overcurrent your USB port and cause COM port to reset (I was using this method for many modules and it worked as long as I removed 220uF capacitor at 5V rail before 1117-3.3V).

    SPECIAL HINT:
    - you can also try connecting second UART which is useful for debugging, but that's not required for succesful flashing
  • #41
    btsimonh
    Level 11  
    p.kaczmarek2 wrote:
    try different baud rates

    mine is not reliable above 1Mbit. My cables are about 10cm....
  • #42
    boozeman
    Level 11  
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

    Mine is Old and Faitfull Sparkfun FT232R. Used many times for flashing esp8266 and esp32 devices and also some ST devices for Blitzortung.org Lightning detectors. In fact it is one of the my old Blitzortung Green station serial adapter :)

    Normal "flashing" connection with VCC,GND, RX, TX White wire is for Gen/I00 to gnd

    The chip is WB2S with connection points similar than posts here.

    I have tested BK7231N abd BK7231 in Beken Writer. Serial adapter discovered by COM10 and baut rates tested 115200 and 921600.
    There's no slower than 115200.

    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Edit: Managed to read flash. It is surely voltage drop when connecting vcc when try to write flash.
  • #43
    p.kaczmarek2
    Moderator Smart Home
    boozeman wrote:

    Edit: Managed to read flash. It is surely voltage drop when connecting vcc when try to write flash.

    Congratulations! Welcome to Bekken readers club!
    Were you powering from USB? Did you leave that big, bulk, electrolytic capacitor at 5V rail?
    As I said several times, if you are willing to reset by powering off and on, this capacitor must be removed. Otherwise the large current surge at power on will overload USB port and cause USB device reset.
    I remove that capacitor every time:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Anyone else still have problems flashing?
  • #44
    Vampir8
    Level 16  
    And isn't it better to adapt BK7231T SDK under Arduino Core? I think such project could have more interest and involve more users. Then we are not limited to one project :)
  • #45
    boozeman
    Level 11  
    p.kaczmarek2 wrote:

    Congratulations! Welcome to Bekken readers club!


    Heh, thanks. No luck for writing yet. Tested with external power source adjusted for 3.3VDC (DC-DC converter with Lenovo laptop powersupply as input).
    I have to butcher the Nedis plug, so for now I am workin with only plain WB2S module.

    Erasing flash:0x00011000 FAIL

    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Reading works every time. I start it with VCC, TX, RX, GND and CEN o GND connected to usb-serial converter, start reading and then take off CEN wire

    Is the other settings OK?
  • #46
    kuba2k2
    Level 12  
    boozeman wrote:
    Erasing flash:0x00011000 FAIL

    This also happens to me, on WB2L. Additionally, I can't read anything at all, it just fails at the start.
    What helps for flashing is to simply trim the binary file to about 956KB (1MB - 1048576 bytes, minus the offset). Then, the flasher does not try to erase the area above 1MB and the flashing goes on properly.
    I suspect the bootloader in some devices may be somehow locked so it doesn't allow that erase?

    edit: I'm powering the module on its own original PCB, from 12V from a lab ATX (the board has a switching regulator), so I don't think it could be caused by voltage drops.
  • #47
    p.kaczmarek2
    Moderator Smart Home
    I haven't experienced this issue yet. Let's try with trimmed down flash (thanks @btsimonh for clear up work)
    OpenBK7231...0202fx.zip Download (5.52 MB)
    Does it work for you, @boozeman @kuba2k2 ? Burn UA as usual.

    Also note: this release contains my PWM dimmer support and initial version of Bekken OTA bootloader hack by @btsimonh (usage: enter rbl link to given form, you must self-host rbl somewhere on http server)
  • #48
    MrTechGadget
    Level 2  
    Sorry I haven't replied in a few days, I will definitely give the python flasher a try and report back.
    I am currently still trying to get the plastic case of my FEIT BPPLUG smart plug without resorting to extreme force. :)
    I have 3, so if one ends up in a "test only" state that will be ok, but ideally I wouldn't damage it too much and could still use it outside of testing.

    I should have some time this week to work on it.
  • #49
    boozeman
    Level 11  
    p.kaczmarek2 wrote:
    I haven't experienced this issue yet. Let's try with trimmed down flash (thanks @btsimonh for clear up work)
    Does it work for you, @boozeman @kuba2k2 ? Burn UA as usual.


    I Run flash test with the new Firmware (UA.bin). Same error occurred at the same step. Erasing.. FAILED

    But no worries, we can sort this out sooner or later :)
  • #50
    p.kaczmarek2
    Moderator Smart Home
    @boozeman , would it be possible for you to try with usb to ttl hw 597 (the one with pinouts: 5V, VCC, 3V3, TXD, RXD, GND), which can be bought for 1$?

    I know it sounds strange, but I myself witnessed one UART to USB failing while flashing XR809 (MCP2221 as far as i remember), and there was also user in my XR809 thread with the same issue (and other UART dongle, not MCP one)...
  • #51
    kuba2k2
    Level 12  
    p.kaczmarek2 wrote:
    Does it work for you,@boozeman@kuba2k2 ? Burn UA as usual.

    No, it won't work, the UA flash file is too big. Your file is 1,09MB in size, and it needs to be exactly 956KB.
    Try flashing the file from the attachment @boozeman. It's the wifi_scan example from bdk_freertos, compiled with my CMake SDK. My SDK is configured to output smaller binary files by default, so that I never have this flashing problem.

    edit: I tried flashing with CP2102 and CH340G, and both succeed for smaller binaries, and fail for larger ones.
  • #52
    p.kaczmarek2
    Moderator Smart Home
    kuba2k2 wrote:

    No, it won't work, the UA flash file is too big. Your file is 1,09MB in size, and it needs to be exactly 956KB.

    What? No way, it's supposed to be smaller!

    Wait... you're right and now I realized something. Thank you!

    I think I know what's wrong:
    
    out_json = {
        "magic": "RT-Thread",
        "version": "0.1",
        "count": 4,
        "section": [
            {
                "firmware": "bootloader_enc.bin",                    
                "version": "1.00",
                "partition": "bootloader",
                "start_addr": "0x00000000",
                "size": "68K"
            },
            {
                "firmware": "bk7231_common_1.0.1_enc.bin",
                "version": "1.00",
                "partition": "app",
                "start_addr": "0x00011000",
                "size": "1150832"
            }
        ]
    }
    

    The sector size is hardcoded in the packager settings and it's always padded to this value (1 150 832). Our real flash size is ATM 489 264.
    I will send another bin for testing right away.


    EDIT: 978 944 bytes sector size, you say?
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Still, I think we could settle with even lower sector size, just to be sure...
    but first please check new version:
  • #53
    kuba2k2
    Level 12  
    Yes, that's exactly how it works. There's no need to make the image even smaller, as the code won't fit if it exceeds that size.
    You can see that the beken_packager shows "image size: 1024KB" which is exactly 1MB, which makes the flasher not erase 0x00011000.

    Wait, I just now realised something. @boozeman are you sure the program said exactly 0x00011000? This is the start address, so it would mean it can't erase just about anything.
    My issue (with the 1MB bin size) is about erasing 0x00110000 (4 zeroes).
    Nevertheless, I had the same issue with the start address. If the erasing error happens at the very beginning of the flashing process, change the baud rate to 921600. For me, 115200 produces that error at the start and doesn't allow to flash anything.
  • #54
    btsimonh
    Level 11  
    for me, I thought I had broken the python flasher read function until I realised I was not setting the baud rate and it was defaulting very high.
    I find 115200 works well but slowly, but I can't use above 1Mbit, else it 'finds the bus' but then fails to read.

    Also, be aware that there is an option on the python flasher for 'unprotect' - I never needed it, and so never tested it. It seems to be connected with certain types of chips....

    Q: When you read from 0x11000, do you have a 0x1000 byte header containing the string 'RBL'?
    Some of the chips have the bootloader in ROM (bk7231n?), and I believe may flash firmware at 0x00000.... and need unprotect.

    Advise you try the python flasher, and print the returned chip ID - if it's not a 2MByte part, then it's a different beast to the ones which are being programmed successfully.
  • #55
    p.kaczmarek2
    Moderator Smart Home
    kuba2k2 wrote:
    There's no need to make the image even smaller, as the code won't fit if it exceeds that size.

    There could be, because we don't know what's the limiting factor here, maybe there are some BK7231T modules which requires even lower sector size? Who knows, I didn't test many devices yet, but I am constantly ordering new ones:
    Spoiler:

    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

    Can you all @kuba2k2 @boozeman and others specify which BK modules are you flashing? I would like to determine which ones have that 956K limit, because for me, all my WB2S and WB3S moduls so far worked with previous sector size (1150832)

    Dodano po 1 [minuty]:

    kuba2k2 wrote:
    If the erasing error happens at the very beginning of the flashing process, change the baud rate to 921600. For me, 115200 produces that error at the start and doesn't allow to flash anything.

    I confirm, I reported it once in this or previous thread. I had once a situation when 115200 produced error and 921600 worked.

    Dodano po 1 [minuty]:

    btsimonh wrote:

    Q: When you read from 0x11000, do you have a 0x1000 byte header containing the string 'RBL'?
    Some of the chips have the bootloader in ROM (bk7231n?), and I believe may flash firmware at 0x00000.... and need unprotect.

    HINT: You can use my flash memory viewer tool (available in Config section at web panel of OpenBK7231T) to view that.
  • #57
    kuba2k2
    Level 12  
    btsimonh wrote:
    Q: When you read from 0x11000, do you have a 0x1000 byte header containing the string 'RBL'?

    I can't read using the flasher at all. I haven't got time to try the python flasher yet.
    I made a simple flash dump over UART (included in my SDK/examples), which does seem to skip some bytes for an unknown reason... but I do have "RBL" around this offset. So yes, 0x11000 is the correct offset. The firmware runs correctly, so hey, it can't be wrong.

    p.kaczmarek2 wrote:
    Can you all@kuba2k2@boozeman and others specify which BK modules are you flashing? I would like to determine which ones have that 956K limit, because for me, all my WB2S and WB3S moduls so far worked with previous sector size (1150832)

    WB2L, with BK7231TQN32. Sourced from a Hama Wi-Fi RGB LED bulb (model 176581). Btw, you can add a flashing pin header easily, without damaging the bulb, I can add pictures if someone requests.

    btsimonh wrote:
    Advise you try the python flasher, and print the returned chip ID - if it's not a 2MByte part, then it's a different beast to the ones which are being programmed successfully.

    I printed the flash ID with SDK's functions (Link). It shows 0x1C7015, which corresponds to a 2MB flash. And that's correct, my dump successfully read (nearly) 2MB of data, with the TLV section way over 1MB. It seems that the problem is just during erasing, and the flash is indeed 2MB. I guess that an OTA update, which does not include the bootloader's flashing method, could overcome this 'over 1MB' problem.

    BTW, @p.kaczmarek2 what would you say about integrating your OpenBK7231T_App with my CMake SDK? The code shouldn't require any porting, as it's built around the same code, and IMO it allows for a much cleaner building solution, usable in an IDE with full code assistance, and buildable without even requiring Cygwin.
  • #58
    p.kaczmarek2
    Moderator Smart Home
    btsimonh wrote:

    I think it's a good idea to crowdfund information, but also remember to link to my detailed Elektroda teardowns like:
    https://www.elektroda.pl/rtvforum/topic3822484.html
    https://www.elektroda.pl/rtvforum/topic3798114.html
    Every my device will have a detailed teardown, but of course I am not able to check out all available BK devices, so any help is welcome. Please submit all the information you know.

    PS: EDM-01-AA-EU KER_V1.6 progress (full teardown will be posted on Elektroda soon):
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    I will add TuyaMCU support for that (thanks @btsimonh for OTA hack so I can work on that effectively)
  • #59
    btsimonh
    Level 11  
    eager to see results from the python flasher - I think it only erases a sector (0x1000) at a time.
    Ref the SDK, I'd love to integrate into PlatformIO & VSCode - so it's easy for someone to pick this up without having to ensure they have pythod, cygwin, etc. etc. PIO does all that for you, but I have no idea how you build a package which would get all the 'stuff' for you :).

    The OTA should work.

    "I can't read using the flasher at all." - maybe there is 'copy protect' which the bootloader observes?
  • #60
    boozeman
    Level 11  
    Hi,

    I will buy another USB2Serial converter for tomorrow. It is based for CP2101 chip. My current steam powered old and faithful FTDI 1207-C can't handle 921600bps. It was built at a time when the proponent of such a speed would have been burned instantly as a witch :D

    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    The Nedis WIFIPO120FWT chip looks like this.

    About those Nedis devices, i made "warning" about Tasmota Templates git The older batch comes with esp8266.

    Dodano po 13 [minuty]:

    p.kaczmarek2 wrote:
    PS: EDM-01-AA-EU KER_V1.6 progress (full teardown will be posted on Elektroda soon):


    Do you have Kicad Symbols for WB2S and WB3S already? I can try to reverse schematic for Nedis device. Not the high voltage section but normal what pin goes where BL0937 etc.

    Edit:

    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

    Successfull write for OpenBK7231T-20220203fx-956kb\OpenBK7231T_App_UA_1.0.0.bin finally. Well, I can't see any wifi network to connect with my phone. I will continue to debug :)