Chapter 7. Building Applications and Customizing Linux for Home Automation

In the previous chapters, you learned how to develop applications that work on Intel Galileo. You learned how to deploy applications to Intel Galileo with the help of Eclipse IDE or by using the SCP command-line tool to transfer application binary files. During the course of this chapter, we will work on how we can make our application or applications start at boot time and run as a background process or service on Intel Galileo Yocto Linux operating system.

While we arrived around the end of the development process, we would like to run our application in Intel Galileo when the board boots up with no user command to start it. We want the Smart Home application to run automatically because managing a home from just a command line terminal interface is not so user friendly. Therefore, for this, we will also need to define some features for the Smart Home application to send commands to and receive data from.

Let's start with what we can do to make Smart Home application start as a background process in Intel Galileo.

Customizing Linux with the Yocto Project

Customizing Linux means changing the default system configurations and adding or removing applications and kernel modules to the default Linux build for Intel Galileo. While following the steps described in the Chapter 1, Getting Started with Intel Galileo, we didn't make any changes, and so we only built the default full image defined for Intel Galileo.

In the previous chapters, we briefly mentioned adding the OpenCV library and added the cp210x kernel module to our default Linux image. The OpenCV library is an open source package and it was already delivered with the Intel Galileo board support package. In this section, we will make the required configurations and changes to build a structure to add our own application to the Linux image.

Adding a new application to Yocto Project

To add a new application, we need to add a Yocto Project recipe to the Intel Galileo Yocto Project. If you have developed a whole software stack with a number of applications, services, and libraries, it would be better to create your own directory like meta-clanton-distro and add the required configuration files to the directory. Finally, we need to add the new directory in the setup.sh file to the BBLAYERS variable. Configuration files make the BitBake script read the whole directory under the defined directory, parse the recipes, and build all of them. We will only deploy one application to the Intel Galileo default Yocto Linux image. Therefore, we will work only on the existing recipes and create our own Yocto Project recipe. Before going forward, it will be a good practice to review Chapter 1, Getting Started with Intel Galileo, to remember the steps you have initialized for the Yocto Project build environment. The build directory (BUILD_DIR) was /home/onur/galileo_build, and the Yocto Project files were stored in meta-clanton_v1.0.1.

Let's go into the build directory to create a new file and start writing the recipe, as shown in the following commands:

$ cd $BUILD_DIR/meta-clanton_v1.0.1
  1. First, we need to create a directory for the recipe in /meta-oe/meta-oe/recipes-support. Directories such as recipes-multimedia, recipes-core, recipes-extended, and others, can also be selected. How you classify your application depends on you. Now create a directory using the following command:
    $ mkdir meta-oe/meta-oe/recipes-support/smarthome
    
  2. Create the .bb file with which we will define our application for the build process; save it empty with Ctrl + O:
    $ nano meta-oe/meta-oe/recipes-support/smarthome_0.0.1.bb
    
  3. We will start by entering the description for the application, application section, and a library or a base. Then you need to enter the software license type; like all the code in this book, this application is also licensed under the MIT license, license file checksum, and build dependency.

    Tip

    Generate the checksum for the COPYING file under the project folder. You can generate the checksum with the md5sum tool as shown in the following command:

    $ md5sum COPYING
    

    We defined the first lines of smarthome_0.0.1.bb file which are given here:

    DESCRIPTION = "Home Automation Application for Intel Galileo"
    SECTION = "base"
    LICENSE = "MIT"
    LIC_FILES_CHKSUM = "file://COPYING;md5=c5bb609535f48d5cd02fe14a780f3d8c"
    DEPENDS += "opencv"
  4. Now, we will proceed to define the source code path to fetch the source code. In Yocto Project, recipes define the upstream source path. BitBake script downloads the source code from the given path and builds the downloaded source code. Paths are defined to the SRC_URI variable. Paths can be a Git, SVN repository, HTTP, FTP, or a local path. In the sample recipes, we defined the source in a local path. We also need to enter the checksum and sha256sum values. We added the following lines to our recipe:
    SRC_URI = "file:///home/onur/tmp/galileo_ha/smarthome-0.0.1.tar.gz"
    SRC_URI[md5sum] = "46ef1371208ee57f89cbfa793a689eba"
    SRC_URI[sha256sum] = "dd4d8cd85c86c440173e60af36e227fbc2cffb151877ebaed8d474625cd889cd"

    Tip

    You can use sha256sum Linux to generate a sha256sum value with the following commands on your host machine:

    $ sha256 /home/onur/tmp/galileo_ha/smarthome-0.0.1.tar.gz
    
  5. In this step, we will define how to build our application. First, we define the source directory where the source code will be extracted. Then, we define how the BitBake script will build the application. We defined in the recipe to use autotools for the build process. We created the applications in Eclipse by selecting autotools. Autotools configurations are already defined in Yocto Project, and so we inherited configurations from an existing project to the recipe. See the next two lines for descriptions:
    S = "${WORKDIR}/${PN}-${PV}"
    inherit autotools gettext
  6. Finally, we define the installation steps to make our home automation application automatically start and write outputs to a defined file. The BitBake script will use the defined autotools installation process and it will install the application into /usr/bin. The following definition will carry out extra installation steps for auto start descriptions. We will add the following lines at the end of the smarthome_0.0.1.bb file:
    do_install_append(){
      mkdir ${D}/etc
      mkdir ${D}/etc/init.d
      mkdir ${D}/etc/rcS.d
      install -m 0755 ${S}/scripts/startha.sh ${D}${sysconfdir}/init.d
      ln -sf ../init.d/startha.sh ${D}${sysconfdir}/rcS.d/S99startha.sh 
    }

    As you see in the previous installation definition, we also have a startup script to execute at boot time. We have included the script to the project source folder under scripts folder as startha.sh. The startup script is defined as given in the following command:

    #!/bin/sh
    # Start the Home Automation Application
    echo "Starting Home Automation Application" > /var/log/homelog
    /usr/bin/smart_home &> /var/log/homelog
  7. The final content of the Yocto Project recipe for Smart Home application is as follows:
    DESCRIPTION = "Home Automation Application for Intel Galileo"
    SECTION = "base"
    LICENSE = "MIT"
    LIC_FILES_CHKSUM = "file://COPYING;md5=c5bb609535f48d5cd02fe14a780f3d8c"
    DEPENDS += "opencv"
    SRC_URI = "file:///home/onur/tmp/galileo_ha/smarthome-0.0.1.tar.gz"
    SRC_URI[md5sum] = "46ef1371208ee57f89cbfa793a689eba"
    SRC_URI[sha256sum] = "dd4d8cd85c86c440173e60af36e227fbc2cffb151877ebaed8d474625cd889cd"
    S = "${WORKDIR}/${PN}-${PV}"
    inherit autotools gettext
    do_install_append(){
      mkdir ${D}/etc
      mkdir ${D}/etc/init.d
      mkdir ${D}/etc/rcS.d
      install -m 0755 ${S}/scripts/startha.sh ${D}${sysconfdir}/init.d
      ln -sf ../init.d/startha.sh ${D}${sysconfdir}/rcS.d/S99startha.sh 
    }
  8. We are done with creating our Yocto Project recipe. It is time to build our new image and start it on Intel Galileo. Run the following commands inside the meta-clanton_v1.0.1 folder to build Smart Home application and create the new image:
    $ source poky/oe-init-build-env yocto_build
    $ bitbake image-full-galileo

Note

You can follow the defined steps in Chapter 1, Getting Started with Intel Galileo, to copy the created files to an SD card and boot the image on Intel Galileo.

Tip

You can apply the preceding steps to any application you have to add into the custom Linux image. To gain more expertise, you can read the Yocto Project Developer's Manual at http://www.yoctoproject.org/docs/current/dev-manual/dev-manual.html.

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

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