Other Projects

Remote controlled BananaPi music alarm clock

6 3880
danboid  
Edited by danboid at Jan 21, 2016 16:14

danboids_BParth_alarm.jpg

For some time now I've been using my phone as my one and only alarm clock. Whilst that's convenient it's also not an ideal solution because, as anyone who's ever tried using a smartphone as an alarm clock knows, it's all too easy to go to sleep without realising how low your phones battery is and then not being awoke because your phone battery died overnight. This happened to me again recently so I decided to offload the task to the Banana Pi server sat in the corner of my room which uses a similar amount of power to my phone but has the notable advantages of being permanently powered on and attached to a hard drive with hundreds of gigs of space for music storage to provide varied playlists to wake me up that don't rely on my internet connection. Apart from serving as a cheap and low-power Owncloud server, now my BPi is also a remote controlled digital jukebox alarm clock than can play pretty much every audio format including MP3, ogg, wav, FLAC and m4a/AAC. It's hard to tell from the photo but the speaker is entirely powered by a USB cable plugged into the BPi.

These instructions are written specifically for Arch running on the Bpi but it would be trivial to adapt this short guide to anything running a modern Linux distro. Under Arch, the packages you will need to install are:

moc - A powerful and easy to use console audio player with ncurses UI to create playlists etc. The command itself is called mocp.
alsa-utils - This package contains amixer which we'll use to adjust the volume
fcron - An improved, systemd-friendly version of cron. This is used to trigger the alarm (mocp) at a specified time.

The first thing I had to do was get a remote control that is compatible with the IR receiver integrated into the BPi. A quick search for universal remote controls online led to me buying the electrotec URC22B Universal Remote Controller. It cost me £5 and it supports hundreds of different devices and manufacturers. I've read that the BPi IR receiver is compatible with NEC IR devices but thankfully I was spared having to experiment with different device codes or use the remotes auto-discover feature because the default SAT mode button switches the remote into using a BPi-friendly IR protocol.

Linux requires that users who wish to use audio devices are members of the audio group. You can check which groups your user is a member of using the `id` command. If the user you wish to run moc as isn't in the audio group you can add your user with a command such as:
  1. sudo usermod -aG audio <user>
Copy the Code
Before attempting to use a remote control, it'd be worth checking your kernel includes the sunxi-ir driver. Running
  1. dmesg | grep sunxi-ir
Copy the Code
Should return something like this under a propery configured sunxi kernel:
  1. [    2.517226] input: sunxi-ir as /devices/virtual/input/input0
Copy the Code
On my Arch installation, I had to fix my /boot/script.bin file to get IR working with the sunxi 3.x kernel. The line beginning "ir_rx=" should read "ir0_rx=". To check and fix this for the sunxi 3.x kernel, you need to install the sunxi-tools package. As root, do the following then reboot:
  1. # cd /boot
  2. # bin2fex script.bin > script.fex
  3. # vim script.fex # Change "ir_rx=" to "ir0_rx="
  4. # fex2bin script.fex > script.bin
Copy the Code
A different set of steps are required to be took to enable IR under 4.X kernels. First, make sure the sunxi-cir kernel module is present. To load the required module at boot, create a file called /etc/modules-load.d/sunxi-ir.conf just containing:
  1. sunxi-cir
Copy the Code
We also need to define the IR protocol to use. I believe the 3.X kernel defaults to using the NEC protocol and I've been able to get my remote working using the NEC protocol under Linux 4.4.0 so that's what we'll use. Create a file called /etc/systemd/system/sunxi-ir.service containing the following:
  1. [Unit]
  2. Description=sunxi-ir

  3. [Service]
  4. ExecStart=/usr/bin/bash -c 'echo nec > /sys/class/lirc/lirc0/device/protocols'

  5. [Install]
  6. WantedBy=multi-user.target
Copy the Code
You can run cat /sys/class/lirc/lirc0/device/protocols to list the available IR protcols if NEC doesn't work for you.

With that in place we can instruct systemd to load it on every boot by running:
  1. # systemctl enable sunxi-ir.service
Copy the Code
To capture the input from the IR receiver and convert the signals (keystrokes) into commands I used keybinder. Installing keybinder is as simple as running these few commands:
  1. $ git clone https://github.com/elopez/keybinder.git
  2. $ cd keybinder
  3. $ sudo make install
Copy the Code
If you have the same remote as I do then you can use this /etc/keybinder.conf config:
  1. 148,amixer set Master 5%+
  2. 149,amixer set Master 5%-
  3. 155,mocp -f
  4. 157,mocp -x
  5. 158,mocp -c;mocp -S && mocp -a /home/dan/Music/playlist.m3u -p
  6. 201,mocp -P;sleep 600;mocp -U
  7. 212,mocp -r
