How to control Raspberry Pi GPIO via http web server


Node-RED

Node-RED is an open-source flow-based programming tool designed to help you connect hardware devices, APIs, and online services.

Being designed for the Internet of Things (IoT), Node-RED is a perfect match for the Raspberry Pi and can be useful for dealing with the Pi’s GPIO.

This software package will allow you to interpret data from your Raspberry Pi’s GPIO pins and create an action based on that data. You can also use Node-RED to interact with the GPIO pins based on an event.

Node-RED on Raspberry Pi  (https://nodered.org/docs/getting-started/raspberrypi)


Installing Node-RED

We need to install the build-essential package. This package is used to build any modules that NPM requires.
Run the command below to install build-essential: $ sudo apt install build-essential Finally, we can install Node-RED by running the following command. This command will install several things to our Raspberry Pi. These packages include Node.js, npm, and Node-RED itself: $ bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered) The first prompt asks if you really want to proceed with the installation: Are you really sure you want to do this ? [y/N] ? Type Y and then press ENTER to continue.

The second question will ask if you want to install Raspberry Pi specific Node-RED packages: Would you like to install the Pi-specific nodes ? [y/N] ? Again to this question, press the letter Y and then press ENTER to continue.

While Node-RED is available through the Raspbian package repository, the script is the officially recommended install method as it also installs npm. npm is incredibly useful as it is a package manager for the javascript programming language that Node-RED is built on. Using npm, you will be able to install additional modules for use with Node-RED.

Reboot


Starting Node-Red on the Raspberry Pi

With Node-RED finally installed to our Pi, we can now go ahead and start the software up. There are two different ways to do this. One is to launch it as a service. The other is to launch it directly.

Launching Node-RED Directly
To launch Node-RED directly without starting it as a service, all you need to do is type in the following command: $ node-red The advantage of starting Node-RED like this is that you have access to its terminal. You will see things such as errors appear here.

Starting Node-RED as a Service
Alternatively, you can start up the Node-RED service by using the command below: $ sudo systemctl start nodered If you run into issues with Node-RED not starting correctly on your Raspberry Pi, we recommend trying the other method.
The alternative method should show you any errors that may be occurring during the start up process.
Alternatively, you can also check the status of the Node-RED service by making use of the following command: $ sudo systemctl status nodered This command will give you various details about Node-RED and should let you know whether it is successfully running as well as the last few lines it outputted to the terminal.
Node-RED should now start silently in the background, and you should now be able to access its web interface.


Opening the Node-RED Editor

Once Node-RED is running you can access the editor in a browser.
When browsing from another machine you should use the hostname or IP-address of the Pi: http://[hostname]:1880. You can find the IP address by running hostname -I on the Pi. http://[IPADDRESS]:1880 If everything is working correctly, you should now be greeted Welcome to Node-RED!.


Getting Node-RED to Start on Boot

As the Node-RED install script automatically sets up a service file, all we need to do to get it to start at boot is to enable it.
To enable the Node-RED service on your Pi, you need to use the following command: $ sudo systemctl enable nodered Alternatively, if you want to stop Node-RED from automatically starting on boot, you can make use of the following command: $ sudo systemctl disable nodered


Additional Recommendations

### ADDITIONAL RECOMMENDATIONS ### - Remove the /etc/sudoers.d/010_pi-nopasswd file to require entering your password when performing any sudo/root commands: sudo rm -f /etc/sudoers.d/010_pi-nopasswd - You can customise the initial settings by running: node-red admin init - After running Node-RED for the first time, change the ownership of the settings file to 'root' to prevent unauthorised changes: sudo chown root:root ~/.node-red/settings.js


If you are interested in learning more about how to use Node-RED we recommend checking out the
official guide on creating your first Node-RED flow (Node-RED docs).


Control Raspberry Pi GPIO via Node-RED Dashboard

Although the node-RED flow that we created did achieved the result, but it is an editor page, it is not really a web page where user can use. Won't it be better to have a graphical UI like the one shown below? sure, let's do it.
What you seen requires Node-RED Dashboard, an add-on that you could installed via npm. To install it, navigate to the user node-red directory which is at ~/.node-red or /home/pi/.node-red if you are running Raspberry Pi as pi user. $ cd ~/.node-red $ npm install node-red-dashboard

Node-RED dashboard can be access via http://192.168.xxx.xxx:1880/ui, however the path /ui is disabled by default, we need to change the setting at settings.js. Use nano editor to make the change: $ nano settings.js Press ctrl-w or simple scrolling down to search for line with ui: { path: "ui" } and uncomment out the line by deleting the // in front of the line. Pressed ctrl-x to save the file. If you had configured to run Node-RED as a daemon service (i.e. auto-run at boot up), you will need to restart the node-red for the change to take on effect: $ sudo systemctl restart nodered But if you are running node-RED manually, you can ignore this restart command. Now let's create our dashboard.


You will noticed that there is an newly installed Dashboard category on the node palette, these are the UI widgets can be displayed on the Dashboard.