
Building an automatic prayer-time relay controller with a resilient RTC-driven scheduler
Challenge
A team needed a small, reliable embedded controller that automatically switches a relay - to trigger external equipment such as an amplifier or lighting circuit - at each of the day's five prayer times, without anyone needing to be present to press a button, using a schedule that can span months or years and can be updated in the field without reflashing firmware, while staying accurate through power loss and correctly handling the case where the device is powered off across one or more scheduled events instead of misbehaving when it comes back.
Solution
We designed a compact binary schedule format we called PRAY2: a 64-byte header - spanning a start date, day count, the calculation method used to generate the times, per-prayer default relay-on durations, and an optional one-shot flag - followed by a tightly packed table of five 16-bit minute-of-day values per day, enough to hold a multi-year prayer-time table in a few kilobytes, together with a header-only C parser that drops into the firmware unchanged. Updated schedule files reach the device over XMODEM and land in RAM/SD storage, validated - magic bytes, version, header size, and every offset and table size checked against the actual file length - before anything is trusted, so a truncated or corrupted transfer is rejected instead of read out of bounds. The scheduler itself runs on a 1Hz tick against a battery-backed MCP7940 real-time clock, driven through a proper Zephyr out-of-tree driver module with its own devicetree binding, alongside a matching SSD1306 OLED driver module for the on-device status display, walking today's five prayer times and firing the relay the moment the clock reaches the next one. If the device is powered down across a day boundary or misses one or more events - a dead battery, a firmware update, simply being off - the scheduler recomputes the day index and fires only the single earliest missed event instead of replaying every missed prayer back to back, so the relay never does something operators didn't design it to do just because the clock caught up. A manual button lets an operator trigger the relay directly with its own timeout, independent of the schedule, and the schedule file can optionally carry a one-shot timestamp that sets the RTC itself on first load, so commissioning a new unit in the field is one file transfer instead of a separate clock-setting step.
Results
The result is a relay controller that runs the day's five prayer-time triggers unattended, recovers sensibly from power loss instead of misfiring every missed event at once, and can have its entire multi-year schedule replaced over a simple serial file transfer with the file validated before any of it is trusted. Its RTC and display run on proper reusable Zephyr driver modules rather than inline bit-banged code, and the one-shot RTC-set flag in the schedule file means a freshly commissioned unit is ready to run correctly from its very first file load, with no separate time-setting step for an installer to forget.
Kick-start your project
Get in touch with our team to discuss your business needs and see how we can help with a project like this one.