logo elektroda
logo elektroda
X
logo elektroda

Adding remote control (Home Assistant integration) to USB Switch with OBK

p.kaczmarek2 1584 0
ADVERTISEMENT
  • Image of a module with a circuit board and cables for testing.
    In this tutorial we will show you how to simulate a short button press on non-smart device with a WiFi module addon. We'll use it to control the Ugreen 30346 USB switch remotely via Home Assistant. For this purpose, we'll use an OBK-compatible WiFi module (could be a Beken chip, or Winner Micro, or BL602, LN882, or even ESP32) and an extra step down converter to power it.

    The Ugreen 30346 USB switch used here allows you to easily switch between 4 USB devices. Each button press activates next connected device. The LED next to the active USB port is lit. This allows you to, for example, share a single keyboard between multiple computers.
    Desk with two monitors, a keyboard, and various electronic devices.

    So, let's start with basic requirements. What do we need to do to simulate a button press?
    - we need to add our WiFi module to hub and power it, but USB is 5V, and WiFi module uses 3.3V, so we will need a step down converter or LDO regulator
    - we need to send a short pulse to a button (short it to ground), for like 0.5 second, so we will need to script OBK for that

    Let's start with looking into our switch. There is a single button that when held, shorts the Nuvoton MCU pin to ground:
    Electronic layout of the Ugreen 30346 USB switch with visible components and USB ports.
    The pin is pulled high by default. We can connect it to our WiFi module GPIO directly in most cases. If you need to have isolation, use optocoupler. Here is a photo showing it during testing phrase with our dev board:;
    Image of a module with a circuit board and cables for testing.
    See breakout topic: Solderless conversion board for CB2S/WB2S Tuya modules compatible with ESP12 boards
    You can also see related solution (nodeMCU conversion):
    [Youtube] How to make BK7231 development board - NodeMCU conversion - soldering guide, hot air, SMD
    In order to keep the pin high by default, we've used Rel_n role, because Rel_n is active-low, while simply Rel is active-high in OBK. Here is OBK config:
    Code: JSON
    Log in, to see the code

    In order to make the pin return to high state after half a second, we've also added a short autoexec.bat script.
    
    // 0.5 is delay in seconds, 1 is number of repeats - once
    addChangeHandler Channel1 == 1 addRepeatingEvent 0.5 1 setChannel 1 0
    

    The following is enough to control it via Home Assistant, just do HASS Discovery and you'll get:
    Screenshot of the MQTT user interface for the BK7231N device, showing controls, diagnostic information, and event log.
    Let's test it - observe the LED moving between ports, it marks active port:



    Okay, the prototype is now working, but we need to pack it all together in the case.
    We will need a 3.3V source for that.
    We've had a cheap step down converter module at hand:
    Miniaturized step-down converter module on AliExpress.
    It's a very cheap and little device that can be useful for many projects. Aparat from that, no other external parts are required by WiFi module like CB2S or WB2S, it works out of the box, but it's good to add some decoupling capacitors as well:
    Photo showing a circuit board with electronic modules and a resistor on a wire.
    Resistor on the photo is the pull up, it may not be necessary, as Beken has internal pull-ups on GPIOs.

    With this modification, it is even possible to connect the USB switch to Stream Deck and control it from there:



    Look for the device LEDs on each USB port on the right side of the video. They switch with each press.

    The same approach can be used to control, toggle and reset many other devices. Futhermore, the following mechanism can be integrated into Home Assistant automations, so we a reset can happen automatically at given time of day or if a problem is detected. So now our device is much more versatile! We could have also added a feedback mechanism (read which LED is on to determine which port is active), but that's a task for another day...

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11919 posts with rating 9984, helped 572 times. Been with us since 2014 year.
  • ADVERTISEMENT
ADVERTISEMENT