Copy the Code
Keys 148/149 are the volume up/down buttons, 155/212 skip tracks with the left and right arrow buttons, 157 is the stop button which exits mocp, 158 is the power button which reloads and starts the playlist and 201 is the Play button which pauses the playlist for 600 seconds / 10 minutes aka sleep. Everyone will need to at least adjust /home/dan/Music/playlist.m3u to point to a valid m3u playlist. You can create m3u playlists using moc and its worth noting that these playlists can contain any type of audio file that moc can play, not just MP3s.

If you are using a different remote then you'll have to adjust the key values. You can test your remote and find out the values for each button by running as root:
  1. # evtest /dev/input/event0
Copy the Code
For the 3.X kernels. Use /dev/input/event1 for Linux 4.X.

I prefer moc to play my playlists in a random order and I have fcron running mocp as root so I had to create the file /root/.moc/config containing:
  1. Shuffle=yes
Copy the Code
If you want to use moc or any other app thart requires audio as a non-root user under Arch you must add them to the audio group. I don't reboot my BPi very much but I want keybinder to be loaded automatically on every boot so I created a systemd service for it under /etc/systemd/system/keybinder.service like this:
  1. [Unit]
  2. Description=keybinder

  3. [Service]
  4. ExecStart=/usr/local/bin/keybinder /dev/input/event0

  5. [Install]
  6. WantedBy=multi-user.target
Copy the Code
After creating that file you can tell systemd to load it on every boot by running:
  1. # systemctl enable keybinder.service
Copy the Code
The final set of steps is to configure fcron to trigger mocp at the desired times. Arch, like almost every other Linux distro, has moved from using sysv init scripts to systemd. Systemd has its own integrated alternative to cron jobs called timers. I don't know yet if its a bug specific to running systemd under Arch ARM or if it affects all current systemd-based ARM Linux distros but I experienced multiple issues with my systemd timers including not being able to enable them for regular users and I failed to get scripts involving mocp to work as root so I have used fcron to trigger the alarm instead. cron has been used pretty much since the dawn of mankind to schedule jobs under UNIX and fcron is simply an improved cron. The good news for those familiar with cron is that fcron is compatible with the crontab format so you can use the same cron commands, guides and crontab generator tools and web apps to configure it if you don't know or can't remember the (f)cron format.

Create the script which will be used to launch playlist, /usr/local/scripts/alarm.sh , which should contain something like:
  1. #!/bin/bash
  2. mocp -c;mocp -S && mocp -a /home/dan/Music/playlist.m3u -p
Copy the Code
Substituting /home/dan/Music/playlist.m3u for the path to your playlist, make the script executable
  1. chmod +x /usr/local/scripts/alarm.sh
Copy the Code
Add a job to your fcrontab by running fcrontab -e then adding a line such as the following to it:
  1. 30 7 * * 1,2,3,4,5 /usr/local/scripts/alarm.sh
Copy the Code
This fcron job will start the alarm/playlist at 7:30am every Mon-Fri.

To ensure fcron is started on every boot, run:
  1. systemctl enable fcron.service
Copy the Code
danboid  
UPDATE 18th Jan 2016

Added note on making sure user is in the 'audio' group and how to fix it if not.

danboid  
Edited by danboid at Jan 19, 2016 18:15

UPDATE 19th Jan 2016

The guide has been updated with the new address for the keybinder git repo and to highlight that the IR fix only applies to the sunxi 3.x kernels.

UPDATE 2

I have had a quick try at getting the IR receiver to work under 4.4.0 but I have no success yet. I have updated the guide with the name of the kernel module to load / check for anyway.

danboid  
UPDATE 21st Jan

I have now got IR working under Linux 4.4.0 so I have updated the guide to cover bringing up the IR receiver at boot under both Linux 4.X and 3.X.

Unfortunately, although I can see my remote is working via evtest, I've not been able to get keybinder to work under 4.4.0 yet.

Edited by mmorpglol at Feb 03, 2016 20:51

Looks cool, I have made a alarm clock as well, I attached some pictures
1454552945239478271884.jpg 1454552907207-2008921578.jpg 1454552795310-518203038.jpg   it's running Raspbian , LCD is a cheep 3.5 inch touchscreen, built speaker and diy amplifier with the LM386. Using cron for alarms, mplayer for audio. Also can torrent , it has transmission daemon.

danboid  
Great! Thanks for sharing your pics mmorpglol!

Did you use a BPi or an RPi? Is that screen USB powered and what res is it? I'm going to buy a screen soon. I want it to be at least 1024 pix wide and USB powered.

I've since given my el-super-cheapo BPi case a lick of paint, upgraded it to a 2 TB HD and I'm currently experimenting with NetBSD on it.

I'll be posting a comparison of Linux vs NetBSD on the BPi soon.

Spotify is one of the most popular streaming services in the world. According to Business Insider by way of Edison Research, it's the second most popular audio streaming service next to Pandora. It has it all, from the biggest artists of today and yesteryear. Click spotify web player to create new account.

You have to log in before you can reply Login | Sign Up

Points Rules