40 Gpio on a Raspberry Pi is the best thing to play.
They can be directly controlled by c,/c++, Python, Java and other languages.
Now, let's see how to do the same thing with node. js.
Before you try, install node. js, and move to: < Raspberry Pi. Installation System +NODE.JS+MONGODB Series Environment >
1. Prepare the Test folder
mkdir/home/pi/documents/test-gpio-demo/
2. Create a file Package.json
{ "name":"Test-gpio-demo", "version":"0.0.1", "Dependencies":{ "Rpio2":"0.4.1" }}
3. Create a Master file Blank.js
//Blink.jsConstGpio = require ('/home/pi/documents/test-gpio/node_modules/rpio2/lib/index.js'). Gpio;varLED =NewGpio ( $);//Create P36 PinLed.open (Gpio.output, gpio.low);//set to OUTPUT, default low for(vari =0; I < -; i++) {led.toggle (); //Toggle the level status of LEDsLed.sleep ( -);//wait 500ms}led.close ();
4. Hardware Circuit Connection
36th Foot (GPIO16) through a resistor (220 kohm or 1K), and then received the positive level of the LED
Led negative level to 34th foot (GND)
5. Trial run
Node/home/pi/documents/test-gpio-demo/blank.js
Flash Light Effect:
Bright Lights
Extinguishing lamp:
Test success!
=============================== Divider Line =========================
Rpio2 General Usage:
Synchronously
Const Gpio = require ('./lib/index.js'). Gpio; Const New Gpio (gpio.open); (gpio.output); for (var0; i++) { gpio.toggle (); Gpio.sleep (+);} Gpio.close ();
Asynchronously
ConstGpio = require ('.. /lib/index.js'). Gpio;ConstGpio =NewGpio ( +); Gpio.open (gpio.output);voidfunction Loop () {Promise.resolve (Gpio.toggle ()). Then (Gpio.sleep.bind (NULL, -,true) . Then (Loop)} ();p Rocess.on ("SIGINT", function () {gpio.close (); Console.log ('shutdown!'); Process.exit (0);});
Toggle with button
ConstGpio = require ('.. /lib/index.js'). Gpio;Constbutton =NewGpio ( +);ConstOutput =NewGpio ( +); Button.open (gpio.input); Output.open (Gpio.output, Gpio.low) ;//Button downButton.on ('Rising', function () {output.toggle ();}); Process.on ("SIGINT", function () {button.close (); Output.close (); Console.log ('shutdown!'); Process.exit (0);});
Raspberry Pi. Using node. js to control Gpio