How to build an office alarm system with Home IoT devices
Tags: ruby, python, api, iot
Published: 2020-10-14 15:11:17

## Building a security system with alarm and notifications using Home IoT hardware

Office security is a 100 billion dollar business. Trustworthy products and service providers come with a steep price tag. Specialised cloud-based solution providers come with the potential threat of misusing or selling your office’s IoT sensor data. Data privacy raises major concerns, since every supplier with access to data gets a full view of an office schedule—how employees behave and when the office is empty.

Hiring a security service company offers a good alternative to this. The security company could install a closed IIoT system within your company WiFi. But such services are very expensive.

A less pricey option is to use consumer market Home IoT devices and install it yourself. In this blog post, we show you how to make your own security system with just a bit of time and hacker spirit. Save a lot of money and no trust into an external security service company is required. We will show how to set up a text notification, play an alarm sound and change the color of a light to red when a motion sensor detects movement after 8pm.

![iiot office alarm system](https://dev-to-uploads.s3.amazonaws.com/i/k04w5zpbl5ceknt0qb0t.gif)

## Setting up the Home IoT motion sensor with lights

In Home IoT applications, motions sensors are often used to switch lights on only when they register movement within their range. In the right room this saves energy and money. The motion sensors keep associated lights switched off and switch them on only for a configurable timespan when they detect movement. If there is further movement around the sensor, the devices will remain switched on.

Home applications for this type of setup are often found in garages or driveways. Rooms in which people sit and stay usually don’t benefit. Also, pets can create unwanted side effects when they trigger motion sensors. In a small office, usage patterns are different to a private home. In an office before and after a workday, movement should trigger an alarm. Plus, it’s the kind of thing an office manager would like to know about.

For this post, we use the motion sensors of a global swedish furniture chain. Follow the basic setup as described by the vendor and our [initial post](https://dev.to/oliverxapixio/how-to-setup-a-local-api-for-home-iot-devices-in-your-office-using-wifi-3173). Screw open the back of the motion sensor device so you can see three buttons and put in batteries. Pair the motion sensor first with your hub device and then with the lights.

Go into the manufacturer's mobile app and test your setup. Use the app to name your light, e.g “room-a”.

## Creating a service to report events and trigger an alarm

To access and react to events sent by the motion sensor, find out the device’s communication protocol. If the device communicates using keep-alive connections, e.g. CoaP or MQTT, then observe it’s messages. Otherwise frequently request, or poll, the device’s state.

The hardware we use works best when it does not directly observe the motion sensor but observes the associated light instead. That too is handled by drivers in [IIoT Server](https://github.com/xapix-io/iiot_server). In Xapix IIoT Server drivers for this are included and you only need to configure the devices in the file `./config/devices.yml`. Additionally configure your speaker and put your desired alarm audio file, e.g. “alarm.wav”, into a new folder `./tmp/iiot_office/media/`.

```yaml
devices:
  home_office:
    main_tradfri_hub:
      model_class: TradfriHub
      mac: XX:XX:XX:XX:XX:XX
      devices:
        room_a_bulb:
          model_class: TradfriLedColorBulb
          name: room-a
    speaker:
      model_class: Audio
      installation_name: iiot_office
``` 

One option to trigger alarms and send text notifications is to create your own local web server application. The web application needs to have an HTTP POST endpoint accepting JSON formatted data. In the code for the endpoint,put the decision logic for the alarm and notifications. Enter the endpoint’s URL as an environment variable named `EVENT_WEBHOOK_URL` for IIoT server.

A second option is to use our free Xapix Community Edition and it’s web UI to trigger data pipelines which change the light color, play an alarm sound and send text notifications.

{% vimeo 468129090 %}

If interested, then [**> CONTINUE READING <**](https://xapix.io/post/how-to-build-an-office-alarm-system-with-home-iot-devices?utm_source=dev-to&utm_medium=editorial&utm_campaign=home_automation_3) the full version of this article on our Xapix blog.

## Join our Home IoT community

We are working on follow-ups to this blog series to inspire you with new ideas about how to use the [Xapix IIoT Server](https://github.com/xapix-io/iiot_server) once you have set it up. We would love to hear your ideas and collaborate!

Contact me on [Twitter](https://twitter.com/xapix_io/status/1316392832496791553) if you would like to discuss this tutorial or if you have questions or feedback about the Xapix Community Edition. We look forward to hearing from you!