Oracle by Example brandingIntegrating the Node.js JavaScript Simulator Device

section 0Before You Begin

This tutorial shows you how to install and configure a Node.js simulator by using the Oracle Internet of Things (IoT) Cloud Service JavaScript Client Software Library. This tutorial takes approximately 15 minutes to complete.

Background

To integrate Oracle Mobile Cloud Service with Oracle IoT Cloud Service, you can use simulator devices.

For this tutorial, you use the Node.js JavaScript simulator device, which sends messages to Oracle IoT Cloud Service. Based on the messages, Oracle IoT Cloud Service sends alerts and notifications to Oracle Mobile Cloud Service.

What Do You Need?

  • A computer running Windows 8 or later
  • Node.js 4.x.x version or later for your system platform, downloaded and installed from https://nodejs.org/dist/latest-v4.x/
  • JavaScript Client Software Library binary iotcs-csl-javascript-bin-release.zip file (where release is the version number), downloaded from Oracle Technology Network
  • Access to an instance of Oracle IoT Cloud Service

section 1Install the Prerequisite Software

  1. Extract the DirectlyConnectedDeviceSample.js file from iotcs-csl-js-samples-version.zip.
  2. Extract the Node Package Manager (npm) tar file from iotcs-csl-js-bin-version.zip. This JavaScript package manager file is contained within Node.js and typically takes the form of iotcs-csl-device-version.tgz.
  3. Move DirectlyConnectedDeviceSample.js to a new directory.
  4. Open a command prompt and browse to that directory.
  5. Install the prerequisite software using npm ():
    npm install node-forge
    npm install iotcs-csl-device-x.x.x.tgz


section 2Modify the Sample Code

  1. In a text editor, open the DirectlyConnectedDeviceSample.js file.
  2. In the device activation line, replace humidity_sensor with transport_container.
    dcd.activate(['urn:com:oracle:iot:device:transport_container'], function (device, error)...
  3. In the device model retrieval line, replace humidity_sensor with transport_container.
    device.getDeviceModel('urn:com:oracle:iot:device:transport_container', function (response, error)...
  4. Modify the variable sensor values for the transport container device model:
    var sensor = {
            humidity: 0,
            temperature: 0,
            ora_latitude: 44.481429,
            ora_longitude: 26.104218
        };
  5. Change the send function code for the transport container device model:
    var send = function () {
            sensor.humidity = 40 + Math.floor(Math.random() * 20);
            sensor.temperature = 30 + Math.floor(Math.random() * 25);
            virtualDev.update(sensor);
        };
  6. Save and close the file.

Your code should look similar to this file: DirectlyConnectedDeviceSample.js.


section 3Register the Device

  1. In a web browser, enter the URL for your instance of Oracle IoT Cloud Service, enter your user name and password, and click Sign In.
  2. On the Oracle IoT Cloud Service home page, click Action Menu icon, select Devices, and then select Registration. Click Register Single Device.
    Oracle IoT Cloud Service device registration page
    Description of the illustration device_registration.png
  3. Enter the following values and click Register:

    • Activation ID: JS-SIMULATOR-01
    • Activation Secret: 12345
    • Name: JS-SIMULATOR-01
    • Manufacturer: JS-SIMULATOR-01
    • Serial Number: JS-SIMULATOR-01
    • Model Number: JS-SIMULATOR-01
    Oracle IoT Cloud Service single device registration page
    Description of the illustration single_device_registration.png
  4. On the Registration Successful page, enter the file protection password twice and click Download Provisioning File.
    Oracle IoT Cloud Service registration successful page
    Description of the illustration registration_successful.png
  5. Move the conf file to the same directory as the DirectlyConnectedDeviceSample.js file.
  6. Click Finish.

section 4Run the Simulator

  1. In a command-line interface, browse to DirectlyConnectedDeviceSample.js.
  2. Run the simulator and replace password with the value that you specified when you downloaded the conf file.
    node DirectlyConnectedDeviceSample.js provisioning-file.conf password
  3. To stop the simulator, press Control+C.

more informationWant to Learn More?