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.
Raspberry GUI General PrerequisitesFollow 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.

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.

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
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
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
After inputting the syntax above and pressing enter, the terminal display will change to the image below.

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);
});
After inputting the above code, the display on the terminal will be as shown below.

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
After the Node.js file is executed, the display in the terminal will be like in the following image

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