Node.js와 라즈베리 파이


Raspberry Pi는 소형의 다목적 컴퓨터입니다.

Node.js를 사용하면 Raspberry Pi로 놀라운 일을 할 수 있습니다.


라즈베리파이란?

Raspberry Pi는 작고 저렴하며 놀라운 기능을 갖춘 신용 카드 크기의 컴퓨터입니다.

이것은 Raspberry Pi Foundation에서 개발했으며 지금까지 만들어진 가장 다재다능한 기술일 것입니다.

제작자 Eben Upton의 목표는 프로그래밍 기술과 하드웨어 이해도를 향상시킬 수 있는 저렴한 장치를 만드는 것이었습니다.

장치의 작은 크기와 가격으로 인해 땜장이, 제조업체 및 전자 제품 애호가의 광범위한 프로젝트의 중심이 되었습니다.


라즈베리 파이와 Node.js

Raspberry Pi에는 GPIO(범용 입력/출력) 핀 행이 있으며 이를 사용하여 실제 세계와 놀라운 방식으로 상호 작용할 수 있습니다. 이 튜토리얼은 Node.js와 함께 사용하는 방법에 초점을 맞출 것입니다.


내가 무엇이 필요 하나?

이 자습서에는 Raspberry Pi가 필요합니다. 이 예제에서는 Raspberry Pi 3을 사용하지만 이 자습서는 대부분의 버전에서 작동합니다.

필요한 하드웨어:

  • 라즈베리 파이 컴퓨터
  • MicroSD 메모리 카드(16GB 이상의 클래스 10 권장)
  • MicroSD-SD 메모리 카드 어댑터(일반적으로 MicroSD 카드에 포함)
  • Raspberry Pi에 전원을 공급하는 마이크로 USB 전원 공급 장치(2.5A 이상 권장)
  • Raspberry Pi용 WiFi/Ethernet 케이블 연결(Raspberry Pi 3에는 WiFi가 내장되어 있으므로 필요하지 않음)
  • 인터넷 및 SD 메모리 카드 리더기가 있는 작동 중인 컴퓨터(Raspberry Pi용 OS(운영 체제)를 메모리 카드에 가져오는 데 사용됨). 튜토리얼에서는 이를 위해 Windows 컴퓨터를 사용하지만 원하는 경우 Mac 또는 Linux 컴퓨터를 사용할 수 있습니다.
  • HDMI 모니터, USB 키보드(Raspberry Pi의 첫 번째 부팅에만 일시적으로 필요함)

이 자습서의 이후 장에서는 Raspberry Pi에 연결하는 특수 센서 또는 장치를 사용합니다. 관련 장에서 이를 특별 요구 사항으로 지정합니다.

이미 Raspbian, 인터넷 및 활성화된 SSH를 사용하여 Raspberry Pi를 설정했다면 "Raspberry Pi에 Node.js 설치" 단계로 건너뛸 수 있습니다.


MicroSD 카드에 Raspbian OS 이미지 쓰기

Raspberry Pi를 사용하기 시작하기 전에 OS를 설치해야 합니다.

라즈비안은 데비안 리눅스 기반의 무료 운영체제로 라즈베리파이에 최적화되어 있습니다.

https://www.raspberrypi.org/downloads/raspbian/ 에서 최신 Raspbian 이미지를 컴퓨터로 다운로드합니다.

우리는 튜토리얼에서 "LITE" 버전을 사용합니다. 왜냐하면 Raspberry Pi를 헤드리스 서버로 설정하기 때문입니다(키보드/디스플레이를 연결하지 않고 SSH를 통해 연결합니다). 원하는 버전을 사용할 수 있지만 이 튜토리얼은 "LITE" 버전을 중심으로 작성되었습니다.

MicroSD 메모리 카드를 컴퓨터에 삽입합니다(필요한 경우 SD 어댑터를 통해). 파일 탐색기를 열어 작동하는지 확인합니다.

Etcher는 메모리 카드에 이미지를 플래싱하는 프로그램입니다. https://etcher.io/ 에서 Etcher를 다운로드하고 설치합니다.

