Wednesday 24 July 2013

Turn your Raspberry Pi into an Azaan/Prayer clock


Recently I've been playing with the raspberry pi and given it's Ramadan, I thought I'd see if I could turn it into an Azaan (Islamic call to prayer) clock. Given the RPI is low powered I can leave it on and it doesn't need a monitor, keyboard, mouse, etc plugged in.

This is what I did:

1. Plug in a speaker into the 3.5mm audio output on the raspberry pi

2. Download python code from prayTimes.org to calculate prayer times

3. Download azaan mp3 from prayTimes.org again.

4. Write a python script to set jobs in crontab to play the azaan mp3 at the appropriate time (see code below or download here). The python code from prayTimes.org has to be in the same folder for this to work.

My script utilizes the python crontab library. More info on setting this up available on the links below

Python crontab: http://pypi.python.org/pypi/python-crontab/
Installing modules: http://docs.python.org/2/install/index.html

The bits you may need to configure yourself are:

a) Longitude and Latitude (currently set to London UK)
lat = 51.5171
long = 0.1062

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

c) Location of azaan mp3:
strPlayAzaanMP3Command = 'omxplayer -o local /home/pi/Downloads/Abdul-Basit.mp3 > /dev/null 2>&1'

The code from prayTimes.org is really configurable, so you can change the prayer time calculation method and a whole load of other adjustments. 

5. Add a job in crontab to run this script on a daily basis to update the azaan times:

# m     h       dom     mon     dow     command
0 1 * * * python /home/pi/Documents/updateAzaanTimers.py > /dev/null 2>&1

6. You're done. For each prayer , the raspberry pi will play the azaan mp3 via the speaker.

For All code and audio files I've used/written are available on the link below:
https://www.dropbox.com/sh/r0e787digng1rgv/OmCXiu9Vx_/RPi%20Azaan%20Clock


#!/usr/bin/env python

import datetime
from praytimes import PrayTimes


#Get Prayer Times
#--------------------
lat = 51.5171
long = 0.1062

now = datetime.datetime.now()



PT = PrayTimes('ISNA') 
times = PT.getTimes((now.year,now.month,now.day), (lat, long), 0,1) 

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()

strPlayAzaanMP3Command = 'omxplayer -o local /home/pi/Downloads/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()




26 comments:

  1. would you be able to help me set this up? I am confused on where i should put these files and how to add a job in crontab

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thanks so much! I will be integrating some of this code on my RPI and HomeAssistant to play Azan on my Google Home Mini and turn on SONOFF lights of certain portion of the house for several minutes

    ReplyDelete
  4. Hi,
    I am working on a similar project but not getting the correct timings from prayertimes.py
    I tried to play with the parameters but could not tune it exactly. We follow these timings in our mosque:

    https://www.gebetszeiten.de/Chemnitz/gebetszeiten-Chemnitz/169213-mwl07

    ReplyDelete
  5. Hello, I have no experience with programming. I will not go beyond the fourth step. Could someone help me with this? Thanks in advance. Burak

    ReplyDelete
  6. Check fiveprayers website for a free service to create prayer times display systems. All what you need is a TV screen (smart with browser) or with RP connected. No programming is needed at all. Again it is free (fiveprayers.org).

    ReplyDelete
  7. Hi Can someone help me develop it , I am willing to pay the service charge for the same. my email id islaminamdar@gmail.com

    ReplyDelete
  8. Asalaamu alikum all,

    this project helped us to work on our own project which also performs similar functionalities (currently Version 1.0) with latest QT and C++ functionalities. The codei is open source for all to benefit from and please feel free to contact us.

    https://github.com/aquibrash87/Takbeer

    ReplyDelete
  9. As-salamu alaykum,
    You can turn your Raspberry Pi into an azan clock by simply running smartazanclock.com on it in kiosk mode. Full instructions are here:
    https://smartazanclock.com/diy-azan-clock-raspberry-pi-instructions It lets you manage the settings using your phone on the same network so it can run "headless" without a monitor attached to it.

    ReplyDelete
  10. When I run 'python updateAzaanTimers.py', I get the error IOError: Please specify user or filename to write. Any idea what causes this?

    ReplyDelete
    Replies
    1. Hello there!

      I am just checking in to see if you have been able to solve this issue maybe? if so mind sharing wt u did please

      Delete
    2. I actually figured out what the issue is! You have to create a user in crontab and that should give you the permission to write to a file. See this: https://stackoverflow.com/questions/31172723/python-working-with-crontab

      Delete
  11. Get prayer times in dubai City. Calculate Islamic namaz timing in Dubai, UAE for Fajr, Dhuhr, Asr, Maghrib and Isha.

    ReplyDelete
  12. Get prayer times in New York City (NY). Calculate Islamic namaz timing in New York City (NY), United States for Fajr, Dhuhr, Asr, Maghrib and Isha.

    ReplyDelete
  13. Those Muslims who lives in Sydney, Australia and they want to know about Salat times have a look at Muslim prayer times Sydney. Namaz is the vital pillar of Islam.

    ReplyDelete
  14. try this simple azan for raspberry pi app: https://www.smartazan.com/how-azan-for-google-home-raspberry-pi

    ReplyDelete
  15. Hey There. I found your blog using msn. This is a very well written article. I'll make sure to bookmark it and come back to read more of your useful info. Thanks for the post. I will certainly return.

    ReplyDelete
  16. Thanks for the good writeup. It in reality was a amusement account it. Look complex to far introduced agreeable from you! By the way, how can we keep in touch?

    ReplyDelete
  17. the Cron Job keeps getting written over with the Medina azan so I cant customize the adahan selection

    ReplyDelete
  18. How to make this prayertime, to show on web

    ReplyDelete
  19. Thank you so much for sharing this. It works great!

    ReplyDelete
  20. Asalamu alikum, I am interested in this project to help my Muslim friend who wants to automate the Azaan on remote areas. I follow the instruction and the attachment as well but i am not able to run the crontab. I have errors on python like "raise IOError("Please specify user or filename to write.")
    OSError: Please specify user or filename to write"

    I guess the error comes from system_cron.write()

    Can you please help me?
    Thank you very much

    ReplyDelete
    Replies
    1. Thanks, I found the solution.

      Delete
  21. could you post that solution

    ReplyDelete
  22. omxplayer is no longer supported ... can you update your code for VLC ?

    ReplyDelete