Raspberry PI and GPIO pins: Controlling the LED from a web interface

In the latest posts of this series, We saw they are the GPIO pins and to serve, and How to control the power of leds from the terminal; before you start We have installed the GPIO libraries for Python, and the assembled electronic circuit and working.
Web application in HTML + PHP + Python to handle a few Leds through the GPIO.

On this occasion we will install the apache web server, and learn to write the code in HTML and PHP to control the same LED, or several, from a web interface.

gpioprogramar0pngThe first thing we must do is install the LAMP web server, so type the following commands in terminal:

#Actualizamos los repositorios y el software de la pi
usuario@maquina:~$ sudo apt-get update && sudo apt-get upgrade

#Instalamos LAMP
usuario@maquina:~$ sudo apt-get install apache2
usuario@maquina:~$ sudo apt-get install php5 libapache2-mod-php5
usuario@maquina:~$ sudo service apache2 restart

Now we'll add as sudoer to the user www-data, This is not the option more secure but remember that we are doing a test in a controlled environment; for an environment in production we should look for another way to run scripts in python as sudoer.

To edit these permissions, wrote sudo visudo in the terminal and at the end of the file Add to www-data as sudoer, It must be so.

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
pi ALL=(ALL) NOPASSWD: ALL
www-data ALL=(ALL) NOPASSWD: ALL

For each pin GPIO We need to create a subdirectory and three scripts in Python; one to turn on, another to turn off, and a final script for making our LED flashing, Here is an example with the PIN 4.

#!usr/bin/env/ python
## enciende.py
#Importamos la libreria y le cambiamos el nombre a GPIO
import RPi.GPIO as GPIO
#We establish the numbering system that we want to, 
#in this case the system BCM
GPIO.setmode(GPIO. AC.)
#Configure the GPIO pin 4 as an output
GPIO.setup(4, GPIO. OUT)
#We turn on the led
GPIO.output(4, GPIO. HIGH)
#!usr/bin/env/ python
## apaga.py
#We import the library and change her name to GPIO
import RPi.GPIO as GPIO
#We establish the numbering system that we want to, 
#in this case the system BCM
GPIO.setmode(GPIO. AC.)
#Configure the GPIO pin 4 as an output
GPIO.setup(4, GPIO. OUT)
#Turn off led
GPIO.output(4, GPIO. LOW)
#And we liberate the GPIO
GPIO.cleanup()
#!usr/bin/env/ python
## parpadea.py
#We import the library and change her name to GPIO
import RPi.GPIO as GPIO

#Necessary for the delays
import time

#We establish the numbering system that we want to, 
#in this case the system BCM
GPIO.setmode(GPIO. AC.)

#Configure the GPIO pin 4 as an output
GPIO.setup(4, GPIO. OUT)

#Turn on and turn off the led 5 times
for i in range(0,5):

        GPIO.output(4, GPIO. HIGH)
        time.sleep(1)(1)
        GPIO.output(4, GPIO. LOW)
        time.sleep(1)(1)

#And we liberate the GPIO
GPIO.cleanup()

Now is the time to create our home page in HTML and PHP, contains buttons that we will press to run the scripts in Python from a web interface, later we will see how the final hierarchy.

<html>
 <head>
                        <!--index.php--> 
 </head>
 <body>

  <!--GPIO17--> 
  <form action="" method="post">
   GPIO 17&nbsp;<input type="submit" name="encender17" value="Encender">
   <input type="submit" name="apagar17" value="Apagar">
   <input type="submit" name="parpadear17" value="Parpadear">

 <br></br>

  <!--GPIO27--> 
  <form action="" method="post">
   GPIO 27&nbsp;<input type="submit" name="encender27" value="Encender">
   <input type="submit" name="apagar27" value="Apagar">
   <input type="submit" name="parpadear27" value="Parpadear">

 <br></br>

  <!--GPIO4--> 
  <form action="" method="post">
   GPIO 04&nbsp;<input type="submit" name="encender4" value="Encender">
   <input type="submit" name="apagar4" value="Apagar">
   <input type="submit" name="parpadear4" value="Parpadear">

 <br></br>

  <!--GPIO22--> 
  <form action="" method="post">
   GPIO 22&nbsp;<input type="submit" name="encender22" value="Encender">
   <input type="submit" name="apagar22" value="Apagar">
   <input type="submit" name="parpadear22" value="Parpadear">

 </body>
</html>

<?php

// Funciones PHP del pin GPIO 17

  if ($_POST[encender17]) { 
   $a- exec("sudo python /var/www/leds/gpio/17/enciende.py");
   echo $a;
  }

  if ($_POST[apagar17]) { 
   $a- exec("sudo python /var/www/leds/gpio/17/apaga.py");
   echo $a;
  }

  if ($_POST[parpadear17]) { 
   $a- exec("sudo python /var/www/leds/gpio/17/parpadea.py");
   echo $a;
  }

// Fin de las funciónes del pin GPIO 17


// Funciones PHP del pin GPIO 27

  if ($_POST[encender27]) { 
   $a- exec("sudo python /var/www/leds/gpio/27/enciende.py");
   echo $a;
  }

  if ($_POST[apagar27]) { 
   $a- exec("sudo python /var/www/leds/gpio/27/apaga.py");
   echo $a;
  }

  if ($_POST[parpadear27]) { 
   $a- exec("sudo python /var/www/leds/gpio/27/parpadea.py");
   echo $a;
  }

// Fin de las funciónes del pin GPIO 27


// Funciones PHP del pin GPIO 4

  if ($_POST[encender4]) { 
   $a- exec("sudo python /var/www/leds/gpio/4/enciende.py");
   echo $a;
  }

  if ($_POST[apagar4]) { 
   $a- exec("sudo python /var/www/leds/gpio/4/apaga.py");
   echo $a;
  }

  if ($_POST[parpadear4]) { 
   $a- exec("sudo python /var/www/leds/gpio/4/parpadea.py");
   echo $a;
  }

// Fin de las funciónes del pin GPIO 4


// Funciones PHP del pin GPIO 22

  if ($_POST[encender22]) { 
   $a- exec("sudo python /var/www/leds/gpio/22/enciende.py");
   echo $a;
  }

  if ($_POST[apagar22]) { 
   $a- exec("sudo python /var/www/leds/gpio/22/apaga.py");
   echo $a;
  }

  if ($_POST[parpadear22]) { 
   $a- exec("sudo python /var/www/leds/ejecgpio/22/parpadea.py");
   echo $a;
  }

// Fin de las funciónes del pin GPIO 22

?>

I have grouped it all on the same page as if creating a page in PHP for each option, to run it, I was sent to the script page (a blank page); now run the code and stays on the same page.

At last We must achieve the following hierarchy within the subdirectory of the web server /var/www.

Screenshot of 2014-10-05 00:06:52If you like you can follow me on Twitter, Facebook, Google +, LinkedIn, or share it with the buttons under this publication, If you have any questions or suggestions please do not hesitate to comment.

Help us to reach more readers Share on LinkedInShare on FacebookTweet about this on TwitterShare on Google+Email this to someone

71 thoughts on"Raspberry PI and GPIO pins: Controlling the LED from a web interface

  1. Good, After looking at Internet solutions home automation, I decided by your system, seems well exposed.
    Even so and because of my nil experience in soft. doubts me, I wish you could solve them, I've been doing many tests and nothing, I don't get it….:)
    perform all the steps that explain and I manage the gpio from terminal with putty, but since web can not activate the python script.
    I need to do a page on /val/www with html (index.html) so serve it I apache. By placing your website here if the buttons leave me, but does not execute the php script. In the page index.php, not be put inside.
    Sorry for the inconvenience and thank you.

      • Thank you for responding so quickly, Yes I did step to give permissions in the file that you put, I'm confused with the index.php page, you hang the GPiO directory, only see the buttons on the web if I put it in /val/www….in html!…index.html, but does not execute the PHP script. That should put the index.php which hang GPIO directory?

        • If you look closely, index.php /var/www/leds 🙂 hangs if still does not work, comment this article and look at it.

          If /var/www/index.php cuelgas will see it in this direction but it will not work anything:

          http://ipdelapi

          If the hang of /var/www/leds/will work properly but since this route:

          http://ipdelapi/leds

          I hope this resolves your doubts and that you can move into this so exciting world.

          • Good, I have the same problem as the fellow above.

            My index.php hangs from /var/www/html/index.html
            , If I get my index html from there not shown, but I have no problem in having it there, My problem comes when pulse button and not run me the scrip.

            PD: I gave him everything and permits

            Thank you for your answer.
            A greeting

            J.G.

  2. Good, your work is serving me much, you are a machine!
    I'm trying to make the automation system to control multiple leds from the web but I have a problem you can not alleviate.
    The theme is that I have activated the GPIO I need, I activate them and deactivate from terminal, I connect another PC connected on the same network to 192.168.X.X/leds/ and the website looks good, the menu for the control of the home looks perfect, but the leds do not respond, I've tried by active and passive, something I'm doing wrong…
    The only thing I can say is that you displaying the source code of the page and only shows the html code, php does not appear, not be if it should or not.
    I think it may be something PHP but I'm not very convinced because I tried a test PHP and it does it well, I'm desperate lol

    • Hi John,

      Me alegro much that may be usefulness my articles 🙂 always are welcome the positive comments.

      All PHP code in the browser is displayed as HTML, Since the PHP function is at the time and dynamically create web pages that are served in the browser.

      Running Python scripts directly if the leds are lit? the right thing would be start testing from here and go forward.

  3. Thank you for answering quickly!
    Finally format the raspberry again, I followed all the steps again and everything works beautifully, not be that could fail.
    Anyway thanks for the attention.
    A greeting

    • Thank you for share your doubts, I am glad that it has finally arranged.

      You have changed something in your Raspberry as well as format, as for example the transformer microUSB?

      • Yes, When I bought it I put a sink to the micro to speed him up and so will work faster, but neither is very noticeable change.
        That was the only thing that change you to the rasp.

        • Hi John, I am glad to read you again here.

          In principle does not have that influence the Overclock in this aspect, but it may have been any configuration; now that you're working with a Raspbian cleaning returns to give problems after being in production, Now if that would recommend you to find the root of the problem.

          Change in the Overclock you will notice it in according to specific tasks which need much computing power, but some factors such as the Ethernet port will continue according to bottle-neck that uses.

          You can see the review I did of Raspberry PI B models, B + and B2 in the following link, with full performance testing:

          http://www.peatonet.com/raspberry-pi-y-los-pines-gpio-controlando-el-led-desde-una-interfaz-web/

          I hope you will be useful, see you soon 🙂

  4. Hello, I have a little question/problem. I would like to connect a buzzer instead of an led, but I want to try first with an led to learn. It is necessary to make an index.html and other .php?
    I have the led connected to the gpio4, and I have an on.py script and other off.py inside the folder 4. Console work perfectly.
    and I can not put the index.html because it will not let me, but just for led functionality 4.

    I hope that you can help me, I have visited your facebook profile and I hope someday both of Raspberry Pi as you know. A greeting and thanks.

  5. A web in PHP may contain HTML by which only need an index.php

    If you put the PHP commands to run your scripts in python in an HTML file, will not run.

    Now that the terminal works, you could just copy the index.php of this tutorial, eliminating the other GPIO and leaving only the 4 that is what you use; and it should work.

    If you can not edit files in/var/www surely will be by permission, in this tutorial is the solution and it should work, If it doesn't comment and I will try to give you a hand.

  6. Hello, I'm going to take to put another question here, If you or someone can take me a cable. I have this script and would like to delete the folder with the date once rebooted, and what interests me, know how to do that when it comes to an x-size amount of catches, all be erased or restarts the RPi. Thanks a greeting.

    #! /bin/sh
    # /etc/init.d/motionStartup
    #

    # Some things that always run
    #touch /var/lock/motionStartup
    logger - s “Script called motionStartup”

    # Carry out specific functions when asked to by the system
    case “$1” in
    Start)
    logger - s “Script motionStartup started”
    TIMESTAMP = $(date + %m %d %H %M %S and % | SED's / / / g') # No spaces
    logger - s “Script motionStartup $TIMESTAMP”
    sudo mkdir/motiontmp/$TIMESTAMP || logger - s “Mkdir start error”
    find /motiontmp/motion /. -type f - size 0 -print0 - delete
    find /motiontmp/motion /. -MaxDepth 1 -type f | \
    XARGS - I '{}’ sudo mv {} /motiontmp / $TIMESTAMP
    ;;
    stop)
    logger - s “Script motionStartup stopped”
    ;;
    *)
    echo “Usage: /etc/init.d/motionStartup {Start|stop}”
    exit 1
    ;;
    ESAC

    exit 0

  7. If you add this line at the beginning of your script, each time that you run the script before you do anything you will erase all folders inside/motiontmp /

    sudo rm - rf/motiontmp / *

    With regard to the delete files when you reach a certain size, you could try concatenating the command df - h showing a directory disk usage, or a partition:

    df - h /motiontmp

    With another command like wc or sed so that it only reads the phrase referring to the MB or GB used, Finally with if you could program the part that if the result is greater than N, run the command described above rm.

    • To the delete perfect folders, but then…
      Sorry but I'm a bit new and I am lost, as it would be for example for when reaches the directory /motiontmp/motion / 5 Gigas? Thank you greetings.

  8. Sorry for so many posts followed but as I am finding out update. Something like this might work?

    #! /bin/sh
    # /etc/init.d/motionStartup
    #

    size = du - sh /motiontmp

    read “size”

    If [ “$size” > “2048000K” ]; then

    sudo rm - rf/motiontmp / *

    else

    logger - s “Script called motionStartup”

    # Carry out specific functions when asked to by the system
    case “$1” in
    Start)
    logger - s “Script motionStartup started”
    TIMESTAMP = $(date + %m %d %H %M %S and % | SED's / / / g') # No spaces
    logger - s “Script motionStartup $TIMESTAMP”
    sudo mkdir/motiontmp/$TIMESTAMP || logger - s “Mkdir start error”
    find /motiontmp/motion /. -type f - size 0 -print0 - delete
    find /motiontmp/motion /. -MaxDepth 1 -type f | \
    XARGS - I '{}’ sudo mv {} /motiontmp / $TIMESTAMP
    ;;
    stop)
    logger - s “Script motionStartup stopped”
    ;;
    *)
    echo “Usage: /etc/init.d/motionStartup {Start|stop}”
    exit 1
    ;;
    ESAC

    exit 0

    fi

  9. Hello that such a friend, Sorry follow your example and the led is lit, there is no problem, but when you click on the button turn off, the led flashes, already check the codes and everything is correct :/ you have any idea that could be doing wrong?

    • Hello Fco, Thanks for your comment.

      On the web, There are three buttons:

      Turn on, turn off, and flashing.

      If you can, paste here the code that you've assigned to the button turn off, so comment is possible that button turn off running the script that should be associated with the flashing button.

      see you soon.

  10. Hello again to all, another doubt more to add lol.

    Before I put in background, It is to activate a buzzer sound (buzzer) It should be a loop that put your pin to 1 and a 0 constantly, not as a led that while 1 It still.
    The problem is how to stop this loop to use it in a web php, because console by adding at the end of the script call a & It runs in the background and let the console free to call the script that ends it, example:

    sudo python sonar.py &

    These are two scripts, that makes it sound and making sure it stops ringing:

    import time
    import RPi.GPIO as gpio

    GPIO.SetWarnings(False)
    GPI.setMode(GPIO. BOARD)
    GPIO.Setup(7,GPIO. OUT)

    GPIO.output(7,1)
    time.sleep(1)(.3)
    GPIO.output(7,0)
    time.sleep(1)(.3)

    and the shutoff:

    import time
    import RPi.GPIO as gpio

    GPIO.SetWarnings(False)
    GPI.setMode(GPIO. BOARD)
    GPIO.Setup(7,GPIO. OUT)

    GPIO.output(7,0)
    GPIO.cleanup()

    but simply to disrupt the first worked. Thanks a greeting.

  11. You can concatenate several commands, for that based on your script name, the terminal look your PID and subsequently kill its execution with Kill.

    Once found the command, change the function intended to close with Ctrl + C, by one instead, buzzing N times Matt it automatically with this command.

  12. If with Ctrl + C closes in console, but I try to implement it through PHP as your post leds, then will have to be another way I say.

    • Above you have the solution to kill your process with BASH, finding your PID from the file name; you only have to integrate it into your PHP website with “exec”.

  13. Hello.
    Thanks for the tutorial.

    I have a problem, does not execute the script, If you run them from console, but since the page not, I don't know that it can be. I've done what you comment of
    “We write sudo visudo at the terminal and at the end of the file add to www-data as sudoer, It must be so.”

    But nothing. What else can I look at?

    Best regards.

  14. Hello, Good morning,
    At nothing, Thank you.

    It allows a question, everything works perfectly, now, I would like to be able to add a screen that indicates when the leds are switched on or off, I found some articles even with images showing colour depending on whether changing lamps are functioning or not, try to understand the operation but fails to make it work, I get to the web with commands but nothing is activated, I hope to not bother with this and another look, Thank you for your amazing kindness.

    Ignacio from Belgium

    • Hello Ignacio, Thank you very much for your comment.

      I guess that you're using Javascript to do this, integrating the PHP functions that activate the Python script for, manage the power-luminaires.

      JavaScript not domino, Since I am not a web developer but perhaps you can orient yourself a little better, or some other reader can help you get what you want; but to do so it would need to see the code, or the tutorials that you followed to try to detect where the bug is.

      see you soon, and lots of encouragement!

  15. Hello to all many thanks, I learn a lot.
    I have followed all the steps exposed, I can turn on the leds from the local terminal, load the php patch as instructed by ullu, grant permissions even one 775—-etc. Load the page with the buttons after led/gpio but nothing. I'm using Raspberry pi 2…..will be something with this card?. I appreciate your cooperation.

    • Hello Rafael, Thank you for comment.

      It is possible that the solution is in the comments of the previous tutorials in this series devoted to affordable home automation based on Raspberry PI.

      The steps to follow are compatible with any versions of IP, It occurs to me that you could change the execution of the script in python on the website by the following command which will create a file in the home directory of your Raspberry:

      sudo touch /home/pi/funciona

      If all is working well, This file should be created.

      • Thank you for your quick response. I made the changes, as he told it to me, in index.html by removing the part of python and placing in return: sudo touch /home/pi/funciona , but unfortunately the file is not created in my raspberry. I think it is an excellent test and the result I believe it concentrated the problem in the web area. I will look more information in other tutorials. Thanks for your help. Keep in touch.

        • Hello Rafael, then you could do the same test by slightly changing the path of the file to check if the problem lies with the permissions of the directory/var/www.

          sudo touch /var/www/funciona

  16. Hello, I could tell a way to put a tag with LED status from PHP web interface
    that change automatically to give clicking said button either to turn on, turn off, flashing… have a label that tells you the status. Thanks for your input I hope you can help me..

    • Hello Chaka, Thank you very much for your comment.

      I think that you could do it with PHP and JavaScript if you want to give some more visual effect, or make changes to the button.

      If you only want to add a text caption according to button pressure, You should only extend the functions associated with each input; I enclose a list of the functions available in PHP:

      http://php.net/manual/es/indexes.functions.php

    • Hello Jameson,

      I do not well understand the question, but to find the IP of a device you can use NMAP.

      You also have tools with graphical interface as ZenMap based on NMAP and Fing for Android devices.

    • Hello Jameson,

      The IP that you have to type in your browser is indeed that of Raspberry PI since this is that acts as a web server, also if your php file not listed it as index.php you will have to add the name of your separate file by one after the ip “/”.

      In my case the IP is in the range 10.42.0.0 Since it is an IP of the NAT type provided by a virtual network interface.

  17. Please I need help. The issue is that I have everything as such which explain it to you, but do not work in PHP scripts.

    Thanks a lot

    • Hi Sergio,

      I guess you are referring to scripts in Python, others had the same problem and there are several solutions in the previous comments; If after testing them persists the failure if you bring more data perhaps other readers or I could help you.

      see you soon.

    • Hello Roberto, Welcome.

      I am not an expert in php, and in those moments I looked for a tutorial to execute orders in the interpreter bash from php and thus found it.

      It would be more appropriate to use to = exec instead of?- exec?

      Thank you very much for your comment, see you soon.

  18. If not you work the web but if scripts, I will soon make an article so you can fix it; Thanks to the information I have collected various users that has not worked.

    If you run you hurry to fix it, You can check if the var directory exists in your raspberry, and if so you must move all the files to that path and replace in scripts “/var/www” by “/var/www/html” since now
    This is the default web server path.

    It is also possible that you have not deleted the index by default, “index.html” or missing you to change it by “index.php”.

    I hope to be of help, see you soon.

  19. Hello Antonio,
    I was wondering if a web server can be mounted to fit a home automation system and try the twitter api to turn on lights etc… (I've seen that it can also be done by python with some libraries and easy to follow tutorials… but you could do as I say and with php? i.e., the raspberry is the web server with twitter and gpio ports ignite leds
    Thanks in advance.

    • Nano Hello,

      From the web in php can run any command in the interpreter command BASH, that includes scripts in Python.

      You can use the library or Twitter API so that when you will send a tweet citing of the bot account's user name, turn off or turn on the light or appliance associated with that message in particular.

      I see a problem, and it is that anyone could turn on you and turn you lights and home appliances; unless that it can restrict depending on the user that sent the tweet.

      It would be interesting if you're commenting on what you find in this regard.

      Thanks for commenting, see you soon.

  20. Good afternoon,
    I followed the tutorial, copy the code of the web, save it as .php, in the folder that created me the server var , because if I put the index.php out of the html folder do not find me the file, but good, It seems that everything is OK. I'm only testing with the GPIO 17, I then created two .py, to turn on and to turn off, I have located them in the same folder as the file index.php. I open the browser, entered in the index, but will not operate me the led. If I have ssh access archive ignites and execute it via console if that works. not be where I be failing. Of course change the path to the scripts from php, Since it is different from your tutoring.
    I hope you can lend me a hand, I'm caught.
    A greeting.

    Thanks a lot.

  21. Hello peatonet, I'm trying to do an invention to activate a module of 4 relays from the web to turn on or turn off the lights of a local I have, the problem is that in the test, led with redistencia starts and stops at will from the web, but when I connect the pin40 (which is which I have configured) removing the resistance and led, shuts off the red light of the relay module and not turned on or turned off I have to test the bulb.

    Please help me because no longer is if that is wrong the relay module (damn aliexchinapress) but in none of the 2 modules that I have works or is that the raspberrypi not sent enough voltage to the relay (as VCC I have you since the pin3)

    Sorry for the tirade and thanks in advance

    Greetings friends

    • Hello, autorespondo me I friends, the problem had it that in the pin20 (in my case) had to put a resistance, I put it in 330 earthed.
      I suppose it's because when you pass led to relay remove resistance

      Thank you for everything ?

      • Hello Jesus, Many thanks to you for sharing the solution with us.

        Sorry if I could not answer before but I catch with fever, in the last week of classes preparing exams, and up work (the Freelance does not have permission to get sick).

        With respect to the pin that you have connected the VCC, in my case I connect to pin 2 or to the 4 failing already requiring 5V power supply.

        If you want you can give more details of your proof of concept, you are then using leds instead of 220V AC outlets?

        Thanks for your comment.

  22. Hello, I'm doing a project for school and was wondering if there is the way that you can emulate this system from an android app, i.e., How to handle the pins of the raspberry in this way it seems 100 times more easier than in an android application, I wonder if creating an application could in any way automatic directing me to this type of system to avoid having to learn from scratch to program such a thing in android. Unfortunately I am obliged to deliver the project as a mobile application, not of web form.
    I hope your answer

  23. Hello and thank you for everything, I repeat my question, What is the difference for them to work in the raspberry pi 3, in advance,

    Thank you

    • Hello Ignacio, Thanks for commenting.

      This tutorial is developed on a Raspberry PI 3 so it should work without complications.

      If still you can not make it work, You can share those errors or problems that you have found.

  24. Hello colleagues, need of your help because I'm immersed in a project between 3 specialties, electric, Electronics and computer science, I domino the part of electricity but I am not specialist in the other two. Les explained, I need to connect a M2DL2 Network Analyzer to a Raspberry pi in order to record data of current consumption and to transmit them to the raspberry and register it and displayed in a data table that is in turn. I know that signal that makes the scanner is RS485 and RS232 which should enter the raspberry is what I need a converter of one signal to another, What don't I know is as do the interconnection between the Analyzer, the converter and the raspberry. You know anything about this or can guide me where find information?
    Best regards.
    Ramon.

  25. Hello as you are?
    If I would like to check the status of a gpio, through php, so I say if this ON / OFF since I'm remote. As you should do?

    Perhaps there is some way of that the rasp return the value that this the gpio.

    But the hare in a db by entering 1 o 0 to the press those buttons, but that I see medium rustic

    • Hola Juani, Thanks for commenting.

      The subsystem GPIO is to the end and after the part of the system of files/sys of GNU / Linux, and actually managed by editing the values of each GPIO port-mapped files.

      You can check if a GPIO is active or inactive reading the file containing your value, an example in PHP for the GPIO 18:

      $estado_gpio18 = file_get_contents(“/sys/class/gpio/gpio18/value”);

      If the value is “1” the GPIO is active, on the other hand, if the value is “0” is idle.

      see you soon.

  26. Hello, first thank you for the hassle of sharing your knowledge with which we have similar concerns.

    I would like to make a query (I hope it's not very silly), months ago that I've been mulling over the issue of the creation of web interfaces for the raspi. Your comment the topic give permissions to www-data, but that, It would be nice as a comment for access from local environments or for testing. Would that solutions recommend for safer environments?

    A greeting and thanks again

    • Hello Gon, Thank you for your comment; I only try to document as I'm learning every day about this fascinating world, form online so that we can learn together.

      I would like to remind all readers that despite being functional this panel is not at all perfect, is a really basic example of what it can do and the more I learn more I realize how much that could be improved; This year I'm trying to improve my skills as a programmer since I always spent more to systems and security, and he didn't just notions about code structures, design methodologies or algorithms; I hope in brief take time to improve the web panel and share a next version of which already do not me so much averguence.

      In principle since version 3 the Web is sufficient to add user www-data to the GPIO group to manage the State of the pins avoiding give sudoer permissions; This can be done using the following command:

      sudo usermod-a-G gpio www-data

      Anyway I think that your question is more focused on restricting access to the panel by means of authentication, and this can be done with the mod_auth module for Apache2; It is also advisable to use mod_ssl either with a self-signed certificate.

      If you are going to access from the outside always you can add one layer of security by configuring a VPN, to which you can protect the access with x 509 certificates, username and password, and systems of prevention such as fail2ban among others.

      I hope I have clarified a little your doubts, Thank you for your interest in the article.

      • Hello Antonio! First of all thank you for responding so quick and.

        A mi me goes a bit backwards, I am developer web trying to soak me of the world of those systems and it security. It is a pleasure that people there are like you that invest their time documenting their projects so others can learn a little more every day.

        Tell you that I've helped a lot. I have a small project connected to the raspi, and it took months circling the interface and without much to do decided me ( If php, that if cylon.js, type mydevice already created services and similar…) and I always found the same doubts: permissions, Security, …etc. Your article (believe me that read many) is the few that go a little further as puncturing the GPIO cable, from there the ask you.

        Thanks again, you have clarified many doubts.

        I'll be testing and implementing as you have free time, and if you're interested I mention results.

        A greeting

  27. Hello that applications are can use in the car
    It could that work milrrolling
    Someone to cast any project on this topic
    Saúde

Leave a Reply