Adding new use cases

We have finished working with devices for home automation. We covered almost all the common devices used for home automation. As we have completed adding a camera, we have successfully achieved the state where we can monitor a home.

Now we will proceed to make a few last additions to the Smart Home application such as adding some rules; this includes completing some actions when a certain device value is changed,. For example, when motion is detected, we will make the Smart Home application perform a capture.

Let's first include the camera module in the Smart Home application. We have added the camera.c and camera.h files to manage a network camera and also changed the Makefile.am file to link OpenCV libraries for the build process. The following header file can be used within the application:

#ifndef CAMERA_H_
#define CAMERA_H_
#define IP_CAM "http://admin:[email protected]:80/mjpeg.cgi?user=admin&password=1 23&channel=0&.mjpg"
#define USBCAM 0
/**
 * Capture Frame from Network Camera if defined else from USB Camera
 * @param void
 * @return void
 */
void capture_frame(void);
/**
 * Record Video from Network Camera if defined else from USB Camera
 * @param int duration in seconds
 * @return void
 */
void record_video(int sec);

#endif /* CAMERA_H_ */

We made a small change in our code as previously shown in the sample camera applications. We used a predecessor to check whether we have defined the IP camera video URL for capture, for conditional compilation of the code. If we don't define it, the compiler will automatically build the application for a USB camera. After we add the predecessor, the capture_frame function looks like this:

