Install Node.js on Raspberry Pi

With the Raspberry Pi properly set up, login in via SSH, and update your Raspberry Pi system packages to their latest versions.

Update your system package list:

pi@w3demopi:~ $ sudo apt-get update

Upgrade all your installed packages to their latest version:

pi@w3demopi:~ $ sudo apt-get dist-upgrade

Doing this regularly will keep your Raspberry Pi installation up to date.

To download and install newest version of Node.js, use the following command:

pi@w3demopi:~ $ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

Now install it by running:

pi@w3demopi:~ $ sudo apt-get install -y nodejs

Check that the installation was successful, and the version number of Node.js with:

pi@w3demopi:~ $ node -v

Get Started with Raspberry Pi and Node.js

Now you have a Raspberry Pi with Node.js installed!

 

Having an SSH connection established with your Raspberry Pi, enter the following commands to install Node-RED:

pi@raspberry:~ $ bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)

The installation should be completed after a couple of minutes.

Autostart Node-RED on boot

To automatically run Node-RED when the Pi boots up, you need to enter the following command:

pi@raspberry:~ $ sudo systemctl enable nodered.service

Now, restart your Pi so the autostart takes effect:

pi@raspberry:~ $ sudo reboot

Testing the Installation

When your Pi is back on, you can test the installation by entering the IP address of your Pi in a web browser followed by the 1880 port number:

http://YOUR_RPi_IP_ADDRESS:1880

In my case is:

http://192.168.1.98:1880

A page like this loads:

node-red-first-look

Node-RED overview

Let’s take a look at the Node-RED visual interface.

node-red-raspberrypi

Main sections

On the left-side, you can see a list with a bunch of blocks. These blocks are called nodes and they are separated by their functionality. If you select a node, you can see how it works in the info tab.

In the center, you have the Flow and this is where you place the nodes.

node-red-raspberrypi-sections

Creating a simple flow

Let’s test a simple example of a flow. Start by dragging an Inject node to your flow. Then, also drag a Debug node.

simple-floww

Connect your nodes together. You can drag them to confirm that they are connected.

Now, let’s edit the inject node. Double-click the node. In the figure below you can see different settings you can change.

Select string and type Hello!.

edit-inject-node

To save your application, you need to click the deploy button on the top right corner.

deploy-button

Your application is saved.

Testing the flow

Let’s test our simple flow. Open the debug window and click the Inject node to trigger the “Hello!” string.

node-red-f

As you can see, our message is being printed in the debug window. This is a very basic example and it doesn’t do anything useful. However, the purpose of this post is to get you familiar with the Node-RED interface.

Wrapping Up

This post is a quick guide to setup Node-RED on a Raspberry Pi and create a simple flow. I’ll be posting future tutorials doing things with a real world application, but I thought it would be useful to introduce this software first.

Source : https://randomnerdtutorials.com/getting-started-with-node-red-on-raspberry-pi/

En poursuivant votre navigation sur mon site, vous acceptez l’utilisation des Cookies et autres traceurs  pour réaliser des statistiques de visites et enregistrer sur votre machine vos activités pédagogiques. En savoir plus.