Sending Data Using Node.js

In this project you will be directed to send simple data from Raspberry Pi to Antares IoT Platform using Node.js.

Prerequisites

The materials required follow the General Prerequisites on the previous page. If you have not prepared the requirements on that page, then you can visit the following page.

pageRaspberry GUI General Prerequisites

Follow These Steps

1. Switching on the Raspberry Pi

To switch on the Raspberry Pi, insert the SD card that has the OS installed in the slot available on the Raspberry Pi board. Then connect the Raspberry Pi to the monitor using a Micro HDMI to HDMI cable. Also connect the keyboard and mouse to the USB slot on the Raspberry Pi as shown below. Finally, connect the Raspberry Pi board to the power source using USB-C.

A powered-on Raspberry Pi will be signalled by an illuminated LED indicator near the USB-C port.

2. Switching on the Monitor and Opening the Terminal

After the Raspberry Pi is connected to the power source, then you can turn on the monitor. Raspberry Pi that has booted and entered the OS is shown as in the image below.

If after switching on the monitor nothing appears on the screen, this means that the Raspberry Pi is booting the OS. You can wait a while until the GUI appears.

Next, you can open the terminal on the top left as shown in the following icon image.

The opened terminal will look like the following image.

3. Install Node.js and NPM Library

To send data to Antares using Node.js, you need to install Node.js and the NPM library first by entering the following syntax in the terminal.

sudo apt update
sudo apt install nodejs npm

Node.js is a runtime environment for JavaScript that is open-source and cross-platform. With Node.js, you can run JavaScript code anywhere, not just limited to the browser environment.

NPM (Node Package Manager) is the default JavaScript Package Manager of Node.js. Through NPM, you can create Node.js packages (projects) and manage the use of external packages used.

The following figure shows the successful installation of Node.js and the NPM library.

To confirm whether Node.js and the NPM library were installed successfully, you can input the following syntax in the terminal.

node -v
npm -v

If you can see the version of Node.js and the npm library after inputting the syntax above as in the following image, then the installation process has been successful.

4. NPM Library Initiation

To be able to use the npm library, you need to initiate the library first by entering the syntax below in the terminal.

npm init --yes

If the npm library initialisation process is successful, the terminal will output as shown below.

5. Install Modul Request

In order for the Raspberry Pi to send data to the Antares platform, it is necessary to install the request module in Node.js. You can input the syntax below to install.

npm install request --save

The request module is a module contained in Node.js to make HTTP requests to the server or API. This module can facilitate the request-response process between client and server to exchange data.

After the HTTP Request module has been successfully installed, the display on the terminal will be as shown below.

6. Creating a Node.js File

Setelah berhasil melakukan instalasi library yang diperlukan, Anda perlu membuat file dengan format Node.js. Masukkan syntax di bawah pada terminal untuk membuat file Node.js.

sudo nano your-file-name.js

You need to change your-file-name to your preferred filename.

After inputting the syntax above and pressing enter, the terminal display will change to the image below.

The above view shows an empty Node.js file ready to be inputted with programme code.

7. Inputting programme code

To send data to Antares, you can input the code below.

var request = require("request");

var options = {
  method: 'POST',
  url: 'masukkan URL',
  headers: {
    'cache-control': 'no-cache',
    'content-type': 'application/json;ty=4',
    'x-m2m-origin': 'masukkan access-key'
  },
  body: {
    "m2m:cin": {
      "cnf": "message",
      "con": JSON.stringify({
        "status": "0",
        "dim": "10"
      })
    }
  },
  json: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

Don't forget to input the device URL in the url section and the access-key in the x-m2m-origin section according to the Antares IoT Platform. Make sure there are no programme writing errors and pay attention to the indentation.

After inputting the above code, the display on the terminal will be as shown below.

To save the created Node.js file, you need to press Ctrl+X > Y > Enter.

8. Executing a Node.js File

After the Node.js Request file is successfully saved. Next you can execute the file by inputting the following syntax.

sudo node your-file-name.js

Don't forget to change your-file-name according to the name created earlier.

After the Node.js file is executed, the display in the terminal will be like in the following image

The image above shows that the data has been successfully sent to Antares IoT Platform

9. Check Data on Antares IoT Platform

To ensure that the data has been sent correctly from the Raspberry Pi to the Antares IoT Platform, you can open your device URL. The following image shows the display on the Antares IoT Platform that has received data from the Raspberry Pi.

Last updated