Node.js နှင့် Raspberry Pi
Raspberry Pi သည် သေးငယ်ပြီး အစုံသုံးနိုင်သော ကွန်ပျူတာဖြစ်သည်။
Node.js ဖြင့် သင်သည် သင်၏ Raspberry Pi ဖြင့် အံ့သြဖွယ်ရာများကို လုပ်ဆောင်နိုင်သည်။
Raspberry Pi ဆိုတာ ဘာလဲ။
Raspberry Pi သည် သေးငယ်သော၊ တတ်နိုင်သော၊ အံ့သြဖွယ်ကောင်းသော အကြွေးဝယ်ကတ်အရွယ်အစားရှိသော ကွန်ပျူတာဖြစ်သည်။
၎င်းကို Raspberry Pi Foundation မှ ဖန်တီးထားပြီး ၎င်းသည် ဖန်တီးဖူးသမျှ စွယ်စုံရအရှိဆုံးနည်းပညာဖြစ်နိုင်သည်။
တီထွင်သူ Eben Upton ၏ ရည်မှန်းချက်မှာ ပရိုဂရမ်းမင်းကျွမ်းကျင်မှုနှင့် ဟာ့ဒ်ဝဲနားလည်မှု တိုးတက်စေမည့် တန်ဖိုးနည်းစက်ပစ္စည်းကို ဖန်တီးရန်ဖြစ်သည်။
စက်၏သေးငယ်သောအရွယ်အစားနှင့်စျေးနှုန်းကြောင့်၎င်းသည် tinkerers၊ ထုတ်လုပ်သူများနှင့် အီလက်ထရွန်နစ်ပစ္စည်းဝါသနာအိုးများ၏ ကျယ်ပြန့်သောပရောဂျက်များ၏ဗဟိုဖြစ်လာသည်။
Raspberry Pi နှင့် Node.js
Raspberry Pi တွင် GPIO (General Purpose Input/Output) ပင်နံပါတ်တန်းများပါရှိပြီး လက်တွေ့ကမ္ဘာနှင့် အံ့သြဖွယ်နည်းလမ်းများဖြင့် အပြန်အလှန်တုံ့ပြန်ရန်အတွက် ၎င်းတို့ကို အသုံးပြုနိုင်သည်။ ဤသင်ခန်းစာသည် Node.js နှင့် မည်သို့အသုံးပြုရမည်ကို အာရုံစိုက်ပါမည်။
ငါဘာလိုလဲ
ဒီသင်ခန်းစာအတွက် Raspberry Pi လိုအပ်တယ်။ ကျွန်ုပ်တို့၏နမူနာများတွင် Raspberry Pi 3 ကိုအသုံးပြုသော်လည်း ဤသင်ခန်းစာသည် ဗားရှင်းအများစုအတွက် အလုပ်လုပ်သင့်သည်။
လိုအပ်သော ဟာ့ဒ်ဝဲ-
- Raspberry Pi ကွန်ပျူတာ
- MicroSD မန်မိုရီကတ် (ကျွန်ုပ်တို့သည် 16 GB သို့မဟုတ် ထို့ထက်ပိုသော အတန်း 10 ကို အကြံပြုပါသည်)
- MicroSD မှ SD မန်မိုရီကတ် အဒက်တာ (အများအားဖြင့် MicroSD ကတ်ဖြင့် ပါ၀င်သည်)
- Raspberry Pi ကိုအားသွင်းရန် Micro USB ပါဝါထောက်ပံ့မှု (2.5A နှင့်အထက် အကြံပြုထားသည်)
- Raspberry Pi အတွက် WiFi/Ethernet Cable ချိတ်ဆက်မှု (Raspberry Pi 3 အတွက် WiFi ဖြင့် တည်ဆောက်ထားသောကြောင့် မလိုအပ်ပါ)
- အင်တာနက်နှင့် SD မန်မိုရီကတ်ဖတ်စက်ဖြင့် အလုပ်လုပ်သော ကွန်ပျူတာ (Raspberry Pi အတွက် OS (Operating System) ကို မန်မိုရီကတ်ပေါ်သို့ ရယူရန် အသုံးပြုသည်)။ ကျွန်ုပ်တို့၏ သင်ခန်းစာတွင် ၎င်းအတွက် Windows ကွန်ပျူတာကို အသုံးပြုသော်လည်း သင်နှစ်သက်ပါက Mac သို့မဟုတ် Linux ကွန်ပျူတာကို အသုံးပြုနိုင်သည်။
- HDMI မော်နီတာ၊ USB ကီးဘုတ် (Raspberry Pi ၏ ပထမဆုံး boot အတွက် ၎င်းတို့ကို ယာယီသာ လိုအပ်သည်)
ဤသင်ခန်းစာ၏ နောက်ပိုင်းအခန်းများအတွက် ကျွန်ုပ်တို့သည် Raspberry Pi သို့ ချိတ်ဆက်ထားသော အထူးအာရုံခံကိရိယာများ သို့မဟုတ် စက်များကို အသုံးပြုပါမည်။ သက်ဆိုင်ရာအခန်းများတွင် ၎င်းတို့ကို အထူးလိုအပ်ချက်များအဖြစ် သတ်မှတ်ပေးပါမည်။
အကယ်၍ သင့်တွင် Raspberry Pi ကို Raspbian၊ အင်တာနက်နှင့် ဖွင့်ထားသော SSH ဖြင့် စနစ်ထည့်သွင်းပြီးပါက၊ သင်သည် "Install Node.js on Raspberry Pi" အဆင့်သို့ ကျော်သွားနိုင်ပါသည်။
Raspbian OS Image ကို MicroSD ကတ်တွင်ရေးပါ။
ကျွန်ုပ်တို့သည် ကျွန်ုပ်တို့၏ Raspberry Pi ကို မည်သည့်အရာအတွက်မဆို စတင်အသုံးမပြုမီ၊ ကျွန်ုပ်တို့သည် OS ထည့်သွင်းထားရန် လိုအပ်ပါသည်။
Raspbian သည် Debian Linux ကိုအခြေခံထားသော အခမဲ့လည်ပတ်မှုစနစ်ဖြစ်ပြီး ၎င်းကို Raspberry Pi ကို အကောင်းဆုံးဖြစ်အောင်ပြုလုပ်ထားသည်။
နောက်ဆုံးထွက် Raspbian ရုပ်ပုံကို https://www.raspberrypi.org/downloads/raspbian/ မှ သင့်ကွန်ပျူတာသို့ ဒေါင်းလုဒ်လုပ်ပါ။
ကျွန်ုပ်တို့သည် Raspberry Pi ကို headless server တစ်ခုအဖြစ် သတ်မှတ်နေသောကြောင့် ကျွန်ုပ်တို့၏ "LITE" ဗားရှင်းကို အသုံးပြုပါသည် (၎င်းနှင့်ချိတ်ဆက်ထားသည့်ကီးဘုတ်/မျက်နှာပြင်ပြသစရာမလိုဘဲ SSH မှတဆင့် ၎င်းနှင့်ချိတ်ဆက်ပါမည်)။ သင်အလိုရှိသော မည်သည့်ဗားရှင်းကိုမဆို သင်အသုံးပြုနိုင်သော်လည်း ဤသင်ခန်းစာကို ၎င်း၏အာရုံအဖြစ် "LITE" ဗားရှင်းဖြင့် ရေးသားထားသည်။
သင့်ကွန်ပျူတာတွင် MicroSD မန်မိုရီကတ်ကို ထည့်သွင်းပါ (လိုအပ်ပါက SD adapter မှတဆင့်)။ ၎င်းသည် လုပ်ငန်းလည်ပတ်ကြောင်း အတည်ပြုရန် File Explorer ကိုဖွင့်ပါ။
Etcher သည် ပုံများကို မန်မိုရီကတ်များသို့ flashing ပြုလုပ်သည့် ပရိုဂရမ်တစ်ခုဖြစ်သည်။ 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:
- Insert the MicroSD memory card into the Raspberry Pi
- Connect the USB keyboard
- Connect the HDMI cable
- Connect the USB Wi-Fi adapter (or Ethernet cable). Skip this step if you are using a Raspberry Pi 3
- Connect the micro USB power supply
- 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
Open the Raspberry Pi Software Configuration Tool:
pi@raspberrypi:~ $ sudo raspi-config
You should see a menu like this:
Select option 5 Interfacing Options
:
Select option P2 SSH
, to activate SSH:
Confirm with YES
, to activate SSH:
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:
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:
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:
When selecting Finish
, you will get the option to reboot. Select Yes
to reboot the Raspberry Pi.
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
:
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.