Apr 6, 2020

Athan setup on Raspberry Pi

Ramadan around the corner and I was researching for Athan software that can run on a low powered  Raspberry Pi.  Finally, I was able to setup one with the help of internet search. Detailing steps here.

What you need:

  • Raspberry Pi running with any OS on a 8gb sd card. I installed Raspbian OS using NOOBS

  • Any speakers connected to Pi using 3.5mm connector cable.

  • Python 2.x and above installed on Pi. NOOBS comes with python 2.7.13.

  • Download python script "praytimes.py" from praytimes.org site to calculate prayer times.

  • Download Athan audio mp3 (for ex. Abdul-Basit.mp3) from praytimes.org site to play on speakers.


Next steps

1. Install pip software by running following commands
curl -D - https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo -E python get-pip.py

2. Check pip version
pi@raspberrypi:~ $ pip -V
pip 20.0.2 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

3. Install python-crontab, Crontab module for reading and writing cron jobs using python.
sudo pip install python-crontab

4. Install mp3 player to play audio files downloaded above from cron jobs
sudo apt-get install mplayer

5. Copy the below code and save it as updateAthanTimers.py script
#!/usr/bin/env python

import datetime, time
from praytimes import PrayTimes

#Get Prayer Times Default set to Washington, D.C.
#--------------------
lat = 38.9072
long = -77.0369

now = datetime.datetime.now()
dst = time.localtime().tm_isdst

PT = PrayTimes('ISNA')
times = PT.getTimes((now.year,now.month,now.day), (lat, long), -5,dst)

print times['fajr']
print times['dhuhr']
print times['asr']
print times['maghrib']
print times['isha']

#Update Crontab with Prayer Times
#---------------------------------

from crontab import CronTab

#Function to add azaan time to cron
def addAzaanTime (strPrayerName, strPrayerTime, objCronTab, strCommand):

job = objCronTab.new(command=strCommand,comment=strPrayerName)

timeArr = strPrayerTime.split(':')

hour = timeArr[0]
min = timeArr[1]

job.minute.on(int(min))
job.hour.on(int(hour))

print job

return

system_cron = CronTab('pi')

strPlayAzaanMP3Command = 'mplayer /home/pi/iPray/Abdul-Basit.mp3 > /dev/null 2>&1'

jobs = system_cron.find_command(strPlayAzaanMP3Command)

print jobs

for j in jobs:
system_cron.remove(j)

addAzaanTime('fajr',times['fajr'],system_cron,strPlayAzaanMP3Command)
addAzaanTime('dhuhr',times['dhuhr'],system_cron,strPlayAzaanMP3Command)
addAzaanTime('asr',times['asr'],system_cron,strPlayAzaanMP3Command)
addAzaanTime('maghrib',times['maghrib'],system_cron,strPlayAzaanMP3Command)
addAzaanTime('isha',times['isha'],system_cron,strPlayAzaanMP3Command)

system_cron.write()

6. Keep the python scripts praytimes.py, updateAthanTimers.py and Abdul-Basit.mp3 in same folder. My folder /home/pi/iPray/

7. Configure these changes for your location in updateAthanTimers.py script

a) Longitude and Latitude (currently set to Washington, D.C.)
lat = 38.9072
long = -77.0369

b) Timezone (currently set to -5 GMT and daylight savings =1 )
times = PT.getTimes((now.year,now.month,now.day), (lat, long), -5,1)

8. Make changes in praytimes.py for  the prayer time calculation method (default: MWL), Asr calculation method (default: Standard) and a whole load of other adjustments according to your needs.

9. Add a job in crontab to run updateAthanTimers.py script on a daily basis to update the athaan times automatically:

Run crontab by following command
crontab -e

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute
0 1 * * * python /home/pi/iPray/updateAthanTimers.py > /dev/null 2>&1

10. Test by running the command manually and check the crontab file to make sure entries are added.
python /home/pi/iPray/updateAthanTimers.py

11. Setup complete. For each prayer , the raspberry pi will play the athaan mp3 via the speaker