Node.js và Raspberry Pi


Raspberry Pi là một máy tính nhỏ, đa chức năng.

Với Node.js, bạn có thể làm những điều tuyệt vời với Raspberry Pi của mình.


Raspberry Pi là gì?

Raspberry Pi là một máy tính cỡ thẻ tín dụng nhỏ, giá cả phải chăng và có khả năng hoạt động đáng kinh ngạc.

Nó được phát triển bởi Raspberry Pi Foundation và nó có thể là công nghệ linh hoạt nhất từng được tạo ra.

Mục tiêu của người sáng tạo Eben Upton là tạo ra một thiết bị giá rẻ giúp cải thiện kỹ năng lập trình và hiểu biết về phần cứng.

Do kích thước nhỏ và giá cả của thiết bị, nó đã trở thành trung tâm của một loạt các dự án của những người mày mò, chế tạo và những người đam mê điện tử.


Raspberry Pi và Node.js

Raspberry Pi có một hàng chân GPIO (đầu vào / đầu ra Mục đích chung) và những chân này có thể được sử dụng để tương tác theo những cách tuyệt vời với thế giới thực. Hướng dẫn này sẽ tập trung vào cách sử dụng chúng với Node.js.


Tôi cần những gì?

Đối với hướng dẫn này, bạn cần có Raspberry Pi. Trong các ví dụ của chúng tôi, chúng tôi sử dụng Raspberry Pi 3, nhưng hướng dẫn này sẽ hoạt động với hầu hết các phiên bản.

Phần cứng cần thiết:

  • Máy tính Raspberry Pi
  • Thẻ nhớ MicroSD (Chúng tôi khuyên dùng loại 10 với 16 GB trở lên)
  • Bộ chuyển đổi thẻ nhớ MicroSD sang SD (thường đi kèm với thẻ MicroSD)
  • Nguồn điện Micro USB để cấp nguồn cho Raspberry Pi (khuyến nghị 2,5A trở lên)
  • Kết nối WiFi / Cáp Ethernet cho Raspberry Pi (Không cần thiết cho Raspberry Pi 3 vì nó đã tích hợp WiFi)
  • Một máy tính đang hoạt động có internet và đầu đọc thẻ nhớ SD (được sử dụng để lấy Hệ điều hành (Hệ điều hành) cho Raspberry Pi vào thẻ nhớ). Trong hướng dẫn của chúng tôi, chúng tôi sử dụng máy tính Windows cho việc này, nhưng bạn có thể sử dụng máy tính Mac hoặc Linux nếu bạn thích
  • Màn hình HDMI, bàn phím USB (chúng tôi chỉ cần những thứ này tạm thời cho lần khởi động đầu tiên của Raspberry Pi)

Đối với các chương sau của hướng dẫn này, chúng tôi sẽ sử dụng các cảm biến hoặc thiết bị đặc biệt mà chúng tôi kết nối với Raspberry Pi. Chúng tôi sẽ nêu rõ đây là những yêu cầu đặc biệt trong các chương có liên quan.

Nếu bạn đã thiết lập Raspberry Pi với Raspbian, internet và SSH được kích hoạt, bạn có thể bỏ qua bước "Cài đặt Node.js trên Raspberry Pi".


Ghi hình ảnh hệ điều hành Raspbian vào thẻ MicroSD

Trước khi có thể bắt đầu sử dụng Raspberry Pi cho bất kỳ việc gì, chúng ta cần cài đặt hệ điều hành.

Raspbian là một hệ điều hành miễn phí dựa trên Debian Linux và nó được tối ưu hóa Raspberry Pi.

Tải xuống hình ảnh Raspbian mới nhất từ https://www.raspberrypi.org/downloads/raspbian/ vào máy tính của bạn.

Chúng tôi sử dụng phiên bản "LITE" trong hướng dẫn của mình, vì chúng tôi đang thiết lập Raspberry Pi làm máy chủ không đầu (chúng tôi sẽ kết nối với nó thông qua SSH mà không cần bàn phím / màn hình kết nối với nó). Bạn có thể sử dụng bất kỳ phiên bản nào bạn muốn, nhưng hướng dẫn này được viết với phiên bản "LITE" làm trọng tâm.

Cắm thẻ nhớ MicroSD vào máy tính của bạn (qua bộ chuyển đổi SD nếu cần). Mở File Explorer để xác minh rằng nó đang hoạt động.

Etcher là một chương trình để nhấp nháy hình ảnh vào thẻ nhớ. Tải xuống và cài đặt Etcher từ: https://etcher.io/

Khởi chạy Etcher:

Khởi chạy 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.