Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Larvitar/MoodeIrRemote

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MoodeIrRemote

Simple project using PiIR written to help with IR remote setup for MoodeAudio.

Supports

  1. Moode Web API
  2. Spotify
  3. Shell commands
  4. BT volume control

Requirements

  1. Python3 and a few Python libraries - requirements.txt.

  2. An IR receiver connected to one of GPIO ports and a remote controller matching this receiver.

    or

    USB remote

  3. pigpio (IR remotes only)

    pigpiod has to be running. Remember that if you're using a sound overlay (HiFiBerry boards etc.) you HAVE to run pigpio with -t0 argument. See pigpio/#152 for more information.

    [Unit]
    Description=pigpio daemon
     
    [Service]
    Type=forking
    PIDFile=pigpio.pid
    ExecStart=/usr/bin/pigpiod -t0
     
    [Install]
    WantedBy=multi-user.target
    

    pigpio/util

Installation

  1. Clone this repository to your RPi. You might want to set 700 permission since Spotify credentials will be stored there.

    git clone https://github.com/Larvitar/MoodeIrRemote
    chmod -R 700 MoodeIrRemote
    
  2. Install requirements:

    sudo python3 -m pip install -r requirements.txt
    
  3. Create a systemd service /etc/systemd/system/moode_ir_remote.service :

    [Unit]
    Description=MoodeIrRemote
    After=pigpiod.service
     
    [Service]
    Type=Idle
    ExecStart=/usr/bin/python3 /home/pi/MoodeIrRemote/mpd_control.py
     
    [Install]
    WantedBy=multi-user.target
    
  4. And start it:

    sudo systemctl daemon-reload
    sudo systemctl start moode_ir_remote
    sudo systemctl enable moode_ir_remote
    

    You can check if script started successfully by running systemctl status moode_ir_remote

Note: Remember to disable the service before running the script manually for tests/configuration.

IR Setup

What you need is an IR receiver (i.e. TSOP4836) and a remote working on the same frequency (36kHz in this example). Connect it to power and RPi GPIO pin as instructed in receivers datasheet. In most cases 2 pins will have to be connected to 5V/GND and the 3rd one to GPIO (usually requiring additional capacitor and resistor).

You can test your setup in command line using PiIR#command-line-usage.

USB Remotes

Most standard USB remotes register themselves as a keyboard and send standard keyboard key commands. Basic media keys are defined in usb_keyboard.json config. If this does not work for you try registering a new USB remote using remotes configuration.

Remember to enable enable_usb_remote in config and add usb_keyboard.json to remotes list. Keep in mind that the script will register key events from ALL connected USB keyboards.

Note: After connecting/disconnecting your remote restart of the script is required.

Configuration

Configuration is stored in config.json

{
  "remotes": ["default.json"],              # List of remote controllers that will be loaded on startup
  "ir_gpio_pin":  24,                       # GPIO pin where IR receiver is attached
  "enable_ir_remote": true,                 # Whether to listen to IR events
  "enable_usb_remote": false,               # Whether to listen to USB keyboard events or not
  "keyboard_event_type": "up",              # up/down - generate event on key press (down) or key release (up)
  "logging": {
    "level": "INFO",                        # Level of console logs
    "file_level": "DEBUG",                  # Level of file logs
    "global_level": "WARNING",              # Level of root logger (logs from any imported libraries)
    "log_all_to_file": true                 # Write logs from imported libraries to log file
  },
  "spotify": {                              # Spotify configuration
    "redirect_uri": "http://moode.local:8080/auth",
    "client_id": "",
    "client_secret": "",
    "auth_server_listen_ip": "0.0.0.0",
    "auth_server_listen_port": 8080
  },
  "default_moode_set": "set_playlist"         # Default set
}

Remotes configuration

