Setting Up PI and Sample Node.js program to Use GPIO Pins
www.google.com

Setting Up PI and Sample Node.js program to Use GPIO Pins

Hi Geeks and Nerds,

    Recently i got a opportunity to work on the Raspberry pi 2 B model. Once i started out there was a lot of resource which was available on the internet but none of them were precise and simple for me to follow, so i took some pain and time to explore things and Research. After a weeks of research and implementation i got things right and working perfectly . I wanted to share my experience  for other enthusiasts and beginners to guide them in working with Raspberry Pi.

Setup

Device : Raspberry PI 2 - MODB 1 GB - Quad Core

What Will You Need.

1. 8 GB  Sd Card (Min)

2. Power Supply

3. Ethernet or Wifi Dongle

4 . Monitor , HDMI cable, Mouse ,Keyboard (One Time - Initially , Will teach you how to use Remote Connection to connect to the pi and you system at the same time with ease)  

Initial Setup -Download the OS image and install it in SD card

NOTE:  You can also use NOOB to install Different OS on the SD Card.

Once after installation plugin the SD card to the pi and connect it to the Monitor.open up terminal in pi and find the ip address of the machine use the following command:

ifconfig

Based on whether you are using an ethernet or Wifi dongle find the ip address of the Pi and note. Now you can remove the mouse,keyboard,and HDMI cable (it should be connected to the internet ). In Your PC open up Terminal/CMD and try pinging to the PI machine. If the ping fails check if the Firewall is switched off in the PC. Once Ping gets through you can connect the the PI's terminal through ssh command:

ssh pi@"ip address"

pi is the username and the default password is : raspberry.

Once connect to the Pi through terminal the next process is to install VNC in the Pi and Connect to the Pi's Screen through remote connection.Use the following command to install VNC

sudo apt-get install tightvncserver

After installation setup password for VNCserver by running "tightvncserver" in Pi's terminal. use the following command to start screen share

vncserver :1 -geometry 1920x1080 -depth 24

Now, on you Mac, open up safari and navigate to

vnc://"pi's ip":5901

from the address bar.  5901 is the port for screen sharing . Safari will prompt for the ip address again. Press enter and a screen sharing password should prompt, provide the VNC's password to start screen share.

Use the following command to stop the VNC share:" service lighted stop"

The above process is simple but the only problem is, we need to run "vncserver :1 -geometry 1920x1080 -depth 24" every time we restart the pi.

To work around that, we will add this command to the list of boot tasks. So as soon the pi boots, the vncserver will start. Refer here to add to pi's boot ( Automation and run at boot - Section).

Installing Node.js on Pi

The current build of raspbian comes with default node installation. you can verify the package by "node -v" command.

You can install node and npm from the following process. (Recommended).

curl -sL https://deb.nodesource.com/setup | sudo bash -

Install it by running

sudo apt-get install nodejs

verify the installation of node and npm by using "node -v" and "npm -v" commands. Now node and npm step up is complete. next we need to install the distribution for providing access to hardware architecture. We are using NodeSource  in our project.

Do the following based on the version of node installed in your PI

Node.js v5.x:

  1. curl -sL https://deb.nodesource.com/setup_5.x | bash -
  2. apt -get install -y nodejs

Node.js v4.x:

  1. curl -sL https://deb.nodesource.com/setup_4.x | bash -
  2. apt -get install -y nodejs

Now the setup is complete. You can start coding in node. 

 

 

 

 

 

 

Sample Node Program.

  1. var Gpio = require('onoff').Gpio, 
  2.  led = new Gpio(18, 'out'); //The number corresponds to the GPIO Pin from above  figure
  3.  var iv = setInterval(function(){led.writeSync(led.readSync() === 0 ? 1 : 0)}, 500); 
  4. // Stop blinking the LED and turn it off after 5 seconds.
  5. setTimeout(function() 
  6. {    clearInterval(iv); // Stop blinking  
  7.   led.writeSync(0);  // Turn LED off.
  8.     led.unexport();    // Unexport GPIO and free resources
  9. }, 5000);

Bread board setup.

The resistor will be connected to ground and the led to the GPIO port (In this case GPIO 18). of the pin configuration in the PI.

For Any further Queries and Reference for Source Code.Ping Me

- chao. :)

vaghula krishnan

Technical Product Architect - Mobile and Backend Infrastructure

8 年

Ya true but looking for potential idea.. Already built complete project but looking for cooler ideas

回复
RaviKumar Nagaraja

Senior Principal Software Engineer - Cloud.com(Netscalar) previously Citrix

8 年

U can build complete IOT using one of cool sensor like Thingy which was in kickstarter

回复
RaviKumar Nagaraja

Senior Principal Software Engineer - Cloud.com(Netscalar) previously Citrix

8 年

U

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了