Node.js ट्यूटोरियल

Node.js होम Node.js परिचय Node.js प्रारंभ करें Node.js मॉड्यूल Node.js HTTP मॉड्यूल Node.js फ़ाइल सिस्टम Node.js URL मॉड्यूल Node.js एनपीएम Node.js घटनाएँ Node.js फ़ाइलें अपलोड करें Node.js ईमेल

Node.js MySQL

MySQL प्रारंभ करें MySQL डेटाबेस बनाएँ MySQL तालिका बनाएँ MySQL सम्मिलित करें MySQL से चुनें MySQL कहाँ MySQL ऑर्डर बाय MySQL हटाएं MySQL ड्रॉप टेबल MySQL अद्यतन MySQL सीमा मायएसक्यूएल जॉइन

Node.js MongoDB

मोंगोडीबी आरंभ करें MongoDB डेटाबेस बनाएँ MongoDB संग्रह बनाएँ मोंगोडीबी डालें मोंगोडीबी खोजें मोंगोडीबी क्वेरी मोंगोडीबी सॉर्ट मोंगोडीबी हटाएं MongoDB ड्रॉप संग्रह मोंगोडीबी अपडेट मोंगोडीबी सीमा मोंगोडीबी शामिल हों

रास्पबेरी पाई

रासपी आरंभ करें रासपी जीपीआईओ परिचय रासपी ब्लिंकिंग एलईडी रासपी एलईडी और पुशबटन रासपी बहने वाली एल ई डी रासपी वेबसाकेट रासपी आरजीबी एलईडी वेबसाकेट रास्पी अवयव

Node.js संदर्भ

अंतर्निहित मॉड्यूल

Node.js और रास्पबेरी पाई


रास्पबेरी पाई एक छोटा, बहु-उपयोग वाला कंप्यूटर है।

Node.js के साथ आप अपने रास्पबेरी पाई के साथ अद्भुत चीजें कर सकते हैं।


रास्पबेरी पाई क्या है?

रास्पबेरी पाई एक छोटा, किफायती और आश्चर्यजनक रूप से सक्षम, क्रेडिट कार्ड आकार का कंप्यूटर है।

इसे रास्पबेरी पाई फाउंडेशन द्वारा विकसित किया गया है, और यह अब तक की सबसे बहुमुखी तकनीक हो सकती है।

निर्माता एबेन अप्टन का लक्ष्य एक कम लागत वाला उपकरण बनाना था जो प्रोग्रामिंग कौशल और हार्डवेयर समझ में सुधार करेगा।

डिवाइस के छोटे आकार और कीमत के कारण, यह टिंकरर्स, निर्माताओं और इलेक्ट्रॉनिक्स उत्साही लोगों द्वारा परियोजनाओं की एक विस्तृत श्रृंखला का केंद्र बन गया है।


रास्पबेरी पाई और Node.js

रास्पबेरी पाई में GPIO (सामान्य प्रयोजन इनपुट / आउटपुट) पिन की एक पंक्ति होती है, और इनका उपयोग वास्तविक दुनिया के साथ अद्भुत तरीके से बातचीत करने के लिए किया जा सकता है। यह ट्यूटोरियल इस बात पर ध्यान केंद्रित करेगा कि इन्हें Node.js के साथ कैसे उपयोग किया जाए।


मुझे क्या ज़रुरत है?

इस ट्यूटोरियल के लिए आपको रास्पबेरी पाई की आवश्यकता है। हमारे उदाहरणों में हम रास्पबेरी पाई 3 का उपयोग करते हैं, लेकिन यह ट्यूटोरियल अधिकांश संस्करणों के लिए काम करना चाहिए।

हार्डवेयर की जरूरत:

  • रास्पबेरी पाई कंप्यूटर
  • माइक्रोएसडी मेमोरी कार्ड (हम 16 जीबी या उच्चतर के साथ कक्षा 10 की सलाह देते हैं)
  • माइक्रोएसडी से एसडी मेमोरी कार्ड एडेप्टर (आमतौर पर माइक्रोएसडी कार्ड के साथ शामिल)
  • रास्पबेरी पाई को बिजली देने के लिए माइक्रो यूएसबी बिजली की आपूर्ति (2.5A या अधिक अनुशंसित)
  • रास्पबेरी पाई के लिए वाईफाई/ईथरनेट केबल कनेक्शन (रास्पबेरी पाई 3 के लिए जरूरी नहीं है क्योंकि यह वाईफाई में बनाया गया है)
  • इंटरनेट और एसडी मेमोरी कार्ड रीडर के साथ एक कार्यशील कंप्यूटर (मेमोरी कार्ड पर रास्पबेरी पाई के लिए ओएस (ऑपरेटिंग सिस्टम) प्राप्त करने के लिए प्रयुक्त)। हमारे ट्यूटोरियल में हम इसके लिए एक विंडोज कंप्यूटर का उपयोग करते हैं, लेकिन आप चाहें तो मैक या लिनक्स कंप्यूटर का उपयोग कर सकते हैं
  • एचडीएमआई मॉनिटर, यूएसबी कीबोर्ड (हमें रास्पबेरी पाई के पहले बूट के लिए केवल अस्थायी रूप से इनकी आवश्यकता है)

इस ट्यूटोरियल के बाद के अध्यायों के लिए हम विशेष सेंसर या उपकरणों का उपयोग करेंगे जिन्हें हम रास्पबेरी पाई से जोड़ते हैं। हम इन्हें प्रासंगिक अध्यायों में विशेष आवश्यकताओं के रूप में निर्दिष्ट करेंगे।

यदि आपके पास पहले से ही रास्पियन, इंटरनेट और सक्षम SSH के साथ रास्पबेरी पाई स्थापित है, तो आप "रास्पबेरी पाई पर Node.js स्थापित करें" चरण पर जा सकते हैं।


माइक्रोएसडी कार्ड में रास्पियन ओएस इमेज लिखें

इससे पहले कि हम किसी भी चीज़ के लिए अपने रास्पबेरी पाई का उपयोग शुरू कर सकें, हमें एक ओएस स्थापित करना होगा।

रास्पियन डेबियन लिनक्स पर आधारित एक मुफ्त ऑपरेटिंग सिस्टम है, और इसे रास्पबेरी पाई के लिए अनुकूलित किया गया है।

अपने कंप्यूटर पर https://www.raspberrypi.org/downloads/raspbian/ से नवीनतम रास्पियन छवि डाउनलोड करें ।

हम अपने ट्यूटोरियल में "लाइट" संस्करण का उपयोग करते हैं, क्योंकि हम रास्पबेरी पाई को एक हेडलेस सर्वर के रूप में सेट कर रहे हैं (हम इसे एसएसएच के माध्यम से कनेक्ट करेंगे, बिना कीबोर्ड/डिस्प्ले से जुड़े)। आप जो चाहें संस्करण का उपयोग कर सकते हैं, लेकिन यह ट्यूटोरियल "लाइट" संस्करण के साथ इसके फोकस के रूप में लिखा गया है।

अपने कंप्यूटर में माइक्रोएसडी मेमोरी कार्ड डालें (यदि आवश्यक हो तो एसडी एडेप्टर के माध्यम से)। यह सत्यापित करने के लिए फ़ाइल एक्सप्लोरर खोलें कि यह चालू है।

Etcher मेमोरी कार्ड में छवियों को चमकाने का एक प्रोग्राम है। एचर को यहां से डाउनलोड और इंस्टॉल करें: https://etcher.io/

एचर लॉन्च करें:

एचर लॉन्च करें

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.