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 रास्पबेरी पाई GPIO - ब्लिंकिंग LED


आउटपुट के लिए GPIO का उपयोग करना

इस अध्याय में हम एलईडी ब्लिंक करने के लिए रास्पबेरी पाई और उसके जीपीआईओ का उपयोग करेंगे।

हम GPIO को नियंत्रित करने के लिए ऑनऑफ़ मॉड्यूल के साथ Node.js का उपयोग करते हैं।

चालू करने के लिए एक एलईडी लाइट प्राप्त करने के लिए, हम "आउटपुट" के रूप में एक जीपीआईओ पिन का उपयोग करते हैं, और इसे चालू और बंद करने के लिए एक स्क्रिप्ट बनाते हैं।


हमारी जरूरतें क्या हैं?

इस अध्याय में हम एक सरल उदाहरण बनाएंगे जहां हम एक एलईडी लाइट को नियंत्रित करते हैं।

इसके लिए आपको चाहिए:

विभिन्न घटकों के विवरण के लिए ऊपर दी गई सूची में लिंक पर क्लिक करें।

नोट: आपके द्वारा उपयोग किए जाने वाले एलईडी के प्रकार के आधार पर आपके द्वारा उपयोग किया जाने वाला अवरोधक हमारे द्वारा उपयोग किए जाने वाले अवरोधक से भिन्न हो सकता है। अधिकांश छोटे एल ई डी को केवल एक छोटे अवरोधक की आवश्यकता होती है, लगभग 200-500 ओम। यह आम तौर पर महत्वपूर्ण नहीं है कि आप किस सटीक मूल्य का उपयोग करते हैं, लेकिन रोकनेवाला का मूल्य जितना छोटा होगा, एलईडी उतनी ही तेज होगी।


सर्किट का निर्माण

अब हमारे ब्रेडबोर्ड पर सर्किट बनाने का समय आ गया है।

यदि आप इलेक्ट्रॉनिक्स के लिए नए हैं, तो हम अनुशंसा करते हैं कि आप रास्पबेरी पाई के लिए बिजली बंद कर दें। और इसे नुकसान पहुंचाने से बचने के लिए एंटी-स्टैटिक मैट या ग्राउंडिंग स्ट्रैप का इस्तेमाल करें।

रास्पबेरी पाई को कमांड के साथ ठीक से बंद करें:

pi@w3demopi:~ $ sudo shutdown -h now

रास्पबेरी पाई पर एलईडी बंद होने के बाद, फिर रास्पबेरी पाई से पावर प्लग को बाहर निकालें (या इससे जुड़ी पावर स्ट्रिप को बंद कर दें)।

ठीक से बंद किए बिना बस प्लग को खींचने से मेमोरी कार्ड खराब हो सकता है।

ब्रेडबोर्ड के साथ रास्पबेरी पाई 3।  सरल एलईडी सर्किट

सर्किट के उपरोक्त चित्रण को देखें।

  1. रास्पबेरी पाई पर, पहले जम्पर तार के मादा पैर को ग्राउंड से कनेक्ट करें आप किसी भी GND पिन का उपयोग कर सकते हैं। इस उदाहरण में हमने फिजिकल पिन 9 ( जीएनडी , रो 5, लेफ्ट कॉलम) का इस्तेमाल किया है।
  2. ब्रेडबोर्ड पर, पहले जम्पर तार के पुरुष पैर को दाईं ओर ग्राउंड बस कॉलम से कनेक्ट करें। आपके ब्रेडबोर्ड का वह पूरा कॉलम जुड़ा हुआ है, इसलिए इससे कोई फ़र्क नहीं पड़ता कि कौन सी पंक्ति है। इस उदाहरण में हमने इसे पंक्ति 1 . से जोड़ा है
  3. On the Raspberry Pi, connect the female leg of the second jumper cable to a GPIO pin. In this example we used Physical Pin 7 (GPIO 4, row 4, left column)
  4. On the Breadboard, connect the male leg of the second jumper wire to the Tie-Point row of your choice. In this example we connected it to row 5, column A
  5. On the Breadboard, connect one leg of the resistor to the Ground Bus column on the right side. That entire column of your breadboard is connected, so it doesn't matter which row. In this example we have attached it to row 5
  6. On the Breadboard, connect the other leg of the resistor to the right side Tie-Point row of your choice. In this example we have used row 5, column J
  7. On the Breadboard, connect the cathode leg (the shortest leg) of the LED to the same Tie-Point row that you connected the resistor from GND to. In this example we used row 5, column F
  8. On the Breadboard, connect the anode leg (the longest leg) of the LED to the same Tie-Point row that you connected the jumper from the GPIO pin to. In this example we used row 5, column E

Your circuit should now be complete, and your connections should look pretty similar to the illustration above.

Now it is time to boot up the Raspberry Pi, and write the Node.js script to interact with it.



Raspberry Pi and Node.js Blinking LED Script

Now that we have everything set up, we can write a script to turn the LED on and off.

Start by making a directory where we can keep our Node.js scripts:

pi@w3demopi:~ $ mkdir nodetest

Go to our new directory:

pi@w3demopi:~ $ cd nodetest

Now we will create a new file called "blink.js" using the Nano Editor:

pi@w3demopi:~ $ nano blink.js

The file is now open and can be edited with the built in Nano Editor.

Write, or paste the following code:

blink.js

var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var LED = new Gpio(4, 'out'); //use GPIO pin 4, and specify that it is output
var blinkInterval = setInterval(blinkLED, 250); //run the blinkLED function every 250ms

function blinkLED() { //function to start blinking
  if (LED.readSync() === 0) { //check the pin state, if the state is 0 (or off)
    LED.writeSync(1); //set pin state to 1 (turn LED on)
  } else {
    LED.writeSync(0); //set pin state to 0 (turn LED off)
  }
}

function endBlink() { //function to stop blinking
  clearInterval(blinkInterval); // Stop blink intervals
  LED.writeSync(0); // Turn LED off
  LED.unexport(); // Unexport GPIO to free resources
}

setTimeout(endBlink, 5000); //stop blinking after 5 seconds

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

Run the code:

pi@w3demopi:~ $ node blink.js

Now the LED should blink for 5 seconds (10 times) before turning off again!