Adding security features to the Smart Home application

We have already added the Philio multisensor to our system to read illumination and temperature values. Let's add a flood detector to the Aeon USB controller; and as you have in previous chapters, follow inclusion instructions from its manual. Then we can start the required implementation in the application.

Before proceeding to work with security sensors, we need to define sensor types and command classes to identify messages.

The following commands are used to report sensor changes:

Sensor type

Message sent

Report value

Binary sensor

COMMAND_CLASS_BINARY_SENSOR, 0x30

BINARY_REPORT, 0x03

Sensor alarm

COMMAND_CLASS_SENSOR_ALARM, 0x9C

SENSOR_ALARM_REPORT, 0x02

These constants have been defined in the message.h file, as shown here:

#define FloodSensorNodeID    0x05

enum BINARY_SENSOR_TYPE {
  GENERAL_PURPOSE = 0x01,
  WATER_DETECTION_SENSOR = 0x06,
  TAMPER_SENSOR = 0x08,
  DOOR_WINDOW_SENSOR = 0x0A,
  MOTION_DETECTION_SENSOR = 0x0C,
  GLASS_BREAK = 0x0D
};
/**
 * Sensor Alarm Types
 */
enum ALARM_TYPE {
  GENERAL_ALARM = 0x01,
  SMOKE_ALARM = 0x02,
  CARBON_MOXIDE_ALARM = 0x03,
  HEAT_ALARM = 0x04,
  FLOOD_ALARM = 0x05
};
/**
 * ALARM COMMANDS
 */
enum SENSOR_ALARM_COMMAND{
  SENSOR_ALARM_GET = 0x01,
  SENSOR_ALARM_REPORT = 0x02,
  SENSOR_ALARM_SUPPORTED_GET = 0x03,
  SENSOR_ALARM_SUPPORTED_REPORT = 0x04
};
………..
/*
 * ZWave Command Classes
 */
enum ZWAVE_COMMAND_CLASS {
  .
  COMMAND_CLASS_ALARM = 0x71,
  COMMAND_CLASS_SENSOR_ALARM = 0x9C,
  COMMAND_CLASS_SILENCE_ALARM = 0x71,
  ……
  COMMAND_CLASS_SENSOR_BINARY = 0x30,
  ……..
  COMMAND_CLASS_WAKE_UP = 0x84,
  …….
};

Motion detection

Motion sensors are binary sensors that report whether there is motion or not. We will make some additions to the message handler to identify the message from the motion sensor.

While we were dealing with the Z-Wave Temperature or Luminance sensor, we checked if the incoming message's command class is a multilevel sensor (0x31); this time we will check for the command class binary sensor (0x30) and check the sensor type. The following code needs to be added to the message handler to parse an incoming message from a motion sensor:

.. 
else if (command_class == COMMAND_CLASS_SENSOR_BINARY) {

      if (message[8] == BINARY_REPORT) {

        if (data_length == 0x04) {

          
if (message[10] == MOTION_DETECTION_SENSOR) {

            if (message[9] == ON) {
              printf("Motion DETECTED
");
            } else if (message[9] == OFF) {
              printf("NO Motion Detected
");
}}}}}

When a motion is detected, the motion sensor sends a message to the Z-Wave controller and our Smart Home application receives and parses the message to create the following output:

Received: 0x1, 0xa, 0x0, 0x4, 0x0, 0x2, 0x4, 0x30, 0x3, 0xff, 0xc,0x37, 
Response From Multi-Sensor Node Received: Motion DETECTED

Door/window sensor detection

Door/window sensors are binary sensors too. It reports when the door is opened, and so we will use the same command class as we did for the motion sensor. We have already added the sensor type in the previous section, and so we only need to add another case to control if the reporting binary sensor is a door/window sensor. Message handling for binary sensors will be as follows:

..
else if (command_class == COMMAND_CLASS_SENSOR_BINARY) {
      if (message[8] == BINARY_REPORT) {
        if (data_length == 0x04) {
          if (message[10] == DOOR_WINDOW_SENSOR) {
            if (message[9] == ON) {
              printf("Door/Window Sensor is OPEN
");
            } else if (message[9] == OFF) {
              printf("Door/Window Sensor is CLOSE
");
            }
          } else if (message[10] == MOTION_DETECTION_SENSOR) {
            if (message[9] == ON) {
              printf("Motion DETECTED
");
            } else if (message[9] == OFF) {
              printf("NO Motion Detected
");
            }
          } }}}
..

When a door/window sensor is opened, it will send a message to the Z-Wave controller. Our Smart Home application will parse the message and show the following output:

Received: 0x1, 0xa, 0x0, 0x4, 0x0, 0x2, 0x4, 0x30, 0x3, 0xff, 0xa, 0x31, 
Response From Multi-Sensor Node Received: Door/Window Sensor is OPEN

When the door/window sensor is closed, it will send a message indicating that the sensor status is closed.

Received: 0x1, 0xa, 0x0, 0x4, 0x0, 0x2, 0x4, 0x30, 0x3, 0x0, 0xa, 0xce, 
Response From Multi-Sensor Node Received: Door/Window Sensor is CLOSE

Flood detection

Let's add our new device to the application. The Everspring Flood detector has a long cable ending with two metal pins, as seen in the previous image. When those two pins come into contact with water, they raise an alarm.

After inclusion of the flood detector, it will be assigned node ID 0x05 by the USB controller. The flood detector will send a sensor alarm to the home automation system. The flood detection alarm uses the COMMAND_CLASS_SENSOR_ALARM Z-Wave command class.

An alarm command class, alarm commands, and sensor types have been defined. We can proceed to handle incoming alarm messages from the flood detector. We will only try to deal with the sensor alarm at this moment.

In order to handle the sensor alarm, we need to handle a new command class case. The following lines will be added to our message handler to identify the sensor alarm and to alert the user. We also added the smoke alarm case here:

else if (command_class == COMMAND_CLASS_SENSOR_ALARM) {
  printf("Sensor Alarm ");
  if (message[8] == SENSOR_ALARM_REPORT) {
    printf("Reported ");
    if (message[10] == FLOOD_ALARM) {
      if (message[11] == ON) {
        printf("FLOOD Detected
");
      } else if (message[11] == OFF) {
        printf("NO Flood Danger
");
      }
    } else if (message[10] == SMOKE_ALARM) {
      if (message[11] == ON) {
        printf("SMOKE Detected
");
      } else if (message[11] == OFF) {
        printf("NO Fire Danger
");        }}}}

Let's make a test with the flood detector. We first put the metal pins into a glass of water to see if we are able to receive alarms from the flood detector.

A sample output from the Smart Home application is as follows:

Received: 0x1, 0xd, 0x0, 0x4, 0x0, 0x5, 0x7, 0x9c, 0x2, 0x0, 0x5, 0xff, 0x0, 0x0, 0x90, 
Response From Flood Sensor Node Received: Sensor Alarm Reported FLOOD Detected

When we take the pins out of the glass, the flood detector reports no flood detected to the application.

Received: 0x1, 0xd, 0x0, 0x4, 0x0, 0x5, 0x7, 0x9c, 0x2, 0x0, 0x5, 0x0, 0x0, 0x0, 0x6f, 
Response From Flood Sensor Node Received: Sensor Alarm Reported NO Flood Danger

We have added new cases to handle reports from security sensors for the Z-Wave message handler. In the next session, we will make an overview of the system.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset