Simple project using PiIR written to help with IR remote setup for MoodeAudio.
- Moode Web API
- Spotify
- Shell commands
- BT volume control
-
Python3 and a few Python libraries - requirements.txt.
-
An IR receiver connected to one of GPIO ports and a remote controller matching this receiver.
or
-
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
-
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
-
Install requirements:
sudo python3 -m pip install -r requirements.txt
-
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
-
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.
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.
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 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 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 Premium is required.
-
You will need to create a Spotify developer app.
- Log into https://developer.spotify.com/dashboard/login
- Click on "CREATE AN APP" and fill required information.
- Save your "Client ID" and "Client Secret".
-
Now open
config.json
:- Fill
client_id
andclient_secret
. redirect_uri
- replacemoode.local
with your RPi IP ifmoode.local
is not accessible from your PC.auth_server_listen_ip
- IP to start the auth server on.0.0.0.0
will listen on every interface.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 inredirect_uri
).
- Fill
-
Open Spotify developer page:
- In your app page click
EDIT SETTINGS
. - Add address from
redirect_uri
in Redirect URIs section.
- In your app page click
-
You can start
mpd_control.py
now. If all Spotify settings were filled script should run a server athttp://moode.local:8080
.- 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.
- You should see Authentication success message. Server on port
8080
will now be disabled. - 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.
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.