IoTivity-Getting-Started

IoTivity logo

Getting Started Getting Started FAQ Digging Deeper GitHub Repository IoTivity.org

This guide will show you how to download, build, and run two simulated devices on the same Linux PC:

The two apps talk to each other over a loopback connection, using the OCF protocol.

OCF protocol over loopback connection

To use Windows: To download, build, and run on a Windows PC instead, check the Iotivity FAQ.

Requirements

To carry out this tutorial as tested, you will need the following:

Install IoTivity and Emulator Sample

  1. On the development PC, open a terminal.

  2. Download source and set up the IoTivity-Lite environment by running this command, which takes several minutes to complete:

    cd ~
    curl https://openconnectivity.github.io/IOTivity-Lite-setup/install.sh | bash
    

    Alternatively, you can download the install.sh script, review and run it from anywhere.

  3. Install the project scripts:

    curl https://openconnectivity.github.io/Project-Scripts/install.sh | bash
    

    Rerun this script if an error occurs.

  4. Install the repository containing emulator sample code. Answer “y” if prompted:

    curl https://openconnectivity.github.io/Emulator-Code/emulator/install.sh | bash
    

    You may need to run source ~/.bashrc or reboot the computer at this point to ensure that your environment variables are set.

  source ~/.bashrc

  or

  sudo reboot -h now
  1. Create a project directory:

    cd ~
    mkdir workspace
    cd workspace
    
  2. Create the IoTivity project (we’ll call it myexample):

     create_project.sh myexample
     cd myexample
    
  3. Copy the sample project to the current directory and set it up:

    cp ~/Emulator-Code/IoTivity-lite/emulator/setup.sh ./
    ./setup.sh
    

The sample project now has all dependencies installed and is ready for code generation and building.

Build and Run the Server App

  1. Generate code for the server app by running these commands:

    cd ~/workspace/myexample (if not already there)
    gen.sh
    

    This script starts from a default JSON configuration file that can be edited to specify the capabilities of your actual device. The script calls the DeviceBuilder app to generate source code for the server app you’ll run on your smart device (in this case, the dimmable light Emulator app).

  2. Build the server app with this command:

    build.sh
    
  3. Reset and run the server app by running these commands:

    reset.sh
    run.sh
    

A small window should open with a graphical image of a light bulb, an on/off switch and slider dimming switch.

Leave the terminal window open. The server app is now waiting for commands from the client app, which you’ll install next.

Build and Run the Client App (If you’ve already installed OTGC once, you can skip the installation here and jump to step 3.)

NOTE: The instructions below are for building and running the OTGC client application on Linux. Alternatively, you can run the Android version of OTGC on your smart phone or tablet. Get the installation package here: (https://github.com/openconnectivity/otgc-android). You can then just run it and skip the Linux OTGC installation in the steps below. If you run the Android app, you can basically follow the instructions is step % below. The circle icon will start the discovery process and should find the Emulator server.

The sample client application is called OTGC (Onboarding Tool and Generic Client). On Linux, you’ll download and build the binary with a script:

  1. On the development PC, open another terminal window.

  2. Download and build the Linux OTGC client by running this command, which takes several minutes to complete:

    curl https://openconnectivity.github.io/otgc-linux/setup.sh | bash
    

    Troubleshooting: If the build process finishes but an error occurs, manually run the dpkg command from the setup.sh script.

    sudo dpkg -i ./otgc-linux/build/debian/out/otgc-2.9.0.deb
    #run only in the event of an error and substitute the version of otgc
    #that is available at this location (for example, the version number will
    #increment as new versions are created (e.g. otgc-2.10.0.deb))
    
  3. Launch the Linux OTGC client by running this command (or locate and click the application icon):

    /usr/bin/otgc.sh
    
  4. Click to OK the End User License Agreement.

    OTGC starts and automatically scans all visible OCF devices, listing them in the app’s left-hand pane. If you want to restart the discovery process, click on the discovery button or (on Android) the circular arrow button.

  5. Locate and align both the terminal window that is awaiting incoming connections, plus the app window, so that both are visible.

    As you proceed with the remaining steps in this section, notice that each action taken in the client app generates console output in the terminal window that had been awaiting incoming connections. This simulates controlling your smart home device with, for example, a mobile phone client:

    • Click to select the device listed in the left-hand pane, then click the Onboard button.

      The Select OTM (Ownership Transfer Method) dialog box pops up.

    • Choose an onboarding security method (any should work) from the drop-down selector and click OK in the Select OTM dialog box.

      As device ownership is transferred, the Select OTM dialog box closes and is replaced by the Set Device Name dialog box.

    • Change the device name, if you wish. Click OK to close the dialog box.

    • Click to reselect the device in the left-hand pane.

  6. In OTGC, find the /binaryswitch and /dimming sections and click the switch value button on the left. The light bulb animation will turn on or off, controlled by the client app over the OCF protocol. Also, the dimming slider will move from 0 to 100. If you change the dimming value (type to get the value accepted), the dimmer slider in the GUI should reflect what you type and the light animation should change. If you type 100, the light should turn on fully and the binary switch should turn on. Notice that the console output in the server terminal also responds to your actions in the client.

  7. Now “observe” (monitor) the touch buttons on the emulator window in the OTGC app by turning on the “observe” button on the /binaryswitch and /dimmer sections of OTGC, then clicking the switch button or dimming slider on the emulator application. Notice that the output in the OTGC app detects the changes in the emulator application.

  8. Press Ctrl-C in the server terminal or click the “X” in the emulator window to exit the server app.

Digging deeper: Next steps for development

Now that you’ve created a device simulation based on IoTivity, you’re ready to dig deeper to explore other platforms and learn how to apply IoTivity to your own platform and devices. The next example creates a server on actual hardware (Raspberry Pi).

Digging Deeper