Getting Started -- Adding a Device

Let’s keep going: Controlling onboard LED with button.

Please ensure your have read Getting Started before reading this section.

Step 6: Adding External Devices

  1. Switching back to your daily wireless network.
  2. Find the Push Button Module (model CK002) from the developer kit. We will use it to control the status of the LED onboard. You can look it up on the rap website according to the label on the package. Also on the website, you can find information about drivers that support this device.
  3. Execute the command rap device add button in the application directory (button here is the ID of the new device we are going to add, to be used by the program).

    rap device add button
  4. Enter the model CK002 of the external device and confirm. rap will search the Ruff software package registry and list available drivers.

    ? model: CK002
    Searching supported drivers from Rap registry...
    ? select a driver for device "button"(CK002): (Use arrow keys)
    > button-gpio@2.0.3
    ...
  5. Choose the latest version of button-gpio and confirm. rap will automatically download the driver and save the device information to the app.json file.

Step 7: Hardware Layout

  1. After adding external devices, we need to generate the new layout for the hardware. Execute the command rap layout in the application directory:

    rap layout

    rap will automatically distribute resources among external devices, calculate a possible mapping.

    rap also provides a GUI layout editor and can be used to visually confirm wire connections and make basic changes. To set up this function, execute the command rap layout --visual in the application directory. rap will download the pictures of devices and visually display them with wire connections.

  2. Connect hardware according to the calculated (or edited) layout mapping. We recommend disconnecting your power cable when changing device connections.

Step 8: Binding Button Events

Modify the index.js file under src folder and bind events push and release for button:

$.ready(function (error) {
if (error) {
console.log(error);
return;
}

// Turn on `#led-r` when `#button` is pushed.
$('#button').on('push', function () {
console.log('Button pushed.');
$('#led-r').turnOn();
});

// Turn off `#led-r` when `#button` is released.
$('#button').on('release', function () {
console.log('Button released.');
$('#led-r').turnOff();
});
});

$.end(function () {
$('#led-r').turnOff();
});

The APIs provided by other drivers can be found at the Ruff Registry.

Step 9: Redeploying the Application

  1. Reconnnect to Ruff-[SN] hotspot. Please ensure your commputer have already connnected to the hotspot for further operation.
  2. Run the following command to deploy your application.

    rap deploy -s

Pushing and releasing the button should now turn the red onboard LED on and off.

Step 10: Viewing the Application Log

To track application execution status, we can use the command rap log to add a log to the application that allows us to view what’s been logged.

  1. Create a new command line window, change the directory to the application folder, and run the following command:

    rap log

    The console outputs the following content once the connection between rap and the development board is set up.

    Connection established.
  2. Pushing the button, turns the red onboard LED on and the log will be

    Button pushed.

    Releasing the button, turns the red onboard LED off and the log will be

    Button released.

Next Step

Connecting to network is an very important step for IoT application. Visit here to continue your Ruff journey.