Elektroda.com
Elektroda.com
X

Search our partners

Find the latest content on electronic components. Datasheets.com
Elektroda.com

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

p.kaczmarek2 32079 704
This content has been translated flag-pl » flag-en View the original version here.
  • [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Hello, here I will present the progress report of my work on the open firmware for BK7231T. This topic will be written in the form of a tutorial, I will present here step by step how I developed my own, cross-platform (running on Windows and BK7231T) mini HTTP server, pin configuration system via the web panel and how I connected it with MQTT and ultimately with Home Assistant. Of course I will attach here full source code for download and a Visual Studio project for those who want to play with my server under Windows.

    First part of the story
    This topic is a continuation of this thread:
    https://www.elektroda.pl/rtvforum/topic3850712.html

    Official repository
    As per users requests, here is public repo:
    https://github.com/openshwprojects/OpenBK7231T

    Topic content
    In this topic, I will present:
    - how to write your own simple C-language HTTP mini-servers on Windows
    - how to prepare this server to be multi-platform and run it on WB2S / BK7231T
    - how to organize cleverly a pin configuration for the BK7231T in Tasmota style
    - how I finally organized the pin configuration and MQTT support for my open firmware for BK7231T
    - as in the case of the current version of the project, run my firmware on my own system from the BK7231T series (WB2S, WB3S, maybe others) and connect it to the Home Assistant via MQTT
    Why will the server be multi-platform?
    For the following reasons:
    - it will allow me to efficiently create a website for the configurator and management of the BK7231T module on Windows without having this module physically, without the need to upload a firmware to it, which takes a long time, etc.
    - this will allow me to use the same configurator and page in the case of another family of systems that I also intend to "open", and more specifically XR809
    - to some extent it will force me to better organize the project and keep everything compiled on all platforms and emphasize the division into modules

    NOTE - C and networking basics
    I will try to present the topic as interestingly as I can, but for obvious reasons I have no way to explain everything line by line, and I will not explain the absolute basics of C, HTTP and TCP. In addition, in the topic I allow myself some simplifications that should not be used in the target product, such as using the dangerous strcat function (it does not check if there is room for what we copy in the target buffer).

    Own HTTP server - work organization
    I decided to write my own HTTP server in Visual Studio C ++ 2008 Express Edition. This is the IDE I have worked with the most and I know it quite well so the choice for me was obvious, but you can use any other C/C++ compiler.
    This IDE compiles my server to Windows platform (exe) - the HTTP server in the initial phase will be created and tested on this platform.

    Own HTTP server - starting point
    HTTP is a protocol that operates at the TCP level (not to be confused with UDP). TCP was chosen here by the creators of HTTP due to the reliability of the transmission, and more specifically the support for retransmission of lost packets, which UDP does not offer (UDP is connectionless). So we will start with a simple TCP server written under Winsock (which is basically practically 1: 1 compatible with sockets under Linux and also those on BK7231T):
    Code: c
    Log in, to see the code

    The above code opens the listening on port 80 (we cannot have it busy on our machine - when we have, for example, Apache running, the port will be already occupied and the program will not take it over), so we can "talk to it" from a web browser (in fact, you could use any other port - you can include the destination port in the URL, but let's not complicate it).
    If there is a client, the code above handles it and maintains a TCP connection with it.
    We are most interested in three sections of this code.
    This is where the packet is retrieved (recv blocks the program until it receives something, although it can be nonblocking on Berkeley sockets):
    Code: c
    Log in, to see the code

    Here we create the text that we will send:
    Code: c
    Log in, to see the code

    We send it here:
    Code: c
    Log in, to see the code

    Let's run the program:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    You need to enter an address in your browser. You can also enter IP. Our IP (those in the LAN - type 192.168 etc.) can be found, for example, with the ipconfig command, but there is an easier way to refer to our machine. You can use the localhost address, which is 127.0.0.1:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    As we can see, the server replied "This is an example reply from my server". What did it receive?
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Here we can see the content of an example GET request:
    Code:

    GET / HTTP/1.1
    Host: 127.0.0.1
    Connection: keep-alive
    Cache-Control: max-age=0
    sec-ch-ua: " Not;A Brand";v="99", "Microsoft Edge";v="97", "Chromium";v="97"
    sec-ch-ua-mobile: ?0
    sec-ch-ua-platform: "Windows"
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.69
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    Sec-Fetch-Site: none
    Sec-Fetch-Mode: navigate
    Sec-Fetch-User: ?1
    Sec-Fetch-Dest: document
    Accept-Encoding: gzip, deflate, br
    Accept-Language: pl,en;q=0.9,en-GB;q=0.8,en-US;q=0.7

    The most important thing is the ability to navigate the pages - let's see how the package will change when we ask, for example, about the index subpage:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    This time request looks like this:
    Code:

    GET /index HTTP/1.1
    Host: 127.0.0.1
    Connection: keep-alive

    What about the arguments of the GET query? These are the arguments that we even see on the Electroda when we write a message, for example:
    Code:

    https://www.elektroda.pl/rtvforum/privmsg.php?mode=reply&p=6195972

    How will our server display them?
    Code:

    GET /rtvforum/privmsg.php?mode=reply&p=6195972 HTTP/1.1
    Host: 127.0.0.1
    Connection: keep-alive

    We'll parse this in a moment, but first a little tweak.
    Our program displays strange characters after received packet:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    This is because the package does not contain the NULL terminating character, which is a byte zero that marks the end of an ASCII string. We will add it manually:
    Code: c
    Log in, to see the code

    I added setting the next next byte (outside the packet, because we address arrays in C from 0) to 0 (NULL byte). In addition, I reduced the size of the available buffer by 1, so as not to go beyond its range.

    Own HTTP server - parsing the current page
    Parsing the current page, that is, extracting the target resource address on the server, I will do completely from 0. You can do it in various ways, you can copy this address to another variable (such smarter strcpy), or you can check it "in place" and only get the result true / false if it matches the given ...
    But first we need to check if this is a GET query at all. For this I prepared my own function http_startsWith :
    Code: c
    Log in, to see the code

    The function sequentially compares two strings of characters, passes character by character. ++ increments the pointer by one unit (here it points to char, a byte-sized type, so one byte).
    Now the package handling looks like this:
    Code: c
    Log in, to see the code

    The program will reject queries without the "GET" header. Now let's move on, let's do a function that checks if the resource address requested by the client matches the given name:
    Code: c
    Log in, to see the code

    The function works like the previous one, except that it stops the comparison when it encounters a space or question mark. This is because in the query:
    Code:

    https://www.elektroda.pl/rtvforum/privmsg.php?mode=reply&p=6196006

    the resource (php file) address ends before the question mark, then there are GET arguments.
    Let's try to use our function:
    Code: c
    Log in, to see the code

    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    It works! Recognizes which page we are asking about.

    Own HTTP server - argument parsing
    Now it's time to somehow read in the arguments that follow the question mark in the body of the GET request. The format of the arguments is fairly simple, we have a question mark, then a variable name, an equal sign, the value of the variable and if there is anything else then the & sign and the next variable.
    Code:

    rtvforum/privmsg.php?mode=reply&p=6196006

    We will also load this ourselves. This time, however, we will move the value of the variable to a separate buffer. In this case there is a high risk of "buffer overflow" problems and in the production version every effort should be made to prevent this from being possible, but for this demonstration I will not go into more detail. I will only add that exceeding the buffer size may even allow remote execution of malicious code in our program ...
    Code: c
    Log in, to see the code

    Here I have a function that checks the name of the argument and copies its contents to an external buffer separately. The important thing here is the handling of characters such as & and '' and the byte zero telling us that we need to stop parsing.
    I'm not saying here that my approach is the best (because here I iterate all arguments in this http_getArg function call, and when I want to load 5 different arguments, I will iterate all 5 times), but on such a scale it doesn't matter.
    As a demonstration of this parsing, let's make a simple calculator:
    Code: c
    Log in, to see the code

    The program above checks if both arguments are available. If so, it prints their sum (atoi converts string to integer, and sprintf converts, among others, integers to string), and if not, it shows the appropriate message:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    The program works.
    Of course, sending the reply by HTTP is still to be done.

    Own HTTP server - sending a response
    What should the server send us in response to a GET request?
    It is best to find out in practice. I used Wireshark for this - a packet capture tool:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    You can see that typical HTTP answer consists of two layers:
    - HTTP layer
    - the content of the response itself (usually html, although there may also be an image, etc., depending on the content type)
    The answer can be simplified to:
    Code:

    HTTP/1.1 200 OK\r\n
    Content-Type: text/plain\r\n
    \r\n

    First we have the famous code 200 OK (analogous to the 404 you probably know), and then the type of content. Then one blank line - that's important too. After that we have the answer content (eg. html text).
    Let's enter it in the code:
    Code: c
    Log in, to see the code

    Of course, the Content-type can also be different (e.g. for a picture), but we don't need that here. The text/javascript format will be for Javascript files, which may also be useful in the future.
    And let's prepare a helper function that initializes the HTTP header:
    Code: c
    Log in, to see the code

    And let's do some content on the HTTP page:
    Code: c
    Log in, to see the code

    Is it working?
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Our first HTML page written on our own HTTP server is up and running!

    Own HTTP server - cross-platform
    We moved away a bit from the WB2S / BK7231T topic, but now it's time to come back to it.
    Let's think - how can we organize our server to run on Windows and WB2S at the same time?
    A cut has to be made - we separate Windows stuff into one place, HTTP stuff into another.
    From now on, my old main.c file (where the winsock code is) is called win_main.c and it contains:
    Code: c
    Log in, to see the code

    The HTTP_ProcessPacket function is in another file, it takes the input packet and the output buffer as arguments (yes, then you need to add a check if you are not exceeding the range).
    The HTTP_ProcessPacket function is now in a new file - new_http.c:
    Code: c
    Log in, to see the code

    Of course you also need to make a header (.h, header file and be able to #include it) - but I assume we know C ...
    Code: c
    Log in, to see the code

    This feature allows us to use our server on any platform, including the BK7231T.
    On the BK7231T we connect to the TCP code (just set it to listen on port 80) like this:
    Code: c
    Log in, to see the code

    For convenience, I added detection of how many fields have actually changed.
    NOTE: All of the above (including switching channel / relay values) can be tested in my batch in the Windows version! You don't even have to have WB2S to develop it and work on it. Very comfortable.

    Connecting the pins for BK7231
    My pin configurator and the HTTP server work fully on Windows. This is possible because the pin handler associated with BK7231T is appropriately separated in the new_pins.c file:
    Code: c
    Log in, to see the code

    However, the change of the channel value itself is independent of the platform:
    Code: c
    Log in, to see the code

    Yes, one int represents the channel values - one bit equals one channel. Therefore, the limit of the number of channels is now 32.32 bits in an integer on a 32-bit platform.


    Connecting the MQTT service (sending channel changes to the MQTT server)
    I wrote about the MQTT itself in the first part. Here I will only show when and how information is sent that the user pressed a physical button on the casing and changed the state of the device.
    This callback (pointer to function) is used for this:
    Code: c
    Log in, to see the code

    Let's see what the use of CHANNEL_SetChangeCallback looks like, already on the BK7231T side, in tuya_device.c:
    Code: c
    Log in, to see the code

    The code in the above version does not distinguish which channel has been modified, it just does MQTT publish the values of the first channel (CHANNEL_Check (1)) to the MQTT server it is connected to.

    Connecting the MQTT service (receiving commands from Home Assistant)
    The other way communication is carried out in an analogous way. From the MQTT you can get the command to change the channel value to another (to 1 or 0) and then we have to use CHANNEL_Set to propagate it further to the pins.
    The reception of this by the MQTT looks like this:
    Code: c
    Log in, to see the code

    We have separate functions for publish (variable name) and data (its value), but at the moment I am not using it to make things more complicated. I just assume there is one channel and that is enough for simple situations.

    Possibility to rigidly configure in code
    The current version of the project does not support saving settings to Flash memory, so it is worth configuring everything for our device in the code. For me it looks like this:
    Code: c
    Log in, to see the code

    The settings above work fine on SmartSwitch Tuya WL-SW01_16 16A:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

    Connecting to the Home Assistant
    Connecting to the Home Assistant is very simple. It requires modification of the configuration.yaml file, where we include, among others what MQTT publish we are listening to:
    Code:

    switch:
      - platform: mqtt # Again, it's an MQTT device
        name: "WB2S" # Choose an easy-to-recognize name
        state_topic: "wb2s/get" # Topic to read the current state
        command_topic: "wb2s/set" # Topic to publish commands
        qos: 1
        payload_on: 0 # or "on", depending on your MQTT device
        payload_off: 1 # or "off", depending on your MQTT device
        retain: true # or false if you want to wait for changes

    [i] NOTE: the final code in this topic requires a different MQTT name format, no longer wb2s / get but wb2s / 1 / get where 1 is the channel / relay name, details later.
    Similarly, on the WB2S side, you also need to complete the information on our MQTT server and the command names, respectively:
    Code: c
    Log in, to see the code

    Code: c
    Log in, to see the code

    Code: c
    Log in, to see the code

    Full code in the attachment at the end of the topic.


    Demonstration of operation - version with one relay
    Below is a presentation of the action on the video:
    [movie: cd6415ef5e] https://filmy.elektroda.pl/24_1643033014.mp4 [/ movie: cd6415ef5e]
    As you can see (I hope - because there are problems with the electrode player on some browsers) Home Assistant immediately responds to pressing the physical button on the device, and the device responds immediately to clicking on the status change on the Home Assistant website. Communication is efficient and fast, it is very responsive.
    The only thing missing is refreshing the device's own page, but this can be added quite easily via a JS script.

    Support of multiple relays on one device
    Supporting multiple relays on one device turned out to be quite simple to implement. It was possible mainly thanks to the so-called MQTT wildcards, which allow you to listen to multiple topics at the same time.
    The modified subscription setting with wildcard (+ sign) looks like this:
    Code: c
    Log in, to see the code

    This code above is used to receive information about changes from Home Assistant through MQTT.
    Just receiving subscriptions now requires dereferencing and saving the channel index (so far I did it in a trivial way, then it will improve):
    Code: c
    Log in, to see the code

    Wildcard (plus) is replaced with the channel index.
    The announcement of the change from the WB2S / WB3S level now looks like this:
    Code: c
    Log in, to see the code

    Simply put, the channel (relay) index is also stored in the message.
    For testing, I used this 4-relay driver on the WB3S:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Now also the way of defining relays in Home Assistant in configuration.yaml changes. From now on it looks like this:
    Code:

    switch:
      - platform: mqtt # Again, it's an MQTT device
        name: "WB2S" # Choose an easy-to-recognize name
        state_topic: "wb2s/1/get" # Topic to read the current state
        command_topic: "wb2s/1/set" # Topic to publish commands
        qos: 1
        payload_on: 0 # or "on", depending on your MQTT device
        payload_off: 1 # or "off", depending on your MQTT device
        retain: true # or false if you want to wait for changes
       
      - platform: mqtt # Again, it's an MQTT device
        name: "WB2S2" # Choose an easy-to-recognize name
        state_topic: "wb2s/2/get" # Topic to read the current state
        command_topic: "wb2s/2/set" # Topic to publish commands
        qos: 1
        payload_on: 0 # or "on", depending on your MQTT device
        payload_off: 1 # or "off", depending on your MQTT device
        retain: true # or false if you want to wait for changes
       
      - platform: mqtt # Again, it's an MQTT device
        name: "WB2S3" # Choose an easy-to-recognize name
        state_topic: "wb2s/3/get" # Topic to read the current state
        command_topic: "wb2s/3/set" # Topic to publish commands
        qos: 1
        payload_on: 0 # or "on", depending on your MQTT device
        payload_off: 1 # or "off", depending on your MQTT device
        retain: true # or false if you want to wait for changes
       
      - platform: mqtt # Again, it's an MQTT device
        name: "WB2S4" # Choose an easy-to-recognize name
        state_topic: "wb2s/4/get" # Topic to read the current state
        command_topic: "wb2s/4/set" # Topic to publish commands
        qos: 1
        payload_on: 0 # or "on", depending on your MQTT device
        payload_off: 1 # or "off", depending on your MQTT device
        retain: true # or false if you want to wait for changes

    A few explanations:
    - "wb2s" here is the proper name of the device (if we have two devices, it will have to be changed, e.g. in the code ...)
    - in the name "wb2s / X / get" the X character means the channel number (relay number)
    - if we have a single relay, we only use "wb2s / 1 / get" and "wb2s / 1 / set"
    - payload values tell what we are sending (or receiving) to determine the state of the device, just 1 or 0 here
    Result:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

    What does the WB2S module require to run?
    I wasn't quite sure where I should put this paragraph, so I'll just leave it here.
    I received from @PIXELO some desoldered WB2S modules, which are as found for testing.
    I checked that such a module does not require any external connections except a 3.3V power supply to work. It can also be easily programmed.
    Below is a photo report from soldering / commissioning:
    [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
    [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
    [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
    The module prepared in this way works, one UART is used for programming, the other is for receiving the log.
    I do not describe the connections themselves, because I described them in the first topic of the series.

    How to add support for a new device?
    Below I present full instructions on how to add support for a new device from BK7231T to the current version of my project.
    [This section will be updated soon because Pin Configurator is now working]
    1. Download the latest version of the SDK from this topic (complete the missing toolchain in \ platforms \ bk7231t \ toolchain)
    2. Complete the data in apps \ my_alpha_demo \ src \ tuya_device.c
    - SSID and password in connect_to_wifi
    - MQTT username and password in mqtt_connect_client_info_t
    - MQTT server IP at ipaddr_aton
    - optionally change the device name in MQTT packets in tuya_device.c, i.e. in "wb2s / + / set" change "wb2s" to a unique name, different for each device in your network (similarly for get)
    3. Complete the pin configuration in apps \ my_alpha_demo \ src \ tuya_device.c in device_init:
    Code: c
    Log in, to see the code

    4. Compile the project "my_alpha_demo" following the instructions from here:
    https://www.elektroda.pl/rtvforum/topic3850712.html
    5. Upload "my_alpha_demo" according to the instructions from here:
    https://www.elektroda.pl/rtvforum/topic3850712.html
    6. Complete configuration.yaml from Home Assistant according to the pattern:
    Code:

    switch:
      - platform: mqtt # Again, it's an MQTT device
        name: "WB2S" # Choose an easy-to-recognize name
        state_topic: "wb2s/1/get" # Topic to read the current state
        command_topic: "wb2s/1/set" # Topic to publish commands
        qos: 1
        payload_on: 0 # or "on", depending on your MQTT device
        payload_off: 1 # or "off", depending on your MQTT device
        retain: true # or false if you want to wait for changes
       
      - platform: mqtt # Again, it's an MQTT device
        name: "WB2S2" # Choose an easy-to-recognize name
        state_topic: "wb2s/2/get" # Topic to read the current state
        command_topic: "wb2s/2/set" # Topic to publish commands
        qos: 1
        payload_on: 0 # or "on", depending on your MQTT device
        payload_off: 1 # or "off", depending on your MQTT device
        retain: true # or false if you want to wait for changes
       
      - platform: mqtt # Again, it's an MQTT device
        name: "WB2S3" # Choose an easy-to-recognize name
        state_topic: "wb2s/3/get" # Topic to read the current state
        command_topic: "wb2s/3/set" # Topic to publish commands
        qos: 1
        payload_on: 0 # or "on", depending on your MQTT device
        payload_off: 1 # or "off", depending on your MQTT device
        retain: true # or false if you want to wait for changes
       
      - platform: mqtt # Again, it's an MQTT device
        name: "WB2S4" # Choose an easy-to-recognize name
        state_topic: "wb2s/4/get" # Topic to read the current state
        command_topic: "wb2s/4/set" # Topic to publish commands
        qos: 1
        payload_on: 0 # or "on", depending on your MQTT device
        payload_off: 1 # or "off", depending on your MQTT device
        retain: true # or false if you want to wait for changes

    Ready!
    A pin configurator is available, but it doesn't save the settings to Flash yet, so it's basically just for us to play and experiment.

    Testing support on Windows
    The final version of the project (the one I'm publishing here) still supports the compilation of the HTTP server and pin / relay / channel system for Windows via the Visual Studio environment - just open the myHTTP2022.vcproj project from the my_alpha_demo folder and compile. After starting it, it will put us a server on port 80 on our machine. Thanks to this, it is much easier and faster to develop the entire HTTP module from the project, test the appearance of different pages, change the division into subpages, etc.

    Further development of the project
    Of course, this is only an initial demo and I'm going to add soon:
    - easier handling of MQTT devices (random name based on the MAC address of the device, the ability to change this name via the web)
    - saving the settings to the flash memory so that you do not have to recompile the project forever
    - system for resetting the device and configuring it via an open WiFi network
    - XR809 support (the same HTTP code will be and the organization for two different families of microcontrollers)
    - maybe I will move partially to Ajax with this HTTP panel of the page, for example to refresh the states of the buttons / relays ...

    Project support
    If you like the idea and initiative, you can support the project through
    - testing (in some time I will create a github repository for both projects, because I'm also doing an open source batch for XR809)
    - collecting information in what devices are what modules (you can send photos, etc., even in this topic. I know about the list https://templates.blakadder.com/)
    - paypal donation:
    https://paypal.me/openshwprojects

    (There are some of these different devices and I want to test as many of them as possible, moreover, I have already put a lot of teardown at https://www.elektroda.pl/rtvforum/forum507.html, the funds will go to the purchase of more devices)
    - if you have, for example, BK7231T modules that you have left after replacing with ESP12F or damaged smart devices, etc., I can accept them for testing (contact PW)
    There are many more modules similar to WB2S and WB3S, each of them I intend to ultimately support.

    Summary
    This is how the world's first open source firmware for BK7231T really starts to take shape. From a few simple demos of UDP, TCP, HTTP and MQTT presented in the previous part, I have already managed to make the first really practical firmware, which could finally be used in a normal 'smart' home to control lighting and more (if we do not add Relay, we can read the states of "Buttons" and work on them in Home Assistant, so the door opening sensor, etc).
    Another update is coming soon. If you like the initiative, let me know, I need to know how many potential users we have and what is the interest in the whole idea.
    Finally, I would like to thank @strigona and @PIXELLO for giving me test modules.

    Cool? Ranking DIY
    Can you write similar article? Send message to me and you will get SD card 64GB.
    About Author
    p.kaczmarek2
    Level 26  
    Offline 
    p.kaczmarek2 wrote 1814 posts with rating 3587, helped 94 times. Been with us since 2014 year.
  • #2
    lemgo
    Level 14  
    As long as the code, description, etc. was not found on some public repo - I would not count on any response
    The electrode forum is not the best collaboration tool.

    Why so archaic VS?

    Can I suggest one sentence for the code? About 30 years ago, we moved away from declaring variables at the beginning of a function.
  • #3
    khoam
    Level 41  
    @ pkaczmarek2 I think that some code error diagnostics in the form of checking results of atoi () function would be useful. Such a loose attention ;)

    lemgo wrote:
    About 30 years ago, we moved away from declaring variables at the beginning of a function.

    Who exactly? :) There is nothing wrong with that, and it is more a matter of style.
  • #4
    mpier
    Level 28  
    p.kaczmarek2 wrote:
    - collecting information in what devices are what modules (you can send photos, etc., even in this topic.

    Hama 176581, LED, 10W, 806lm, E27: WB2L .
    I bought in Biedronka for PLN 14.99. I was counting on esp8266, apparently they did it once.

    Greetings.
  • #5
    p.kaczmarek2
    Level 26  
    lemgo wrote:
    As long as the code, description, etc. was not found on some public repo - I would not count on any response

    The repository will be established in a few days, because several people have already asked about it. The delay is basically because I wanted to organize two projects in one repo (XR809 and BK7231T support together with a shared configurator), but for now I will focus on BK ...

    lemgo wrote:
    Why so archaic VS?

    In the case of such a simple project as one Winsock file + several pure C files, even a student should be able to compile it in any other VS or Code Blocks, I use it in 2008 and because it is very light. Of course, I also have newer Visuals for more serious projects.

    lemgo wrote:
    Can I suggest one sentence for the code? About 30 years ago, we moved away from declaring variables at the beginning of a function.

    It seems to me that in some standards it will not compile, e.g. here:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

    khoam wrote:
    @ pkaczmarek2 I think that some code error diagnostics in the form of checking results of atoi () function would be useful. Such a loose attention ;)

    I haven't done any error checking in many places, I'm not sure yet how much it will develop and if everything will work out sensibly. I have such a habit that when I try to make a prototype, I skip certain things and make optimistic assumptions that the user, for example, will not force my buffer over to me.


    mpier wrote:

    Hama 176581, LED, 10W, 806lm, E27: WB2L .
    I bought in Biedronka for PLN 14.99. I was counting on esp8266, apparently they did it once.

    Super helpful news, I'll see if I have them for sale, because somehow I have never seen anything smart in my city in ladybugs. Or maybe someone has one?

    I haven't tested WB2L yet and I'd love to check it out
  • #7
    p.kaczmarek2
    Level 26  
    @hellcube, prepare the UART converter, connect it according to the instructions in the first part and for now download the current software through it to have a backup.

    I can't see the element markings clearly in the photos, but this large chip is probably a microcontroller - HOLTEK BS6600? If it is a microcontroller and the BK7231T communicates with it via UART, then you will have to add a little bit to the current version of the software, probably some simple TuyaMCU support:
    https://tasmota.github.io/docs/TuyaMCU/#tuyasend-command

    Where did you buy these switches, I could use one for testing, it will be 100% remotely difficult to run TuyaMCU on the BK7231T and I do not have any similar smart device at my disposal.

    My topics about smart / wifi devices with UART communication:
    https://www.elektroda.pl/rtvforum/topic3819498.html
    https://www.elektroda.pl/rtvforum/topic3825966.html
  • #8
    ex-or
    Level 27  
    p.kaczmarek2 wrote:
    Text / plain will be for Javascript files

    The MIME type for javascript is text / javascript
  • #9
    p.kaczmarek2
    Level 26  
    ex-or wrote:

    The MIME type for javascript is text/javascript

    Seriously? It looks like you are right.
    Oh hell, then I've been wrong for 5 years, because when I started playing with HTTP on microcontrollers on PIC18F67J60, in their example (HTTPDemo from MikroC) JS scripts were using "text/plain". Thanks for the proofreading. But at least I know it worked with "text/plain" as well.
  • #10
    p.kaczmarek2
    Level 26  
    Major update - configurator works, there is no need to compile firmware for each user!

    Current installation instruction (2022.01.28):
    1. Get openbk7231t_20220128_UA.bin from here:
    https://github.com/openshwprojects/OpenBK7231T/tree/master/releases
    2. Use method from this topic to burn firmware to BK7231T:
    https://www.elektroda.com/rtvforum/topic3850712.html
    NOTE: instead of repowering the chip, you can try resetting it with CEN pin
    3. Connect to created WiFi:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    4. Configure connection to your wifi (do not enter SSID/password incorrectly):
    [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 [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    BK will not reset automatically now, you must power off it yourself!
    5. Find out the Ip of new device that connected your network:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    6. Enter this IP in browser, go to Quick config if you have one of already tested devices:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Or set pin roles manually (you might need to repower device for this to fully take effect):
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Now you can control relays etc from device WWW panel:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    7. Configure MQTT connection as well:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    8 Now generate Home Assistant config - go to Home Assistant Cfg generator:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Copy and paste it to configuration.yaml, but don't repeat the "switches" keyword, it should be only once.
    You can change device display names.
    9. Restart Home Assistant and here it is:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

    So far no TuyaMCU support, no PWM, but it's coming soon.

    PS: There is a simple failsafe built-in in case you lose access to your WiFi or enter wrong creditentials.
    You must have at least pin configured as button.
    Power of the module, power on and quickly do a "double click" on a button many times. Wait few seconds, power off chip, power on again, and it will be an open access network once more.
  • #12
    p.kaczmarek2
    Level 26  
    Beken flashing protocol is different from ESP so you can't just use esptool.py or anything like that. It has to be Beken Writer, I attach a copy in this post for your convenience.

    There was also user, @btsimonh , who (if I remember correctly) ported/compiled BK writer in Python, but I haven't used his version so far.
  • #13
    MrTechGadget
    Level 2  
    That’s what I was afraid of, I only see a Windows binary, not Linux or OSX, so I’ll have to dig out a Windows device. If there is another option I’d love to try it.
  • #14
    p.kaczmarek2
    Level 26  
    So let's try this hid_download modified by @btsimonh :

    https://github.com/btsimonh/hid_download_py

    "run this, then re-power the unit, repeating until the flashing starts"
    Code:

    python uartprogram simon_light_pwm_demo_UA_1.0.0.bin -d com4 -w


    EDIT: IMPORTANT it's better to reset the module by pulling temporary CEN to ground instead of disconnecting power totally. Disconnecting power and reconnecting gives me stability issues (sometimes programming fails) on a certain WB3S LED PWM dimmer
  • #16
    p.kaczmarek2
    Level 26  
    btsimonh wrote:
    I use my (beken's :) ) uartdownload from hi_download_py exclusively - but on windows. Please report success on linux


    If you (I mean @mrtechgadget ) have some issues with this beken python uartdownload, I can always help, I have multiple Linux VMs and one physical machine at hand. One has Ubuntu, second Debian, etc...

    I started work on PWM support and I will focus on it for now:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    The story of this WB3S LED dimmer (along with REPAIR) is here:
    https://www.elektroda.pl/rtvforum/topic3798114.html
    And this is the dimmer that has random issues with BKwriter programming when using "power off and power on" method. It programs nicely when I use "pull CEN to ground to reset" method instead.
  • #17
    ferbulous
    Level 9  
    Hi, big thanks for releasing this
    I have this switch device with wb3s
    Some question on flashing the firmware
    Is there any pins i have to pull down like how we need to ground gpio with esp?
    Seems like i only need to connect to the following the pins, reboot the device and start bk-writer? [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
  • #18
    p.kaczmarek2
    Level 26  
    ferbulous wrote:
    Hi, big thanks for releasing this

    No problem, the firmware is gonna get much more fancy soon! I am also actively looking for more devices to support, I hope to get some BK7231T smart light bulbs soon. I will post here photos when the packages arrive.

    Also, the gift smart plug (UK version) from @strigona is still on the way to Poland!

    ferbulous wrote:

    Is there any pins i have to pull down like how we need to ground gpio with esp?

    No

    ferbulous wrote:

    Seems like i only need to connect to the following the pins, reboot the device and start bk-writer?

    Yes, and by "reboot the device" first I meant "power off and power on", but now I think that better way is to reset/reboot it with temporary connecting CEN to ground (think about it as a RESET).

    As per your photos, @ferbolous , could you please show whole board? But if I am seeing correctly, then you have a device that should be fully supported right now. The SOIC chip looks like a touch button controller for me, a simple touch button controlller, which just output high or low state on each pin to be read as a button of WB3S. So it should work with current firmware version.
  • #19
    ferbulous
    Level 9  
    Thanks, as for the 2tx, 2rx pins, do i still need to connect them to a 2nd uart?

    I also have some wifi light bulbs, do you think this e14 could be BK7231T? How do i open the metal casing. The pinouts looks similar

    And there’s also this gu10 ewelink bulbs that use BL602. I did order some ESP-01D modules to replace this

    [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 [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 [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
  • #20
    pvxvictor
    Level 2  
    LSPA9
    aliexpress.com / item / 4000478798085.html
    [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 [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
  • #21
    p.kaczmarek2
    Level 26  
    pvxvictor wrote:
    LSPA9

    Thanks! That's what I want.
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    Let's see how my firmware works on this little plug.
    (for the record - I bought it from eBay, as it supports Paypal payments and I have donations for that, but it's the same model)
    It will be extra-interesting because I haven't tried BK7231N so far.

    ferbulous wrote:
    Thanks, as for the 2tx, 2rx pins, do i still need to connect them to a 2nd uart?


    2nd uart is useful for debugging, to check if firmware starts, etc, what happens if there are problems. It's not required if everything goes OK

    No idea about bulbs.
    ferbulous wrote:
    BL602

    I haven't seen this chip before. So far I am going to support BK7231T and XR809:
    https://www.elektroda.com/rtvforum/topic3806769.html
    but if BL602 has toolset available, then support also might be possible in the future. As said in the first post, my main http server and configurator is a multiplatform design (for example, the server itself can be run on Windows, it will make CSS and HTML and maybe ajax webpage design faster - yes, I know, the current webpage is just white and black, but it's only because it would take too much to make it nice at this moment).
  • #22
    pepesuriano
    Level 9  
    What an excellent site to come after opening your smart plug and finding a WB2S chip! Congratulations on this great work!!!

    I would like to contribute with the feedback trying to flash my smart plug marked as PF161 on the case, I understood how to connect RX, TX, 2RX and 2TX from this post (WB2S/BK7231 Tutorial - writing custom firmware - UDP/TCP/HTTP/MQTT) but it wasn't clear for me where I have to connect 5V, since my board is different, here are some pictures:

    [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

    Any hints on where to plug 5V?

    Cheers
  • #23
    btsimonh
    Level 10  
    Beware the SDK makefile. It writes and links ALL object files from Debug/objs having removed folder references... so it seems to me you can't have two .C files with the same name - they would overwrite?
    Also, it does not remove the .o /.d files, and if you MOVE a file (e.g. move a header from our src folder to a subfolder, make refuses to build because it's using old dependency info - you have to delete the Debug folder AND all the .o files in the whole SDK to make it work again.
  • #24
    p.kaczmarek2
    Level 26  
    btsimonh wrote:

    Also, it does not remove the .o /.d files, and if you MOVE a file

    Are you sure about that? I'd rather say that you yourself commented out o. files removal make command in a misguided optimization attempt. Still, I'm not sure, but I clearly remember you doing it.

    Having two .C files with the same filename is in general considered a bad practice.

    pepesuriano wrote:
    it wasn't clear for me where I have to connect 5V, since my board is different, here are some pictures:

    Again, I'd kindly ask you to post more detailed photos because I can't clearly see what is where, for example, what is this SOIC 8 chip? Is this a power supply controller? Or maybe a step down converter that creates 3.3V from 5V or 12V?
    But after a second glance I spotted something:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    What's that? Isn't it a LM1117?
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant


    BK7231 update: We have a PWM progress! Dimmer works, but right now only by HTTP (MQTT is still pending):
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
  • #25
    pepesuriano
    Level 9  
    No problem! Here you go:

    The SOIC8 appears to be a BL 0937
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

    And the other one was an AMS1117 indeed!
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant

    (I will need to get a magnifier for this stuff :P)

    Cheers
  • #26
    btsimonh
    Level 10  
    p.kaczmarek2 wrote:
    Are you sure about that? I'd rather say that you yourself commented out o. files removal make command in a misguided optimization attempt. Still, I'm not sure, but I clearly remember you doing it.

    yes, but removed the 'echo' and still no go.

    p.kaczmarek2 wrote:
    Having two .C files with the same filename is in general considered a bad practice.

    no - without searching the whole sdk to avoid duplicates we would not know what to avoid. Same named C files should work from different folders, but I am not convinced that they will in this repo. Header files maybe - because you may not know where they come from without care.

    Personally, I'm thinking we should build a library out of the SDK - then we can just link our app against that.
  • #27
    boozeman
    Level 11  
    Hi,

    I will try to flash Nedis WIFIPO120FWT Smart Plug with Power Meter soon.

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

    Pinout based on WB2S:

    PWM0 = Led
    PWM1 = BL0937 CF
    PWM2 = BL0937 CF1
    PWM4 = BL0937 SEL
    PWM5 = Relay
    TX1 = Button
  • #28
    Glaedr304
    Level 1  
    I left a message on your reddit post about the smart sockets I have using a SPI interface. I am reposting here as you requested. I had to exclude the links as my account is too new, I added more description as was probably necessary.

    I bought them on amazon, they are Aoycocr smart plugs, they were a pack of 4, Model is X5P. On tasmota's site it is documented that these used to have ESP chips, but were changed recently.

    I included what I consider the two clearest photos of what I believe you were asking for, I can certainly take more photos from more angles as you need.

    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
  • #29
    p.kaczmarek2
    Level 26  
    btsimonh wrote:

    Personally, I'm thinking we should build a library out of the SDK - then we can just link our app against that.

    Good idea, you can try changing makefile to do that, I am still focusing on supporting more devices.


    boozeman wrote:

    I will try to flash Nedis WIFIPO120FWT Smart Plug with Power Meter soon.

    BL0937 is not supported yet, but I will try to buy this device and start working on that. Still, it's good to know that BL0937 is used along with BK7231T.

    Glaedr304 wrote:

    I bought them on amazon, they are Aoycocr smart plugs, they were a pack of 4, Model is X5P. On tasmota's site it is documented that these used to have ESP chips, but were changed recently.

    Don't worry about losing Tasmota, I will try to gradually implement all required Tasmota features in my BK7231T firmware.

    As per your photos... that's exacly what I needed. Now take a look:
    [BK7231T] My HTTP server, configurator, MQTT support from Home Assistant
    (remember, it's the dot what marks the first pin, not the label orientation!)
    I can clearly see that both UART ports are being routed from the chip to the second board, are you 100% sure that they are not present on the bottom side of the PCB?

    I'd bet they are, but if not, then @btsimonh did a great job with SPI flasher.
  • #30
    pvxvictor
    Level 2  
    p.kaczmarek2 wrote:
    but if BL602 has toolset available, then support also might be possible in the future.

    BL602 IoT SDK (Pine64 fork) https://pine64.github.io/bl602-docs/#
    https://github.com/bouffalolab
    https://lupyuen.github.io/articles/book

    Quote:
    BL0937 is not supported yet, but I will try to buy this device and start working on that.

    LSPA9 used BL0937.
    https://github.com/openshwprojects/OpenBK7231T/tree/master/apps/bk7231t_bl0937_1_plug_demo
    And start work in BLE.
    Has WiFi OTA Tuya.