Akshay Chauhan

@akschhn

Building a security alarm using raspberry pi

Internet of things is a buzzword nowadays and if you are one of those people who have been itching to get started with it but were unable to do so, this is an attempt to help you understand the different pieces involved in setting up an IoT system. I had an opportunity to work on an IoT based project at workplace and would like to share my learnings over here.

Resources used :

  1. Raspberry Pi Model B
  2. Grovepi Starter Kit
  3. IP camera
  4. Azure Service Bus
  5. Azure Mobile Services
  6. Azure Blob Storage
  7. Native Windows Mobile application

(I) In order to get started we booted our pi device with raspbian operating system which is a debian optimized for raspberry pi hardware(Installation).

Each pi device has a row of general purpose input output (GPIO) pins which acts as an interface between the pi and the outside world. We can connect sensors to these pins and program them according to our requirements. In model A and B of raspberry the gpio pins look like this :

You have two options to connect sensors to raspberry :

  1. Use breadboard, resistors, wires to form the required circuit.
  2. Use connectors to do the above job for you.

We went with the second option and used a device named grove pi (now upgraded to grovepi +) which is a modular board designed specifically for raspberry pi. With this you just need to plug the board on the pi and then connect the required sensors with the board. Installation steps of grove pi on raspberry device can be seen over here. Once this was done we were ready to start experimenting with the sensors.

(II) Use Case : Setting up a Security Monitor to detect burglary alarm and send notification to the user with the pictures of the event

Here is a breakdown of the tasks for our use case:

  1. Connecting sound sensor to pi device.
  2. Setting up azure cloud services.
  3. Script to read sound input from surrounding using sound sensor and send a message to a queue when the sound level crosses a particular threshold value (This will be the frequency value at which the burglary alarm works)
  4. Writing a consumer for the theft message sent.
  5. Showing notification with the burglary pics on the app which will be used by the end user.

Step 1: Connecting sound sensor to raspberry pi

(i) Grove Pi has separate slots for connecting analog and digital sensors. The analog sensor ports are labelled as A1 — An and digital ports are labelled as D1-Dm. Since sound sensor is an analog sensor we connected it to one of the analog ports.

(ii) In order to code on the raspberry pi we connected keyboard, mouse to the USB ports available on it. Also HDMI port was connected to the LCD monitor and LAN cable was connected to the LAN port. Once this was done our setup looked like this :

Raspberry pi + grove pi + sensors setup

Step 2: Setup Azure cloud services

We needed to setup following cloud services on azure:

  1. Azure Blob Storage: A blob can be any type of text or binary data, such as a document, media file, or application installer.
  2. Azure Service Bus Queue: Service bus queue supports brokered messaging model. When using service bus queue, components of distributed application do not interact directly they rather exchange messages via the queue which acts as intermediary (Reference)
Azure Service bus queue architecture

3. Azure Mobile service (now mobile app service) : This is a mobile backend as a service which provides cloud storage, authentication and push notification services. It is based on RESTful programming interface to perform CRUD operations (using HTTP get/put/post/delete methods). We used mobile service for cloud storage and to provide push notification service in our mobile application.

Our IOT system architecture

Step 3: Script for sound sensor

This step is further divided into following sub-steps:

  1. Code to read sound level from the external environment.
  2. Whenever the sound level reaches beyond the threshold sound value (frequency at which burglary alarm works) we need to :
(i) Connect to the IP camera at our place and save the current frame image on raspberry.
(ii) Upload the saved image on azure blob storage.
(iii) Send a message on azure service bus queue with the image name and time-stamp.

Step 4: Creating consumer for the service bus queue

We decided to use azure mobile service for this purpose as it provided us a feature of scheduler where we can schedule a task to be run continuously after some time interval. In our case we wanted our consumer to do 2 things:

  1. Read the image name from the message sent in the queue.
  2. Store the image name and time-stamp in a table storage.

Step 5: Create a mobile application

We needed a mobile application to do following things:

  1. Show instant notification whenever a theft alarm goes off
  2. Show the theft picture on click of the notification.
  3. Show the past history of theft pics.

Our mobile application was connected with the azure mobile service to use it’s push notification feature and in order to get the names of the theft pictures from the cloud storage. Corresponding to each theft pic name in the table storage there was a picture stored in blob storage on azure from where the application finally fetched the image to display it to the user.

Once this was done we were able to connect the different pieces required in setting up our security system with a raspberry pi and basic sensor kit.We did this work about an year ago, now most of the cloud services like azure and amazon provides IoT frameworks which internally has the above mentioned services packaged together.

Thanks for taking the time to view this post, I hope it helped you in understanding the different components involved in setting up an IoT system.

Topics of interest

More Related Stories