Chapter 5. Home Monitoring with Common Security Sensors

In Chapter 3, Energy Management with Environmental and Electrical Sensors and Chapter 4, Energy Management with Light Sensors and Extending Use Cases, we worked with sensors, to achieve energy management. Now, we will proceed to learn about the security concept in home automation and use cases with Intel Galileo.

Security is another major topic in home automation. Securing a residential area while creating a home automation system is done with various sensors and devices. For example, a motion sensor can be used to monitor an area if someone breaks into it. A door or a window sensor can be used for a similar purpose such as monitoring a door and window to check whether they have been opened without your knowledge. Network cameras are another set of devices used for security in home automation systems to monitor homes or any other area included remotely.

Besides securing residential areas from burglary, you must also look out for devices that sense fire, gas leaks, or water leaks. Smoke detectors, gas detector, water leaks, and flood detectors can be included into the home automation system. With the addition of these sensors, any damage or disaster can be prevented.

In this chapter, we will briefly examine the sensors existing in the market, that are usable with Intel Galileo for security. We will also look at devices with home automation protocols.

Security sensors with Intel Galileo

There are many sensors, that can be used with Intel Galileo to create security devices. Let's look at some of them.

PIR motion sensors

There are many motion or passive infrared detection (PIR) sensors available to be used with Intel Galileo. The sensor shown in the following image, manufactured by SeeedStudio, is one of them. Their use is pretty straightforward. All you need to do is connect VCC, Ground, and one GPIO pin for detection output. After you have connected pins, you will read HIGH (1) from the connected GPIO pin; otherwise, you would read LOW (0).

PIR motion sensors

Note

You can find more information about motion sensors from SeeedStudio from the following link: http://www.seeedstudio.com/wiki/PIR_Motion_sensor_module

Motion sensors can be used to develop a device that can sense movement and alert the user. Another use case with energy management is that you can turn on or off a light bulb after a motion has been detected.

Magnetic sensors

These sensors are the main mechanism used for door/window sensors. Digital magnetic sensors work at a very basic level. They produce a digital high value if there is a magnetic object near the sensor. The following image shows a very basic sensor from DFRobot, whose pins can be connected to VCC, GND, and GPIO to read digital values in order to sense whether any magnetic object is nearby:

Magnetic sensors

Note

The preceding image shows a magnetic sensor from dfrobot; more information about this can be found at the following link http://www.dfrobot.com/wiki/index.php/Digital_magnetic_sensor_SKU:_DFR0033.

By using a magnetic sensor, it is possible to develop a device to detect if a door or window has been opened. It is also possible to use magnetic sensors for energy management; if any door has been opened, you can switch on the lights in the given room.

Gas sensors

There are many gas sensors available to be used with Intel Galileo. Each gas sensor is able to detect a specific gas. Some sensors detect the levels of carbon monoxide, methane, propane, and alcohol in the air. It helps you to detect any rise in the level of dangerous gases so that you can take precautions immediately to prevent any accident.

Using gas sensors with Intel Galileo

Here, we have an example of a MQ-9 gas sensor, which is sensitive to carbon monoxide, methane, and liquid petrol gas. Changes in the level of the given gases affect the conductivity of the sensor. A change in conductivity results in the output voltage of the sensor increasing. MQ-9 is an analog sensor, and so we need to use analog pins in Intel Galileo to read voltage changes.

Note

There are multiple sources for similar MQ-9 datasheets. Just search using your favorite search engine. One of the sources is here: http://www.sgbotic.com/products/datasheets/sensors/MQ-9.pdf

The following image is the MQ-9 sensor we've used in the following sample application:

