> For the complete documentation index, see [llms.txt](https://docs.antares.id/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.antares.id/en/code-and-library-examples/raspberry-pi/ssh-secure-shell/sending-data-using-node.js.md).

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

{% content-ref url="/pages/fgs2HChb8kYTL59m2zE8" %}
[Common Raspberry SSH Prerequisites](/en/code-and-library-examples/raspberry-pi/ssh-secure-shell/common-raspberry-ssh-prerequisites.md)
{% endcontent-ref %}

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

{% hint style="info" %}
A powered-on Raspberry Pi will be signalled by an illuminated LED indicator near the USB-C port.
{% endhint %}

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/KjNp3eK2qYoycAJH6SyS/2.%20Power%20Source%20RasPi.png" alt=""><figcaption><p>Connecting Raspberry Pi with Power Source</p></figcaption></figure>

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/eU9ewQTFrdeEZzSSUkC9/1_PuTTY.png" alt=""><figcaption><p>PuTTY App View</p></figcaption></figure>

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/0ngq2o2DslzeWoVcKhSb/3_Ping.png" alt=""><figcaption><p>Ping ke Raspberry Pi</p></figcaption></figure>

{% hint style="info" %}
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 r**estart the Raspberry Pi** by unplugging and reconnecting the power source from USB.
{% endhint %}

{% hint style="info" %}
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.
{% endhint %}

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/RkmVmstp4mxcwKqatxP1/9_Uname%20&amp;%20Password.png" alt=""><figcaption><p>Changing the WiFi Connection on Raspberry Pi</p></figcaption></figure>

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/v2I1DTTN83wVBvh1BHQ8/4_SSH.png" alt=""><figcaption><p>Inputting Host Name in PuTTY Application</p></figcaption></figure>

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.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/y5J2QzBJAsOkJ0jUOioL/5_Terminal.png" alt=""><figcaption><p>Display of Raspberry Pi Terminal Accessed by SSH</p></figcaption></figure>

### 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
```

{% hint style="info" %}
**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.
{% endhint %}

{% hint style="info" %}
**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.
{% endhint %}

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.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/DMDv1Pvb4eDHoPPmQHya/6_Node%20Version.png" alt=""><figcaption><p>Node.js version and npm Library</p></figcaption></figure>

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/Et7NEuHgu8GnH6nfg6Kx/7_Inisiasi%20npm.png" alt=""><figcaption><p>Jika proses inisialisasi pustaka npm berhasil, terminal akan mengeluarkan output seperti yang ditunjukkan di bawah ini.</p></figcaption></figure>

### 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
```

{% hint style="info" %}
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.
{% endhint %}

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/MBNcUiTthzzZurDWMMQr/8_Modul%20Request.png" alt=""><figcaption><p>HTTP Request Module Installation</p></figcaption></figure>

### 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
```

{% hint style="info" %}
You need to change **your-file-name** to your preferred filename.
{% endhint %}

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/64sVnzpxg5WQbZu0YB51/9_File%20Node.js.png" alt=""><figcaption><p>Created Node.js File</p></figcaption></figure>

{% hint style="info" %}
The above view shows an empty Node.js file ready to be inputted with programme code.
{% endhint %}

### 9. Inputting Programme Code

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

{% code lineNumbers="true" %}

```python
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);
});
```

{% endcode %}

{% hint style="info" %}
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.
{% endhint %}

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/URZwmlwtsL9B7nG0cKlM/10_Kode%20Kirim%20Data.png" alt=""><figcaption><p>Inputted Programme Code</p></figcaption></figure>

{% hint style="info" %}
To save the created Node.js file, you need to press **Ctrl+X > Y > Enter.**
{% endhint %}

### 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
```

{% hint style="info" %}
Don't forget to change **your-file-name** according to the name created earlier.
{% endhint %}

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/1SXQY7reaUeFVvHbDDWs/11_Sudo%20node.png" alt=""><figcaption><p>Terminal Output when Node.js File is Executed</p></figcaption></figure>

{% hint style="info" %}
The image above shows that the data has been successfully sent to **Antares IoT Platform**
{% endhint %}

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

<figure><img src="https://3873791589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cujmJ5QHdJaAjH815aZ%2Fuploads%2FtKPBYqN1s6rZBdLEmhc5%2Fimage.png?alt=media&amp;token=b3dc4b8b-3b47-4641-89b7-6d02cc8b08a3" alt=""><figcaption><p>Display on Antares IoT Platform that has Received Data from Raspberry Pi</p></figcaption></figure>
