7- Regular PC using Node.js

This section explains how to connect send data to the AskSensors IoT platform from regular PC using node.js.


a) Prerequisites :

b) Required Hardwares :

  • Raspberry Pi Model B+ or Model B (you can also use a Raspberry Pi 2 Model B)
  • USB micro cable to power up your Pi.
  • MicroSD card, I recommend a class 10 card that is 16 GB or higher.
  • MicroSD to SD memory card adapter, you will need to use the SD card reader on your laptop/desktop to write to the microSD card.

c) Install Node.js and NPM :

  • download Node.js . It’s free and available for Windows, MacOs and Linux (in my case I’m using Windows).
  • Run the installer, follow the prompts in the installer. Accept the license agreement, click the NEXT button a bunch of times and accept the default installation settings.
  • NPM is distributed with Node.js, which means that when you download Node.js, you automatically get npm installed on your computer.
  • Restart your computer. You will not be able to run Node.js until you restart your computer!

d) Test the Node.js :
To Make sure you have Node.js and NPM installed, we will run simple commands to see what version of each is installed and to run a simple test program:

    • Test Node.js: To confirm that you have Node.js installed, type this command in your terminal:

  node -v

    • This should print a version number.


    • Test NPM: To check if you have NPM installed, you can run this command in your terminal:

  npm -v

e) Software :
Download the .js file from github. It requires the installation of the https npm package.
You will need to set your Api Key In.

  var ApiKeyIn = '................'; // Api Key In

f) Run the code :
Type this command to run the .js file:
  node https_GET.js

g) Source Code :
A basic source code is shown below. Please refer to the AskSensors Github page to get the latest version and updates.

* AskSensors node.js API
* Description: This demo shows how to send data to AskSensors IoT platfom over HTTPS GET Requests.
* Author: https://asksensors.com, 2018
* github: https://github.com/asksensors/AskSensors-node.js-API
// includes
var https = require('https');
const request = require('request');

// node.js User Configuration
var ApiKeyIn = '...................'; // TODO: fill your sensor Api Key In given by https://asksensors.com
var writeInterval = 25000; // TODO; adjust your timer interval (in ms)
// API host name
var host = 'https://api.asksensors.com';
// Function declaration: send data
function send_data(){
var url = host + '/write/';
url+= ApiKeyIn;
url+= '?module1='
url+= 100*Math.random();// random data in module 1;
url+= '&module2='
url+= 100*Math.random();// random data in module 2;
console.log('Data sent to:' + url);
request(url, { json: true }, (err, res, body) => {
if (err) { return console.log(err); }

send_data();// send first data
// Send data every writeInterval
setInterval(send_data, writeInterval); // setInterval

Was this article helpful to you? Yes No

How can we help?