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.

pageCommon Raspberry SSH Prerequisites

Follow These Steps

1. Switching on the Raspberry Pi

Before turning on the Raspberry Pi, make sure you have installed the SD Card that has the OS installed in the slot available on the Raspberry Pi board. You can turn on the Raspberry Pi by connecting it to the power source using USB-C as shown below.

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

2. Open PuTTY App

The PuTTY application is used to access the Raspberry Pi via SSH. The image below shows the initial appearance of the application when opened.

3. Check Raspberry Pi Connection with WiFi Network

To connect the Raspberry Pi via SSH using the PuTTY application, you need to input the Host Name or IP Address of the Raspberry Pi. To check whether the Raspberry Pi is connected to a WiFi network, you can do the "ping" command to the Raspberry Pi hostname. Open a command prompt and enter the following syntax.

ping raspberrypi

If the Raspberry Pi is connected to a WiFi network, the device will send a reply message to the computer as shown below.

If the ping process fails, then it is likely that the Raspberry Pi has not been connected to the WiFi network that was set up during the OS installation process. In this condition, what you can do is wait a few moments until ping can be done. If it still fails, you can restart the Raspberry Pi by unplugging and reconnecting the power source from USB.

If the Raspberry Pi still cannot connect to the Wi-Fi network that you have set during the OS installation process, then the last step that can be done is to change the Wi-Fi network used by installing the OS again in the Configure Wireless LAN section as shown below.

4. Access Raspberry Pi by SSH

After ensuring that the Raspberry Pi can connect to our computer, then we can input the Raspberry Pi hostname in the PuTTY application as shown below, then press Open.

Select Accept in the display that appears, then enter the username and password in the terminal that opens. Input the username and password according to what was created during OS installation. After successfully logging in, the display on the terminal will be as follows.

5.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.

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.

6.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.

7. Instal Permintaan Modul

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.

8. Creating a Node.js File

After successfully installing the necessary libraries, you need to create a file with the Node.js format. Enter the syntax below in the terminal to create the Node.js file.

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.

9. 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.

10. 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

11. Check Data on Antares IoT Platfom

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