Tuesday, December 12, 2017

Raspberry Pi Music Player Feedback Light

I have a pretty simple setup of a Pi that plays an internet radio stream. One of the problems was that we were constantly turning off the stereo but leaving the Pi playing the stream and we had no feedback that it was still on and consuming bandwidth.

So a simple LED light when MPC was playing should fix that.

After installing a base image of DietPi installed all the mpc and mpd components and got the radio stream going. I installed WiringPi in order to control the GPIO pins.

So, now - how to get the LED to light up with MPC was playing? There are a few approaches such as starting MPC from a script that also lights the LED, but as I sometimes start MPC from the command line, sometime from a custom webpage, I didn't want to remember to run a special script to start it up. I wanted it to be monitoring if MPC was playing and light up no matter the method of starting.

Since cron only runs at 1 minute intervals, I had a problem. I didn't want to wait 1 minute to get feedback to turn the LED on or off. I wanted a smaller resolution than 1 minute intervals, but 1 second was okay. So I settled on cron starting a script every minute, and the script checking MPC each second to see if it was playing or not.

If the length of the string returned was 68 characters, it meant that MPC wasn't playing as it was the general MPC info returned. If MPC was playing it would included the song details, and therefor be greater than 68 characters, so we assume it's playing and light the LED.

First I need to set up the pin at boot time, so I saved this in a startup script located in /etc/init.d/startupscript.sh

#!/bin/bash

#register GPIO 4 for input and output
gpio -g mode 4 out


I had this code saved a script that cron called every minute:

#!/bin/bash

#This file is used in a cron job to test if mpc is playing or not.
for i in `seq 1 60`;
do
sleep 1

PLAYING=$( mpc status )

#Test the length, check the staus, then
#if mpc is not playing then the string length is 68 char long
#if mpc is playing, string length is longer
if [ ${#PLAYING} -gt 68 ]; then
#echo "MPC is playing";
#turn LED on
gpio -g write 4 1
else
#turn LED off
gpio -g write 4 0
fi

done

And I tested it out by wiring up the LED to the right GPIO pin, SSH to the Pi and issue the command 'mpc play' and watch the LED light up almost straight away (max 1 sec), then 'mpc stop' and the LED switched off.

Saturday, March 25, 2017

TFTP

When using Busybox TFTP client

get file:
tftp -g -r filename.txt tftpserverip_or_hostname

put file:
tftp -p -l filename.txt tftpserverip_or_hostname

Getting a file not found error? It seems that the file must exist on the server first, so if you want to upload a new file, first go to the server via SSH or something and create a dummy file of the same name.

Getting permission denied error? You don't have enough permissions on the dummy file you are trying to upload over. SSH to the TFTP server and chmod 777 the file. Upload it. Then change the permissions back to 644 or something.

When using OSX
get file, launch Terminal and type these commands:
tftp
binary
connect tftpserverip_or_hostname
get filename.txt

put file, launch Terminal and type these commands:
tftp
binary
connect tftpserverip_or_hostname
put filename.txt