Skip to content

Clebiez/raspberry-cec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

HDMI-CEC Monitor for Kodi on Raspberry Pi

This project provides an automated HDMI-CEC monitoring solution that manages the Kodi media player based on TV state changes. It intelligently controls Kodi by starting and stopping the application in response to TV power states and HDMI source switching, creating a seamless integration between your Raspberry Pi and TV.

The monitor actively listens to HDMI-CEC commands from your TV and responds appropriately. When the TV enters standby mode, it gracefully stops Kodi after a configurable delay. When the TV switches to the HDMI input where the Raspberry Pi is connected, it automatically launches Kodi. This automation enhances the user experience by eliminating the need for manual intervention in starting and stopping the media center.

Repository Structure

.
└── cec-monitor.py    # Main script that handles CEC monitoring and Kodi control

Usage Instructions

Prerequisites

  • Raspberry Pi with HDMI-CEC support
  • Python 3.x installed
  • Kodi media player installed
  • cec-utils package installed for CEC control
  • Sudo privileges for CEC commands

Installation

  1. Clone the repository to your Raspberry Pi:
git clone [repository-url]
cd [repository-name]
  1. Install required system packages:
sudo apt-get update
sudo apt-get install cec-utils procps

Quick Start

  1. Run the monitor script:
sudo python3 cec-monitor.py
  1. The script will:
    • Detect the physical address of your Raspberry Pi
    • Start monitoring CEC commands
    • Automatically manage Kodi based on TV state changes

Detailed Examples

TV Power State Changes

When your TV enters standby mode:

Received from TV to all (0 to 15): STANDBY (0x36)
> Script detects STANDBY command
> Initiates 10-second countdown
> Gracefully stops Kodi process

When your TV powers on and selects Raspberry Pi input:

Received from TV to all (0 to 15): ACTIVE_SOURCE (0x82):
        phys-addr: 1.0.0.0
> Script detects ACTIVE_SOURCE command
> Verifies physical address matches Raspberry Pi
> Launches Kodi automatically

HDMI Source Switching

When switching to Raspberry Pi input:

Received from TV to all (0 to 15): ROUTING_CHANGE (0x80):
        orig-phys-addr: 0.0.0.0
        new-phys-addr: 1.0.0.0
> Script detects source change to Raspberry Pi
> Starts Kodi if not already running

When switching away from Raspberry Pi:

Received from TV to all (0 to 15): ACTIVE_SOURCE (0x82):
        phys-addr: 0.0.0.0
> Script detects different source activation
> Initiates Kodi shutdown sequence

Delay Behaviors

The script implements smart delays to prevent unwanted Kodi shutdowns:

# Default delay before Kodi shutdown
time.sleep(10)  # 10 seconds delay

# Optional extended delay (uncomment to use)
# time.sleep(300)  # 5 minutes delay

Monitoring Output

Example of successful monitoring initialization:

Physical Address: 1.0.0.0
Starting Monitoring
Monitoring CEC with physical address 1.0.0.0

Data Flow

The script implements a monitoring system that processes HDMI-CEC commands to control Kodi's execution state. It continuously monitors the CEC bus for relevant commands and manages Kodi accordingly.

TV CEC Command ──► CEC Monitor ──► Command Parser ──► Kodi Control
     │                  │                │                │
     │                  │                │                │
     └──► STANDBY      └──► Detection   └──► Delayed     └──► Start/Stop
          ACTIVE_SOURCE     Threading        Processing       Kodi Process

Component Interactions:

  1. CEC Monitor continuously listens for CEC commands using cec-ctl
  2. When a command is detected, it's parsed to identify the type (STANDBY/ACTIVE_SOURCE)
  3. Physical address matching ensures commands are relevant to this device
  4. Threading handles delayed Kodi shutdown to prevent premature termination
  5. Process management handles Kodi start/stop operations with proper signal handling
  6. Error handling ensures robust operation even if Kodi or CEC commands fail
  7. State tracking prevents duplicate operations on Kodi

About

Monitor cec to launch kodi

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages