Stage 4: Registering Your End Node with the Network Server

Update the Device Profile Codec to Decode the Payload

  1. Click Device profiles in the menu on the left.

  2. Click the Device profile link to the device profile you created earlier.

  3. Select the Codec tab.

  4. Choose JavaScript functions from the Payload codec options, as shown in Figure 13.

    /uploads/documents/images/NS07-codec-options-javascript.png

    Figure 13: Codec tab (A), and JavaScript functions Payload codec option (B).

  5. Paste the following code into the Codec functions field that appears, replacing all the boilerplate code:

      // Decode uplink function.
    //
    // Input is an object with the following fields:
    // - bytes = Byte array containing the uplink payload, e.g. [255, 230, 255, 0]
    // - fPort = Uplink fPort.
    // - variables = Object containing the configured device variables.
    //
    // Output must be an object with the following fields:
    // - data = Object representing the decoded payload.
    function decodeUplink(input) {
     let data = {};
    
     // check that this is an application message, which is sent over Frame Port 1 in our code
     if (input.fPort === 1) {
      // decode the encoded moisture reading
      data.moisture = input.bytes[0] + input.bytes[1] * 256;
     }
    
     // return the object containing the data field, conforming to the specification
     return {
      data: data
     }
    }
    

    /uploads/documents/images/NS07-decode-function.png

    Figure 14: Completed Codec form.

  6. Click the Submit button to save the codec to the device profile.