에칭 실행:

에쳐 실행

Click "Select image" button and find the Raspbian zip file that you downloaded.

Click the "Select drive" button and specify the memory card as the target location.

Click the "Flash!" button to write the image to the memory card.

After Etcher is finished writing the image to the memory card, remove it from your computer.


Set up Your Raspberry Pi

To get the Raspberry Pi ready to boot we need to:

  1. Insert the MicroSD memory card into the Raspberry Pi
  2. Connect the USB keyboard
  3. Connect the HDMI cable
  4. Connect the USB Wi-Fi adapter (or Ethernet cable). Skip this step if you are using a Raspberry Pi 3
  5. Connect the micro USB power supply
  6. The Raspberry Pi should now be booting up

When the Raspberry Pi is finished booting up, log in using username: pi and password: raspberry


Set Up Network on the Raspberry Pi

If you will use a Ethernet cable to connect your Raspberry Pi to the internet, you can skip this step.

For this section we will assume you have a Raspberry Pi 3, with built in WiFi.

Start by scanning for wireless networks:

pi@raspberrypi:~ $ sudo iwlist wlan0 scan

This will list all of the available WiFi networks. (It also confirms that your WiFi is working)

Now we need to open the wpa-supplicant file, to add the network you want to connect to:

pi@raspberrypi:~ $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

This will open the file in the Nano editor. Add the following to the bottom of the file (change wifiName and wifiPassword with the actual network name and password):

network={
  ssid="wifiName"
  psk="wifiPassword"
}

Press "Ctrl+x" to save the code. Confirm with "y", and confirm the name with "Enter".

And reboot the Raspberry Pi:

pi@raspberrypi:~ $ sudo reboot

After reboot, log in again, and confirm that the WiFi is connected and working:

pi@raspberrypi:~ $ ifconfig wlan0

If the WiFi is working propery, the information displayed should include an IP address, similar to this:

inet addr:192.168.1.50

Write down that IP address, as we will use it to connect to the Raspberry Pi via SSH.



Enable SSH, Change Hostname and Password

Now your Raspberry Pi is connected to the internet, it is time to enable SSH.

SSH allows you up use the Raspberry Pi without having a monitor and keyboard connected to it.

(You will need a SSH client for this on your non-Raspberry Pi computer. We use PuTTY for windows)

Open the Raspberry Pi Software Configuration Tool:

pi@raspberrypi:~ $ sudo raspi-config

You should see a menu like this:

raspi-config Main Screen

Select option 5 Interfacing Options:

raspi-config Main Screen

Select option P2 SSH, to activate SSH:

raspi-config Main Screen

Confirm with YES, to activate SSH:

raspi-config Main Screen

SSH is now enabled, and you should be in the main menu again.

Select 1 Change User Password, and follow the instructions to change the password. Choose a secure password, but something you will remember:

raspi-config Main Screen

After you have finished changing the password, you should be back in the main menu.

Select 2 Hostname, and follow the instructions to change the hostname:

raspi-config Main Screen

After you have finished changing the hostname, you should be back in the main menu.

Now we will close the menu and save the changes:

raspi-config Main Screen

When selecting Finish, you will get the option to reboot. Select Yes to reboot the Raspberry Pi.

raspi-config Main Screen

You can now unplug the monitor and keyboard from the Raspberry Pi, and we can log in using out SSH client.

Open PuTTY, type in the IP address for your Raspberry Pi, and click Open:

raspi-config Main Screen

Log in using the username pi and the new password you specified.

You should now see a command line like this: (we used w3demopi as our hostname)

pi@w3demopi:~ $

You are now able to run your Raspberry Pi in "Headless-mode", meaning you do not need a monitor or keyboard. And if you have a WiFi connection, you do not need a ethernet cable either, just the power cable!


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!

If you want to learn more about Node.js, follow our tutorial: https://www.w3schools.com/nodejs/

In the next chapter we will get to know the GPIO and how to use it with Node.js.