Armbian – Remote access to the GPIO in non-root

With Armbian it is necessary to be ‘root’ to be able to access the GPIO. Here we will see how to solve this difficulty on an Orange Pi One Plus when you want to control the GPIO remotely via the web. This and of course applicable to other Single Board Computers under Armbian.

Test program on pin 26

We are going to create a small oscillator for 10s of pin 26 of the GPIO which corresponds to the GPIO 227 internally. To learn more about accessing the Orange Pi One Plus GPIO, go to this article:
https://f1atb.fr/index.php/2020/06/16/gpio-on-orange-pi-one-plus/

This program, ‘test_pin26.py’ is written in python 3.

#!/usr/bin/python3           address Python 3 environment


import os            # To execute system command
import time         # this lets us have a time delay

numgpio=227                     # OK for Orange Pi One Plus H6 , Pin 26

numgpio=str(numgpio)
print ("Pin 26, numgpio:"+numgpio)


os.system("sudo echo "+numgpio+" > /sys/class/gpio/export")
os.system("sudo echo out > /sys/class/gpio/gpio"+numgpio+"/direction")       


print ("Square signal around 50Hz for 10s")
T0=time.time()+10
while time.time()<T0:
        os.system("echo 0 > /sys/class/gpio/gpio"+numgpio+"/value")
        time.sleep(0.01)
        os.system("echo 1 > /sys/class/gpio/gpio"+numgpio+"/value")
        time.sleep(0.01)


os.system("echo 0 > /sys/class/gpio/gpio"+numgpio+"/value")   # set port/pin value to 0/LOW/False
os.system("sudo echo "+numgpio+" > /sys/class/gpio/unexport") # Clean GPIO
print ("Bye.")

and see on an oscilloscope the signal at about 50Hz.

The first step is to install a web server like apache. The procedure is simple as described here for an Orange Pi Zero.
https://f1atb.fr/index.php/2020/11/12/apache-on-orange-pi-zero/

In the configuration of the site in Apache, it is necessary to implement access to dynamic pages with a cgi-bin folder.

Apache runs under the user and the group ‘www-data’. We will give this user root privileges to start python3 and gain access to the GPIO without password. The key is to write a file, for example ‘python_web’ giving the rights.

www-data ALL=(ALL) NOPASSWD: /usr/bin/python3

This very simple ‘python_web’ file is to be placed in the /etc/sudoers.d/ folder. It is possible to modify it if you want access for a user other than www-data.

All that remains is to write a dynamic launch script ‘launch_test_pin26.sh’ to also put in cgi-bin.

#!/bin/bash

echo "Content-type:text/html"
echo ""
echo "<div style='width:100%;background-color: black;color:white;'>"


x=$(sudo python3 test_pin26.py)


echo "${x//$'n'/<br>}"
echo "</div>"

To limit access to apache, put the files ‘test_pin26.py’ and ‘launch_test_pin26.sh’ with 744 rights and the user www-data and the group www-data.

Call up your web page:

http://<ip server>/launch_test_pin26.sh

After 10s you will see the comments generated by the python test program displayed.
Pin 26, numgpio:227
Square signal around 50Hz for 10s
Bye.

If you have a problem, take a look at the Apache error messages which can be found here: /var/log/apache2/error.log.

F1ATB André

Ham Radio - Home automation - Photovoltaic

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *