logo elektroda
logo elektroda
X
logo elektroda

Simple cooling automation on two OpenBeken devices without Home Assistant

p.kaczmarek2 1344 2
ADVERTISEMENT
  • User interface for cooling automation using OpenBeken.
    OpenBeken can be used to create simple automations involving multiple devices without Home Assistant. No central server is required, you only need to have flashed Tuya devices. Here I will show you one such automation, where two devices are used - first is a power meter, used to measure the power usage of device, and second is a relay used to turn cooling on and off. Cooling will be enabled when power usage is above given threshold.

    This simple setup is required for my DIY laptop cooling tab, as shown here:
    Will a DIY cooling stand made from 12V fans help with laptop overheating?

    First, let us consider requirements:
    - cooling will be used in simple on/off mode, no PWM adjust yet
    - first device will only measure power usage and send it to second device
    - second device will offer automatic or manual modes, in automatic mode it will compare power with given threshold and enable relay if threshold is exceeded
    The same could be done with a more reliable temperature measurement if needed.

    Now let's first consider the available hardware.
    - laptop is cooled by fans which are run at 5V currently. This is important information, because it will be used to simplify circuit later
    12V fans mounted on a wooden surface in a DIY setup.
    - LSPA9 plug measures power usage:
    Energy measurement screen of the BK7231T_LSPA9_BL0937 device.
    - a DIY device controls the power of fans, this device can run directly on 5V power, no mains required

    Short introduction
    OpenBeken runs currently on many various WiFi modules. Those modules can be easily then connected to Home Assistant, but it is not necessary for simple automations. OpenBeken supports scripting system via LittleFS, here you can view some sample scripts. For more info, see our documentation:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/README.md

    DIY device for fans control
    My current fans don't work well with PWM, so I decided to go for relay instead. In the future, I may introduce PWM control as well, but now I just needed a basic ability to toggle them on and off. That's why I decided to convert broken 230V relay control to 5V:
    Printed circuit board with various electronic components on a wooden background.
    Following switch module has a simple, mains-powered step down converter that provides 5V for the relay, which is later fed to 3.3V LDO that powers WiFi module.
    Close-up view of a printed circuit board with traces and electronic components.
    In case of powering whole circuit from 5V, the first 230V to 5V step down converter is not needed, but remember that voltage higher than 5V may just burn the 3.3V LDO and the WiFi module. If you want to use 12V, a 12V to 3.3V step down converter is required.
    Final circuit:
    Image of a circuit board on a wooden surface with connected cables.
    I have also replaced full bridge rectifier with a simple diode, as the DC is here already at the input, and the extra diode is only used for reverse-polarity protection.
    DHT11 routed out for later experiments:
    DIY electronic circuit with wires and components on a wooden surface OpenBeken user interface displaying DHT11 sensor data
    Case:
    Modified switch module on a wooden surface. Modified smart switch with wires on a wooden table

    Setup for power measurement
    LSPA9 was already flashed, it only needed a script to report power usage to target device.
    I've decided to do it via HTTP GET command with Tasmota-style syntax:
    
    again:
    
    SendGET http://192.168.0.111/cm?cmnd=setChannel%202%20$power
    delay_s 0.5
    
    goto again
    

    Make sure that IP of the target device is correctly entered in the script, also make sure that it won't change (do MAC reservation on router).
    This basically sends the channel setting command:
    
    setChannel 2 $power
    

    and the $power is expanded to current power value from BL0937.

    Setup for relay device
    Relay device will have a bit more advanced script.
    I would like script to offer two modes:
    - automatic mode, where relay is set automatically to open or closed, depending on current reading and given margin
    - manual mode, where user can control relay freely.
    Futhermore, I would like the margin to be configurable on device GUI.
    Luckily all that is possible with OBK channel types:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/channelTypes.md
    Let's start writing script by deciding the channels we need:
    
    // main relay
    setChannelType 1 Toggle
    setChannelLabel 1 Relay
    // working mode
    setChannelType 2 Toggle
    setChannelLabel 2 Automatic Mode
    // received power [W]
    setChannelLabel 3 Power
    setChannelType 3 Power
    // min power to enable cooling
    setChannelType 4 TextField
    setChannelLabel 4 Min Power To Cool
    

    It might be also worth to save those channels in flashvars, so they are kept between reboots:
    
    // remember channels
    setStartValue 1 -1
    setStartValue 2 -1
    setStartValue 3 -1
    setStartValue 4 -1
    

    Now, we need to implement the main logic... the relay must be set to the value determined by the relation of given threshold and current reading. This must be done often, like every second or so, maybe 10 seconds... luckily it turns out it can be done with few lines:
    
    again:
    if $CH2==1 then setChannel 1 $CH3>$CH4
    delay_s 1
    

    If channel 2 is 1 (if mode is automatic), then set relay channel to the result of margin and value comparison. If channel 3 is larger than 4, it will yield 1, thus turning relay on.

    And here is the OBK panel for this script:
    User interface for cooling automation using OpenBeken.

    Using temperature sensor instead of the Power
    Power usage may not be the best indicator for the cooling requirement, but the shown script can be simply adjusted to use measured temperature instead. OpenBeken supports DHT series of sensors, DS18B20 should also run correctly, so if you store temperature in channel 3, the same logic that works for power can now work for temperature. No futher changes in script are required.


    Testing the automation script with OpenBeken Simulator
    This section is not mandatory, it's just a recommendation for developers.
    OpenBeken features an IoT device simulator that comes with automatic self test system. Self tests are used to make sure that each build does not breaks compatibility with expected OBK features and behaviours set.
    Self tests are run automatically on Github with each online build.
    Here's the self test I designed:
    Code: C / C++
    Log in, to see the code

    The following code setups the mentioned script inside the simulator and then simulates different use case scenarios, first the scenario with automatic control off, and then the scenario with automatic control on. It also simulates random power reading and verifies if the relay behavious is as expected. All expected OBK states are asserted with ASSERT preprocessor macros. If given assertion fails, then SelfTests are stopped with error, so a developer can check what's wrong.

    The selftest mechanism is very useful because it allows us to quickly check if the new changes are working as expected. Selftest will fail if any of the new commited changes is breaking some of old features that are covered by self tests.

    Selftests can run also on your PC, just like mentioned OBK Simulator.

    Summary
    This way you can make a very simple thermostat-like behaviour, with either just one or even two separate devices. The thermostat can be easily toggled on and off and the threshold value is also available on GUI.
    If you need more advanced solution, you can also consider
    creating custom driver.
    That's all for now, but in the next part I'll show a similiar mechanism, but with PWM (gradual) cooling support. This will be used to reduce fan noise when strong cooling is not needed.

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11907 posts with rating 9979, helped 571 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 21379196
    chemik_16
    Level 26  
    just in about six months I will have to put the whole iot in the new house, so I am thinking of getting rid of HA completely as an automation element - I would leave it possibly only for log collection and preview. For me it works hopelessly as the "heart" of the installation. It would be better if all components communicated directly with each other,
  • #3 21379212
    p.kaczmarek2
    Moderator Smart Home
    In OBK you can either use off-the-shelf support Tasmota Device Groups (for grouping lights, switches and lights, etc) or script SendGET queries so that devices directly "talk" to each other. You can also create a simple custom driver in C , which you then compile online , and automatically upload via OTA . No toolchain is needed.
    You can also test automation in The OBK Simulator which runs on Windows and also supports a possible Home Assistant.
    Here is a list of supported platforms:
    
    - BK7231T (WB3S, WB2S, WB2L, etc)
    - BK7231N (CB2S, CB2L, WB2L_M1, etc)
    - BK7231M, this is a non-Tuya version of BK7231N with 00000000 keys, also sometimes in BL2028 flavour
    - T34 (T34 is based on BK7231N), see flashing trick
    - BL2028N (BL2028N is a Belon version of BK7231N)
    - XR809 (XR3, etc)
    - BL602 (SM-028_V1.3 etc), see also BL602 flash OBK via OTA tutorial (Magic Home devices only)
    - LF686 (flash it as BL602)
    - W800 (W800-C400, WinnerMicro WiFi & Bluetooth), W801
    - W600 (WinnerMicro chip), W601 (WIS600, ESP-01W, TW-02, TW-03, etc)
    - LN882H by Lightning Semi - datasheet, see flashing how-to, see sample device teardown and flashing, see new flash tool, see dev board
    Windows, via simulator
    - ESP32 (working good, guide will be released soon, development topic)
    - RTL8710C/RTL8720C (WBR2, WBR3, etc)
    - BK7238 (WIP)
    - TR6260 (see guide)
    
    .
    Helpful post? Buy me a coffee.
ADVERTISEMENT