NEQTO Docs
  • Languages iconEnglish
    • 日本語
  • Docs
  • API
  • FAQ

›neqto.js Snippets

Getting Started

  • NEQTO Hello World!
  • Tutorial Step 1. NEQTO Console Setting
  • Tutorial Step 2. Device Settings & Launching Service (NEQTO Bridge)
  • Tutorial Step 2. Device Settings & Launching Service (STM32 Discovery)
  • Tutorial Step 2. Device Settings & Launching Service (SPRESENSE)
  • Tutorial Step 3. Application development using scripts

NEQTO

  • NEQTO Account Registration
  • Sub-accounts
  • API Usage
  • NEQTO CloudSync for GCP
  • NEQTO Engine Firmware List
  • Support Guidelines

NEQTO Console

  • Introduction
  • Fundamentals
  • Administrative Actions
  • Device Management
  • Linux-based Device Management
  • Batch Registration
  • Scripts
  • Actions and Contacts
  • View Data from the Console
  • NEQTO Apps

    • About NEQTO Apps
    • NEQTO Infinitypool
    • NEQTO Insights
    • NEQTO Custodia
    • NEQTO Flow
  • Machine Driver
  • Recommended Browsers
  • Billing Information

SPRESENSE

    Hardware Specifications

    • 01. About Spresense

    Software Specifications

    • 01. Operational Flow
    • 02. Initial Installation
    • 03. Spresense Wi-Fi Initial Setup
    • 04. Spresense LTE-M Initial Setup
    • 05. Debug Log Acquisition
    • 06. System LED Indications
    • 07. Event Messages
    • 08. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. Sleep
    • 10. RTC
    • 11. GPIO
    • 12. UART
    • 13. SPI
    • 14. I2C
    • 15. ADC
    • 16. GNSS
    • 17. Camera
    • 18. Utils
    • 19. nqSpresense
    • 20. nqService
    • 21. nqMqtt
    • 22. nqFOTA
    • 23. nqWiFi
    • 24. nqLte

STM32 Discovery

    Hardware Specifications

    • 01. About STM32 Discovery Kit (B-L4S5I-IOT01A)

    Software Specifications

    • 01. Operational Flow
    • 02. Initial Installation
    • 03. STM32 Discovery Wi-Fi Initial Setup
    • 04. Debug Log Acquisition
    • 05. System LED Indications
    • 06. Event Messages
    • 07. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. Sleep
    • 10. RTC
    • 11. UserSW
    • 12. GPIO
    • 13. UART
    • 14. SPI
    • 15. I2C
    • 16. ADC
    • 18. Utils
    • 19. nqDiscovery
    • 20. nqService
    • 21. nqMqtt
    • 22. nqFOTA
    • 23. nqWiFi

NEQTO Bridge

    Hardware Specifications

    • 01. NEQTO Bridge Module
    • 02. NEQTO Bridge Wi-Fi Module
    • 03. NEQTO Bridge LTE-1 Module
    • 04. NEQTO Bridge LTE-M/NB Module
    • 05. NEQTO Bridge IO Board
    • 06. NEQTO Bridge Digital IO Board
    • 07. NEQTO Bridge Connector Board

    Software Specifications

    • 01. Operational Flow
    • 02. NEQTO Bridge Wi-Fi Module Initial Setup
    • 03. NEQTO Bridge LTE Module Initial Setup
    • 04. Debug Log Acquisition
    • 05. System LED Indications
    • 06. Event Messages
    • 07. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. Sleep
    • 10. RTC
    • 11. UserSW
    • 12. GPIO
    • 13. UART
    • 14. SPI
    • 15. I2C
    • 16. ADC
    • 17. BLE
    • 18. Utils
    • 19. nqBridge
    • 20. nqService
    • 21. nqMqtt
    • 22. nqFOTA
    • 23. nqWiFi
    • 24. nqLte
    • 25. nqLAN
    • 26. nqEx

Linux-based device

    Software Specifications

    • 01. System Requirements
    • 02. Installation
    • 03. Software Configurations
    • 04. Operational Flow
    • 05. Debug Log Acquisition
    • 06. Event Messages
    • 07. Updating Software

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. RTC
    • 10. UNIXSocket
    • 11. FileSystem
    • 12. SubProcess
    • 13. Utils
    • 14. nqLinux
    • 15. nqService
    • 16. nqMqtt
    • 17. nqFOTA
    • 18. nqLAN

neqto.js Libraries

  • About neqto.js Libraries
  • UART

    • GM65 Barcode Reader
    • SRF Ultrasonic Range Finder - Serial Mode

    I2C

    • HTS221 v2 Temperature and Humidity Sensor
    • LIS2DW12 v2 Accelerometer
    • SRF Ultrasonic Range Finder - I2C Mode
    • [Archive] HTS221 Temperature and Humidity Sensor
    • [Archive] LIS2DW12 Accelerometer

    Utils

    • RTC Alarm Synchronized Scheduler

    Integration

    • AWS S3 v2
    • AWS IoT Core v2
    • Azure IoT v2
    • GCP IoT Core
    • [Archive] AWS S3
    • [Archive] AWS IoT Core

neqto.js Snippets

  • About neqto.js Snippets
  • DataDog
  • Dropbox
  • Google Sheets
  • InfluxDB
  • Oracle Cloud Object Storage
  • Salesforce
  • SAP Cloud Platform Internet of Things
  • Splunk
  • Niagara

Release Notes

  • NEQTO Console Updates
  • NEQTO Firmware (Bridge Wi-Fi/LTE Module) Releases
  • NEQTO Firmware (STM32 Discovery Wi-Fi) Releases
  • NEQTO Firmware (Spresense Wi-Fi/LTE-M) Releases
  • NEQTO Engine for Linux Releases

SAP Cloud Platform Internet of Things

This snippet provides a function to send IoT data to SAP Cloud Platform Internet of Things.

Resources used: HTTPS x 1 or MQTTS x 1


Details

The send_sap function can be used to POST the passed sensor data object 'payload' to an SAP IoT device 'sensor capability' using its REST API, over HTTPS. The result (error/response) is then passed to the callback function.

The mqtt_sap function returns an MQTT client object connected to the SAP IoT MQTT broker. The user can use the client to PUBLISH/SUBSCRIBE to an SAP IoT device topic, over MQTTS.

To start using this snippet, HOST (Address of the SAP Cloud IoT Instance), DEVICE_ALTERNATE_ID (Alternate ID of the Device), CERT (Device Certificate), KEY (Un-encrypted Private Key), and CA are required to be configured by the user.

The certificates downloaded from the SAP IoT device management page has both, the certificate, and the private key which is encrypted by default. To be used with this snippet, the password must be removed from the private key. It can be done with openSSL using the following command.

openssl rsa -in encrypted_privatekey.pem -out un-encrypted_privatekey.pem

NOTE: The provided function can handle data sizes up to 4KB. To handle larger data sizes, please refer to divided writing for https objects in the neqto.js documentation.

Click here to learn how to get a CA.

var CA = "-----BEGIN CERTIFICATE-----\n...<CA>...\n-----END CERTIFICATE-----"

HTTPS (REST)

//=================================================================
// SAP HTTP SNIPPET
//=================================================================

//=================================================================
// The following configuration are MANDATORY. Set by user.
//=================================================================
// The address of the SAP Cloud Internet of Things instance.
var HOST = '<YOUR_HOST>';

// Alternate ID of the device.
// eg. xxxxxx
var DEVICE_ALTERNATE_ID = '<YOUR_DEVICE_ALTERNATE_ID>';

// The public certificate part of the device certificate obtained from the device management page.  
// eg. '-----BEGIN CERTIFICATE-----\n...<CERT>...\n-----END CERTIFICATE-----'
var CERT = '<YOUR_CERT>';

// The private key part of the device certificate (un-encrypted) obtained from the device management page.  
// eg. '-----BEGIN RSA PRIVATE KEY-----\n...<KEY>...\n-----END RSA PRIVATE KEY-----'
var KEY = '<YOUR_KEY>';

// Public certificate of the certificate authority that signed the SAP server certificate for SSL/TLS handshake.  
// eg. '-----BEGIN CERTIFICATE-----\n...<CA>...\n-----END CERTIFICATE-----'
var CA = '<YOUR_CA>';
//=================================================================

/**
 * Post data to SAP Cloud Internet of Things using client based authentication.
 * https://developers.sap.com/tutorials/iot-cf-send-data-rest.html
 * @function send_sap
 * @param {number} sensorAlternateId - Alternate ID of the sensor, as a Number.
 * @param {number} capabilityAlternateId - Alternate ID of the capability, as a Number.
 * @param {Array} payload - The data to be sent to SAP, as either an Array of arrays of specified measures, or as an Array of JSON objects where the name of each property defined in the capability is the key.
 * @param {function} callback - User callback to return the result (error/response).
 * @returns {undefined}
 */
var send_sap = function (sensorAlternateId, capabilityAlternateId, payload, callback) {
    var body = JSON.stringify({
        "sensorAlternateId": sensorAlternateId,
        "capabilityAlternateId": capabilityAlternateId,
        "measures": payload
    });
    var options = {
        "method": 'POST',
        "host": HOST,
        "path": `/iot/gateway/rest/measures/${DEVICE_ALTERNATE_ID}`,
        "headers": {
            "Content-Type": 'application/json',
            "Content-Length": body.length.toString()
        },
        "cert": CERT,
        "key": KEY,
        "ca": CA
    };
    var request = https.request(options, function (response) {
        response.on('end', function () {
            callback(null, { "statusCode": response.statusCode, "statusMessage": response.statusMessage, "body": response.read() });
        });
    });
    request.on('error', function () {
        callback({ "errCode": request.errCode }, null);
    });
    request.end(body.toString(), function () {
        print("[request] SUCCESS");
    });
}

