7 Replies Latest reply on May 21, 2015 6:00 AM by Zoran.Roncevic

    [Help] Write data to enableIoT Cloud from NodeJS, it is possible?

    jchozas

      Hi all,

       

      I have an Intel Edison (arduino expansion board)  and I am tring to guess how can I connect to the enableIoT Cloud from Javascript but I not able to understand how to do it.

      I have read some examples from

      https://github.com/enableiot/iotkit-samples/blob/master/node

      https://github.com/enableiot

      https://software.intel.com/en-us/node/537151

       

      but it is difficult to understand because I cannot see any API-NODE module to make easier this task.

       

      I know how to write to cloud from console by "iotkit-admin observation temp 10" command, but I have an script in NodeJs which generates an array from database information and I don't know how to pass the array values to the cloud.

       

      I think first thing is to select a MQTT protocol rather than REST

      # iotkit-admin protocol mqtt

      # systemctl restart iotkit-agent

       

      but I don't know how to follow.

       

      Any example how to upload a dummy value to enableiot cloud from node.js?

      Any other help?

       

      Thanks

        • 1. Re: [Help] Write data to enableIoT Cloud from NodeJS, it is possible?
          mhahn

          some notes:

          • make sure you have updated iotkit agent to latest / recent version (check descriptions on github)
          • "iotkit-admin observation" would be a bad idea to use in "production code" - it is meant only for tests as it has a huge overhead / latency and bypasses the local iotkit agent
          • the idea is to use the iotkit agent which listens on udp / tcp (not sure on REST/MQTT for recent versions - the examples look like but I think REST/MQTT on the local agent has been deprecated). Just use the examples on github you already pointed to: iotkit-samples/node at master · enableiot/iotkit-samples · GitHub
          • the agent would then take care of the communications with the cloud (REST or MQTT)
          • if you want to use actuators you would have to set the iotkit agent for MQTT (at least used to be the case - maybe no longer true)
          • you can check the logs (/var/log/... ) of iotkit-agent to check whether your data have successfully been submitted to the local agent
          • 2. Re: [Help] Write data to enableIoT Cloud from NodeJS, it is possible?
            jchozas

            Thanks mhahn,

             

            From my point of view there is a lack of information of how to do the connection with the IoT Analytic Cloud in MQTT because I do not know which files I need to configure or which information is needed.

             

            For example, there is a Cloud called "theThings.IO" which they have a API-Node theThings/thethingsio-api-node · GitHub and they have examples of how to configure the file to write data to the Cloud. On the contrary for the Intel Cloud I do not have no idea how to do the same because there is not an explanation in the documentation file so I would like to know if someone has an example of uploading data to the Cloud.

             

            Any help?

             

            Thank you.

            • 3. Re: [Help] Write data to enableIoT Cloud from NodeJS, it is possible?
              adua

              Hello jchozas,

               

              You could use 'iotkit-comm' the nodejs connectivity library already installed on the Edison. The quick-start guide is here  JSDoc: Tutorial: Start Here

               

              The cloud tutorial is here JSDoc: Tutorial: Cloud.

               

              NOTE: for the above to work you need to upgrade to the latest Edison firmware. You can do this by running the command configure_edison --upgrade, just make sure that your device has Internet access.

               

              Hope this helps,

              Akshay

              1 of 1 people found this helpful
              • 4. Re: [Help] Write data to enableIoT Cloud from NodeJS, it is possible?
                babaltz

                Hi.

                The focus of the documentation for interacting directly with the IOT Cloud is based on the REST API. I have a short Node.js script that will submit data observation(s) directly to the cloud via MQTT for a component/sensor named, "temp". The script uses your credentials that were stored when you activated your device using "iotkit-admin". You'll need to locate the iotkit-agent configuration file. It also depends on the certificate that is installed with the iotkit-agent package.

                 

                This script submits data, directly to the cloud via MQTT and does not depend on your iotkit-agent. The device just needed to be activated with "iotkit-admin". I find it's better to run this script in the iotkit-agent folder as MQTT has problems with newer versions of Node.js. Hopefully this helps.

                 

                #!/usr/bin/env node

                var mqtt = require('mqtt');

                var util = require('util');

                // Load iotkit-agent configuration file settings

                var config = require('/usr/lib/node_modules/iotkit-agent/data/device.json'); // iotkit-agent config file location

                 

                var client;

                // iotkit-agent credentials needed to connect and submit data

                var uname = config.device_id;

                var account = config.account_id;

                var password = config.device_token;

                var comp_name = "temp"; // your sensor name

                var broker = "broker.us.enableiot.com";

                var port = 8883;

                ca_certs = [ "/usr/lib/node_modules/iotkit-agent/certs/AddTrust_External_Root.pem"]; // default CA cert location

                 

                // Authentication info

                var mypass = {

                    "username": uname,

                    "password": password,

                    "keepalive": 60,

                    "ca": ca_certs,

                    "rejectUnauthorized": true

                }

                 

                // get current timestamp

                var now = (new Date).getTime();

                 

                // Look-up the component-ID for the sensor you are submitting

                // data for.

                var cid;

                var comp = config.sensor_list.filter(function(obj) {

                    return obj.name === comp_name;

                });

                cid = comp[0].cid

                 

                // Construct a data observation message. Multiple

                // observations can be submitted at once as an array.

                var msg = {

                    "accountId": account,

                    "did": uname,

                    "on": now,

                    "count": 1,

                    "data": [

                      {

                        "on": now,

                       "value": "33.8",

                        "cid": cid

                      }

                    ]

                }

                 

                // MQTT topic for data submission

                topic = util.format('server/metric/%s/%s', account, uname);

                client = mqtt.createSecureClient(port, broker, mypass);

                // publish data observation(s)

                client.publish(topic, JSON.stringify(msg), function() {

                  console.log("Observation is submitted");

                  client.end(); // Close the connection when published

                });

                1 of 1 people found this helpful
                • 5. Re: [Help] Write data to enableIoT Cloud from NodeJS, it is possible?
                  jchozas

                  Thank you so much babaltz .Your script is quite good as well as your explanation

                  • 6. Re: [Help] Write data to enableIoT Cloud from NodeJS, it is possible?
                    jchozas

                    Yes,   that was exactly I was asking for. Thank you so much adua, this is gonna helping me.

                    • 7. Re: [Help] Write data to enableIoT Cloud from NodeJS, it is possible?
                      Zoran.Roncevic

                      Hi,

                       

                      find iotkit-agent node modules folder. There are simle send_udp.js file as example for sending data to enableiot.com from node.

                       

                      You have to enable iotkit-agent service with : systemctl enable iotkit-agent command from linux console. Without this, after power-on or reboot, iotkit would be disabled.

                      Iotkit-agent listen two port UDP 41234 and TCP 7070. ... There are many node modules (like 'request' ...) for communicatio over TCP/UDP.

                       

                      Another way for sending data to enableiot cloud is REST API... On GitHub are some nice examples (iotkit-samples/api at master · enableiot/iotkit-samples · GitHub)