Jump to content

Jack Bizon

Members
  • Posts

    11
  • Joined

  • Last visited

Reputation Activity

  1. Like
    Jack Bizon got a reaction from Tido in [Project / Tutorial] Web control GPIO (A20 board)   
    Hi everyone!
     
    I was asked by tkaiser to share something about my PHP control of GPIO pins. I can tell you that I will be happy to do so. Mostly because I'm really extremly good in one thing and that is forgetting how i did something. Now let's cut bulls**t down and start with small introduction of what I'm up to. I'll decsribe what I'm doing and what is working for me so it should all click together as project documentation and tutorial. Also all I'm going to describe is being done on Cubieboard2 (A20) but i guess that it can work almost on any ARM SBC running Debian or Ubuntu.
     
    Introduction / Motivation
     
    Basically, I'm really interested in connecting low-level hardware to high-level software applications. Why? Why throw some rellays on 8-bit atmega, connect the whole contraption to internet and control your lamp from wherever in the world? Well... because we can and also why not! So with this mindset, all that Raspberry Pi hype and working atmega contraption on my table I decided to buy Cubieboard2 back in 2013. Aaaand it was the most stupid thing I could do. Support absolute zero,community non-existent and bootable images were unusable. But it was nice dust collector running Android! Long story short, few weeks ago I came across Armbian, saw how active and supported it is and how it spans over multiple boards. Bingo, this is what I need for doing another cool thing. Standalone internet connected GPIO device that could be used by multiple devices / apps without any special driver or communication protocol and whatever else.
     
    Project description
     
    Whole idea is that Cubieboard will run webserver with PHP application. This application will provide API that will allow GPIO control using http protocol and GET requests. Security here is not a concern as all of this will be hidden behind NAT in secured private LAN.
     
    Preparations
     
    Let's assume that you have clean install of Armbian 5.0 with Legacy kernel 3.4.110 running on your A10 or A20 board. Very first thing to do is to decide how many GPIO pins and which one you want to use. Most if not all pins that you have physically availible have more than one function. UART, eMMC, SPI, I2C, LCD and many other connections. Basically you need to look into documentation of your board and check if you can use your chosen pins as GPIO instead of thier original purpose. When you have this solved you need to modify script.bin located in /boot. I will take this short way. Take script.bin, convert it to fex using sunxi-tool bin2fex, add your chosen pins into [gpio_para] section, convert modified fex back into bin, put it back to /boot, reboot board. More about this can be found here.
     
    After you prepared your pins in script.bin it's about time to get things rolling. I recommend to run first this two commands if your install is as clean as holy water
    apt-get update apt-get upgrade Apache and PHP5 installation
     
    This is easy step as we do not need any configuration and defaults will do just fine. Execute following command
    apt-get install apache2 php5 FTP server instalation (optional)
     
    This is optional but will help with uploading php files to board. First install ProFTPd as standalone server.
    apt-get install proftpd Now we need to configure ftp server. I will save you labor, config you need is attached just replace original one in /etc/proftpd/proftpd.conf with it. After you replace config restart proftpd
    service proftpd restart Next you need to create FTP account that will be used for connection to board via FTP. First lets create group named ftpgroup
    addgroup ftpgroup Now we add user and add it to the ftpgroup
    adduser ftpuser -shell /bin/false -home /var/www/html adduser ftpuser ftpgroup And last thing is to make this ftpuser owner of /var/www/html
    chown -R ftpuser:ftpgroup /var/www/html Done. FTP is working now and you can connect and upload PHP files via Total Commander or similar file manager.
     
    Link GPIO to web directory
     
    If we want control GPIO from PHP we need to make GPIO accessible from web directory. Therefore we have to create symlink from our web directory to GPIO folder
    ln -s /sys/class/gpio /var/www/html/gpio Setting permissions
     
    Now we need to set permissions to allow our PHP scripts access GPIO in full scale. Let's start with exporting and unexporting pins from PHP. To do so user www-data have to be able to write into /sys/class/gpio/export and /sys/class/gpio/unexport. We can achieve this by making www-data sudoer. Run this command
    visudo and in section # User priviledge specification add following line under line with root
    www-data ALL=NOPASSWD: ALL Save and exit.
     
    Accessing GPIO pins from PHP
     
    Here is example code how to access GPIO pin from PHP.
    shell_exec("sudo bash -c 'echo 37 > gpio/export'"); shell_exec("sudo bash -c 'echo out > gpio/gpio37/direction'"); shell_exec("sudo bash -c 'echo 1 > gpio/gpio37/value'"); That's all for today. If you are interested I can also write up something about PHP library itself and API possibly too.
    new_proftpd.zip
  2. Like
    Jack Bizon got a reaction from tkaiser in [Project / Tutorial] Web control GPIO (A20 board)   
    Hi everyone!
     
    I was asked by tkaiser to share something about my PHP control of GPIO pins. I can tell you that I will be happy to do so. Mostly because I'm really extremly good in one thing and that is forgetting how i did something. Now let's cut bulls**t down and start with small introduction of what I'm up to. I'll decsribe what I'm doing and what is working for me so it should all click together as project documentation and tutorial. Also all I'm going to describe is being done on Cubieboard2 (A20) but i guess that it can work almost on any ARM SBC running Debian or Ubuntu.
     
    Introduction / Motivation
     
    Basically, I'm really interested in connecting low-level hardware to high-level software applications. Why? Why throw some rellays on 8-bit atmega, connect the whole contraption to internet and control your lamp from wherever in the world? Well... because we can and also why not! So with this mindset, all that Raspberry Pi hype and working atmega contraption on my table I decided to buy Cubieboard2 back in 2013. Aaaand it was the most stupid thing I could do. Support absolute zero,community non-existent and bootable images were unusable. But it was nice dust collector running Android! Long story short, few weeks ago I came across Armbian, saw how active and supported it is and how it spans over multiple boards. Bingo, this is what I need for doing another cool thing. Standalone internet connected GPIO device that could be used by multiple devices / apps without any special driver or communication protocol and whatever else.
     
    Project description
     
    Whole idea is that Cubieboard will run webserver with PHP application. This application will provide API that will allow GPIO control using http protocol and GET requests. Security here is not a concern as all of this will be hidden behind NAT in secured private LAN.
     
    Preparations
     
    Let's assume that you have clean install of Armbian 5.0 with Legacy kernel 3.4.110 running on your A10 or A20 board. Very first thing to do is to decide how many GPIO pins and which one you want to use. Most if not all pins that you have physically availible have more than one function. UART, eMMC, SPI, I2C, LCD and many other connections. Basically you need to look into documentation of your board and check if you can use your chosen pins as GPIO instead of thier original purpose. When you have this solved you need to modify script.bin located in /boot. I will take this short way. Take script.bin, convert it to fex using sunxi-tool bin2fex, add your chosen pins into [gpio_para] section, convert modified fex back into bin, put it back to /boot, reboot board. More about this can be found here.
     
    After you prepared your pins in script.bin it's about time to get things rolling. I recommend to run first this two commands if your install is as clean as holy water
    apt-get update apt-get upgrade Apache and PHP5 installation
     
    This is easy step as we do not need any configuration and defaults will do just fine. Execute following command
    apt-get install apache2 php5 FTP server instalation (optional)
     
    This is optional but will help with uploading php files to board. First install ProFTPd as standalone server.
    apt-get install proftpd Now we need to configure ftp server. I will save you labor, config you need is attached just replace original one in /etc/proftpd/proftpd.conf with it. After you replace config restart proftpd
    service proftpd restart Next you need to create FTP account that will be used for connection to board via FTP. First lets create group named ftpgroup
    addgroup ftpgroup Now we add user and add it to the ftpgroup
    adduser ftpuser -shell /bin/false -home /var/www/html adduser ftpuser ftpgroup And last thing is to make this ftpuser owner of /var/www/html
    chown -R ftpuser:ftpgroup /var/www/html Done. FTP is working now and you can connect and upload PHP files via Total Commander or similar file manager.
     
    Link GPIO to web directory
     
    If we want control GPIO from PHP we need to make GPIO accessible from web directory. Therefore we have to create symlink from our web directory to GPIO folder
    ln -s /sys/class/gpio /var/www/html/gpio Setting permissions
     
    Now we need to set permissions to allow our PHP scripts access GPIO in full scale. Let's start with exporting and unexporting pins from PHP. To do so user www-data have to be able to write into /sys/class/gpio/export and /sys/class/gpio/unexport. We can achieve this by making www-data sudoer. Run this command
    visudo and in section # User priviledge specification add following line under line with root
    www-data ALL=NOPASSWD: ALL Save and exit.
     
    Accessing GPIO pins from PHP
     
    Here is example code how to access GPIO pin from PHP.
    shell_exec("sudo bash -c 'echo 37 > gpio/export'"); shell_exec("sudo bash -c 'echo out > gpio/gpio37/direction'"); shell_exec("sudo bash -c 'echo 1 > gpio/gpio37/value'"); That's all for today. If you are interested I can also write up something about PHP library itself and API possibly too.
    new_proftpd.zip
  3. Like
    Jack Bizon got a reaction from Igor in Cubieboard2 GPIO help   
    I tought it have to do something with kernel. As I need 3.4 kernel, then I had to modify script.bin. I took script.bin from Cubian and from Armbian, merged gpio_para sections together and voilà... GPIO works nicely on armbian same way as on cubian. I need to do some minor tweaks but I can migrate app to armbian in matter of minutes now. Thanks for your help guys!
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines