Files
relay_chn/examples/relay_chn_single
ismail 9ee974e677 Add a single channel example
Added a single channel example with run limit and tilting
features. Refs #1104 and closes #1105.
2025-09-09 18:15:32 +03:00
..
2025-09-09 18:15:32 +03:00
2025-09-09 18:15:32 +03:00
2025-09-09 18:15:32 +03:00
2025-09-09 18:15:32 +03:00
2025-09-09 18:15:32 +03:00

Relay Channel Single Example

Introduction

This example demonstrates how to use the relay channel component to control a 2-relay setup with button inputs and LED status indication. It showcases:

  • Basic relay channel operations (forward/reverse running, stopping)
  • Secondary operations (tilting, direction flipping)
  • State change event handling with multiple listeners
  • Relay channel run limit
  • Button event handling using esp-iot-solution's button component
  • Visual feedback using esp-iot-solution's LED indicator component

How to Use Example

This example has been tested on an ESP32-C3-DevKitM-1U board. However, it can be adapted to any ESP32-based board with at least six available GPIO pins by adjusting the configuration options.

Hardware Required

  • An ESP32-based development board
  • 2 relays connected to GPIO pins (default: GPIO4, GPIO5)
  • 3 buttons connected to GPIO pins:
    • UP button (default: GPIO0)
    • DOWN button (default: GPIO1)
    • STOP button (default: GPIO2)
  • 1 LED for status indication (default: GPIO3)

Hardware Schematic

Hardware Schematic

Configuration

The example can be configured through menuconfig under "Relay Channel Single Example Configuration":

  1. Button active level (EXAMPLE_RLCHN_BTN_ACTIVE_LEVEL)

    • Select between active LOW or HIGH logic level for buttons
  2. GPIO assignments:

    • UP button pin (EXAMPLE_RLCHN_BTN_UP_IO_NUM, default: 0)
    • DOWN button pin (EXAMPLE_RLCHN_BTN_DOWN_IO_NUM, default: 1)
    • STOP button pin (EXAMPLE_RLCHN_BTN_STOP_IO_NUM, default: 2)
    • LED indicator pin (EXAMPLE_RLCHN_LED_INDICATOR_IO_NUM, default: 3)
  3. Long press timing:

    • EXAMPLE_RLCHN_BTN_LONG_PRESS_TIME_MS: Duration for long press actions (1500-3000ms, default: 2000ms)

Button Operations

The example uses esp-iot-solution's button component to handle the following operations:

  • UP button:

    • Short press: Start forward movement
    • Long press: Start forward tilt (stops on release)
  • DOWN button:

    • Short press: Start reverse movement
    • Long press: Start reverse tilt (stops on release)
  • STOP button:

    • Short press: Stop movement
    • Long press: Flip movement direction

LED Indicator States

The example uses esp-iot-solution's led_indicator component to show different states:

  • Running: LED blinks at 300ms on, 100ms off
  • Tilting: Fast blink at 100ms on, 50ms off
  • Operation Success: Two quick blinks
  • Operation Fail: One long blink

Dependencies

This example requires:

  • ESP-IDF v4.1 or later
  • esp-iot-solution components:
    • button v4.1.1 or later
    • led_indicator v1.1.1 or later
  • relay_chn component

Example Output

When the application boots, it will wait for a button event. Then the two state listeners will print state changes.

I (273) main_task: Calling app_main()
I (273) RELAY_CHN_SINGLE_EXAMPLE: Initializing default NVS storage
I (283) RELAY_CHN_SINGLE_EXAMPLE: nvs_flash_init: NVS flash init return: ESP_OK
I (283) RELAY_CHN_SINGLE_EXAMPLE: Initializing relay channel
I (293) gpio: GPIO[4]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (303) gpio: GPIO[5]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (313) RELAY_CHN_SINGLE_EXAMPLE: Initializing buttons
I (313) RELAY_CHN_SINGLE_EXAMPLE: Initializing buttons with active level: 0
I (323) gpio: GPIO[0]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (323) button: IoT Button Version: 4.1.3
I (333) gpio: GPIO[1]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (343) gpio: GPIO[2]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (343) RELAY_CHN_SINGLE_EXAMPLE: Setting up button callbacks. Configured long press time: 2000 ms
I (353) RELAY_CHN_SINGLE_EXAMPLE: Initializing LED indicator
I (363) gpio: GPIO[3]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
I (373) led_indicator: LED Indicator Version: 1.1.1
I (373) gpio: GPIO[3]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (383) led_indicator: Indicator create successfully. type:GPIO mode, hardware_data:0x3fc97be4, blink_lists:custom
I (393) RELAY_CHN_SINGLE_EXAMPLE: Relay Channel Single Example is ready to operate
I (403) main_task: Returned from app_main()
I (3683) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 1 to 3
I (3683) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from IDLE to FORWARD
I (9513) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 3 to 2
I (9513) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from FORWARD to STOPPED
I (9523) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 2 to 6
I (9533) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from STOPPED to REVERSE_PENDING
I (10313) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 6 to 4
I (10313) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from REVERSE_PENDING to REVERSE
I (32173) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 4 to 2
I (32173) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from REVERSE to STOPPED
I (32973) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 2 to 1
I (32973) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from STOPPED to IDLE
I (36423) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 1 to 8
I (36423) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from IDLE to TILT_REVERSE
I (41153) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 8 to 1
I (41153) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from TILT_REVERSE to IDLE
I (47113) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 1 to 7
I (47113) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from IDLE to TILT_FORWARD
I (51913) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_1: Defining new indicator mode for #0 and state change from 7 to 1
I (51913) RELAY_CHN_SINGLE_EXAMPLE: example_event_listener_2: State change for #0, from TILT_FORWARD to IDLE