• Soil moisture sensor

    MaBe4201/01/2025 at 20:46 0 comments

    After my first positive experiences with ESPhome, I had a closer look into the documentation and found out that deepsleep is also implemented.

    I had some soil moisture sensors connected to an ESP8266 to detect if irrigation is necessary. The device is powered by an 18650 cell. It wakes up every 30 mins, switches on the power to the sensor, reads the sensor, sends the measured value via MQTT and goes back to deep sleep. It was not too difficult to implement it in ESPhome:

    esphome:
      name: eh-bodenfeuchte3
      on_boot:
        - switch.turn_on: Sensor_Vcc
        - delay: 1s
    # Terrassensensor
    
    esp8266:
      board: huzzah
    
    # Enable logging
    logger:
    
    # Enable Home Assistant API
    api:
      password: ""
    
    ota:
      - platform: esphome
        password: ""
    
    wifi:
      ssid: !secret wifi_ssid
      password: !secret wifi_password
    
      # Enable fallback hotspot (captive portal) in case wifi connection fails
      ap:
        ssid: "Eh-Bodenfeuchte3"
        password: "EfGXNoIANTV5"
    
    captive_portal:
    
    deep_sleep:
      run_duration: 12s
      sleep_duration: 30min
      id: ds
    
    switch:
      - platform: gpio
        pin: GPIO14 #D5
        id: Sensor_Vcc
    
    binary_sensor:
      - platform: homeassistant
        id: no_deep_sleep
        entity_id: input_boolean.no_deep_sleep
    
    mqtt:
      broker: 192.168.2.204
      discovery: false
      discover_ip: false
    
    sensor:
      - platform: wifi_signal
        name: "WiFi Signal Sensor"
        update_interval: 10s
    
      - platform: adc
        pin: A0
        name: Terrassenfeuchte
        samples: 10
        update_interval: 8s
        id: terrassenfeuchte
        filters:
          - multiply: 1000
        on_value:
          - mqtt.publish:
              topic: "ESP/H2O/Sensor3"
              payload: !lambda |-
                return to_string(int(1*id(terrassenfeuchte).state));
              retain: true
          - if:
              condition:
                binary_sensor.is_on: no_deep_sleep
              then:
                - deep_sleep.prevent: ds
    #            - logger.log: "No sleep!"
              else:
                - deep_sleep.allow: ds
    #            - logger.log: "Let's snooze!"
    

    Each time it wakes up, the **on_boot**-section is carried out. There I switch on the voltage for the sensor. As it does not draw much current, I power it directly from a GPIO.

    The implementation in ESPhome allowed me even to add a switch to HA to prevent the device from going to deep sleep again after the next time it wakes up again. This allows easily flashing OTA without opening the waterproof housing. A feature which I did not have before.

    I put the device into the soil in April and remove it at the end of October. I charge the cell at the beginning of August to be sure that it lasts. That's good enough for me.

  • LED lighting

    MaBe4212/27/2024 at 21:57 0 comments

    An incomplete yaml file to control two LED strips for illumination using two buttons. The buttons allow only to switch between on and off. From HomeAssistant dimming is possible.

    The device uses a Wemos D1 mini.

    The LEDs are controlled using PWM output and are connected using a logic-level n-MOSFET (IRLU 024N). The maximum level is set to 95% to prevent the LEDs from to early degradation (at least I hope so).

    The buttons used to toggle the lights include a very simple debouncing (filter).

    esp8266:
      board: d1_mini
    
    # LEDs: 13, 14 (D7, D5)
    # Taster: 5, 4 (D2, D1)
        
    output:
      - platform: esp8266_pwm
        pin: GPIO14 #D5
        id: Fenster_pwm
        frequency: 440 Hz
        min_power: 0
        max_power: 0.95
    
      - platform: esp8266_pwm
        min_power: 0
        max_power: 0.95
        frequency: 440 Hz
        id: Bett_pwm
        pin: GPIO13 #D7
    
    
    light:
      - platform: monochromatic
        name: "Licht Fenster"
        id: Licht_Fenster
        output: Fenster_pwm
        default_transition_length: 3s
    
      - platform: monochromatic
        name: "Licht Bett"
        id: Licht_Bett
        output: Bett_pwm
        default_transition_length: 3s
    
    #  - platform: status_led
    #    name: "Status"
    #    pin:
    #      number: GPIO2
    #      inverted: true
    #    restore_mode: ALWAYS_OFF
    
    binary_sensor:
      - platform: gpio
        name: "Taster_Fenster"
        pin: 
          number: GPIO4 #D1
          inverted: true
          mode: 
            input: true
            pullup: true
        filters:
          - delayed_on: 50ms
        on_click:
          max_length: 500ms
          then:
            - light.toggle: Licht_Fenster
    
      - platform: gpio
        name: "Taster_Bett"
        pin: 
          number: GPIO5 #D2
          inverted: true
          mode: 
            input: true
            pullup: true
        filters:
          - delayed_on: 50ms
        on_click:
          max_length: 500ms
          then:
            - light.toggle: Licht_Bett
    
    sensor:
      - platform: wifi_signal
        name: "WiFi Signal Sensor"
        update_interval: 300s
    
    

  • Using the command line

    MaBe4212/27/2024 at 21:32 0 comments

    I am working on a Debian Bookworm system. This means, to use python3 with additional packages, I have to use a virtual environment.

    Using a virtual environment is explained in many places on the web e.g. https://linuxconfig.org/how-to-set-up-a-python-virtual-environment-on-debian-10-buster

    To get my environment running I have to run

    source ~/ESPhome/bin/activate

    Then in my working directory (where the yaml-files reside) I only need three commands:

    to create a new yaml file [myproject].yaml:

    esphome wizard [myproject].yaml

     to compile and upload:

    esphome run [myproject].yaml

    to check for errors:

    esphome config [myproject].yaml

  • Replacing OSPi (OpenSprinkler)

    MaBe4210/08/2024 at 20:52 0 comments

    I did not have any luck with OpenSprinkler running on a Raspberry Pi. I had bought the OSPi board and set up everything in my workshop. It worked as intended. After putting it up in its final place in the garage, not any more. Something on the board had given up the ghost. So I set up a kind of replacement with a 74HC595 shift register as on the OSPi board but using a 4x relay board instead of the triacs. That worked.


    However, While I was on summer holiday, for no discernible reason it stopped working. The pi could not be pinged any more. Neither the pi nor the wifi router had been moved. Resetting did not help. this was during the hottest weeks this summer and some of our plants didn't like it that they were not watered for more than a week.

    Putting it back into the workshop everything was fine again but I had lost confidence in this solution.

    Therefore, I decided to replace the pi with an ESP8266 which has enough GPIOs for 4 relays (of which I anyway only used 2). As I have home assistant running (in docker), I gave ESPhome (command line version on my laptop) a try. It was pretty easy to set it up. Now I have to provide the programming of the watering by myself. But with my soil moisture sensors and measurement of the outside temperature this is no big deal. I should have done this right from the beginning. It would have saved me a lot of trouble and some money, too.

    esphome:
      name: eh-irrigator
    
    esp8266:
      board: d1_mini_pro
    
    # Enable logging
    logger:
    
    # Enable Home Assistant API
    api:
      password: ""
    
    ota:
      - platform: esphome
        password: ""
    
    wifi:
      ssid: !secret wifi_ssid
      password: !secret wifi_password
    
      # Enable fallback hotspot (captive portal) in case wifi connection fails
      ap:
        ssid: "Eh-Irrigator Fallback Hotspot"
        password: "u6FGqdp5vY7G"
    
    captive_portal:
    
    switch:
      - platform: gpio
        name: "Wasser Garten"
        pin: GPIO14 #D5
    
      - platform: gpio
        name: "Wasser Balkon"
        pin: GPIO12 #D6
    
    sensor:
      - platform: wifi_signal
        name: "WiFi Signal Sensor"
        update_interval: 2min
    

    The above file was mostly generated using the esphome wizard. Only the switches and the sensor had to be added manually.

    I don't find the documentation very clear with respect to the !secret. But it is as simple as generating the file secrets.yaml in the same directory as the above yaml file and making there the definitions for ssid and password.

    The OTA update of ESPhome works like a charm!