Max2Play Home › Forums › Max2Play on Raspberry PI › [SOLVED] Plugin to play a song when button is pressed (door bell) › Reply To: [SOLVED] Plugin to play a song when button is pressed (door bell)
Hi Soko,
it is possible to connect your doorbell to some audiooutput with Max2Play. You basically need to write your own bash-script to play a MP3. Following things are needed:
1. WiringPI library and a small script written in C that compiles on the PI to capture the event of the GPIO-PIN. This script should execute a Shell Script.
This is an example of a C-Script, that captures a button event on the PI:
//build with gcc -I. -o button button.c -lwiringPi
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
typedef enum { false, true } bool;
static bool globalActive = false;
static int globalCounter = 0;
void myInterrupt(void) {
int pin = digitalRead(2);
if(pin == LOW && globalActive == false){
globalActive = true;
printf ("Button pressed\n");
}else if(pin == HIGH && globalActive == true){
printf ("Button Released\n");
int sysreturn = 0;
sysreturn = system("/opt/max2play/scriptyouwant.sh");
printf("System: %d", sysreturn);
globalActive = false;
}
}
int main (void)
{
wiringPiSetup () ;
wiringPiISR (2, INT_EDGE_BOTH, &myInterrupt) ;
for (;;) {
sleep(1000);
}
return 0 ;
}
2. Shell-Script, that plays a MP3 with either mpc (command line MPD-Tool) or tells the squeezeboxserver (may be on another device in your network) via http-Get request to play a mp3 on one of your audioplayers (or on all). If you go big, use a squeezebox server and make use the command-line arguments like this in your Shell-Script (you may find the documentation for this command line interface in the squeezebox server interface after installation):
# Example CLI-Call to Play current song via http-GET Request to Squeezebox Server on Max2Play
wget -q -O - "http://max2play:9000/status.html?p0=play&p1=preset_1.single&player=[MAC-Address URL-encoded]"
Cheers,
Stefan