Elektroda.com
Elektroda.com
X
Elektroda.com

[Tutorial] Provisioning using HomeAssistant

nielspiersma 975 13
  • OpenBeken Provisioning.
    As an enthusiastic user of HomeAssistant and Smart Home solutions in general, I am always into something new. Recently I’ve become acquainted with OpenBeken. Not wholly voluntary, but is a response to the fact that more Smart Home Devices started using BK7231 vs. good-old ESP82xx.

    Undoubtedly I like the fact of being able to log in to a device and configure it to my needs. However, from a manageability perspective, you need the ability to mass-configure devices. When looking at phone provisioning and large-scale OS deployments, provisioning is standard. There is no way you can log in to 200 web interfaces and set individual settings.

    Luckily for a few weeks, OpenBeken has introduced the “MQTT Group Topic,” which has eased our life tremendously.
    Furthermore, the OpenBeken developers are actively adding configuration parameters with the ability to set them (up) using the MQTT “cmnd”. Combined with the “MQTT Group Topic,” I can now quickly configure multiple devices.

    So why is this important to us? Well, let me start by using our own office as an example. Everyone knows about the high costs of electricity and the new rules coming from the EU regarding Co2 emissions and taxes. Saving on electricity consumption is directly impacting these costs. In our office, we have ceiling lights. About 25 of them. Each is equipped with 8 led lamps but still consumes around 100 Watts when lit. Multiply that by 25, and you have a whopping 2500 watts. We found that, on average, we only need ten ceiling lights powered on at any given moment is fine. Thus reducing our average usage by more than 50%. All our ceiling lights were connected to standard earthed sockets. Reason enough to buy 30 or so Smart Sockets. Initially, I tested the Woox R6080, and after sacrificing one unit, I got it running with OpenBeken. At about the same time, I successfully managed to get cloud-cutter to work, which made my work even more straightforward. It took away the need to desolder 25 Tuya boards.

    I flashed all 34 Smart Plugs in two evenings. So now my real problems have begun. How to configure all these babies in bulk. I wasn’t planning on logging on to each device setting the required settings. So I started playing around with the same commands I used for bulk configuring my Tasmota devices. Unfortunately, not all the commands I needed were implemented at that time. Thanks to the developers, quite some features were added. But things really got up to speed once the MQTT Group Topic was introduced. This made it possible for me to update all 34 plugs in one go. As well as fine-tuning. This is a breeze as I have the same plugs and the same underlying hardware.

    Let me show how I’ve done this;
    Flash your device and add it to your WiFi network. No pin settings are needed; we will configure all settings from HomeAssistant.
    First, I recommend choosing an MQTT Group Topic based on your hardware chip. This will make updating relatively easy and at least somehow prevent you from pushing N setting to T chips and vice versa. Setting up the MQTT Group Topic is done under “Configure MQTT.” Please make sure you set up your username and password correctly so HomeAssistant can “take control.”

    [Tutorial] Provisioning using HomeAssistant

    To follow up on this, your Beken Device must report its status (regularly) to HomeAssistant. I recommend setting it up with a special Startup command. This is done in the Change Startup command text.

    You should add “scheduleHADiscovery 10”

    [Tutorial] Provisioning using HomeAssistant

    Now reboot your OpenBeken device and check if it shows up in HomeAssistant

    [Tutorial] Provisioning using HomeAssistant

    We are now ready to configure the device.
    Firmware update example.
    Now that your device is integrated in HomeAssistent, you might want to update it remotely. Download the latest firmware and store it in a repository accessible via http. This can be a any type of http server. Let us assume your file resides on a local server with IP address 192.168.1.66 and is called fw.rbl. The URL for the update would http://192.168.1.6/fw.rbl. In HomeAssistant create a new script with the following settings;

    [Tutorial] Provisioning using HomeAssistant

    For completeness the yaml code;

    alias: OpenBeken Firmware Update
    sequence:
    - service: mqtt.publish
    data:
    topic: cmnd/BK7321N/ota_http
    payload: http://192.168.1.66/fw.rbl
    mode: single
    icon: mdi:cellphone-arrow-down



    When you run the script, it will request all OpenBeken devices subscribed to “BK732N” to download new firmware from the web server.
    I want to focus on the “topic” and “payload,” as this is where the actual magic happens.
    cmnd/BK7321N/ota_http
    • cmnd instructs sending a command.
    • BK7321N is the MQTT (group) Topic. If you want an individual update, use the MQTT Client/device name
    • ota_http is the command your want to send. You can use the commands from https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md and from https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands-extended.md
    I can tell, it is fun to watch 35 devices updating and rebooting. The aforementioned “scheduleHADiscovery” warrants that your HomeAssistant information is updated. When a new IP address is given or the firmware version changes, this is correctly displayed in HomeAssistant. So you can keep track of the firmware versions and see which devices are not up to date.
    Another example is setting the Flags. Sometimes is handy to have flags set. For instance, I like flag 6 being set. This makes sure all relays are triggered instantly. I also have the desire setting flag 10. Setting both flags will give us a flag value of 1088.
    Again, we will create a script that updates the flag settings.
    [Tutorial] Provisioning using HomeAssistant

    Again small breakdown:
    cmnd/bekens/ota_http
    • cmnd instructs sending a command.
    • bekens is the MQTT (group) Topic.
    • flags the command we want to send
    • payload 1088 in the case representing Flag 6 and Flag 10
    Run the script and all your devices in the MQTT Group “bekens” will get these injected.
    We can also setup the device using the backlog command. This allows us to send multiple commands to a device. Quite handy for setting up the configuration.

    For example, setting up the Action LSC Smart Plugs
    Pins are empty:
    [Tutorial] Provisioning using HomeAssistant

    The device has Wifi set up, has MQTT Group setup, has scheduleHADiscovery set as startup command, and the device has MQTT host, user, and password setup.

    Now configure all pins.

    [Tutorial] Provisioning using HomeAssistant

    Yaml code:
    o service: mqtt.publish
    o data:
    o topic: cmnd/obkBF518BE1/backlog
    o payload: SetPinRole 6 LED_n;SetPinChannel 6,1;SetPinRole 7 Btn;SetPinChannel 7,1;SetPinRole 8 rel; SetPinChannel 8,1;SetPinRole 26 WifiLED; SetPinChannel 26,0
    Run the script and:

    [Tutorial] Provisioning using HomeAssistant

    Pins setup.
    This shows the power of the MQTT Group Topic and the way the OpenBeken developers have come making provisioning a breeze. I am quite sure any future command or setting will be addressable via a MQTT cmnd command.
    You can also play around with HomeAssistant templates, giving you even more power on the commands you want to execute.
    I hope you enjoyed this write up on provisioning on OpenBeken.

    Cool? Ranking DIY
    About Author
    nielspiersma
    Level 6  
    Offline 
    nielspiersma wrote 30 posts with rating 5. Been with us since 2022 year.
  • #2
    p.kaczmarek2
    Moderator Smart Home
    Very good writeup. It will be certainly useful for people, even I can learn new interesting things from that.

    I will just add here two more notes.
    1. If you need anything else (a command, a feature, or a bufix) - message me, I will try to help.
    2. OpenBeken, as you may probably know, supports BK7231T, BK7231N, BL602, W800/W801, W600/W601, XR809, T34 and... and Windows
    Yes, you read right - you can run OpenBeken on Windows, along with IoT device simulator/schematic draw tool and full MQTT support:
    [Tutorial] Provisioning using HomeAssistant
    [Tutorial] Provisioning using HomeAssistant
    [Tutorial] Provisioning using HomeAssistant
    What this mean for end user, you may ask? Well, it means that soon you will be able to test all MQTT stuff, all scripting, automation, etc, without even having an IoT device! Just Home Assistant Virtual Machine + OBK simulator will suffice.
    OBK simulator will be released soon, no binary is yet available.

    Being able to run OBK on Windows also speeds up development process a lot.

    PS: In a meantime, I have also added a LFS_* commands for writing files to our LittleFS file system.
  • #3
    nielspiersma
    Level 6  
    That is so good to hear. Let me know once the emulator is available. Would love to test that. Especially testing my mqtt provisioning .


    Cheers
    Niels
  • #4
    ferbulous
    Level 14  
    Hi, thanks for sharing this method
    Followed the instructions but it's not triggering the ota firmware download on my devices
    Using script and just testing with developer tools

    [Tutorial] Provisioning using HomeAssistant

    [Tutorial] Provisioning using HomeAssistant

    Maybe I'm missing something?
  • #6
    ferbulous
    Level 14  
    @p.kaczmarek2 hi, i also restarted the device but it's not working yet
  • #7
    nielspiersma
    Level 6  
    Correct,

    After setting up mqtt group topic you need to restart the device.

    Niels

    Added after 4 [minutes]:

    I am not sure, but I only tested with port 80 not with port 7000.

    I'll setup a server on port 7000 and will test it

    In them mean time, van you provide a tcpdump or Wireshark capture from your web server? Confirming the http request is actually hitting the web service?

    Niels
  • #8
    ferbulous
    Level 14  
    @nielspiersma i'm using http-here to share the files

    root@rpi4:/home/pi/firmware# http-here --host 0.0.0.0 7200


    when i ran the mqtt publish, no changes in the openbk console/tcpdump
    but if I just copy and paste the link in browser, it downloads the firmware and this is the log (assuming i'm using tcpdump right)

    root@rpi4:/home/pi# tcpdump -i any -c5 -nn port 7200
    tcpdump: data link type LINUX_SLL2
    tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
    listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144                                                                              bytes
    01:21:28.030050 wlan0 In  IP 192.168.1.32.57501 > 192.168.1.40.7200: Flags [S],                                                                              seq 1383112925, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], lengt                                                                             h 0
    01:21:28.030178 wlan0 Out IP 192.168.1.40.7200 > 192.168.1.32.57501: Flags [S.],                                                                              seq 217126541, ack 1383112926, win 64240, options [mss 1460,nop,nop,sackOK,nop,                                                                             wscale 7], length 0
    01:21:28.030327 wlan0 In  IP 192.168.1.32.57502 > 192.168.1.40.7200: Flags [S],                                                                              seq 3285208169, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], lengt                                                                             h 0
    01:21:28.030366 wlan0 Out IP 192.168.1.40.7200 > 192.168.1.32.57502: Flags [S.],                                                                              seq 1245807257, ack 3285208170, win 64240, options [mss 1460,nop,nop,sackOK,nop                                                                             ,wscale 7], length 0
    01:21:28.039814 wlan0 In  IP 192.168.1.32.57501 > 192.168.1.40.7200: Flags [.],                                                                              ack 1, win 513, length 0
    5 packets captured
    7 packets received by filter
    0 packets dropped by kernel
    root@rpi4:/home/pi#
  • #9
    nielspiersma
    Level 6  
    Your mqtt url is connecting to port 7000.
    Your tcpdump shows port 7200.

    Can you confirm the ports you are using?

    Niels
  • #10
    ferbulous
    Level 14  
    @nielspiersma it's 7200 now
    and i've the changed the mqtt publish to use 7200

    http://192.168.1.40:7200/OpenBK7231N_1.15.318.rbl
    http://192.168.1.40:7200/fw.rbl

    do i need to leave the original firmware as or just rename it?
    not working just yet with both filenames tested (if it's related)

    
    alias: Openbeken firmware update N
    sequence:
      - service: mqtt.publish
        data:
          topic: cmnd/BK7321N/ota_http
          payload: http://192.168.1.40:7200/fw.rbl
    mode: single
    icon: mdi:briefcase-download
    
  • #11
    nielspiersma
    Level 6  
    Both in the home assistant script and on your http server?

    Added after 52 [minutes]:

    The file name just has to match.

    I named it fw.rbl so it was easy.

    Just rename it to fw.rbl so it matches the url.

    I'll update my post later so it is more clear


    Niels
  • #12
    p.kaczmarek2
    Moderator Smart Home
    Please try debugging the issue to determine where exactly is your point of failure.
    1. Please try to broadcast a "POWER toggle" command using the same method and topic from HA and check if it works
    2. Please try using the OTA command with your URL directly into OBK Web app console and see what happens
    When diagnosing a problem, it's always good to check your process step by step and determine where exactly things starts to go wrong.

    Btw: windows simulator almost ready, with full MQTT support.
  • Helpful post
    #13
    ferbulous
    Level 14  
    @p.kaczmarek2

    Noted, will try a few other mqtt commands once i home to test it out

    Added after 5 [hours] 28 [minutes]:

    Looks like it's working now, not sure why it wasn't working earlier

    
    
    root@rpi4:/home/pi/firmware# http-here --host 0.0.0.0 7200
    Serving HTTP on 0.0.0.0:7200...
    192.168.1.179 - - [14/Jan/2023 14:59:00] "GET /fw.rbl HTTP/1.1" 200 -
    192.168.1.166 - - [14/Jan/2023 14:59:35] "GET /fw.rbl HTTP/1.1" 200 -
    192.168.1.43 - - [14/Jan/2023 15:00:08] "GET /fw.rbl HTTP/1.1" 200 -
    192.168.1.247 - - [14/Jan/2023 15:00:35] "GET /fw.rbl HTTP/1.1" 200 -
    192.168.1.162 - - [14/Jan/2023 15:01:03] "GET /fw.rbl HTTP/1.1" 200 -
    192.168.1.105 - - [14/Jan/2023 15:01:37] "GET /fw.rbl HTTP/1.1" 200 -
    192.168.1.226 - - [14/Jan/2023 15:02:05] "GET /fw.rbl HTTP/1.1" 200 -
    192.168.1.171 - - [14/Jan/2023 15:02:33] "GET /fw.rbl HTTP/1.1" 200 -
    
  • Helpful post
    #14
    p.kaczmarek2
    Moderator Smart Home
    @ferbulous you need a restart after MQTT group name change and you had a typo in port or something, if I've seen correctly.

    I think I will add some kind of notification about MQTT params restart requirement.