петък, 17 юни 2011 г.

Creating web interface (part 2)

This Example shows how to access GPIO pins via web interface.

First you need to install on mini2440 linux kernel with GPIO support.
Unfortunately FriendlyARM comes with kernel without GPIO and SPI support.
If you decide to try to compile kernel take a look at "Kernel Tweaking" link below. There is a good explanation how to do it.
The other option is to download already compiled kernel from
It is for 7" display but for this example only network access to mini2440 is needed.
I'm not going to explain how to upload the kernel image. It's shown in the board's manual.

When the new kernel is uploaded let's first check the access to GPIO from the command line.
Telnet to mini2440 and go to sys/class/gpio folder:

$ telnet
[root@FriendlyARM /]# cd sys/class/gpio/
[root@FriendlyARM gpio]# ls
export       gpiochip0    gpiochip128  gpiochip160  gpiochip192  gpiochip224  gpiochip32   gpiochip64   gpiochip96   unexport

Folders gpiochip0 , gpiochip128 .... represent GPIO drivers for each port. Ports are named A,B,C....
Check the port for each gpiochipX:

[root@FriendlyARM gpio]# cat gpiochip0/label
[root@FriendlyARM gpio]# cat gpiochip32/label
[root@FriendlyARM gpio]# cat gpiochip192/label

Each pin has it's corresponding access number. For example buzzer is connected to GPB0 ( Port B pin 0) and port B is accessible from gpiochip32 so the number to access the buzzer is 32.
Led1 is connected go GPB5. It's access number is 32+5 = 37
Buttons are connected to port G - gpiochip192

Function    Port    Access number
Buzzer        GPB0    32
Led1           GPB5    37
Led2           GPB6    38
Led3           GPB7    39
Led4           GPB8    40
Btn1           GPG0    192
Btn2           GPG3    195
Btn3           GPG5    197
Btn4           GPG6    198
Btn5           GPG7    199
Btn6           GPG11    203

Now open access to the buzzer. :

[root@FriendlyARM gpio]# echo 32  > export
[root@FriendlyARM gpio]# ls
export       gpio32       gpiochip0    gpiochip128  gpiochip160  gpiochip192  gpiochip224  gpiochip32   gpiochip64   gpiochip96   unexport

A new link gpio32 appeared now. Let's check the current direction and make it an output:

[root@FriendlyARM gpio]# cat gpio32/direction

[root@FriendlyARM gpio]# echo "high" > gpio32/direction
[root@FriendlyARM gpio]# cat gpio32/direction

Turn the buzzer on and off:
[root@FriendlyARM gpio]# echo "1" > gpio32/value
[root@FriendlyARM gpio]# echo "0" > gpio32/value

Close the access:
[root@FriendlyARM gpio]# echo 32  > unexport

First stop the Led player as shown on one of previous posts.

The project consists from the following files:
access_num.h     -    definitions of GPIO driver access numbers            

gpio.h gpio.cpp -     class for control of GPIO. Each instance is for control of one pin

readbtn.cpp    -     CGI program that reads the state of the six board buttons and shows
            it in table on a web page. The page is automatically refreshed each
            two seconds.

ledbuzz.cpp    -     CGI program that reads the query string sent to the web server and
            turns on/off the LEDs and the Buzzer.Then the state of the four board
            leds and the buzzer are shown on a web page as checkboxes.
            The user can change the    checkboxes. When submit button is pressed
            the new states are sent to the web server using GET method and the
            same program is called again.
download the project folder from:

compile with command:
$ arm-linux-g++ gpio.cpp ledbuzz.cpp -o ledbuzz.cgi
$ arm-linux-g++ gpio.cpp readbtn.cpp -o readbtn.cgi

Note: In the project folder there are already compiled binaries ledbuzz.cgi and readbtn.cgi

upload ledbuzz.cgi and readbtn.cgi to the board to /www/cgi-bin folder and give them executable attribute
# chmod a+x ledbuzz.cgi
# chmod a+x readbtn.cgi

Run them from the command line. You should get HTML output

There is no index.html. Open CGIs from PC web browser directly:

Access to GPIO pins explained

Kernel Tweaking

P.S. If you find this articles useful please pay some attention to the ads.
Some of them might be interresting to you too. Thanks.

5 коментара:

  1. Hi do you have the patches for the kernel for GPIO access?

  2. Sorry, what do you mean patches?
    There is already pre-compiled kernel for download from

  3. Този коментар бе премахнат от автора.

  4. nevermind was my mistake for not reading the kernel twiking part you posted, I compile the kernel for the 3.5 inch if anyone needs it is here


  5. Hi The Rapid share files has been expire can you kindly mail me the lebbuzz and button codes to k.sundeep2007@gmail.com