Using gas sensors with Intel Galileo
  1. First, we need to set the Intel Galileo GPIO pin to 37 low to be able to read analog values from analog input 0. Our main function will look like this:
    #define PINMUX 37
    #define ANALOGPIN 0
    /**
     * Analog Device File Operations
     */
    int open_analog_device(int pin_number);
    int read_analog_device_value(int device_file);
    float read_voltage_scale(int pin_number);
    int main(void) {
      // Read from Analog 0, mux GPIO Pin 37 to read voltage values
      if (gpio_set_mode(PINMUX, OUTPUT) < 0) {
        printf("Can't Set GPIO Mux Pin Mode
    ");
        return EXIT_FAILURE;
      }
      if (gpio_set_value(PINMUX, LOW) < 0) {
        printf("Can't Set GPIO Mux Pin Value
    ");
        return EXIT_FAILURE;
      }
      printf("Pin Mux Successful
    ");
      // Read Digital Value Scale
      float v_scale = read_voltage_scale(ANALOGPIN);
      printf("Voltage Scale is %f
    ", v_scale);
      // Open Analog Device
      int analog = open_analog_device(ANALOGPIN);
      if (analog > -1) {
        printf("Analog IO File Opened Successfully
    ");
      }
       // Read Voltage Values from Analog 0
      while (1) {
        printf("Voltage : %d 
    ", read_analog_device_value(analog));
        usleep(1000 * 1000);
      }
      close(analog);
      return 0;
    }
  2. Then, we will read the raw analog values from the filesystem. We need to open /sys/bus/iio/devices/iio:device0/in_voltage0_raw and read the values:
    int open_analog_device(int pin_number) {
      //Analog Device Values Read from
      const char* analog_file_path =
          "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw";
      int fanalog, a_err = -1;
      char analog_file_buffer[MAX_BUF];
      //Set analog reading file path and pin number
      if (sprintf(analog_file_buffer, analog_file_path, pin_number) < 0) {
        printf("Can't create analog file path
    ");
        return a_err;
      }
      fanalog = open(analog_file_buffer, O_RDONLY);
      if (fanalog < 0) {
        printf("Can't open analog device
    ");
        return a_err;
      }
      return fanalog;
    }
    int read_analog_device_value(int device_file) {
      char buf[1024];
      int a_err = -1;
      int ret = read(device_file, buf, sizeof(buf));
      if (ret == a_err) {
        printf("Couldn't get value from Analog Pin
    ");
        return -1;
      }
      buf[ret] = '';
      lseek(device_file, 0, 0);
      return atoi(buf);
    }
  3. There is an extra piece of code to read the scale of the raw values for future conversion requirements. It is done by reading /sys/bus/iio/devices/iio:device0/in_voltage%d_scale :
    float read_voltage_scale(int pin_number) {
      //Analog Device Values Read from
      const char* scale_file_path =
          "/sys/bus/iio/devices/iio:device0/in_voltage%d_scale";
      int fscale, a_err = -1;
      char scale_file_buffer[MAX_BUF];
      //Set analog reading file path and pin number
      if (sprintf(scale_file_buffer, scale_file_path, pin_number) < 0)
    {
        printf("Can't create scale file path
    ");
        return a_err;
      }
      fscale = open(scale_file_buffer, O_RDONLY);
      if (fscale < 0) {
        printf("Can't open scale device
    ");
        return a_err;
      }
      char buf[1024];
      int ret = read(fscale, buf, sizeof(buf));
      if (ret == a_err) {
        printf("Couldn't get value from Analog Pin
    ");
        return -1;
      }
      buf[ret] = '';
      return atof(buf);
    }
  4. Let's run the application on the Intel Galileo. We get the following output from our gasdetector application:
    root@clanton:~/apps# ./gasdetector 
    Pin Mux Successful
    Voltage Scale is 1.220703
    Analog IO File Opened Successfully
    Voltage : 652 
    Voltage : 748 
    Voltage : 776 
    Voltage : 2916 
    Voltage : 2848 
    Voltage : 2812 
    Voltage : 2728 
    Voltage : 2708 

This was a basic sample working with analog sensors. Gas sensors also have a very important place in security. The reliable use of gas sensors can prevent many accidents in residential and industrial areas.

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

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