Driving the Raspberry PI GPIO using NodeJS
Let's continue to explore the possibilities of NodeJS by simply switching On/Off a LED using a web page as HMI.
In this article i will detail how NodeJS can be helpful to drive the Raspberry Pi GPIO by switching ON/OFF a simple LED, using a webpage as Human Machine Interface.
I presented the broad outlines of NodeJS in a previous project, so please refer to the project named "
I2C communication using NodeJS and Raspberry PI".
For this project i used a raspberry PI 0 W, but a PI 3 or 4 can be used. Please refer to the 'Raspberry Config.pdf' file (see attacments) to config your raspberry.
Connect a LED to the raspberry according the 'led-raspberry-pi-0w.JPG' file (see attacments) .
All you need to do, once the raspbbery configuration is over and the LED connected, is to copy the files 'led_01_V2.js' and 'led_01_V2.html' (see attacments) , execute the '.js' script, connect a pc or a tablet to the WiFi network, and connect to the Raspberry.
On the wepage that appears, 2 buttons ('On' and 'Off') allow you to change the state of the LED.
When clicking on a button, you can see the state LED change and a message appears just bellow the buttons, indicating the state of the LED.
To understand how the '.js' and the '.html' scripts work, please refer to the "Explanations.pdf" file of the previous article related to NodeJS ("I2C communication using NodeJS and Raspberry PI").
Now, replace the LED by a servo motor according the 'servo-raspberry-pi-0w.JPG' file (see attacments) .
Copy to the raspberry the files 'servo_03.js' and 'servo_03.html' (see attacments) and execute the '.js' script.
Connect to the raspberry, Open a web browser on a PC or a tablet connected to WiFi and when the page is displayed you can pilot the servo from left position to right position using the cursor that appears on the webpage.
As you can see, NodeJS offers many possibilities, and if you want to go a step further you can test the PWM (see axample here : https://github.com/fivdi/pigpio) to, for example, drive a DC motor through a L298 H bridge.
Enjoy...
I presented the broad outlines of NodeJS in a previous project, so please refer to the project named "
I2C communication using NodeJS and Raspberry PI".
For this project i used a raspberry PI 0 W, but a PI 3 or 4 can be used. Please refer to the 'Raspberry Config.pdf' file (see attacments) to config your raspberry.
Connect a LED to the raspberry according the 'led-raspberry-pi-0w.JPG' file (see attacments) .
All you need to do, once the raspbbery configuration is over and the LED connected, is to copy the files 'led_01_V2.js' and 'led_01_V2.html' (see attacments) , execute the '.js' script, connect a pc or a tablet to the WiFi network, and connect to the Raspberry.
On the wepage that appears, 2 buttons ('On' and 'Off') allow you to change the state of the LED.
When clicking on a button, you can see the state LED change and a message appears just bellow the buttons, indicating the state of the LED.
To understand how the '.js' and the '.html' scripts work, please refer to the "Explanations.pdf" file of the previous article related to NodeJS ("I2C communication using NodeJS and Raspberry PI").
Now, replace the LED by a servo motor according the 'servo-raspberry-pi-0w.JPG' file (see attacments) .
Copy to the raspberry the files 'servo_03.js' and 'servo_03.html' (see attacments) and execute the '.js' script.
Connect to the raspberry, Open a web browser on a PC or a tablet connected to WiFi and when the page is displayed you can pilot the servo from left position to right position using the cursor that appears on the webpage.
As you can see, NodeJS offers many possibilities, and if you want to go a step further you can test the PWM (see axample here : https://github.com/fivdi/pigpio) to, for example, drive a DC motor through a L298 H bridge.
Enjoy...
Discussion (0 commentaire(s))