Elektroda.com
Elektroda.com
X

Search our partners

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

Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

p.kaczmarek2 5889 21
This content has been translated » The original version can be found here
  • Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Hello
    Here I will present a step-by-step process of writing my own open source firmware for the XR809 / XR3 WiFi module using the example of a door opening sensor. My firmware for it will be compatible with Tasmota HTTP protocol and will allow it to be used independently of the manufacturer's servers. It will also give us compatibility with Home Assistant and Tasmota Control application from Google Play. Perhaps it will be the first such open source firmware in the web for this module.
    The software here will be created in C on ARM Cortex-M4F and it will be my first encounter with ARM.

    What will we gain from creating open source software?
    The situation here is the same as with ESP8266 and Tasmota/Domoticz vs. original manufacturer firmware. By using the open firmware, we gain:
    - independence from the manufacturer's servers (e.g. if the manufacturer shuts his server down, our device will continue to work)
    - no tracking and data collection from the manufacturer (some people are concerned about their privacy, some do not care)
    - compatibility with other open source solutions (e.g. here it will be possible to easily connect the device to Home Assistant directly)

    XR809 door sensor used
    You can find more information about used XR809 / XR3 door sensor there:
    https://www.elektroda.pl/rtvforum/topic3771510.html
    Kit picture:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Photo inside:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    XR809 is the ARM Cortex-M4F microcontroller operating at a frequency of 160MHz, offering 384KB SRAM and 2MB Flash ROM, additionally having rich peripherals, including UART, TWI, SPI, PWM, IrDA (T / R), SDIO and ADC:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    The module itself is called XR, its pinout:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    What will be descibed here?
    Let's first create a short write-up of tasks that we will have to complete in order to create our own firmware for XR809.
    Organizational tasks:
    - preparation of the hardware connection necessary to upload the firmware (in case of XR809 an UART adapter is enough)
    - preparation of the software that uploads the firmware from the computer to the chip (for XR809, it's called phoenixMC.exe)
    - preparation of toolchain (compiler) and XR809 SDK
    Tasks to be implemented in code:
    - our firmware must offer a simple website (both for configuration and for Tasmota HTTP)
    - our firmware must be able to read the state of the button and door opening sensor
    - our firmware must offer pairing and RESET functions (we must be able to somehow enter our WiFi credentials once)

    Tasmota HTTP protocol
    All Tasmota HTTP can be visualized with one GET request sent via HTTP from the client (here: phone from Tasmota Control or machine with Home Assistant) to the server (our XR809 device).
    GET query:
    Quote:

    GET /cm?cmnd=status HTTP/1.1
    Host: 192.168.0.110
    User-Agent: HomeAssistant/2020.12.1 aiohttp/3.7.1 Python/3.8
    Accept: */*
    Accept-Encoding: gzip, deflate

    Sample answer:
    Quote:

    HTTP/1.1 200 OK
    Content-type: application/json

    {"Status":{"Module":0,"DeviceName":"Tasmota","FriendlyName":["Tasmota","Tasmota2","Tasmota3","Tasmota4"],"Topic":"tasmota_EC3F8F","ButtonTopic":"0","Power":0,"PowerOnState":3,"LedState":1,"LedMask":"FFFF","SaveData":1,"SaveState":1,"SwitchTopic":"0","SwitchMode":[0,0,0,0,0,0,0,0],"ButtonRetain":0,"SwitchRetain":0,"SensorRetain":0,"PowerRetain":0},"StatusFWR":{"Version":"0.1(pic18f)","BuildDateTime":"2020-11-07T11:57:45","Boot":6,"Core":"0","SDK":"0","CpuFrequency":80,"Hardware":"ESP8266EX","CR":"367/699"},"StatusNET":{"Hostname":"tasmota_EC3F8F-8079","IPAddress":"192.168.0.50","Gateway":"192.168.0.1","Subnetmask":"255.255.255.0","DNSServer":"192.168.0.1","Mac":"5C:CF:7F:EC:3F:8F","Webserver":2,"WifiConfig":4,"WifiPower":17.0},"StatusSNS":{"Time":"2021-01-10T19:27:53","DS18B20":{ "Temperature":16},"TempUnit":"C"},"StatusSTS":{"Time":"2021-01-10T19:27:54","Uptime":"0T00:41:19","UptimeSec":2479,"Heap":24,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":37,"MqttCount":0,"POWER1":"OFF","POWER2":"OFF","POWER3":"OFF","POWER4":"OFF","Wifi":{"AP":1,"SSId":"5G_FULL_POWER","BSSId":"30:B5:C2:5D:70:72","Channel":9,"RSSI":98,"Signal":-51,"LinkCount":1,"Downtime":"0T00:00:03"}}}

    This answer is in JSON format.
    This is a necessary minimum which allows you to read the status of the device. The state of the device is in two places:
    - Power ": 0 - here the 0 is a bit-coded state of successive relays (or buttons). The number is written as decimal.
    - "POWER1": "OFF", "POWER2": "OFF", "POWER3": "OFF", "POWER4": "OFF", -the relays/states are here repeated, but in a readable format. The number of states is automatically determined from the table ["Tasmota", "Tasmota2", "Tasmota3", "Tasmota4"] .

    Protocol details:
    https://tasmota.github.io/docs/Commands/

    Setup of the work environment - Cygwin
    Cygwin is necessary if we want to be able to compile XR809 firmware on Windows. Cygwin is an environment that mimics the Linux command prompt. It is needed here because the makefile from the XR809 SDK uses Linux commands like cp to copy the results of libraries compilation to a given folder. That's the only reason why Cygwin is necessary.
    We can download Cygwin from here:
    https://cygwin.com/install.html
    During installation, after choosing to install from the network (Download) and the server from which we want to download packages, we have to select the components that we need:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    We need search tthe list and mark here make .
    After installing Cygwin, run the bat file:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    This is how we get a Linux command line on Windows:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    This is just like Linux terminal, the navigation is the same, except that when we want to go to disk partition D, for example, we type "cd d:" and it works, it just creates an artificial path /cygdrive/d

    Setup of the work environment - ARM toolchain
    The toolchain needed is gcc-arm-none-eabi-4_9-2015q2-20150609.
    You can download it here:
    https://launchpad.net/gcc-arm-embedded/4.9/4.9-2015-q2-update
    I chose the version for Windows and unpacked it to disk, while correcting the path so that there were no spaces (they might confuse stuff a bit, you have to remember about the quotes ...).
    In the end, the toolchain path for me came out like this: In: /GNU/4.9.2015q2/bin


    Preparation of the work environment - XR809 SDK from Git
    We download the XR809 SDK from git, here:
    https://github.com/XradioTech/XR809SDK/
    You can do it by just by clicking "Download Zip" here:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Or you can download it via Git, you can use the command line or graphical interface, here I'm using Sourcetree:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    After extracting/downloading, we need to update one file, gcc.mk .
    We need to enter the cygwin path to the toolchain there:
    Code: bash
    Log in, to see the code

    Here we are using Cygwin path, i.e. access to the disk via /cygdrive/ .


    XR809 SDK content from Git
    The SDK package is divided into shared libraries and separate examples.
    Shared libraries are in a folder /src/ .
    Larger examples are at /project/, smaller examples are at /project/example/, for example:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Build process
    Firmware build for XR809 (and similar) is done in two steps. First, we compile the shared libraries, this folder:
    XR809SDK/src/
    Then we compile the selected project, for example:
    XR809SDK/project/at_demo/
    Compilation consists 4 commands.
    Step 1.1:
    In the directory /cygdrive/w/GIT/XR809SDK/ src we do make.
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Step 1.2:
    In the /cygdrive/w/GIT/XR809SDK/src folder, we run make install.
    NOTE: this step requires linux cp command (copying files), so we run it in Cygwin. Windows doesn't have a Linux "cp" command in cmd, it does have its own (you could change the make build script to use Windows command set, but it's easier to use Cygwin).
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Step 2.1:
    In the /cygdrive/w/GIT/XR809SDK/project/at_demo/gcc folder (or in the project folder we selected), make:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    Step 2.2:
    In the same folder - make image:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    This command will use mkImage.exe to generate xr_system.img (which we will then upload to XR809 via UART).
    Result:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant


    Simplify the build process
    This step is not required, but I think that build process is too complicated and uses too many steps.
    Let's simplify it and create a Bash script that will do it all with one command.
    It's a linux script, but in cygwin it will of course work.
    Code: bash
    Log in, to see the code

    If we get some errors related to \r (carriage return), it means that we wrote it with Windows line endings standard (\r \n) and we need to convert it to unix line ending. This can be easily done in, for example, Notepad 2:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant


    Hardware connection
    XR809 is programmed via UART just like ESP8266.
    So let's prepare the cheapest dongle, USB TO TTL HW-597 (jumper set 3.3V logical levels):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    In addition, according to the catalog note:
    datasheet wrote:

    During firmware burning, both PB02 and PB03 need to be connected to a low level. When the module works properly, PB02 and PB03 cannot be connected to a low level

    For this pin EN - otherwise RESET - (to be able to reset the system). The board is battery powered.
    So the connections made:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant


    Burning the XR809 firmware
    The compiled firmware is in file xr_system.img , this can be found in XR809SDK\project\at_demo\image\xr809 .
    Now we need to use the programmer software, phoenixMC.exe
    This program requires that the UART port is not in use, so remember to close the port in Realterm or whatever terminal we are using.
    This phoenixMC.exe has chinese interface, but I figured out how to use it.
    Here are the steps on the GUI (we open the generated img file):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    On the hardware side:
    1. connect RESET to ground
    2. connect PB02 and PB03 to ground
    3. disconnect RESET from ground (it can be in the air)
    In other words, while booting, PB02 and PB03 must be on ground to enter programming mode.
    datasheet wrote:

    During firmware burning, both PB02 and PB03 need to be connected to a low level. When the module works properly, PB02 and PB03 cannot be connected to a low level

    Then click Burn:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    It should start uploading the firmware:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Upload ready:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Then we start XR normally, that is:
    On the hardware side:
    1. connect RESET to ground
    2. disconnect PB02 or PB03 to ground
    3. disconnect RESET from ground (it can be in the air)


    Let's try running the XR809 AT firmware
    We already know how to compile and upload the firmware. So we can upload the AT firmware (after its compilation, its binary files are not on the repo).
    Now we should be able to execute commands via UART. The default baud is 115200.
    Let's check the command AT + S.HELP (remembering CR LF, line feed and carriage return):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    AT works.
    Now let's try the command AT + S.SCAN .
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    The module sees the WiFi network. We are able to communicate with him.

    Creating our own AT command for XR809
    Well, let's start with something easy. We will add our own command to AT project. This command will connect XR809 to our WiFi. Of course, the final version of our firmware will not be using AT, but it's worth doing something like this just to check if everything is working correctly.
    Commands are in at_command.c :
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Here is a simple C table of command names, pointers to functions that handle them and short descriptions of these commands (for generating Help).
    I used Notepad2 to edit the code (throughout the topic).
    I added new command at the end:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Full function code (remember to add #include):
    Code: c
    Log in, to see the code

    This command will connect XR809 to our WiFi.
    Does our command work?
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    We can find out about the success of the process from the router logs.
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    In addition, it is worth checking if this is the MAC address that XR has (in AT there is a command to know it) and whether the ping command sees it (and ceases to see when we disconnect the power).


    The first HTTP server
    After a brief look at the SDK, you can see that the HTTP server library is ready to use.
    Is in src\net\shttpd-1.42 .
    The HTTP server demo is also ready, here: src\net\shttpd-1.42\examples\web_server_demo.c
    Let's try to run it. Server setup will be called from command we added earlier. Of course you have to add #include etc, but surprisingly the files were already compiled (a bit strange that they compile web_server_demo.c for the shared library..):
    Code: c
    Log in, to see the code

    After uploading the firmware and executing the command, the server works:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    By the way, you can see that it was a server for a related chip, XR871, not for the XR809 I am using here, but I guess they are basically the same.
    We will use this server as a base for Tasmota HTTP support.

    Implementation of GET packet handler
    After a brief analysis of the code from the XR809SDK\src\net\shttpd-1.42\examples\web_server_demo.c you can easily see how the GET request handling for a given URL is added:
    Code: c
    Log in, to see the code

    You just have to provide the path and a handler function pointer.
    So it's time to use shttpd_register_uri and add support GET /cm?cmnd=status HTTP/1.1 .
    shttpd_register_uri takes as arguments a pointer to context (ctx), a URI path, and a pointer to a function.
    To create my function, I copied and changed set_ap_info :
    Code: c
    Log in, to see the code

    GET registration:
    Code: c
    Log in, to see the code

    The result in the console after sending a GET:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    There is still some kind of issue with parsing GET arguments, but we will fix it later.
    The result in the browser:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    And now the best part - it is now already compatible with Tasmota Control from Google Play.
    https://play.google.com/store/apps/details?id...s_software.TasmotaControl&hl=pl&gl=US
    After adding device by IP, the result in Tasmota Control:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Of course, all these data are placeholders, i.e. everything is hardcoded.

    Parsing of GET parameters
    For a moment I was wondering why GET parsing is not working for me. This function is responsible for him:
    (void) shttpd_get_var ("cmnd", arg-> in.buf, arg-> in.len, value, sizeof (value));
    For POST it worked.
    But the implementation of the function is self-explanatory.
    Code: c
    Log in, to see the code

    arg->in.buf is an argument text buffer for POST, not GET.
    For a GET, it needs to be given a string of GET arguments, like this:
    (void) shttpd_get_var ("cmnd", query_string, strlen (query_string), value, sizeof (value));
    After this change for the query GET /cm?cmnd=status we get:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    This way we can extract the values of the GET arguments.

    Reading the door status
    Now it's to consider how can we read the door status from this sensor:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    This sensor responds to the magnet, the magnet makes the contacts touch. In my opinion, it is enough to read the pin in digital mode.
    I found examples of digital IO pins handling in XR809SDK/project/example/gpio/main.c
    Code: c
    Log in, to see the code

    So I pasted them into the fragment of code I was testing and I also added a test-only request "tst" in the query string to check if the sensor detects something.
    Code: c
    Log in, to see the code

    Door state display (only to check if it works, will be removed in next version):
    Code: c
    Log in, to see the code

    And now, after uploading our firmware, it's time to check if door sensor works.
    Test care - "Door closed":
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Test care - "Open doors":
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    It works - it's time to integrate it with Tasmota HTTP.


    The first signs of functionality
    I cleaned up the code a bit and also connected the button from the board as the second input. Below is the code for generating the response to the Tasmota HTTP query (code specially written quite clearly, it could be done better and shorter, but less readable for beginners):
    Code: c
    Log in, to see the code

    Result in Tasmota Control:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    With active sensor ("closed door"):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    With the button pressed:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    So everything works.
    The history in the UART console shows how often Tasmota Control polls the XR for the status:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    Reset to AP mode - button on the HTML page
    At the moment, my WiFi SSID and password are hardcoded into the firmware. I will now try to fix it.
    It should be possible to enter WiFi data on the XR809 page.
    First, I will add a website button to change XR809 wifi into an open Access Point.
    The XR website is at src/net/shttpd-1.42/examples/source.h.
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    "Reset AP" button added:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    We also need to add GET query support, just as before, this time for path /reset_ap:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    And the function to handle it:
    Code: c
    Log in, to see the code

    (I note right away that it would be better to wait 1 second in the main loop and then change the network state, because in the current version we will not get a response to this GET because the XR809 will disconnect immediately)
    After pressing the button, we lose contact with the module and its WiFi network appears:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    After connecting to this network, at IP address 192.168.51.1, we can reconfigure the module:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Here is our password and SSID (you still have to make the field to be of the password type, but it's not a problem, it's an HTML issue):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    (actually I did not create an open AP here, but an AP with a known SSID and password, but it's the same. You could also add randomization of this "12345" in the SSID so that it includes the MAC for example)


    Reset to AP mode - physical tactile switch on the PCB
    Now we need to create something similiar to well know physical button reset process for smart devices.
    We will make that pressing the on-board button for 5 seconds resets the device to the default AP (XR creates a WiFi network itself), so that we can then connect to it and configure it with data about our router.
    We need to somehow check if the button is pressed for 5 seconds.
    I decided to use the HTTP event polling loop, calling shttpd_poll blocks for 1 second, so just about right:
    Code: c
    Log in, to see the code

    After my changes:
    Code: c
    Log in, to see the code

    This works, but I suspect there's a minor bug here - shttpd_poll doesn't always block for 1000ms, it might block execution for shorter time if a packet arrives.
    In the next version, I might change this XR's button library that handles debouncing and things like this by itself:
    Code: c
    Log in, to see the code

    Details in project/example/button/main.c

    Adding ability to control the on-board LED
    Now let's try to share the on-board LED with Home Assistant via Tasmota HTTP. This way you would be able to control it any way you want.
    It's not really necessary, as the device used here is a door sensor, but I think it can be interesting and useful to show how such thing is done.
    The diode is connected here:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    So we need to setup LED GPIO, PA12, in output mode:
    Code: c
    Log in, to see the code

    Since we want to see its status through Tasmota HTTP, we need to add it to Tasmota HTTP status packet, for example as "Power3". It needs to be included in the "relays" state array:
    Code: c
    Log in, to see the code

    In the code above, g_gpio_led, str_state3 and "Tasmota3" has been added.
    And if we want to be able to control it, we also need to handle the package GET /cm?cmnd=Power1%20Off HTTP/1.1.
    "Powers" are indexed from 1 (not from 0), states can be On or Off.
    My parsing code for this (very prelimary, without any buffer overflow checks, etc):
    Code: c
    Log in, to see the code

    And now, finally, we can blink this LED - via Tasmota HTTP:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    One minor fix - the status pack short reply
    I will not describe it in detail, because this part is purely within the knowledge of the C language in general, and not with XR809, but unfortunately we still have to be compatible with the short Tasmota HTTP response format, i.e. when asked:
    Quote:

    GET /cm?cmnd=POWER1 HTTP/1.1
    Host: 192.168.0.110
    User-Agent: HomeAssistant/2020.12.1 aiohttp/3.7.1 Python/3.8
    Accept: */*
    Accept-Encoding: gzip, deflate

    We must answer:
    Quote:

    HTTP/1.1 200 OK
    Content-type: application/json

    {"POWER1":"OFF"}

    (at this point, the program responds to this packet with the entire device status description)
    See below for fixed version (and this version is finally compatible with Home Assistant):
    Code: c
    Log in, to see the code


    Fixed IP
    Due to the way Tasmota HTTP works (our device is a server, and we ask for its status), a fixed IP will be useful here. To make things simple, I did it by reserving the IP address for the MAC address on the router:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    This could also be done via a specific host name and DNS.

    Final test with Home Assistant
    Finally, I tested the module from Home Assistant using the Tasmota HTTP support Python scripts described here:
    https://www.elektroda.pl/rtvforum/topic3789324.html
    that is the updated version of this plugin, HTTAS by JiriKursky:
    https://github.com/JiriKursky/httas
    ATTENTION - for the moment httas version from Github will not work with newer Home Assistant versions . This is due to a change in the way coroutines are handled. You will get an error: " ValueError: Coroutine not allowed to be passed to HassJob . "
    Therefore, I recommend downloading my corrected version:
    httas-fixe...210306.zip Download (6.91 kB)Points: 0.5 for user

    In /config/configuration.yaml I set up the device location:
    Code: yaml
    Log in, to see the code

    Then you need to reboot Home Assistant. In my case, it immediately detected XR809 after booting up:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    "MyRel1" here is the door open state, "MyRel2" is the button state, "MyRel3" is the LED state.
    The status of the LED can be changed from the Home Assistant. The other fields are read-only.
    Short demonstration:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    Automations in Home Assistant
    My firmware allows us to do automations in the Home Assistant, for example, opening the door will turn on the LED (in its place you can put any HA compatible device):
    Code: yaml
    Log in, to see the code

    switch.socket is the path to the entity previously set in /config/configuration.yaml

    Increased sensor responsiveness
    In the case of the door opening sensor, the reaction time of HA to a change in door state can be further decreased. This is done in the configuration, just add scan_interval (value in seconds):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    If you get an error:
    Quote:

    Logger: homeassistant.components.hassio
    Source: components / hassio / __ init__.py:420
    Integration: Hass.io (documentation, issues)
    First occurred: 11:53:21 (1 occurrences)
    Last logged: 11:53:21

    Invalid config for [switch.httas]: value must be at least 3 for dictionary value @ data ['switches'] ['socket'] ['scan_interval']. Got 1. (See?, Line?).

    then you can reduce the minimum value in custom_components/httas/switch.py:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    I think that the 1 second refresh interval even for the door status is sufficient.

    Problems encountered
    While exploring the XR809, I ran into one, rather unusual, problem. It cost me over an hour of time.
    While working on our WiFi configuration system for the board (the RESET button creating an Access Point with a known password) I saw that suddenly the board resets (returns to main ()) when I want to create an AP or connect to an existing one.
    The same result had the inclusion of the WiFi network scan - everything reset the board.
    What do you think was the reason for this?
    Spoiler answer:
    Spoiler:

    It turned out that during the work, the batteries were a bit too discharged and with their low level, the XR809 resets when WiFi activity starts. I fixed the problem by replacing the batteries with new ones.



    The popularity of the XR809
    Finally, I would like to make one small, final remark, because I know that not everyone could fully understand my intentions. Of course, I know that the XR809 and related ones are not very popular right now and there is much more ESP8266 devices, etc, but I wanted to show the general method of open firmware creation for an unknown chip. I also know there are some other chips, like WB2S or WB3S and I am also going to try figure them out soon.


    For the user - how to use my XR809 firmware
    My XR809 firmware described here is in a very early development version, but still it's functional.
    So here's a short guide on how to use it.
    1. Load my xr_system.img via UART to XR809 (from the project /at_demo/image/xr809/directory)
    2. Press the button for more than 5 seconds
    3. connect to xr-123etc wifi network, password 12345678
    4. open 192.168.51.1, enter the SSID and password of our WiFi there on HTML page
    5. Add MAC XR809 IP reservation on the DHCP settings of WiFi router (so IP won't change during some network outage or something)
    6. In Home Assistant, add the Tasmota HTTP plugin (httas by JiriKursky, in case of any issues, use my fixed version with "coroutines" problem solved)
    7. in Home Assistant, add the IP and configuration for our device in configuration.yaml

    Further plans
    At the moment, I plan to look at the purchased LCD thermometer / hygrometer, which also houses the XR809:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Then I will probably improve the code presented here a bit. Now it is a bit messy, and perhaps it could be crashed by some buffer overflow (when user sends malicious data). And you probably need to add save() call to save the WiFi configuration to the memory in case of removing the battery, but this is not a problem, because at_demo from the SDK has an example save function.

    Summary
    The firmware developed here has made my XR809 sensor completely independent from the manufacturer's servers and allows me to use it directly with the Home Assistant. Writing this software was basically like assembling it from ready-made components, because everything required was already in SDK, including the HTTP server. The amount of free time and the number of devices with XR809 (XR1, XR2?, XR3) will determine the continuation of the project.
    The latest version of my code (entire modified SDK) is attached to this post, but please remember that version is before major rewrite and contains bad coding practises.

    Cool! Ranking DIY
    Can you write similar article? Send message to me and you will get SD card 64GB.
    About Author
    p.kaczmarek2
    Level 25  
    Offline 
    p.kaczmarek2 wrote 1308 posts with rating 2479, helped 94 times. Been with us since 2014 year.
  • #2
    chemik_16
    Level 25  
    Well now it's still a port for BK7231 and you are a famous man ;)
  • #3
    p.kaczmarek2
    Level 25  
    So WB2S, WB3S?
    SDK for them is also on the network, I already have a flasher:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    We'll see how it goes. As long as you have all the "puzzles" you need, it will also do.

    PS: but do not confuse Tasmota HTTP with MQTT, I did Tasmota HTTP in the topic above, I did not implement MQTT, although you could also easily use the SDK for XR809.
  • #4
    chemik_16
    Level 25  
    there is a whole set for 2 years, but no one has touched it, everyone is soldering these modules to esp.
    https://github.com/bekencorp/bk7231_alios_sdk

    systems installed in all cheap smart tuyas, such as sockets with current measurement (~ PLN 25) or switches.
  • #5
    p.kaczmarek2
    Level 25  
    chemik_16 wrote:
    Well now it's still a port for BK7231 and you are a famous man ;)


    The chances of the Bk7231 have increased significantly recently.

    It looks like I can read (and upload?) The batch to him now:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    I can compile some TUYA batch (although I haven't tested it):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    And the batch zz bk7231_alios_sdk:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    I also managed to eavesdrop on the debug output from WB2S (although it's rather a curiosity, unless this key ...)
    Code: c
    Log in, to see the code


    But from what I can see there are two ways of programming - via UART and via SPI.

    Does anyone have any WB2Sy desoldered?
  • #6
    varguit
    Level 2  
    Hi @p.kaczmarek2, first of all congrats for this great job.
    I need help with the firmware uploading process, I'm using an old PL2303 USB to TTL (checked with the check tool and OK).
    When starting the upload I get an error saying "Checking the 1 block error!"
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Before uploading, I did the EN pin reset, but I don't know if the XR3 chip is in programming mode or not because led stops blinking after disconnecting RESET from ground.
    It's my first time doing this and I'm not sure if everything is in position, I had to switch Rx and TX because otherwise I get synch failed error.
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    I have tried many different settings, baud rate, Rx/Tx switch, resets, etc... but the better I can get is what you can see on the photo.
    Have someone any idea ?

    Thanks.
  • #7
    p.kaczmarek2
    Level 25  
    varguit wrote:
    I had to switch Rx and TX because otherwise I get synch failed error.

    Suprisingly, it turns out that you are right.
    I checked connections twice and in my case I also have RX connected to RX and TX to TX. That's strange, it seems that XR3 board manufacturer has strange naming conversions (or accidentally swapped pin roles on silk screen):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant


    varguit wrote:

    Before uploading, I did the EN pin reset, but I don't know if the XR3 chip is in programming mode or not because led stops blinking after disconnecting RESET from ground.

    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    are you sure you're following:
    p.kaczmarek2 wrote:
    1. connect RESET to ground
    2. connect PB02 and PB03 to ground
    3. disconnect RESET from ground (it can be in the air)

    ?

    is your usb to ttl convert in 3.3V mode? Have you checked all connections with multimeter?

    You can also try checking the host board from the smart device. Are the required for programming pins connected somewhere else? Maybe RX or TX pin is used for some other purpose in this device and it's interfering with programming process?
    I had already experienced such issue in the past, but with ESP8266 board on some smart wall switch. I remember that I was unable to flash it at all. Programming failed every time. After checking traces, I realized that UTX (or maybe RX, not sure) was also used on this board as button and had 0603 SMD resistor pull up to VDD which messed up the communication.
    After desoldering the resistor, everything started to work like a charm.
    EDIT: It described it in this topic: https://www.elektroda.pl/rtvforum/topic3749207.html

    Can you tell me the exact model of your smart device and where have you bought it? I am willing to buy it as well, so I can make a teardown and try programming it myself.
  • #8
    p.kaczmarek2
    Level 25  
    (wiadomość po angielsku ze względu na część czytelników)

    So today I have little good news - I have found a fourth device with XR3 chip.
    (but it's kinda ironic, right now I am buying smart devices with hope to find non-esp chip inside - preferrably XR3 - while most of buyers are hoping for the opposite)

    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    It looks like a repurposed device that I already know, the same PCB as in the door sensor, it should be easy to support it.

    So, the current list of known XR3 devices:
    1. Smart Wifi Door Window Sensor Burglar Security Alarm For Alexa Google Tuya
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    2. LCD thermometer
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    3. An unknown PIR-like device posted by user varguit
    varguit wrote:

    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    4. Tuya Smart APP Wifi Smart Water Leakage Alarm Smart Home Mobile Phone Remote Sensor
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    If anyone know more devices based on XR family modules, let me know.
  • #9
    varguit
    Level 2  
    Hi.

    Quote:
    are you sure you're following:
    p.kaczmarek2 wrote:
    1. connect RESET to ground
    2. connect PB02 and PB03 to ground
    3. disconnect RESET from ground (it can be in the air)

    ?

    is your usb to ttl convert in 3.3V mode? Have you checked all connections with multimeter?


    Yes, I'm grounding RESET and PB02,PB03, and then releasing RESET.
    If I do a reset without PB02 and PB03 to ground I can see the device rebooting, but if both PB02 and PB03 are conected to ground, Reset makes nothing in my device. :(
    My usb to ttl has only two power pins, one for 5V and another one for 3.3V, my multimeter measures 3,4V aprox.

    Quote:
    Can you tell me the exact model of your smart device and where have you bought it? I am willing to buy it as well, so I can make a teardown and try programming it myself.

    Of course, that would be very helpful, I would send you one if you were in Spain :). It's a TUYA PIR device bought from Aliexpress at 8€ aprox
    Here's the link:
    Link

    I'm gonna read your other topics to see if I can find where's my problem but my electronic acknowledge is very limited....
    Thanks for your answer.
  • #10
    p.kaczmarek2
    Level 25  
    EDIT: I have double checked and it seems that XR809 has such voltage specs, and it supposedly is able to run on 5V:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    so I don't think that your UART voltages level is at fault here

    Tuya PIR sensor information is very valuable. I ordered it right away and here it is:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    I will try programming it soon.
  • #11
    p.kaczmarek2
    Level 25  
    Today I tested both write and read operations on the device model you have. Everything works without any issue.
    I am able to flash my custom firmware:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Here is my setup. I even tried to use the same wire colours as you. I just had no batteries at hand, so I used 3.3V LDO.
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    My XR Access Point shows up without any problems:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    I also tried reading original firmware first and it works as well.
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    I also OCRed those characters:
    读取\ - Read?

    写入 - Write?

    连续 - continuous

    捺除楔式 - is supposedly "Eliminate wedge", but I think it refers to chunk read size, not sure right now, have to use Translate more

    I have managed to read both config and flash memory:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    In some rare cases I had "read payload data error", but retrying once was enough to get it successfully done.
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    You can open them in Notepad++, they are of course compiled but some string constants are still readable. I wonder if I could get them somehow into my IDA PRO, have to try sometime.
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    You can see some of the original paths and filenames from source files:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    I am attaching the memory dumps (original one before first pairing):
    memory_A_0...200000.zip Download (459.76 kB) flash_A_0x...000000.zip Download (7.4 MB)

    PS: right now I am not sure if it works, but have you tried setting lower speed here?
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    PS2: have you paired your device with Tuya before flashing it? as far as I know it should not matter, it's not like tuya-convert, but... I didn't pair mine here. I only paired the door sensor from the first post with tuya before flashing, not the PIR sensor now.
    PS3: have you tried new batteries?
  • #12
    __Maciek__
    Level 20  
    A bit offtop - but it was already touched

    @ p.kaczmarek2 WB2S recently came to me with FK-PW802 sockets from a well-known auction site .. I try to figure out the examples from freertos (there is also RTT to choose from .. but due to the creators of a lot of bushes - although probably less than for AliOS)

    https://github.com/bekencorp/bdk_freertos

    Have you had a compilation problem? I haven't written for freertos before, this sdk layout is a bit strange for me .. but I'm trying to figure it out and go through the first compilation.
    Did you check their forum? There are links to the documentation and forum, but I feel that some Chinese firewall is blocking ...
  • #13
    p.kaczmarek2
    Level 25  
    As for WB2S, I have made some attempts and it looks promising, but time was short. It was about a month ago, but I'll describe more or less what I know here.

    I know 2 possibilities.

    1. tuya-iotos-embeded-sdk-wifi-ble-bk7231t-master
    https://github.com/tuya/tuya-iotos-embeded-sdk-wifi-ble-bk7231t
    I managed to compile under Cygwin, everything is needed for the repo, but of course the basic configuration independence is required (I don't remember what I changed but it's simple):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    compilation result:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    It can be uploaded via UART using BK Writer:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Unfortunately, these SDKs contain examples that are probably a bit closely related to the Tuya environment:
    https://github.com/tuya/tuya-iotos-embeded-sd...b/master/apps/template_demo/src/tuya_device.c
    And I don't know how much it could be maneuvered there.

    2. bk7231_alios_sdk
    https://github.com/bekencorp/bk7231_alios_sdk/
    It's a bit more fun with the configuration, you have to be able to install the Python you need and then execute a few commands, but the compilation also works:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    ... but BK programming is not successful, because alios SDK expects that we will have an ST-Link / V2 programmer and that it will upload the batch through it.

    in addition, the batches generated by these two methods are of different formats, one is probably a full flash image, and the other is just a section loaded by the bootloader by uart

    Actually, I have this programmer, but I did not have time to solder the cables where needed (and if ST link was needed, it is not a perfect solution):
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    A collection of WB2S and related links (collected a month ago):
    https://mitxela.com/forum/topic/beken-bk3231/2
    github.com/ct-Open-Source/tuya-convert/issues/884
    https://fccid.io/2AMV5-SK509W2SN/User-Manual/15-SK509-W-2S-N-UserMan-US-r1-4836910.pdf
    https://github.com/bekencorp/bk3633_mesh_sdk
    https://fcc.report/FCC-ID/2ANDL-WB2S/4580213.pdf
    https://developer.tuya.com/cn/docs/iot/wb2s-module-datasheet?id=K9ghecl7kc479
    https://www.programmersought.com/article/54836791463/
    https://developer.tuya.com/en/docs/iot/burn-and-authorize-wb-series-modules?id=Ka78f4pttsytd
    oem_bk7231s_light_UA_2.9.5.bin
    BK7231T pdf
    https://github.com/tuya/tuya-iotos-embeded-sd...3b/apps/bk7231t_light_pwm_demo/system/system/
    https://www.programmersought.com/article/73156790997/



    Didn't try what you gave, https://github.com/bekencorp/bdk_freertos. Plus I see it came out 24 days ago and I tried about 30 days ago. Something new.
    But it looks promising at first glance, and there are concrete examples. If you know something more about it, write it, because I will probably try with this particular repository soon

    And if some links do not work for you, give it here or at PW I will check it
  • #14
    __Maciek__
    Level 20  
    A bit of clicking and the RTOS is compiled .. for bk7231u .. .. I do not know how compatible it is with bk7231d ... or what he called BK7231T - initially there is a different version of BT4.0 vs 4.2 ..

    The code also shows that there may be differences in DMA .. but they approached inconsistently and BK7231 throws dma warnings and errors with gpio.c
    (in the header file there is a condition that the definition that causes the problem does not apply to bk7231, and in C they have already omitted.) although it applies to pins not used in WB2S so maybe it is not important) maybe there is something on the forum .. .. page bbs.bekencorp.com:8191 is not responding ..

    - [Hardware Docs] (http://bbs.bekencorp.com:8191/forum.php?mod=forumdisplay&fid=57) - Download all kinds of hardware documents

    I am in the code / I will throw some examples of bk7231u on WB2S and see what will come out of it.
  • #15
    p.kaczmarek2
    Level 25  
    You mean GPIO_SD1_DMA_MODULE?
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    A google search for this leads to:
    https://code.aliyun.com/MXCHIP_FAE/feiyan_sdk...platform/mcu/bk7231s/beken/driver/gpio/gpio.h

    https://code.aliyun.com/MXCHIP_FAE/feiyan_sdk...8e75920ec050d3d2984e7fa0ad6cf30b/platform/mcu

    Here they have bk7231, bk7231s and bk7231u. And they are separate. On the other hand, in the rtos repo, it looks like they linked these SDKs together using a preprocessor and something went wrong.

    This link does not open for me, but I will try to get a Chinese IP

    By the way, how did you solve the missing application.h path?
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
  • #16
    __Maciek__
    Level 20  
    Hhmmm ... I compile without any errors in the original configuration for bk7231u. - clean repo project of course. (60K batch without bootloader)

    (maybe toolchain ..? I was a bit troubled with this .. different versions ... ended up on 5.4_2016q3.) in the code different suggested versions were given .. but for freertos9 it was this period. I was also rummaging around the compilation switches - I turned off errors from the warnings - but eventually I went back to what it was.

    Another thing - I noticed that there are different descriptions of WB2S modules .. and on the thuya website - there is already BT4.2 - so maybe it's this version ..bk7231u.

    As for the application .. I tried to compile an example with a demo yesterday .. but here zonk always comes out the same ... during compilation it changes the data in the config file .. but I have to look at what and how (this file is not present in the repo / so the folder must be created in the compilation process - that's why it changes .. so I wonder .. where to configure what is to compile.?)

    I need to find more time for this ...
  • #17
    p.kaczmarek2
    Level 25  
    __Maciek__ wrote:
    (maybe toolchain ..? did a little bit of a goof with this .. different versions ... ended up on 5.4_2016q3.)

    How the toolchain version would influence the presence / path of the header ...

    __Maciek__ wrote:
    I turned off errors from the warnings

    and I confirm this, the first thing I had to do

    __Maciek__ wrote:
    Hhmmm ... I compile without any errors in the original configuration for bk7231u. - clean repo project of course. (60K batch without bootloader).

    I think I know why it was.
    They have quite a mess there.
    I uncommented in application.mk / entered the path to toolchain / home / tester / gcc-arm-none-eabi-4_9-2015q1 / bin / and thought it was ok because it compiles:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    and it compiled, but it turned out that the same was given a second time in generate_sys_config.h:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    and you also have to manually set the path there ... because the headers will not be generated without it.
    Ultimately, however, I set the path through the environment variable:
    export FREERTOS_EXEC_PATH = / home / tester / gcc-arm-none-eabi-4_9-2015q2 / bin

    And it works, but in general it shouldn't be that they set the same variable in two places

    Well, I also compile:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    __Maciek__ wrote:

    but here zonk always the batch comes out the same ... when compiling it changes my config

    Config just generates generate_sys_config.h. And they actually copy it.
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Open it and you can, for example, change it to 1 here:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant


    __Maciek__ wrote:

    I need to find more time for this ...

    exactly, I also try to do normal projects during longer builds, and additionally with rtoos and related ones, I have not used ARM before (as I wrote in the first post)

    I see that they have something in the repo regarding MQTT, probably rpzyda, for XR809 I also plan to run MQTT normally
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    Now I have to find how, according to the art, attach the files with C code to the compilation demo, because I do not see demo_start:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Of course, it is known that you could enter it into the makefile normally, but I have the impression that they have some system based on scripts
    EDIT: ok, you know, here it is set:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    Here you choose which demo you compile. Do as I wrote and you will have everything you need to pre-test if it works on WB2S
  • #18
    varguit
    Level 2  
    Hi there.

    First of all, thanks for buying the same sensor and test it.
    After many trials and errors I still can't flash it.

    Following your information, I also tried reading original firmware first and it works, I get exactly the same result as in the image below:
    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
    I can save the original image, so, I'm pretty sure everything is right on the connections and communications part but it looks like something is wrong when checking the first block. I also tried to remake the xr_system.img, but still no succes. Maybe I have to buy a different UART (mine is very old and windows 10 drivers are not supported)

    PS: right now I am not sure if it works, but have you tried setting lower speed here?
    Yes, I've tried with different speeds, from 9600 to the maximum, always getting the same error.

    PS2: have you paired your device with Tuya before flashing it? as far as I know it should not matter, it's not like tuya-convert, but... I didn't pair mine here. I only paired the door sensor from the first post with tuya before flashing, not the PIR sensor now.
    Yes, it was paired, but I've deleted from the SmartLife app as well. I hope this is not the reason.

    PS3: have you tried new batteries?[/quote]
    Right now, I'm powering it from an arduino, directly to the battery contacts, as you do.
  • #19
    p.kaczmarek2
    Level 25  
    @varguit , I just accidentally experienced similiar issue to yours. It happens every time when I try to flash XR809 with MCP2221 USB to UART converter, no matter what baud setting I set. I get random errors, sometimes 'synchron error', sometimes 'baud rate set error', and sometimes error while programming first block. On the other hand, it works smoothly if I just connect my TX/RX to USB to TTL HW 597 converter and leave everything else unchanged.
    It seems that your UART must be fast enough to do the job, please try USB to TTL HW 597, it's cheap to buy online.
    PS: I will try MCP2221A soon (MCP2221 works supposedly up to 115200 and MCP2221A to 460800)
  • #20
    joxumac
    Level 1  
    Good morning, I have in my possession a Mini Smart Switch, which has inside a WB2S (BK231T) and I am trying to flash it to integrate it with home assistant without going through Tuya or Smart Life servers.

    I have read the post and the attached documents and the progress with respect to this chip seems incredible to me.

    Currently, I have been able to read the output of the chip by connecting the VBAT pins to 3.3V; GND to GND, 2RX and 2TX to TX and RX of my USB-TTL respectively. By setting a serial terminal at 115200 baud I am able to read what the chip prints to the terminal.

    However, making only that connection I am not able to dump the flash memory to my pc (some users got it, how?). As I see comparing with the XR809 you have to have the PB02 and PB03 pins to GND, but those pins do not exist in the WB2S connection.

    Do you have any idea how I can continue? (I have USB-TTL, ST-LINK v2, Arduinos, ESP and other components to carry out tests that need or see useful for the community and research)
    (Attached table of pins according to your website Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant )

    Dodano po 2 [godziny] 24 [minuty]:

    After carrying out more tests, making the same connection as before but this time connecting to 1RX and 1TX instead of the second line (2RX and 2TX), the BKWriter 1.60 software performs an empty memory dump (.bin) (to 115200 baud). If I open a serial terminal (at 115200 baud) the following appears (it is as if the baud were not correctly selected ??). I still can't make a backup of the original flash :'(

    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant
  • #21
    mihsu81
    Level 2  
    @joxumac Keep the baud rate at 115200 in Device manager, but in Beken Writer you have to set it at 921600 when reading or writing.

    Another thing to keep in mind is which IC is used by the USB-TTL. I could read and write just fine with a CH340G IC, but couldn't with a FTDI FT231X IC. In fact the "USB to TTL HW 597" mentioned as working by @pkaczmarek2 has a CH340G IC.
    Flashing is done on 1RX and 1TX and UAR debugging (serial output) on 2RX and 2TX.

    And finally, the device must be powered off when you press the Read Flash or Write Flash buttons. Switch it on immediately after.

    Open source firmware for XR809 compatible with Tasmota HTTP/Home Assistant

    Now i have to see if i can compile the FreeRTOS mentioned by @__Maciek__ and @pkaczmarek2 .
  • #22
    mihsu81
    Level 2  
    Didn't manage to get any further than @__Maciek__ and @pkaczmarek2 .

    make bk7231 throws general_dma compile errors:

    beken378/driver/general_dma/general_dma.c:37:15: error: 'gdma_get_dma_en' defined but not used [-Werror=unused-function]
    static UINT32 gdma_get_dma_en(UINT32 channel)
    ^
    beken378/driver/general_dma/general_dma.c:193:15: error: 'gdma_get_transfer_length' defined but not used [-Werror=unused-function]
    static UINT32 gdma_get_transfer_length(UINT32 channel)
    ^
    beken378/driver/general_dma/general_dma.c:371:13: error: 'gdma_enable_interrupt' defined but not used [-Werror=unused-function]
    static void gdma_enable_interrupt(void)
    ^
    beken378/driver/general_dma/general_dma.c:436:12: error: 'gdma_get_received_len' defined but not used [-Werror=unused-function]
    static int gdma_get_received_len(UINT32 channel)
    ^
    cc1: all warnings being treated as errors
    make[1]: *** [application.mk:376: out/./beken378/driver/general_dma/general_dma.o] Error 1


    And if you ignore the errors with make -i bk7231 looks like more drivers also fail to compile.

    I've compiled and flashed the binary for bk7231n but the only thing i'm getting from serial is the below output repeated maybe 10 time per second:

    V:BK7231S_1.0.5
    CPSR:000000D3
    R0:00000000
    R1:00DFFEFC
    R2:00802000
    R3:F1CCFFFF
    R4:00010000
    R13:0043F7C8
    R14(LR):0000BCBC
    ST:BEDEAD01
    J 0x10000


    Which is strange because my WB2S has BK7231TQN32 and on Tuya's site it's listed as a BK7231T. On the other hand Beken doesn't have a BK7231T on their site but does have a BK7231S. Link

    I guess I'll have to do more digging as time permits.

    Update1:
    Looks like this is the start-up message, so i guess the bk7231n firmware doesn't work.

    Update2:
    I've compiled the freely available documentation from Link after using a Chinese VPN :lol: .
    Unfortunately for Link you'd need to register with a company account.
    And gcc-arm-none-eabi-5_4-2016q3-20160926 is the correct toolchain as assumed by @__Maciek__. It's listed in Link.

    In the attached files they also mention how to set up the tool chain in order to be properly recognized by make:
    $ sudo mkdir -p /usr/local/share/toolchain/
    $ tar xf gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 -C /usr/local/share/toolchain/
    Add the following environment to ~/.bashrc if you are using bash or ~/.zshrc if you are using zsh.
    export RTT_EXEC_PATH=/usr/local/share/toolchain/gcc-arm-none-eabi-5_4-2016q3/bin
    export FREERTOS_EXEC_PATH=/usr/local/share/toolchain/gcc-arm-none-eabi-5_4-2016q3/bin/
    [/u]