Node.js ကျူတိုရီရယ်

Node.js ပင်မစာမျက်နှာ Node.js နိဒါန်း Node.js ကို စတင်လိုက်ပါ။ Node.js မော်ဂျူးများ Node.js HTTP မော်ဂျူး Node.js ဖိုင်စနစ် Node.js URL မော်ဂျူး Node.js NPM Node.js ပွဲများ Node.js ဖိုင်များ အပ်လုဒ်လုပ်ခြင်း။ Node.js အီးမေးလ်

Node.js MySQL

MySQL စတင်လိုက်ပါ။ MySQL ဒေတာဘေ့စ်ဖန်တီးပါ။ MySQL ဇယားဖန်တီးပါ။ MySQL ထဲသို့ ထည့်သွင်းပါ။ MySQL မှ ရွေးချယ်ပါ။ MySQL ဘယ်မှာလဲ။ MySQL Order By MySQL ကိုဖျက်ပါ။ MySQL Drop Table MySQL အပ်ဒိတ် MySQL ကန့်သတ်ချက် MySQL Join

Node.js MongoDB

MongoDB စတင်လိုက်ပါ။ MongoDB ဒေတာဘေ့စ်ဖန်တီးပါ။ MongoDB သည် Collection ကိုဖန်တီးပါ။ MongoDB ထည့်သွင်းပါ။ MongoDB ကိုရှာပါ။ MongoDB မေးခွန်း MongoDB အမျိုးအစား MongoDB ကိုဖျက်ပါ။ MongoDB Drop Collection MongoDB အပ်ဒိတ် MongoDB ကန့်သတ်ချက် MongoDB Join

Raspberry Pi

RasPi စတင်လိုက်ပါ။ RasPi GPIO နိဒါန်း RasPi မှိတ်တုတ်မှိတ်တုတ် LED RasPi LED နှင့် ခလုတ်နှိပ်ပါ။ RasPi စီးဆင်းနေသော LED များ RasPi WebSocket RasPi RGB LED WebSocket RasPi အစိတ်အပိုင်းများ

Node.js အ ကိုး အကား

Built-in Modules များ

Node.js Raspberry Pi GPIO - မှိတ်တုတ်မှိတ်တုတ် LED


Output အတွက် GPIO ကိုအသုံးပြုခြင်း။

ဤအခန်းတွင် LED မျက်တောင်ခတ်ရန် Raspberry Pi နှင့် ၎င်း၏ GPIO ကို အသုံးပြုပါမည်။

ကျွန်ုပ်တို့သည် GPIO ကိုထိန်းချုပ်ရန်အတွက် onoff module ဖြင့် Node.js ကိုအသုံးပြုသည်။

ဖွင့်ရန် LED မီးတစ်လုံးရရှိရန်၊ ကျွန်ုပ်တို့သည် "Output" အဖြစ် GPIO ပင်ကိုအသုံးပြုပြီး ၎င်းကိုဖွင့်ရန်နှင့်ပိတ်ရန် (မှိတ်တုတ်မှိတ်တုတ်) ဇာတ်ညွှန်းတစ်ခုဖန်တီးပါ။


ငါတို့ ဘာလိုလဲ?

ဤအခန်းတွင် ကျွန်ုပ်တို့သည် LED မီးအား ထိန်းချုပ်သည့် ရိုးရှင်းသော ဥပမာတစ်ခုကို ဖန်တီးပါမည်။

ဤအရာအတွက် သင်လိုအပ်သည်-

မတူညီသောအစိတ်အပိုင်းများ၏ဖော်ပြချက်များအတွက် အထက်ဖော်ပြပါစာရင်းရှိလင့်ခ်များကို နှိပ်ပါ။

မှတ်ချက်- သင်အသုံးပြုသည့် LED အမျိုးအစားပေါ်မူတည်၍ သင်လိုအပ်သော ခုခံအားသည် ကျွန်ုပ်တို့အသုံးပြုသည့်အရာနှင့် ကွဲပြားနိုင်ပါသည်။ သေးငယ်သော LED အများစုသည် 200-500 ohms ဝန်းကျင်ရှိသော သေးငယ်သော resistor တစ်ခုသာ လိုအပ်သည်။ သင်အသုံးပြုသည့်တန်ဖိုးအတိအကျကို ယေဘူယျအားဖြင့် အရေးမယူသော်လည်း resistor ၏တန်ဖိုးသေးငယ်လေ၊ LED သည် ပိုမိုတောက်ပလာမည်ဖြစ်သည်။


ပတ်လမ်းတည်ဆောက်ခြင်း။

ယခု ကျွန်ုပ်တို့၏ Breadboard တွင် ဆားကစ်ကို တည်ဆောက်ရန် အချိန်ကျရောက်ပြီဖြစ်သည်။

အကယ်၍ သင်သည် အီလက်ထရွန်းနစ်ပစ္စည်းကို တတ်ကျွမ်းသူဖြစ်ပါက Raspberry Pi အတွက် ပါဝါပိတ်ရန် အကြံပြုအပ်ပါသည်။ ပြီးလျှင် မပျက်စီးစေရန် ဆန့်ကျင်ဘက် ဖျာ သို့မဟုတ် မြေစိုက်သိုင်းကြိုးကို အသုံးပြုပါ။

အမိန့်ဖြင့် Raspberry Pi ကို ကောင်းစွာပိတ်ပါ။

pi@w3demopi:~ $ sudo shutdown -h now

Raspberry Pi တွင် LED မီးများ မှိတ်တုတ်မှိတ်တုတ်မှိတ်တုတ် ရပ်တန့်ပြီးနောက် Raspberry Pi မှ ပါဝါပလပ်ကို ဆွဲထုတ်ပါ (သို့မဟုတ် ၎င်းနှင့်ချိတ်ဆက်ထားသည့် ပါဝါကြိုးကို ပိတ်ပါ)။

ကောင်းစွာမပိတ်ဘဲ ပလပ်ကိုဆွဲလိုက်ရုံဖြင့် မန်မိုရီကတ်ကို ပျက်စီးစေနိုင်သည်။

Breadboard နှင့်အတူ Raspberry Pi 3  ရိုးရှင်းသော LED ဆားကစ်

ပတ်လမ်း၏အထက်ပုံဥပမာကိုကြည့်ပါ။

  1. Raspberry Pi တွင်၊ ပထမဆုံး jumper wire ၏ အမျိုးသမီးခြေထောက်ကို Ground နှင့် ချိတ်ဆက်ပါ မည်သည့် GND pin ကိုမဆို သုံးနိုင်သည် ။ ဤဥပမာတွင် Physical Pin 9 ( GND ၊ အတန်း 5 ၊ ဘယ်ဘက်ကော်လံ) ကို အသုံးပြုသည် ။
  2. Breadboard တွင်၊ ပထမ jumper ဝါယာကြိုး၏ အထီးခြေထောက်ကို ညာဘက်ရှိ Ground Bus ကော်လံနှင့် ချိတ်ဆက်ပါ။ မင်းရဲ့ breadboard ကော်လံတစ်ခုလုံးကို ချိတ်ဆက်ထားတာမို့ ဘယ်အတန်းကမှ အရေးမကြီးပါဘူး။ ဤဥပမာတွင် ၎င်းကို အတန်း 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!