MQTTS

//=================================================================
// SAP MQTT SNIPPET
//=================================================================

//=================================================================
// The following configuration are MANDATORY. Set by user.
//=================================================================
// The address of the SAP Cloud Internet of Things instance.
var HOST = '<YOUR_HOST>';

// Alternate ID of the device.
// eg. xxxxxx
var DEVICE_ALTERNATE_ID = '<YOUR_DEVICE_ALTERNATE_ID>';

// The public certificate part of the device certificate obtained from the device management page.  
// eg. '-----BEGIN CERTIFICATE-----\n...<CERT>...\n-----END CERTIFICATE-----'
var CERT = '<YOUR_CERT>';

// The private key part of the device certificate (un-encrypted) obtained from the device management page.  
// eg. '-----BEGIN RSA PRIVATE KEY-----\n...<KEY>...\n-----END RSA PRIVATE KEY-----'
var KEY = '<YOUR_KEY>';

// Public certificate of the certificate authority that signed the SAP server certificate for SSL/TLS handshake.  
// eg. '-----BEGIN CERTIFICATE-----\n...<CA>...\n-----END CERTIFICATE-----'
var CA = '<YOUR_CA>';
//=================================================================

/**
 * Create an MQTT Client connected to SAP Cloud Internet of Things using client based authentication.
 * https://developers.sap.com/tutorials/iot-cf-send-data-mqtt.html
 * @function mqtt_sap
 * @param {object} options - MQTT Configuration for connecting to the broker, as an Object.
 * @returns {(Client|undefined)} - {Client} : Generated MQTT {Client} | undefined : {Client} generate failed due to invalid arguments or insufficient resources.
 */
var mqtt_sap = function (options) {
    mqtt.set('ssl.ca', CA);
    mqtt.set('ssl.key', KEY);
    mqtt.set('ssl.cert', CERT);
    return mqtt.connect(`mqtts://${HOST}:8883`, options);
}

Function Usage Example

Sample 1

This is a sample to send data to the server using the send_sap function.

/*
<INSERT ABOVE SNIPPET HERE WITH SET CONFIGURATIONS>
*/

//=================================================================
log.setLevel(-1);       //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE
log.printLevel(2);      //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
//=================================================================
// MAIN SCENARIO
//=================================================================

/**
 * Callback to fetch error/response from the request.
 * @function callback
 * @param {object} err - Error returned if the request failed. Has one property - `errCode`.
 * @param {object} data - Response returned by a successfully completed request. Has three properties - `statusCode`, `statusMessage`, and `body`.
 */
var callback = function (err, data) {
    if (err) {
        print("[error]", err.errCode);
    } else {
        print("[status]", data.statusCode, data.statusMessage);
        print("[response]", data.body);
    }
}

var sensorAlternateId = 123;
var capabilityAlternateId = 123456;
var payload = [
    {
        "testProperty": 'Hello from NEQTO Device' // "property": '<DATA>'
    }
];
send_sap(sensorAlternateId, capabilityAlternateId, payload, callback);

Sample 2

This is a sample to publish and subscribe using the client returned from the mqtt_sap function, with automatic reconnection enabled. After connection, loopback test of transmission and reception is performed.

/*
<INSERT ABOVE SNIPPET HERE WITH SET CONFIGURATIONS>
*/

//=================================================================
log.setLevel(-1);       //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE
log.printLevel(2);      //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
//=================================================================
// MAIN SCENARIO
//=================================================================

var sensorAlternateId = 5;
var capabilityAlternateId = 123456;

var sub_topic = `ack/${DEVICE_ALTERNATE_ID}`;
var pub_topic = `measures/${DEVICE_ALTERNATE_ID}`;

var client = mqtt_sap({
    "clientId": DEVICE_ALTERNATE_ID.toString(), // MANDATORY
    "reconnectCount": 1855
});
if (!client) {
    throw 'Failed to create mqtt instance';
}

var registerEventHandlers = function () {
    if (!client) {
        return;
    }
    client.on('error', function (err) {
        print('ERROR: ' + err.code + ' ERRNO: ' + client.get('errnoConnect'));
        //TODO: Error handling
    });
    client.on('message', function (topic, message) {
        print('TOPIC: ' + topic + ' MESSAGE: ' + message);
        //TODO: Message handling
    });
    client.on('close', function () {
        print("DISCONNECTED");
    });
    client.on('connect', function () {
        print('CONNECTED');
        client.subscribe(sub_topic, { qos: 1 }, function (err) {
            if (err.code > 0) {
                print('MQTT SUBSCRIBE ERROR', err.code);
                //TODO: Error handling
            }
        });
    });
};

