Home Alarm System With Raspberry Pi

Contents hide
4 2.2 Hardware Components

1.1 Introduction

This project is envisioned as an open-source home alarm system. Comprising a Raspberry Pi, an RF receiver, and multiple RF transmitters, it aims to keep homeowners informed with audible alerts and push notifications on registered Android devices. The system will operate under the GNU GPL, ensuring its complete openness.

Home insecurity is a pervasive issue in contemporary life. If a criminal gains unauthorized access to a dwelling, a well-installed alarm system can significantly dissuade them from progressing with the illegal activity. It is essential to notify the homeowner promptly when an attempted break-in occurs, enabling them to rapidly assess the situation and respond accordingly.

Some statistics:

The information that has been attained shows that in 2013 only there were estimated to be 8,632,512 property related crimes in the United States.

Out of these, the estimated rate for a property crime in the same year was 2,730.

For the year 2013, this type of crime ranked highest among all property crimes, contributing 69% of the total incidents of larceny-theft. It stood at 3 percent, and motor vehicle theft stood at 8.

The property crimes in 2013 led to a loss that was estimated to amount to $16.

1.2 Overview

Figure 1.1: Simple System Diagram

The devices will employ PubNub to exchange messages. Thanks to this, the devices only require the PubNub application’s publisher and subscriber keys. Consequently, the user won’t have to handle any network-side configurations, such as assigning a static IP address in the router’s DHCP or setting up port forwarding. PubNub also offers encrypted messages, which means the devices’ security will not be a concern. The Android client will be able to manage the alarm system, which includes things like locking, unlocking, and modifying certain settings, from anywhere with an active internet connection.

1.3 Hardware

Raspberry Pi (model B)

The RPi model B boasts a 700 MHz single-core processor with ARMv6 architecture, coupled with 512 MB of RAM. Notably, it consumes a mere 3.5 watts of power, stores data on SD cards, and offers easy video output options via RCA or HDMI (the latter being the preferred choice). The Raspberry Pi can run a variety of operating systems, including popular Linux distributions such as Raspbian, Debian, and Fedora, as well as RISC, FreeBSD, and NetBSD.

The RPi shines due to its cheap cost and large community, but the best feature it has to offer is its GPIO (General Purpose Input/Output). It opens a whole new world of opportunities to DIY hobbyists. Some of the many projects out there include home automation, home media centres, personal servers (HTTP/FTP/SMTP), cloud storage, phones, time-lapse cameras, jukeboxes, karaoke machines, 3D printers, drones, and robotics.

Figure 1.2: Raspberry Pi Model B

Arduino Mini

Its primary purpose in this project will be to program the ATtiny85 microcontrollers. The Arduino Mini could be used for the same purpose as the ATtiny85’s, but it turns out that these are much cheaper in comparison.

Figure 1.3: Arduino Mini

ATtiny85

433MHz RF

Figure 1.5: 433 MHz receiver (left) and transmitter (right)

1.4   Tools

Arch Linux

LXDE

Qt4

Qt Creator

433Utils

433Utils is an API that should make it easier for a developer when using 433 MHz RF transmitters and receivers with Arduino boards and Raspberry Pi.

PubNub

Arduino IDE

System Integration and Modeling

2.1   System Overview

The system setup is quite straightforward, despite having only three key components: a Raspberry Pi, a 433 MHz RF receiver, and a 433 MHz RF transmitter linked to an ATtiny85. The Raspberry Pi is connected to the receiver on the left, which receives signals transmitted by any 433 MHz RF device, such as a periodic heartbeat signal. All communication between hardware components is managed by software executed on the Raspberry Pi, utilizing GPIO functions.

Here, the Raspberry Pi (RPi) is the core component that takes center stage due to its compact size and low cost, yet impressive processing capabilities, including a 700 MHz processor and GPIO functionality, which enable it to multitask efficiently. The RPi will be responsible for managing all RF communications by processing the input received from the RF receiver as a sequence of binary bits using the 433Utils library. A user-friendly graphical interface (GUI) is also integrated, featuring widgets for user input and output that can execute commands and display visual responses. In addition, the system parallelizes local TCP server and PubNub relay operations, allowing it to receive commands from Android devices and transmit messages to those devices in real-time whenever an alarm event occurs, such as tripping, locking, or unlocking.

The primary function of the Arduino Mini in this project is to act as a programmer for our ATtiny85 microcontrollers. However, in instances where the Raspberry Pi requires extra processing capacity, the Arduino Mini could potentially be utilized to handle RF transmission tasks.

2.2 Hardware Components

In Chapter 1.3, readers are introduced to the crucial hardware components utilized in this project, featuring detailed specifications, cost information, typical applications, and explanations of why each piece of hardware is essential for the project’s overall functionality.

Figure 2.1: Hardware Setup (RPi top, RF receiver left, RF transmitter with ATtiny85 right)

The Raspberry Pi acts as a power source for the RF receiver, providing it with 5 volts through the GPIO pins. The two devices communicate with each other through a single GPIO pin. In contrast, the RF transmitter and ATtiny85 require an external power source, which for this setup is a 5-volt power supply connected to the breadboard. Data exchange between the microcontroller and the RF transmitter also takes place via a single GPIO pin.

Figure 2.2: RPi Hardware Connections
Figure 2.3: RF Transmitter Connections

2.3   Software Design Overview

Timeline explained:

Figure 2.4: Estimated Timeline of the Project

Components:

The alarm system is comprised of multiple software components, each contributing significantly to the overall functionality and equally crucial for its proper operation.

A Node.js relay server will be responsible for processing and managing PubNub messages. Thanks to Node.js’s simplicity in handling websockets and APIs that utilize websockets, setting up the PubNub server is a relatively straightforward process. We opted to establish the PubNub server in this manner due to the existence of a C++ library required by the API’s published version, which is plagued by problems with compilation, setup, and lacks adequate documentation, making it a less desirable option for this project.

A secondary Node.js server, operating concurrently with the PubNub relay, will facilitate a local connection with the Alarm System via TCP protocol. This server will function as a gateway, transmitting messages to and from the PubNub relay, enabling seamless data exchange between the two.

A specific component of the system is responsible for processing the data captured by the RF receiver connected to the Raspberry Pi. The bulk of this processing is enabled by the 433Utils library, which is available for both Raspberry Pi and Arduino platforms. This library’s API is utilized by the handler to continually read the input provided by the RF modules, allowing for efficient and reliable data processing.

In this instance, the Graphical User Interface (GUI) plays a crucial role in handling user input and visual feedback. The interface will feature a basic design, including a PIN pad akin to those found in traditional alarm systems, and a status bar that displays the current system state (locked, unlocked, or tripped). To ensure maintainable, efficient code, the GUI will be built using the Model-View-Controller (MVC) design pattern.

The system logic represents the backbone of the project, taking up a significant proportion of the overall effort. It serves as the glue that connects the graphical user interface, remote communication capabilities, and RF handling functionalities. The logic is accountable for managing commands issued via the GUI, authenticating credentials, parsing remote commands, and, if feasible, incorporating additional features such as system logging and zone management.

Useful Tools:

The UI designer is user-friendly, featuring a wide range of Qt toolkit widgets available for the user’s specific version (Qt4 or Qt5). Creating a UI file is a straightforward process that can be easily imported into C++ code with the IDE automatically generating code for the user upon UI creation.

Qt Creator provides flexibility in creating compiler profiles, enabling us to pre-compile our program for the specific Armv6 architecture used by the Raspberry Pi or other supported architectures, if necessary. Additionally, the IDE features a built-in debugger that offers advanced visualization capabilities, allowing users to examine memory, step into and out of methods, and execute code line by line for thorough debugging.

With Valgrind seamlessly integrated into the development environment, developers can leverage this powerful tool to detect and pinpoint memory misuse (memory leaks) in C or C++ code. By compiling code with debug flags, developers can enable Valgrind to efficiently identify the root cause of memory-related issues.

Figure 2.5: Qt Creator’s UI Designer

2.4   Agile Methodology

Our project employs the Agile methodology, which offers adaptability and direction throughout the product’s lifecycle. Unlike traditional development approaches, Agile enables flexibility in planning and optimizing code through iterations rather than committing to a product before coding begins. This flexibility minimizes the risk of project collapse due to miscalculations or unchecked requirements, unlike the waterfall method, which relies on meticulous planning and identification of requirements before coding commences.

Iterations facilitate the evolution of a product, rather than simply completing the initial project scope. Each iteration enables developers to assess the current product state and plan necessary changes, fixes, and improvements to enhance the next iteration, as the development process is simplified by avoiding the need to envision the final product from the outset.

Design and Testing / User Interface

3.1 User Interface

Explanation

The user interface constitutes a crucial component of this project. An alarm system must have a means of interaction, allowing the user or home owner to engage with the system. In this particular case, we won’t be utilizing a membrane keypad accompanied by a simple LCD display. Instead, a robust graphical user interface (GUI) will be developed to provide an exceptional user experience that can be expanded upon through future updates. Our ultimate goal is to eventually (depending on time constraints) create a comprehensive GUI with settings and options, proportionate to a medium-sized touchscreen.

Why The UI Works

The user interface follows an intuitive conceptual model built from the following:

Visual Affordance

To provide visual cues, it is essential that the perceived and actual fundamental properties of the user interface (UI) elements align with how they can be used. In our case, this involves mirroring the UI elements after typical objects the user is familiar with, such as a standard alarm system.

Transfer Effects

The transfer effects are straightforward. A traditional alarm system typically features a numerical keypad and additional buttons that offer specific functionalities. Our UI deliberately emulates this layout, aiming to provide an instinctive and user-friendly experience that is easy to navigate.

Casuality

The concept of causality can be described as a representation of the outcomes that result from an action or event. In the context of our UI, every button is designed to provide a visual cue when pressed, accompanied by a distinctive beeping sound, ensuring the user is aware of the action’s consequences. The top panel serves as a status indicator, displaying the current state of the alarm system. Consequently, following any user-initiated action, an update message reflecting the system’s new status will be displayed.

Visible Constraints

The UI’s straightforward layout restricts the user to a limited range of actions, primarily consisting of button presses, without providing additional functionality or options.

Mapping

The UI layout cleverly leverages physical representations to create an intuitive and natural fit. The left side is designed to resemble a traditional numerical keypad, while the right side features a collection of self-explanatory buttons. By combining these button groups, we achieve a straightforward and user-friendly layout that feels organically intuitive.

Figure 3.1: A Traditional Home Alarm System

This project’s user interface is built using the Qt 4 toolkit, which is fully supported by the Raspberry Pi. Qt 4 was chosen primarily for personal preference, as well as its comprehensive software support for GUI creation and utilization. Additionally, Qt 4’s widget support for Cascading Style Sheets (CSS) enables the creation of visually appealing GUIs with ease. The CSS support adds significant flexibility, allowing us to avoid hardcoding visual changes, such as modifying widget aesthetics or formatting, which in turn simplifies the development process and ensures a uniform look and feel across the entire program.

The intended use for this UI is for the user to be able to push buttons according to whatever action they want to take. They shouldn’t need to look at many instructions to get the system up and going. After all, keeping it simple is usually the better choice. As a user, I wouldn’t want to see cryptic messages, or buttons that mean absolutely nothing.

Figure 3.2: The First Iteration of the GUI

3.2 Design

Logic of the System

The alarm system design aims to be as straightforward as possible. A logical sequence of events is outlined in Figure 3.3. The Node.js server hosts PubNub logic and a TCP server, which waits for local connections from the alarm system. These components operate independently, employing multi-threading. The alarm’s logic can be updated through various means, including RF switch triggers, GUI updates (e.g., user disabling the alarm using a pin number), or remote messages. When the alarm is activated, the system sounds the alarm, updates the GUI, and sends a notification to remote clients via PubNub. Once the user disarms the alarm, the model is updated, and the alarm is silenced.

Figure 3.3: Flowchart of System’s Logic

3.3 Testing both User Interface and System

Testing Overview

To ensure the system’s integrity and functionality, a comprehensive testing approach will be employed. The GUI will be thoroughly evaluated for various input scenarios, including button presses, to detect any potential errors or anomalies. RF transmissions will be scrutinized for correctness and fault tolerance. Additionally, the alarm system will be running continuously for an extended period to monitor system usage and identify any performance issues. Furthermore, the Valgrind tool will be utilized to detect memory leaks. By combining these diverse tests, it is anticipated that the alarm system’s proper functioning will be conclusively demonstrated.

Since the GUI has a straightforward structure, there isn’t a lot to test for. The GUI testing consisted of pressing buttons repeatedly to identify any unwanted behavior. The buttons will restrict the user’s actions, which is actually preferable to avoid any potential issues that could cause the system to malfunction.

The Tests

Turning off and on a transmitter

To validate the transmitters and receivers’ functionality and reliability, we will evaluate the input signal over time and intermittently test the transmitters’ on and off status. This comprehensive testing approach will ensure the reliable transmission and reception of signals between devices.

Uptime test

In order to thoroughly test the program, we decided to leave it running for extended periods of time, multiple times, in order to simulate various scenarios. During these tests, we monitored the system for potential issues such as false alarm triggers, memory leaks, and random crashes.

