Node.js และRaspberry Pi


Raspberry Pi เป็นคอมพิวเตอร์ขนาดเล็กที่ใช้งานได้หลากหลาย

ด้วย Node.js คุณสามารถทำสิ่งที่น่าอัศจรรย์ด้วย Raspberry Pi ของคุณ


Raspberry Pi คืออะไร?

Raspberry Pi เป็นคอมพิวเตอร์ขนาดบัตรเครดิตขนาดเล็ก ราคาไม่แพง และมีความสามารถอย่างน่าอัศจรรย์

ได้รับการพัฒนาโดยมูลนิธิ Raspberry Pi และอาจเป็นเทคโนโลยีที่หลากหลายที่สุดเท่าที่เคยสร้างมา

เป้าหมายของครีเอเตอร์ Eben Upton คือการสร้างอุปกรณ์ราคาประหยัดที่จะพัฒนาทักษะการเขียนโปรแกรมและความเข้าใจด้านฮาร์ดแวร์

เนื่องจากขนาดและราคาของอุปกรณ์ที่เล็ก ทำให้กลายเป็นศูนย์กลางของโครงการที่หลากหลายโดยคนจรจัด ผู้ผลิต และผู้ที่ชื่นชอบอุปกรณ์อิเล็กทรอนิกส์


Raspberry Pi และ Node.js

Raspberry Pi มีพิน GPIO (อินพุต/เอาท์พุตวัตถุประสงค์ทั่วไป) หนึ่งแถว และสิ่งเหล่านี้สามารถใช้เพื่อโต้ตอบในรูปแบบที่น่าทึ่งกับโลกแห่งความจริง บทช่วยสอนนี้จะเน้นที่วิธีใช้สิ่งเหล่านี้กับ Node.js


ฉันต้องการอะไร?

สำหรับบทช่วยสอนนี้ คุณต้องมี Raspberry Pi ในตัวอย่างของเรา เราใช้ Raspberry Pi 3 แต่บทช่วยสอนนี้น่าจะใช้ได้กับเวอร์ชันส่วนใหญ่

ฮาร์ดแวร์ที่จำเป็น:

  • คอมพิวเตอร์ Raspberry Pi
  • การ์ดหน่วยความจำ MicroSD (เราแนะนำคลาส 10 ที่มี 16 GB หรือสูงกว่า)
  • อะแดปเตอร์การ์ดหน่วยความจำ MicroSD เป็น SD (มักมาพร้อมกับการ์ด MicroSD)
  • แหล่งจ่ายไฟ Micro USB เพื่อจ่ายไฟให้กับ Raspberry Pi (แนะนำให้ใช้ 2.5A หรือสูงกว่า)
  • การเชื่อมต่อสาย WiFi/อีเธอร์เน็ตสำหรับ Raspberry Pi (ไม่จำเป็นสำหรับ Raspberry Pi 3 เนื่องจากมี WiFi ในตัว)
  • คอมพิวเตอร์ที่ใช้งานได้พร้อมอินเทอร์เน็ตและเครื่องอ่านการ์ดหน่วยความจำ SD (ใช้เพื่อรับระบบปฏิบัติการ (ระบบปฏิบัติการ) สำหรับ Raspberry Pi ลงในการ์ดหน่วยความจำ) ในบทช่วยสอนของเราเราใช้คอมพิวเตอร์ Windows สำหรับสิ่งนี้ แต่คุณสามารถใช้คอมพิวเตอร์ Mac หรือ Linux ได้หากต้องการ
  • จอภาพ HDMI, แป้นพิมพ์ USB (เราต้องการเพียงชั่วคราวสำหรับการบูตครั้งแรกของ Raspberry Pi)

สำหรับบทต่อๆ ไปในบทช่วยสอนนี้ เราจะใช้เซ็นเซอร์หรืออุปกรณ์พิเศษที่เราเชื่อมต่อกับ Raspberry Pi เราจะระบุสิ่งเหล่านี้เป็นข้อกำหนดพิเศษในบทที่เกี่ยวข้อง

หากคุณมี Raspberry Pi ที่ตั้งค่าด้วย Raspbian อินเทอร์เน็ตและเปิดใช้งาน SSH แล้ว คุณสามารถข้ามไปยังขั้นตอน "ติดตั้ง Node.js บน Raspberry Pi" ได้


เขียนอิมเมจ Raspbian OS ลงในการ์ด MicroSD

ก่อนที่เราจะสามารถเริ่มใช้ Raspberry Pi ของเราได้ เราต้องติดตั้งระบบปฏิบัติการเสียก่อน

Raspbian เป็นระบบปฏิบัติการฟรีที่ใช้ Debian Linux และได้รับการปรับให้เหมาะสม Raspberry Pi

ดาวน์โหลดรูปภาพ Raspbian ล่าสุดจากhttps://www.raspberrypi.org/downloads/raspbian/ ลงในคอมพิวเตอร์ของคุณ

เราใช้เวอร์ชัน "LITE" ในบทช่วยสอน เนื่องจากเรากำลังตั้งค่า Raspberry Pi เป็นเซิร์ฟเวอร์แบบไม่มีส่วนหัว (เราจะเชื่อมต่อผ่าน SSH โดยไม่ต้องมีแป้นพิมพ์/จอแสดงผลเชื่อมต่ออยู่) คุณสามารถใช้เวอร์ชันใดก็ได้ที่คุณต้องการ แต่บทแนะนำนี้เขียนโดยเน้นที่เวอร์ชัน "LITE"

ใส่การ์ดหน่วยความจำ MicroSD ในคอมพิวเตอร์ของคุณ (ผ่านอะแดปเตอร์ SD หากจำเป็น) เปิด File Explorer เพื่อตรวจสอบว่าทำงานได้หรือไม่

Etcher เป็นโปรแกรมสำหรับแฟลชรูปภาพไปยังการ์ดหน่วยความจำ ดาวน์โหลดและติดตั้ง Etcher จาก: https://etcher.io/

เปิดตัว Etcher:

เปิดตัว 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.