Raspberry Pi node.js and how to start with programming GPIO (Pixpress project on Github) – Part 01

0 0
Read Time:2 Minute, 53 Second

Last time I was a little bit busy… because of playing with Raspberry Pi 🙂 Yeah… I think it made me motivated to digg deeper in nodejs/expressjs.

Firstly what I needed to do was installation of system on SD card. Ive downloaded Raspbian (here) and installed Jessie installation with Xwindows.(here you can find info how to install it) It wasn’t very stable because everytime when Ive installed node and VNC it just collapsed after simple reboot. Than I needed to reinstall it second time / third time / fourth time… I was frustrated and installed simple version of Raspbian – Lite. This version is more stable and since Ive installed it I didnt needed any reinstallation of system. And finally

OS is stable!

What was next? Node.js installation! What you need is you need to connect to your raspberry through SSH. Open your terminal and write:

ssh pi@<IP_of_RaspberryPI>

You will be asked for password. What you need to write (till you change the admin password) is:

raspberry

And now you can start with installation on node.js

sudo apt-get install node

Than install NPM:

sudo apt-get install npm

Short version of installation is:

sudo apt-get install node npm

Lets bring GPIO to life!

Lets create node project:

npm init

Than what you will need is install onoff library:

npm i onoff --save

This will help you to enable / disable GPIO pins. To see how PINs in RPi are numbered lets check this image:

As you can see it rahter has no logic order 🙂 Lets bring it to life! Lets create a file on.js

var GPIO = require('onoff').Gpio,
led = new GPIO(18, 'out'),

led.writeSync(1);

Now run it:

node on.js

Easily when you want to disable the LED you just need to change a code and in place of 1 put 0 like this:

var GPIO = require('onoff').Gpio,
led = new GPIO(18, 'out'),

led.writeSync(0);

Automatic restart of Node.js app

If you are working on one file in node.js and you dont want to restert node app each time when you are changing the code inside the file just install the supervisor:

npm i supervisor -g

Than you can start app with supervisor:

supervisor app.js

Ok so what can you do next? Lets create some light effect. Create new file and add this code:

var GPIO = require('onoff').Gpio,
led04 = new GPIO(4, 'out'),
led05 = new GPIO(5, 'out'),
led06 = new GPIO(6, 'out'),
led07 = new GPIO(7, 'out'),
led08 = new GPIO(8, 'out');

var leds = [led04, led05, led06, led07, led08],
    counter = 0;

console.log(leds.length);
setInterval(function() {
  leds.forEach(function(currentValue) {
    currentValue.writeSync(0);
  });

  leds[counter].writeSync(1);
  counter++;

  if (counter >= (leds.length)) counter = 0;
  console.log(counter);
}, 300);

So what is going on now? fristly led04 will be enabled than 04 will be disabled and 05 will be enabled. Next step 05 disabled 06 enabled. Than 06 disabled 07 enabled. Than 07 disabled 08 enabled. Than 04 enabled 08 disabled. Lets begin in step 01 🙂 As you can see it on video:

As you can see this is pretty nice blinking effect:)
In next part I will try to share information how to include express to this project and start connecting with server from external devices.

You can see the full project on Github.
https://github.com/fedojo/pixpress

About Post Author

Piotr Sikora

Piotr Sikora Founder of WolePapierowe.com Co-founder of Liderazgo.pl MeetJS Kielce Committee member. JavaScript and Python enthusiast.
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *

© UiCore 2024. All Rights Reserved.