Remotes configuration is stored in keymaps/*.json files. Run the script with setup <file_name> parameter and it will guide you through the process of configuring your own remote. If file_name is not provided default.json will be used.

    > python3  mpd_control.py setup test_remote.json
    Running setup of test_remote.json
    Button "power"  (recorded: 0) [(R)ecord / (D)elete last / (C)lear all / (N)ext / (E)nd]: r
            Press the key again to verify
    Button "power"  (recorded: 1) [(R)ecord / (D)elete last / (C)lear all / (N)ext / (E)nd]: n
    Button "ok"     (recorded: 0) [(R)ecord / (D)elete last / (C)lear all / (N)ext / (E)nd]: r
            Press the key again to verify
    Button "ok"     (recorded: 1) [(R)ecord / (D)elete last / (C)lear all / (N)ext / (E)nd]: e
    Setup result: {....}

You can run Record multiple times in order to assign multiple buttons to the same function (i.e. Right and ChannelUp for next song).

Script will scan through commands defined in commands/base.json and commands/custom.json. If you run setup on already existing keymap, you'll be able to update it.

Run script in test mode if you want to verify a keymap:

    > python3 mpd_control.py test test_remote.json
    MoodeIrController:INFO - Loaded keymap test_remote.json
    MoodeIrController:INFO - Monitoring started (test mode)
    Key "ok" received.
    Key "power" received.

If the test was successful you can add test_remote.json to config in remotes list.

Note: Some remotes use 2 alternating codes for the same button. Script will try to recognize it and ask you to click it multiple times but if you run into any troubles just try running Record multiple times.

Note: If by accident you assign same button to multiple functions (or if 2 remotes send same code for different buttons) script will only run the first action that matches that code.

Spotify

Spotify Premium is required.

  1. You will need to create a Spotify developer app.

    1. Log into https://developer.spotify.com/dashboard/login
    2. Click on "CREATE AN APP" and fill required information.
    3. Save your "Client ID" and "Client Secret".
  2. Now open config.json:

    1. Fill client_id and client_secret.
    2. redirect_uri - replace moode.local with your RPi IP if moode.local is not accessible from your PC.
    3. auth_server_listen_ip - IP to start the auth server on. 0.0.0.0 will listen on every interface.
    4. auth_server_listen_port - Port of the auth server. Change if something else is already using this port (You'll also need to change this value in redirect_uri).
  3. Open Spotify developer page:

    1. In your app page click EDIT SETTINGS.
    2. Add address from redirect_uri in Redirect URIs section.
  4. You can start mpd_control.py now. If all Spotify settings were filled script should run a server at http://moode.local:8080 .

    1. Open this address in your browser and you'll be presented with a Authorize with Spotify button. Click it and allow your app to access to your Spotify account.
    2. You should see Authentication success message. Server on port 8080 will now be disabled.
    3. Spotify should now work properly.

Note: If any of spotify settings is not filled this setup process will be skipped.

Note: Script will only wait for 2 minutes for you to complete authentication process. After that server will shutdown and Spotify will not work.

Note: If Spotify authorization expires (password change, revoked app permissions etc.) script will drop all spotify commands and a restart will be required in order to run authorization process again.

Commands

Buttons closely related to playback (play, vol_up, next etc.) are defined in commands/base.json and usually you won't have to change anything in there. Other commands (playlists, radios etc.) should be defined in commands/custom.json you should create yourself. You can see examples of possible commands in commands/example_custom.json. See commands/README for more information.

Example command config:

  "vol_up": {                   <-- Button name
    "moode": {                  <-- When this command can be run (currently active player)
      "target": "moode",        <-- Where to execute command
      "command": "vol_up",      <-- Command to execute
      "value": 2                <-- Value to be send (only applicable for some commands)
    }
  }

Possible states are: roonbridge, airplay, bluetooth, squeezelite, spotify, input, moode, global (always)
Possible targets are: bluetooth, spotify, moode, shell

Note: global will be run only if active player does not match any other command.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages