6- Connect Raspberry Pi over HTTPS

This section explains how to connect your Raspberry Pi to the AskSensors IoT platform 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) Set Up Your Raspberry Pi :
Running a basic setup of Raspbian on Raspberry Pi become easy and clearly explained in this Getting Started Guide.
Here are the main steps:

  • Download NOOBS
  • Extract it on your SD
  • Plug it in and turn on the Raspberry Pi.
  • When prompted, choose to install Raspbian and let it run.

That’s it, Now we have an amazing Raspberry Pi system that can be used for a variety of tasks!

d) Install Node Js :
Here we go in the Node.js installation, Installing an ARM-version of Node become very easy!

  • Make sure you are connected to internet
  • Open the terminal on the Raspberry Pi. Type these commands:

wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb

Basically, It shouldn’t take too long to download and install.

e) Test Node.js :
To make sure that node.js runs correctly, Type the commands below. It should return the current version of node and npm installed.

node -v
npm -v
Make sure it doesn’t give any error. If everything is ok, we can move to the next step!

f) 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

g) Run the code :
Now you are ready to run the final script :
  node https_GET.js

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


/*
* Connect Raspberry Pi to AskSensors
* Description: This code sends dummy data to askSensors IoT platform over HTTPS GET Request.
* Author: https://asksensors.com, 2018
* github: https://github.com/asksensors/AskSensors-Raspberry-Pi
* Instructables: https://www.instructables.com/id/Connect-Raspberry-Pi-to-Asksensors-IoT-Platform-Us/
*/
// includes
var https = require('https');
const request = require('request');

// Raspberry Pi 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?