void capture_frame() {
  //Get Capture Device assign to a memory location
#ifdef IP_CAM
  CvCapture *pCapturedImage = cvCaptureFromFile(IP_CAM);
#else
  CvCapture *pCapturedImage = cvCreateCameraCapture(USBCAM);
#endif
  //Frame to Save
  IplImage *pSaveImg = cvQueryFrame(pCapturedImage);
  //Get TimeStamp
  //Get Current Time
  time_t now;
  struct tm *tm;
  now = time(0);
  if ((tm = localtime(&now)) == NULL) {
    printf("Can't Get Time
");
    return;
  }
  char buf[256];
  printf("Capturing....
");
  sprintf(buf, "capture%d_%d_%d_%d_%d_%d.jpg", tm->tm_year, tm- >tm_mon,
      tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  //Save Image to Filesystem
  cvSaveImage(buf, pSaveImg, 0);
  printf("Saved....
");
  return;
}

As we set the device control in the capture_frame function, we also implemented the same setting in the record_video function.

Let's proceed to add some conditions to our application; let it capture frames if motion is detected or the door sensor is opened.

Adding new rules

While developing the Smart Home application, we added all the devices connected to Intel Galileo. In order to handle periodic updates from devices, we created a thread to query the devices, get the latest updates from them and, according to the updated value, take the necessary emergency action. We called this module device and added device.c and device.h files. The device.h file includes the defined macros, stores the latest status of the devices, and records it to a XML file for external access to device statuses.

#ifndef DEVICE_H_
#define DEVICE_H_
#include "message_queue.h"
/**
 * Transmit Option for ZWave Controller
 */
#define TRANSMIT_OPTION      0x25  //Aeon USB Stick
/**
 * Staus Variable Buffers
 */
#define BUFFER_MAX         256
#define FILE_LINE        1024
/**
 * Define Update Frequency
 */
#define UPDATE_FREQUENCY    2   //Minutes
/**
 * Device Status XML Filename
 */
#define XML_FILE_NAME    "/home/root/smarthome/home.xml"
#define JSON_FILE_NAME    "/home/root/smarthome/home.json"
/**
 * Constant Strings
 */
#define status_sleeping      "Sleeping"
#define status_active        "Active"
#define device_on          "ON"
#define device_off        "OFF"
#define detected          "DETECTED"
#define not_detected        "NOT DETECTED"
#define door_open          "OPEN"
#define door_closed        "CLOSED"
/**
 * Defined Nodes
 */
#define NumberofNodes       5
#define ControllerNodeID     0x01
#define MultiSensorNodeID     0x02
#define WallPlugNodeID       0x03
#define LampHolderNodeID     0x04
#define FloodSensorNodeID    0x05
/**
 * Device Names
 */
#define TemperatureSensorName     "SHT11 Sensor"
#define GasSensorName         "MQ-9 CO Sensor"
#define ControllerNodeName       "AeonUSB Stick"
#define MultiSensorNodeName     "Philio Multi-Sensor"
#define WallPlugNodeName       "Fibaro Wall Plug"
#define LampHolderNodeName       "Everspring Lamp Holder"
#define FloodSensorNodeName     "Everspring Flood Detector"
#define NetworkCameraName      "D-Link Network Camera"
/**
 * Device Status for ZWave Nodes
 */
char multi_sensor_status[BUFFER_MAX];
char wall_plug_status[BUFFER_MAX];
char lamp_holder_status[BUFFER_MAX];
char flood_detector_status[BUFFER_MAX];
char temperature_sensor_status[BUFFER_MAX];
char gas_sensor_status[BUFFER_MAX];
/**
 * Power Level Statuses for ZWave Nodes
 */
char multi_sensor_power_level[BUFFER_MAX];
char wall_plug_power_level[BUFFER_MAX];
char lamp_holder_power_level[BUFFER_MAX];
char flood_detector_power_level[BUFFER_MAX];
/**
 * Battery Levels
 */
int multi_sensor_battery_level;
int flood_detector_battery_level;
/**
 * Current Environment Status
 */
float temperature_f;
float temperature_c;
float relative_humidity;
float illumination;
float co_level;
/**
 * Energy Meter
 */
float current_energy_consumption;
float cumulative_energy_level;
/**
 * Switch Status
 */
char lamp_holder_switch[BUFFER_MAX];
char wall_plug_switch[BUFFER_MAX];
/**
 * Security Sensor Status
 */
char flood_sensor_status[BUFFER_MAX];
char door_sensor_status[BUFFER_MAX];
char motion_sensor_status[BUFFER_MAX];
/**
 * Camera Related Status
 */
char network_camera_status[BUFFER_MAX];
char network_camera_port[BUFFER_MAX];
void* update_status(void* arg);
void print_device_xml(const char* file_name);
#endif /* DEVICE_H_ */

The update_status thread is our worker thread that requests updates from Z-Wave devices, reads from the MQ-9 Gas Sensor, the SHT11 temperature and humidity sensor, and finally takes emergency actions.

/**
 * Periodic update worker
 * @param null
 * @return null
 */
void* update_status(void* arg) {
  set_defaults();
  while (1) {
    /**
     * Periodic Updates from Connected Devices
     */
    /**
     * Philio Multi-Sensor Node Commands
     */
    get_device_status( MultiSensorNodeID, 1);
    get_battery_level( MultiSensorNodeID, 2);
    get_binary_sensor_value( MultiSensorNodeID, DOOR_WINDOW_SENSOR, 3);
    get_binary_sensor_value( MultiSensorNodeID, MOTION_DETECTION_SENSOR, 4);
    get_multilevel_sensor_value( MultiSensorNodeID, TEMPERATURE_SENSOR, 5);
    get_multilevel_sensor_value( MultiSensorNodeID, LUMINANCE_SENSOR, 6);
    get_node_power_level( MultiSensorNodeID, 7);
    /**
     * Fibaro Wall Plug Commands
     */
    get_device_status( WallPlugNodeID, 8);
    get_binary_switch_status( WallPlugNodeID, 9);
    get_meter_level( WallPlugNodeID, POWER, 10);
    get_meter_level( WallPlugNodeID, ENERGY, 11);
    get_node_power_level(WallPlugNodeID, 12);
    /**
     * Everspring Lamp Holder Commands
     */
    get_device_status( LampHolderNodeID, 13);
    get_binary_switch_status( LampHolderNodeID, 14);
    get_node_power_level(LampHolderNodeID, 15);
    /**
     * Everspring Flood Detector Commands
     */
    get_device_status( FloodSensorNodeID, 16);
    get_sensor_alarm_value( FloodSensorNodeID, FLOOD_ALARM, 17);
    get_node_power_level(FloodSensorNodeID, 18);
    /**
     * Read From Sensors Connected to Intel Galileo
     * SHT11 and MQ-9
     */
    delaySeconds(2);
    snprintf(temperature_sensor_status, sizeof(status_active),
    status_active);
    temperature_c = read_temperature();
        delaySeconds(2);
    relative_humidity = read_humidity();
    delaySeconds(2);
    snprintf(gas_sensor_status, sizeof(status_active), status_active);
    co_level = read_gas_sensor();
    /**
     * Check for Emergency Actions
     */
    emergency_actions();
    /**
     * Sleep for 2 Minutes Print Report to devices.xml file
     */
    update_device_xml(XML_FILE_NAME); 
    update_device_json(JSON_FILE_NAME); 
    delayMinutes(UPDATE_FREQUENCY);


  }
  return NULL;
}

The emergency_actions function is an enclosed function inside the device.c file to complete emergency actions as given in the following code snippet:

void emergency_actions() {
  //If There is a Flood Switch Off Wall Plug
  if (strcmp(flood_detector_status, detected) == 0) {
    if (strcmp(wall_plug_switch, device_on) == 0) {
      binary_switch_on_off( WallPlugNodeID, OFF, 0x11);
    }
  }
  //If There is a Gas Leank Switch Off Wall Plug
  if (gas_voltage > GAS_THRESHOLD) {
    if (strcmp(wall_plug_switch, device_on) == 0) {
      binary_switch_on_off( WallPlugNodeID, OFF, 0x11);
    }
  }
  //If There is a motion capture from Network Camera
  if (strcmp(motion_sensor_status, detected)) {
    capture_frame();
  }
  //If Door Window Sensor is Open
  if (strcmp(door_sensor_status, door_open)) {
    capture_frame();
  }
}

As we've seen in the emergency_actions function, when there is motion detection or the door/window sensor is open, the Smart Home application will capture an image from the network camera. This is a very basic security precaution to capture images where there is motion, as this isn't supposed to happen.

We may also add the capture_frame function commands to the Z-Wave message handler as we did in the previous section to directly capture a frame when motion is detected. First, we need to include the camera.h file into the message.c file for this. Then, we can add the capture_frame function, as shown in the following code:

.
//Code section from message.c
if (message[10] == DOOR_WINDOW_SENSOR) {
            if (message[9] == ON) {
              printf("Door/Window Sensor is OPEN
");
              snprintf(door_sensor_status, sizeof(door_open),
                  door_open);
              capture_frame();
            } else if (message[9] == OFF) {
              printf("Door/Window Sensor is CLOSE
");
              snprintf(door_sensor_status, sizeof(door_closed),
                  door_closed);
            }
          } else if (message[10] == MOTION_DETECTION_SENSOR) {
            if (message[9] == ON) {
              printf("Motion DETECTED
");
              snprintf(motion_sensor_status, sizeof(detected),
                  detected);
              capture_frame();
            } else if (message[9] == OFF) {
              printf("NO Motion Detected
");
              snprintf(motion_sensor_status, sizeof(not_detected),
                  not_detected);
            }
          }
……………. //code section end from message.c
..................Content has been hidden....................

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