Raspberry Pi and JavaScript — A simple site health check monitor — Part 1

Recently I bought Raspberry Pi 3 and I am quite excited about it. The first thing I did with Raspberry Pi is to run a JavaScript (Node.js) program in it. In this blog post we will see how to set up a Raspberry Pi and install node.js in it. (In case you don’t know node.js is the server side version of JavaScript). Once we have the raspberry ready to run a node.js program, we will write a program to monitor a website and sends you a mail when that site is down. So get ready for the ride!

Raspberry Pi:

To put it in simple words, Raspberry Pi is a mini CPU and it looks like below:

Raspberry Pi 3

It has ports for a mini SD card which can act as its hard drive, and ports for LAN and USB. You can power up the Pi using your normal Android phone USB charger. You can use your existing laptop’s keyboard and monitor to use with this Raspberry Pi or connect an external one.

Things needed:

Following are the things required for this project.

  1. Raspberry Pi 3. (Purchase from Amazon)
  2. Ethernet cable. (Purchase from Amazon)
  3. USB to ethernet Adapter if your laptop does not have a LAN port. (Purchase from Amazon)
  4. Micro SD card of size 16 or 32 GB. (Purchase from Amazon)
  5. SD card adapter to use the micro SD card in your computer directly. (Purchase from Amazon)
  6. Micro USB cable. (Mostly comes with Android phones to transfer content from computer to phone or vice versa)

It is better to have an external keyboard and monitor to connect the Raspberry Pi. But it is not mandatory. I didn’t have a spare keyboard and monitor. So I used my laptop’s monitor and keyboard to connect the Raspberry Pi.

Once you have all the above items, lets start the project.

Preparing the Raspberry Pi:

Raspberry Pi is a mini CPU. So we need an operating system to be installed to work with the hardware. Fortunately, we have a flavour of Linux called Raspbian to be used with Raspberry Pi. You can use any operating system that you need. We will use the open source Raspbian operating system for the rest of this tutorial.

Before downloading the operating system, we need to install an operating system installer called NOOBS. This is a software which helps us in installing Raspbian into our Raspberry.

Installing OS in Raspberry Pi:

You can purchase micro SD cards that comes pre installed with NOOBS. If not, you can buy a fresh micro SD card and install NOOBS as mentioned below.

  1. Format the SD card with file system as FAT32. You can use free tools like https://www.sdcard.org/downloads/formatter_4/ to format the SD card.
  2. Once it is formatted, download the NOOBS from the Raspberry site. Link: https://www.raspberrypi.org/downloads/noobs/
  3. Choose the ‘offline and network install’ version of NOOBS. This will download the operating system along with the NOOBS.
  4. Once downloaded, extract the zip file and copy all the contents of zip file (Not the outermost folder which you get after unzipping, but the contents inside the folder) into your micro SD card. (Please make sure that your laptop has a card reader. If it can’t read a micro SD card, you can use the micro SD card adapter).
  5. Since we don’t have external monitor or keyboard, lets make sure that the OS is installed silently on power up.
  6. To do that, go to the contents that you have copied in your SD card and go inside the OS folder.
  7. Delete all the folders inside OS except the Raspbian folder. (This is the OS that we are going to install. So lets make sure only that OS exists in the card)
  8. Now go inside the Raspbian folder and open ‘flavors.json’ in your favorite text editor. Make sure it has only “Raspbian” (not “Raspbian — Boot to Scratch”) in the json file. The JSON file should look like below. Also make sure your text editor or OS does not create any temporary files in this folder.
{
"flavours": [
{
"name": "Raspbian",
"description": "A Debian wheezy port, optimised for the Raspberry Pi"
}
]
}

9. Since we are going to silent install Raspbian OS, lets edit recovery.cmdline file and append silentinstall in the argument list. The file contents should look like this now:

runinstaller quiet vt.cur_default=1 coherent_pool=6M elevator=deadline silentinstall

10. Now we have the NOOBS and Raspian OS on our micro SD card. We will insert the micro SD card into the Raspberry Pi. The card slot is below the Raspberry and the slot allows the card in only one direction.

See the Micro SD card at the bottom

11. Before we power up the Raspberry, lets connect one end of the ethernet cable (LAN cable) into the Raspberry Pi and another end to our laptop. If you don’t have a LAN port in your computer, you can use the USB-Ethernet adapter.

LAN cable attached to the Raspberry PI

12. Now, lets share the internet connection of our laptop to Raspberry. If you are on Mac, you can go to System Preferences -> Click on ‘Sharing’ -> Tick mark the ‘Internet Sharing’ option. If you are on Windows you can follow the tutorial here: https://www.youtube.com/watch?v=hnSLtgHIIM8

13. Everything is ready and it is time to power up our Raspberry Pi. Use the USB to micro USB connector (one that comes with Android phones to transfer files). Connect the USB end to your laptop and the micro USB end to the Raspberry Pi. You should see a red light blinking at the Raspberry Pi. (See in the image above).

14. Also if the internet is shared properly, you should see a green and red light below the LAN cable.

15. Leave the Raspberry Pi untouched for the next 1 hour so that the OS Raspbian is silently installed in the Raspberry Pi. (Make sure your laptop does not run out of power and keep your fingers crossed. 1 hour is the upper time limit. The OS should get installed before that). If you have an external monitor and keyboard you can connect it to the raspberry and watch the install as it progresses.

Logging into the Raspberry Pi:

If everything goes fine, the OS should be installed in the Raspberry and it should be ready for use.

  1. Go to the command line of your laptop and type the following command:

arp -a

You should see something like this below:

2. In the above list, it is most likely that 192.168.2.2 is your raspberry’s IP address. But it might change also. So do a trial and error.

3. Type the following command to ssh into the Raspberry Pi

ssh pi@192.168.2.2

4. It will ask for a password. The default password is raspberry.

5. If the above login is successful, then it means you are into the Raspberry and you see something like this below.

Successful login into Raspberry

Type general commands like ‘ls’, ‘cd’, ‘mkdir’ to navigate around the Raspberry in the command line. If you can’t login, then its most likely the case that the OS is not installed properly. So please repeat the OS installation steps as mentioned above.

Installing Node.js in Raspberry:

We will install node.js in the Raspberry using the following commands.

  1. After you login into the Raspberry Pi using command line, update the package manager in Raspberry Pi

sudo apt-get update -y && sudo apt-get upgrade -y

2. Once the package manager is updated, we will download the latest version of node by running the command below

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

3. Install node using the command:

sudo dpkg -i node_latest_armhf.deb

4. If everything goes well, you can check the version of node installed using

node -v

The above command should give you the version of node installed. Mine gave the version number as 6.2.2

Now node.js is installed successfully in our Raspberry and we can start writing programs.

Lets write a simple server health check program using node.js. This program will monitor any site and sends us an e-mail when the site is down. For example, you have your own blog and you want to be notified via email if your blog is down. Using Raspberry Pi, we can write a simple cost effective solution for the above problem. We will see how to do that in Part 2. Until then stay curious :)

A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.