registerEventHandlers();
setInterval(function () {
    if (client.canPublish()) {
        var payload = [
            {
                "testProperty": 'Hello from NEQTO Device' // "property": '<DATA>'
            }
        ];
        var body = JSON.stringify({
            "sensorAlternateId": sensorAlternateId,
            "capabilityAlternateId": capabilityAlternateId,
            "measures": payload
        });
        client.publish(pub_topic, body, { qos: 1 }, function (err) {
            if (err.code == 0) {
                print('Publish OK');
            } else {
                print('Publish failed');
                //TODO: Error handling
            }
        });
    } else {
        print('Cannot publish');
    }
}, 15000);

Sample 3

This is a sample to publish and subscribe using the client returned from the mqtt_sap function, with manual reconnection. If the reconnects fail consecutively, the instance is released and recreated.

/*
<INSERT ABOVE SNIPPET HERE WITH SET CONFIGURATIONS>
*/

//=================================================================
log.setLevel(-1);       //-1:NONE 0:ERROR 1:WARNING 2:DEBUG 3:TRACE
log.printLevel(2);      //0:DISABLE 1:LOG 2:CONSOLE 3:BOTH
//=================================================================
// MAIN SCENARIO
//=================================================================

var sensorAlternateId = 5;
var capabilityAlternateId = 123456;

var sub_topic = `ack/${DEVICE_ALTERNATE_ID}`;
var pub_topic = `measures/${DEVICE_ALTERNATE_ID}`;

var client = mqtt_sap({
    "clientId": DEVICE_ALTERNATE_ID.toString() // MANDATORY
});
if (!client) {
    throw 'Failed to create mqtt instance';
}

var registerEventHandlers = function () {
    var reconnectAttempts = 0;
    var maxReconnectAttempts = 24;
    if (!client) {
        return;
    }
    client.on('error', function (err) {
        print('ERROR: ' + err.code + ' ERRNO: ' + client.get('errnoConnect'));
        if (err.code == 1) { //Connection failed
            if (reconnectAttempts++ < maxReconnectAttempts) {
                print('reconnectAttempts:', reconnectAttempts);
                client.reconnect();
            } else {
                print('ABORT');
                reconnectAttempts = 0;
                client.end(); //Release mqtt instance
                client = undefined;
            }
        }
    });
    client.on('message', function (topic, message) {
        print('TOPIC: ' + topic + ' MESSAGE: ' + message);
        //TODO: Message handling
    });
    client.on('close', function () {
        print("DISCONNECTED");
        client.reconnect();
    });
    client.on('connect', function () {
        reconnectAttempts = 0;
        print("CONNECTED");
        client.subscribe(sub_topic, { qos: 1 }, function (err) {
            if (err.code > 0) {
                print("MQTT SUBSCRIBE ERROR", err.code);
                //TODO: Error handling
            }
        });
    });
};

registerEventHandlers();
setInterval(function () {
    if (!client) {
        print('Recreating MQTT Instance');
        client = mqtt_sap({
            "clientId": DEVICE_ALTERNATE_ID.toString() // MANDATORY
        });
        registerEventHandlers();
    }
    if (client) {
        if (client.canPublish()) {
            var payload = [
                {
                    "testProperty": 'Hello from NEQTO Device' // "property": '<DATA>'
                }
            ];
            var body = JSON.stringify({
                "sensorAlternateId": sensorAlternateId,
                "capabilityAlternateId": capabilityAlternateId,
                "measures": payload
            });
            client.publish(pub_topic, body, { qos: 1 }, function (err) {
                if (err.code == 0) {
                    print('Publish OK');
                } else {
                    print('Publish Failed');
                    //TODO: Error handling
                }
            });
        }
    } else {
        print('Cannot Publish');
    }
}, 15000);
The company names and product names mentioned above are registered trademarks or trademarks of their respective companies.

Updated: 2023-01-20
← SalesforceSplunk →
  • Details
    • HTTPS (REST)
    • MQTTS
  • Function Usage Example
    • Sample 1
    • Sample 2
    • Sample 3
AboutNewsProductsFAQPrivacy Policy}
NEQTO Console
IntroductionFundamentalsAdministrative ActionsDevice Management NEQTO Apps
NEQTO Bridge
NEQTO Bridge ModuleNEQTO Bridge Wi-Fi ModuleNEQTO Bridge LTE-1 ModuleError Logging Event Messages
API Documentation
API UsageGlobal APIRegional APIAPI Terms of Service
Jigsaw, Inc.
© 2023 JIG-SAW INC.