A Raspberry Pi-based alarm system that sends text notifications in the event of commercial power failure.
- Detects power failures and sends notifications via SMS.
- Designed to work with off-the-shelf hardware components.
The following hardware components are required to build the system (all are available on Amazon):
- Raspberry Pi (I used 4B)
- GeeekPi Cooling Fan
- LED and 330Ω resistor
- UPS Hat (from SunFounder)
- Waveshare 4G Hat (based on SIM7600E-H)
- 4G SIM Card
- 3D Printed Case (optional)
Follow these steps to set up the RaspPI-Power-Alarm system:
-
Assemble the hardware components:
-
Attach the UPS Hat below the Raspberry Pi. Make sure to connect all the cables properly.
-
Connect the cooling fan to the GPIO pins 1 & 9 (for 3.3V) or 4 & 6 (for 5V fan) as shown in the image.
-
Connect a Power LED to RaspPi as shown here. It is useful to have a power LED to know the status of the RaspPi while running.
-
-
Insert the prepaid 4G SIM card into the Waveshare 4G Hat.
-
Assemble the Waveshare 4G Hat on top of RaspPi as shown below.
-
Install the necessary software (instructions provided below).
- Install the Raspberry Pi OS on your Raspberry Pi.
- Clone this repository:
git clone https://github.com/your-username/RaspPI-Power-Alarm.git cd RaspPI-Power-Alarm
- The script already contains information necessary to identify the battery status of the UPS as well as for the functioning of the 4G hat. Use any of the script editors to edit.
- Make sure to enter the phone numbers in the line:
# List of phone numbers
recipient_numbers = ["<your_phone_number_1>", "<your_phone_number_2>"]- To have custom messages depending on the power status of the device, edit the message in the power detection section:
# Power detection
if GPIO.input(17): # if port 17 == 1 (power is detected)
if power_failure_start_time is not None: # Power was previously off
power_restored_time = datetime.now().strftime("%d-%m-%Y %H:%M:%S")
duration_of_failure = datetime.now() - power_failure_start_time
message = (f"Commercial power has been restored at {power_restored_time}.\n"
f"Power was OFF for {duration_of_failure}.\n"
f"Power failure started at {power_failure_start_time.strftime('%d-%m-%Y %H:%M:%S')}")
send_sms(recipient_numbers, message) # Replace with your phone numbers
power_failure_start_time = None # Reset after sending SMS
elif init == 1: # Cold boot or after power failure
message = "Power is ON after reboot from extended power failure or device reset."
send_sms(recipient_numbers, message) # Replace with your phone numbers
init = 0 # Set init to 0 after first SMS is sent
while GPIO.input(17): # Do nothing if power stays on
time.sleep(0.1) # Use time.sleep() here
else: # Power is off
if power_failure_start_time is None: # Power just went off
power_failure_start_time = datetime.now()
failure_time_str = power_failure_start_time.strftime("%d-%m-%Y %H:%M:%S")
message = f"Commercial power supply in Somex Lab went OFF at: {failure_time_str}."
send_sms(recipient_numbers, message) # Replace with your phone numbers
while GPIO.input(17) == 0: # Do nothing while power is off
time.sleep(0.1) # Use time.sleep() here- Next, run the script and you are done!
python3 powercheck.py- Power on the Raspberry Pi.
- Monitor the system to ensure proper functioning.
- In the event of a power failure, the system will send an SMS notification to the configured phone number.
Contributions are welcome! Please fork this repository and submit a pull request with your changes.
This project is licensed under the CCZero License. See the LICENSE file for more details.