Testing CPU priorities

To ensure the program’s performance under various conditions, we tested it at different task priority levels, including the default setting. Although the Raspberry Pi would only be running the alarm system alongside other OS/user processes, we opted to assign the alarm system a higher priority to minimize the risk of missing RF messages. To achieve this, we utilized the “nice” command with the “-10” flag, which assigns a priority level above the default (max priority “-20”, lowest priority “19”).

DESIGN AND TESTING / USER INTERFACE

Verify Integrity of RF Transmissions

Before incorporating the RF transmitters and receivers into the project, we tested them separately to ensure their functionality. After programming the Attiny85’s with the transmission code, we ran an RF sniffer independently to verify that the messages being transmitted were error-free. A unique identifier was assigned to each Attiny85, such as 1000 or 0111, to enable easy identification.

Valgrind

3.4 Agile Management

Setup And User Manual

4.1 Introduction

This guide provides instructions on how to set up the alarm system using a Raspberry Pi.
You will need:

  • Technical skills
  • Patience
  • A Raspberry Pi 2 B (preferably), or Raspberry Pi B/B+
  • An SD card for your OS
  • Some kind of way of connecting your SD card to a computer
  • An Arduino Uno/Mini
  • PiTFT 3.5” Touchscreen for Model B or B+ (Model B works with B+ and RPi2)
  • An Internet connection

This tutorial will take you through the process of installing an operating system, making necessary configuration changes, connecting to a WiFi network, downloading the required software, compiling the code, and ultimately, running the project.

4.2 Installing The OS

Get Raspbian

Raspbian is the version of the Debian Linux for Raspberry Pi these features are developed for a microcomputer named Raspberry Pi. Download at: http://www.raspbian.org/RaspbianImages

Flashing image file to SD card

For Windows Systems:

To prepare the SD card, visit http://sourceforge.net/projects/win32diskimager/ and download the Win32 Disk Imager. Once downloaded, use the intuitive GUI to write the Raspbian image to the SD card.

For Linux Systems:

Use the dd tool from the command line to flash the image, like in this example: dd if=/path/to/image.img of=/dev/sdx bs=1M where if is the image file, of is the path to the SD card (usually sdb, depending on how many other devices have been mounted prior).

4.3 Post OS Install

Raspbian Settings To Change

The first thing to do right after installation is boot the system, then log in with the default Raspbian credentials.
Run the command sudo raspi-config then this menu will show:
1. Select Expand Filesystem.
2. Select Change User Password and change the default password to something else
3. Select Enable Boot to Desktop/Scratch and then select Desktop Log in as user ‘pi’ at the graphical desktop. This will enable the Raspbian OS to automatically log in as the user selected.

4. The Raspbian image, by default, will have a UK keyboard layout. Select Internationalization Options, then Change Keyboard Layout, and follow the options to choose the desired layout.

5. Once done making all the required/desired changes. Select.

6. Enter sudo reboot to restart the Raspberry Pi (required for some changes).

Connecting To A WiFi Network

If the Raspberry Pi will be connected through Ethernet, then these steps can be disregarded.

1. Access the WiFi Configuration tool from the main menu.

2. Hit the Scan button to bring up a list of available networks.

3. Double click the desired network.

4. Click Connect.

Grabbing The Needed Software

There are several programs and libraries we will need to download and install to our OS before we can do anything:

1. Open a terminal.

2. Update the repository versions with sudo apt-get update.

3. Upgrade the system with sudo apt-get upgrade.

4. Install the Qt4 compiler with sudo apt-get -y install qt4-qmake. 5. Install WiringPi to the system:

  • Clone the WiringPi repository git clone git://git.drogon.net/wiringPi.
  • Enter cd wiringPi to go into the directory of the cloned repo.
  • Enter sudo./build to build and install the library to your system.
  • Enter cd to go back into your home directory.

Prep The PiTFT 3.5in

If you choose not to enable SSH, you can skip this step for now. Setting up the PiTFT Touchscreen is a straightforward process, thanks to the provided scripts that eliminate the need for manual intervention. For guided instructions on getting the screen up and running, please visit https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi.

4.4 Setting Up The Alarm System

Setup The Code

1. Download the repository with git clone https://github.com/gaperezcortes/CSCE470.git.
2. Put your PIN in the code (no other way at the moment)
3. To setup remote access, set up the PubNub keys:
Open the Server.js file in the nodejs folder with the default text editor, or any other, and replace
the following your PubNub keys:
var pubnub = PUBNUB.init({
publish_key : “pub-x-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,
subscribe_key : “sub-x-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,
ssl: true
});
4. Compile the code:

  • Open a terminal
  • Enter cd CSCE470 to go into the directory of the cloned project repository
  • Enter make to compile all the code
  • Go grab a cup of tea (or whatever beverage)
  • Program is ready to be launched
  • If desired, run the program (will use sudo since RF needs SU privileges to work) by entering ./launcher into the terminal

Autostart Program

To ensure seamless functionality, the alarm system should automatically boot once the X server and desktop environment are operational. This approach eliminates the need for manual startup, which would render the system useless in cases where the Raspberry Pi restarts unexpectedly due to power outages, manual reboots, or system crashes.
To enable the alarm system to autostart:
1. Add the launcher as a desktop shortcut to the autostart folder (~/.config/autostart)
2. Ensure that the RF receiver data pin is hooked up to the GPIO 27 also known as PIN 13 (RPi A,
B, B+, RPi2)
3. Reboot the system to verify alarm system boots on startup

4.5 Using The Alarm System

Currently, the alarm system exists in its most fundamental form. For now, it only enables or disables the alarm, with additional features planned for future implementation.

Arming The System

Press the Arm button.
Note: that once the system is armed, nothing can be done with it until it is disarmed.

Disabling Armed/Tripped Alarm

To access the system, press the buttons corresponding to the correct PIN, followed by the “#” key. If you enter an incorrect PIN, press the “#” key again to clear the buffer, ensuring that no action is taken without the correct authentication.

Concluding The Project

5.1 Summary And Reasons

Quick Summary

This bespoke alarm system leverages the versatile Raspberry Pi 2, empowering it with adaptability and energy efficiency. Its architecture is designed to accommodate future enhancements, enabling seamless upgrades and modifications, including alterations to the user interface for optimal usability.

Why C++ and not Python?

Initially, the project was slated to utilize Python 3, driven by personal preference. As I was already proficient in both Python 2.7 and 3, the extensive libraries available, both native and contributed by the developer community, would have facilitated the implementation of essential functions (such as sound and socket programming) with relative ease, as opposed to C++. However, C++ proved to be an absolute requirement due to the presence of a crucial library for RF communication written in the language. Although this library offered a Python wrapper, the class suffered from bugs and broken functions, necessitating an alternative approach. Despite C++ not being my forte, tackling this project compelled me to expand my knowledge and hone my skills, ultimately yielding a valuable learning experience.

5.2 Application To Field

The primary objective of this project is not to revolutionize the software landscape, as there is no associated research endeavors. Nevertheless, the completion of the alarm system’s fundamental functionalities presents an excellent opportunity for users to leverage the provided code as a learning tool or example project for exploring the Qt4 toolkit. This can serve as a launching pad for personal growth, enabling users to develop familiarity with external toolkits like Microsoft Visual C#/C++ and Java Swing/FX. Moreover, this project aims to introduce Raspberry Pi enthusiasts to the capabilities of the compact, credit-card-sized computer and its GPIOs, empowering them to integrate external hardware. With the open-source nature of the project and the repository’s availability to all, users are encouraged to take ownership of their own projects, building upon the provided foundation.

5.3 Alternate Paths, Future Development, And Final Thoughts

Things I would have done differently

Currently, the project relies on the Raspberry Pi’s RF communication capabilities. With the Raspberry Pi 2 boasting enhanced processing power, this is not a concern. Conversely, the initial Raspberry Pi 1 iteration’s limited computational resources result in the RF sniffer’s significant processing demands, often yielding errors within the system. To address this limitation, implementing an Arduino-based RF communication handling approach would liberate considerable computational resources, enabling the Raspberry Pi 1 to execute the alarm system without hindrance.

Future Additions

These are things that I would implement later on, after the semester ends:

  • Add system logging to keep track of any events that happened throught time
  • Parameterizable features like declaring zones for transmitters
  • Update the User Interface to allow for more implemented features
  • Use an SMTP server to send text messages to a phone number in case of alarm trip

Final Thoughts

The experience of developing the Raspberry Pi Home Alarm System project was profoundly enriching. It afforded me the opportunity to put my skills to the test, utilizing the knowledge acquired throughout my computer science education as well as skills developed independently, to demonstrate my capabilities as a software developer.

Appendix A

UML

Source: Home Alarm System With Raspberry Pi


About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Scroll to Top