Introduction: Ramadan Lights | Home Automation IOT
As Ramadan knocks on the door, cities worldwide light up and prepare for the month of fasting. Lights are hung in public squares and across city streets as well as on outer walls of houses lit during the nights until the sun comes up in the morning.
In this project, I would like to share my contribution, in which I'm automating the lighting up of these decorations based on Adhan times and adding this to my smart home system.
Step 1: Theory
Basically, it's easier and more applicable to automate the lighting based on sunlight intensity, for example. However, I would like this to have the spirit of the holy month that is, the lights will turn on as long as people are allowed to eat during the fasting month. In particular, lights are on starting from Maghrib Adhan until Fajr Adhan on each day without any human interaction.
Step 2: Design
Below I'll list out the requirements/capabilities of this project:
- The system should be able to automatically process on boot up.
- The system should be able to periodically execute the decision (ON/OFF) algorithm.
- The system should be able to call API on a daily bases.
- The system should be able to recover from unplanned restart.
- The system should execute the decision (ON/OFF) algorithm upon receiving data instantly.
- The system should provide a switch for turning on the autopilot mode (Automatic mode).
- The system should provide a switch to manually turning the lights ON/OFF.
- Triggering the manual switch should set the manual mode to OFF.
- When Automatic mode is ON, the result of the decision algorithm should be reflected on the manual switch ON/OFF.
Step 3: Technical Details: the API
To make the theory applicable, it is required to know the Adhan time on daily bases since its variable and act upon it to turn on/off the lights/decorations.
For this purpose, I decided to use a location-based API to serve the data (time for Adhan)
http://muslimsalat.com/api/#location
- This API provides a variety of options and configuration parameters described in the documentation link above.
- It is recommended to use your own API_key which is free upon registration.
- In my design, I'm interested in a daily timeline.
- Time returned is in 12 hr format (AM/PM).
- In terms of the location, I wished if this API could provide more accurate options for locations like coordinates geographic coordinates system (Latitude and Longitude). For this manner, this API can be provided with a city name for example and this is quite enough in most cases. Unfortunately, the results of my specific city were quite biased in multiple tests and there were a couple of minute of difference when comparing Adhan time between API results and the actual time of Adhan in my city. On the other hand, the results were perfectly matched when I provided a close city to the north and I sticked to it!
- The API url I'm using is:
https://muslimsalat.com/.json?key=
Step 4: Technical Details: Hardware
For simplicity, I'm leaving the hardware out of the process of syncing with the API and the time calculation by delegating these operations to the Home Automation System of mine. This will give me more control if I want to tweak the API source and the algorithm of calculation.
Having that said, I used an ESP8266 (NodeMCU) to connect to the local network via WIFI and a relay module as an actuator. The NodeMCU will use MQTT protocol and listen for a specific topic to get commands to turn on/off the relay. As simple as that!
Step 5: Technical Details: Connecting the Dots | Home Automation
My RaspberryPi holds a nodered instance which is capable to handle all that I need for this project. It also has an MQTT server installed to publish our messages.
At first, I thought that I can fetch the API data on daily bases at 9AM and extract both Fajr and Mahgrib Adhan times and keep comparing the current time with these variables that is:
each 30 sec: if: Mahgrib < NOW < Fajr true | turn on the lights | publish an ON message to the specified MQTT topic else: turn off
For comparing time, I am converting hours provided by the API from hh:mm(AM/PM) to full date formate by setting the date portion to a negligible date in history e.g (1/1/1970) since we are comparing the time only to the current time (date portion is also converted).
Unfortunately, this will only work in straight forward cases. Suppose the case when electrical power goes down or the system restarts for an unplanned reason. If this case occurs before 12AM then we're still in the safe side, but after 12AM this will fail our simple algorithm.
To give an example, let's have Magrib Adhan at 7:30 PM and Fajr at 4:10 AM. When the system boots up it will send a new request to fetch the data mentioned earlier. If the time now is 1:45AM we expect our algorithm to return true, but in fact it won't because 1:45AM is less than Fajr (4:10AM) but is not greater that Magrib (7:30PM). This is because we unite the date between all variables. Below is the final version of the algorithm (so far):
each 30 sec: if ( (magrib.getHours() >=12 && fajr.getHours() <=12 ) || fajr < maghrib) { fajr.setDate(fajr.getDate()+1); // handle spanning days endTime if (now.getHours() <=12 ) { now.setDate(now.getDate()+1); // handle spanning days currentTime } } // below is the previous code block if: Mahgrib < NOW < Fajr true | turn on the lights | publish an ON message to the specified MQTT topic else: false | turn off
The complete flow of the process is annotated in the above image.
In terms of UI, I added 2 switches to the UI/interface:
- A switch to turn this process Automatic or Manual.
- A switch to turn ON/Off the lights manually.
Step 6: Wrapping Up..
Time flies never to be recalled. Doing such simple 1-day projects allows you to catch a breath in this fast lane and help you revive your soft skills over time.
I tried as much in this project to keep it simple and reusable across the globe.
Kindly Vote for this instructable if you think this project deserves.
Happy Ramadan!