Skip to content

YutingYou/ifttt-serial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IFTTT Serial Monitor

简体中文 | English

A Python tool for executing specific tasks by monitoring serial port logs.

Features

  • Real-time monitoring and automatic saving of serial logs
  • Rule matching based on regular expressions
  • Support for multiple action types
  • Custom Python function invocation

Quick Start

1. Install Dependencies

pip install -r requirements.txt

2. Basic Usage

python ifttt_serial.py rule.yaml

3. Example Configurations

See the examples/ folder for full configuration options:

  • examples/1-basic/rule.yaml - Basic configuration example
  • examples/2-advance/rule.yaml - Advanced configuration example

Rule File Configuration

Serial Port Configuration (Mandatory)

serial:
  port: /dev/ttyUSB0
  baudrate: 115200

Important Note: The tool automatically creates two log files:

  • Device log file (device_xxx.log) - Records raw data received from the serial port
  • IFTTT log file (device_xxx.ifttt.log) - Records logs generated by action execution

For example, configuring device_20250710_120000 will generate:

  • device_20250710_120000.log - Device raw log
  • device_20250710_120000.ifttt.log - IFTTT action log

Startup Action (Optional)

startup:
  action:
    type: send
    cmd: "reboot"

Trigger Rules (Mandatory)

rules:
  - match: "BOOT_OK"
    action:
      type: save-log
      text: "Device boot completed"
  - match: "ERROR:(\\d+)"
    action:
      type: python
      module: handlers
      function: handle_error
      args: ["{1}"]

Timeout Handling (Optional)

timeout:
  no_data_sec: 30
  on_timeout:
    type: shell
    cmd: "bash reboot_dut.sh"
  max_retries: 3
  on_failure:
    type: exit
    code: 1

Action Types

1. send - Send Data to the Current Serial Port

type: send
cmd: "reboot\r\n"

Parameters:

  • cmd - Command string to send to the serial port

Use Cases:

  • Send startup, reboot, or other commands to the device
  • Send control commands in automated test flows

Note: This action sends data directly to the currently connected serial port.

2. serial - Send Data to a Specified Serial Port

type: serial
port: /dev/ttyUSB1
baudrate: 115200
cmd: "reboot\r\n"

Parameters:

  • port - Target serial device path
  • baudrate - Baud rate, default is 115200
  • cmd - Command to send

Use Cases:

  • Send commands to multiple different serial devices
  • Control external devices (e.g., modems, sensors, etc.)

Note: This action creates a temporary serial connection, which is closed automatically after sending.

3. sleep - Delay Execution

type: sleep
sec: 2

Parameters:

  • sec - Delay in seconds, supports decimals (e.g., 0.5 means 500 ms)

Use Cases:

  • Wait for device response between actions
  • Insert delay steps in test flows

4. save-log - Write to IFTTT Log

type: save-log
text: "Custom log message"

Parameters:

  • text - Content to write to the IFTTT log

Use Cases:

  • Record custom events or debug information to the IFTTT log
  • Save context information when rules are triggered

Note: This writes to the dedicated IFTTT log file, not the device log file.

5. sequence - Execute Multiple Actions in Order

type: sequence
actions:
  - type: save-log
    text: "Sequence Actions start"
  - type: sleep
    sec: 1
  - type: send
    cmd: "reboot\r\n"

Parameters:

  • actions - List of actions to execute in order; each action is configured the same as a standalone action

Use Cases:

  • Execute multiple actions sequentially (e.g., initialization, wait, start, etc.)
  • Combine complex automation flows

Note: Each sub-action in a sequence is executed strictly in order; the next action will not start until the previous one completes.

6. shell - Execute System Commands

type: shell
cmd: "echo 'Hello World'"

Parameters:

  • cmd - Shell command string to execute

Use Cases:

  • Call external scripts
  • Integrate system-level operations into automation flows

Note:

  • The shell action waits for the command to finish before continuing; failures are logged to the IFTTT log.
  • Shell commands are executed in the directory where the config file is located, avoiding the need for long paths.

7. python - Call Python Functions

type: python
module: custom_handlers
function: handle_error
args: ["{1}", "error"]

Parameters:

  • module - Python module name to import (with or without .py suffix)
  • function - Function name to call
  • args - List of arguments to pass to the function, supports placeholders (e.g., {1} for regex groups)

Use Cases:

  • Handle complex logic or custom business requirements
  • Send error information to users via email or messaging APIs

Note:

  • Custom modules must be in the same directory as the rule file or in the Python path
  • Supports passing regex groups and context variables as arguments

8. exit - Exit the Program

type: exit
code: 1

Parameters:

  • code - Exit code (integer), usually 0 for normal exit, non-zero for errors

Use Cases:

  • Automatically exit the program after reaching max retries or detecting fatal errors

Note:

  • The exit action immediately terminates the main program; subsequent actions will not be executed
  • Can be used with timeout and other mechanisms for automated error handling

Placeholder Support

  • {timestamp} - Current timestamp
  • {1}, {2}, ... - Regex match groups

Troubleshooting

  1. Serial Connection Failure

    • Check if the serial device path is correct
    • Confirm device permissions and serial parameters
  2. Configuration File Errors

    • Validate YAML format
    • Check for required fields
    • Ensure correct YAML indentation (YAML is indentation-sensitive)
  3. Python Module Import Failure

    • Make sure custom Python modules and rule files are in the same folder
    • Check module and function names

File Description

  • ifttt_serial.py - Main program
  • requirements.txt - Dependency list
  • examples/ - Example configurations and handlers
  • tests/ - Test-related files

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published