Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HOWTO install C library for GPIO and Example Shutdown system with a GPIO button
#1
Hi There.
This is my second HOWTO here 
Please read it complete before try, to understand what we are doing.
I am not responsible for any damage that could be caused on your system for a bad interpretaion of this guide or any issue.
this is for help others 

On this guide we will install the C library for GPIO WiringPI, updated to support our boards OPI.
After install the library we will see how to compile the examples and then how to create our own program to manage the GPIO.
note that this guide is a step by step to be understand by any users with any level of linux (from novice to expert) with detailed information to novices than experts will see like a brick  so please understand it and skip the extra info.

so we start
to follow this guide we need:
A OPI board connected to internet, keyboard and screen to go trough console or via ssh

open the console or ssh connection to your board and login as pi
I use the Downloads folder as base so after login go to it.
Code:
cd Downloads/

to install the library we need to download sources from github.com

Code:
git clone https://github.com/zhaolei/WiringOP.git -b h3

and we will see
some messages as is cloned and is created a new folder WiringOP

Cloning into 'WiringOP'...

remote: Counting objects: 355, done.
remote: Total 355 (delta 0), reused 0 (delta 0), pack-reused 355
Receiving objects: 100% (355/355), 284.36 KiB | 396.00 KiB/s, done.
Resolving deltas: 100% (189/189), done.
Checking connectivity... done.

so we move to the new folder
Code:
cd WiringOP/

we will do full installation as we are provided with an automatic install we will use it. Rolleyes
a custom install is also available read the INSTALL file on local folder.

Code:
sudo ./build

we will see a list of messages and finally all done and a final note

All Done.


NOTE: To compile programs with wiringBP v2, you need to add:
    -lwiringPi
  to your compile line(s) To use the Gertboard, MaxDetect, etc.
  code (the devLib), you need to also add:
    -lwiringPiDev
  to your compile line(s).

so now is installed, we are ready to test it Idea
in order to read the gpio we don't need to be root, and we can read all pins with

Code:
gpio readall

and if all was ok with the installation we should obatin somethink lke this (This is on my own OPI PC)

Code:
+-----+-----+----------+------+---+-Orange Pi+---+---+------+---------+-----+--+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
|     |     |     3.3v |      |   |  1 || 2  |   |      | 5v       |     |     |
|  12 |   8 |    SDA.0 | ALT5 | 0 |  3 || 4  |   |      | 5V       |     |     |
|  11 |   9 |    SCL.0 | ALT5 | 0 |  5 || 6  |   |      | 0v       |     |     |
|   6 |   7 |   GPIO.7 | ALT3 | 0 |  7 || 8  | 0 | ALT3 | TxD3     | 15  | 13  |
|     |     |       0v |      |   |  9 || 10 | 0 | ALT3 | RxD3     | 16  | 14  |
|   1 |   0 |     RxD2 | ALT3 | 0 | 11 || 12 | 0 | ALT3 | GPIO.1   | 1   | 110 |
|   0 |   2 |     TxD2 | ALT3 | 1 | 13 || 14 |   |      | 0v       |     |     |
|   3 |   3 |     CTS2 | ALT3 | 0 | 15 || 16 | 0 | ALT3 | GPIO.4   | 4   | 68  |
|     |     |     3.3v |      |   | 17 || 18 | 0 | ALT3 | GPIO.5   | 5   | 71  |
|  64 |  12 |     MOSI | ALT4 | 0 | 19 || 20 |   |      | 0v       |     |     |
|  65 |  13 |     MISO | ALT4 | 0 | 21 || 22 | 0 | ALT3 | RTS2     | 6   | 2   |
|  66 |  14 |     SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0      | 10  | 67  |
|     |     |       0v |      |   | 25 || 26 | 0 | ALT3 | GPIO.11  | 11  | 21  |
|  19 |  30 |    SDA.1 | ALT4 | 0 | 27 || 28 | 0 | ALT4 | SCL.1    | 31  | 18  |
|   7 |  21 |  GPIO.21 | ALT3 | 0 | 29 || 30 |   |      | 0v       |     |     |
|   8 |  22 |  GPIO.22 | ALT3 | 0 | 31 || 32 | 0 | ALT3 | RTS1     | 26  | 200 |
|   9 |  23 |  GPIO.23 | ALT3 | 0 | 33 || 34 |   |      | 0v       |     |     |
|  10 |  24 |  GPIO.24 | ALT3 | 0 | 35 || 36 | 0 | ALT3 | CTS1     | 27  | 201 |
|  20 |  25 |  GPIO.25 | ALT3 | 0 | 37 || 38 | 0 | ALT3 | TxD1     | 28  | 198 |
|     |     |       0v |      |   | 39 || 40 | 0 | ALT3 | RxD1     | 29  | 199 |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+-Orange Pi+---+------+----------+-----+-----+

if you obatin this lisk ,
congratulations you have installed the library and we are ready to continue with the provided examples. Tongue

Code:
cd examples/

in order to compile an example we have several options.
we can compile blink with the Makefile provided just using
Code:
make blink
this will generate the new program blink

but first we will read the file blink.c to understand what it will do , cat blink.c
Code:
cat blink.c
/* I have remove some commented lines to have this short */

#include <stdio.h>
#include <wiringPi.h>

// LED Pin - wiringPi pin 0 is BCM_GPIO 17.

#define LED     0

int main (void)
{
 printf ("Raspberry Pi blink\n") ;

 wiringPiSetup () ;
 pinMode (LED, OUTPUT) ;

 for (;;)
 {
   digitalWrite (LED, HIGH) ;  // On
   delay (500) ;               // mS
   digitalWrite (LED, LOW) ;   // Off
   delay (500) ;
 }
 return 0 ;
}

just pay atention to this lines
#define LED     0

where we are defining the led to use the WiringPI pin 0

if you go back and read again the exit from gpio readall
you will see than wiringPI 0 (wPi) is same as Phisical PIN 11 take a look on the mode and V for status Idea

Code:
+-----+-----+----------+------+---+-Orange Pi+---+---+------+---------+-----+--+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
|   1 |   0 |     RxD2 | ALT3 | 0 | 11 ||    |   |      |          |     |     |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+-Orange Pi+---+------+----------+-----+-----+

now we are ready to test the program but as we don't hace connected a led, we will use the gpio readall to see if the pin change the status
in order to modify the GPIO we need to be root so it we execute our 1st example
Code:
./blink

we will obtain a error Huh
pi@RetrOrangePi:~/Downloads/WiringOP/examples$ ./blink
Raspberry Pi blink
wiringPiSetup: Must be root. (Did you forget sudo?)

so we will use sudo the execute it and as we want to have it running on blackgroung to see it works we will also add & at the end of our command, then we will be provided with a process number, please remember this number as we will needed it later.
Code:
sudo ./blink &

pi@RetrOrangePi:~/Downloads/WiringOP/examples$ sudo ./blink &
[1] 1928
pi@RetrOrangePi:~/Downloads/WiringOP/examples$ Raspberry Pi blink

you can see as I have been provided with number 1928 
this is the process number asigned for the system to this process, we will use later to kill the process if needed 

now that our blink is running we will read the gpio several times to see if there any difference, note that as this is intermitent may you will need to read several times to see the changes on realtime

1st attempt
Code:
gpio readall
+-----+-----+----------+------+---+-Orange Pi+---+---+------+---------+-----+--+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
|     |     |     3.3v |      |   |  1 || 2  |   |      | 5v       |     |     |
|  12 |   8 |    SDA.0 | ALT5 | 0 |  3 || 4  |   |      | 5V       |     |     |
|  11 |   9 |    SCL.0 | ALT5 | 0 |  5 || 6  |   |      | 0v       |     |     |
|   6 |   7 |   GPIO.7 | ALT3 | 0 |  7 || 8  | 0 | ALT3 | TxD3     | 15  | 13  |
|     |     |       0v |      |   |  9 || 10 | 0 | ALT3 | RxD3     | 16  | 14  |
|   1 |   0 |     RxD2 |  OUT | 0 | 11 || 12 | 0 | ALT3 | GPIO.1   | 1   | 110 |
|   0 |   2 |     TxD2 | ALT3 | 1 | 13 || 14 |   |      | 0v       |     |     |
|   3 |   3 |     CTS2 | ALT3 | 0 | 15 || 16 | 0 | ALT3 | GPIO.4   | 4   | 68  |
|     |     |     3.3v |      |   | 17 || 18 | 0 | ALT3 | GPIO.5   | 5   | 71  |
|  64 |  12 |     MOSI | ALT4 | 0 | 19 || 20 |   |      | 0v       |     |     |
|  65 |  13 |     MISO | ALT4 | 0 | 21 || 22 | 0 | ALT3 | RTS2     | 6   | 2   |
|  66 |  14 |     SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0      | 10  | 67  |
|     |     |       0v |      |   | 25 || 26 | 0 | ALT3 | GPIO.11  | 11  | 21  |
|  19 |  30 |    SDA.1 | ALT4 | 0 | 27 || 28 | 0 | ALT4 | SCL.1    | 31  | 18  |
|   7 |  21 |  GPIO.21 | ALT3 | 0 | 29 || 30 |   |      | 0v       |     |     |
|   8 |  22 |  GPIO.22 | ALT3 | 0 | 31 || 32 | 0 | ALT3 | RTS1     | 26  | 200 |
|   9 |  23 |  GPIO.23 | ALT3 | 0 | 33 || 34 |   |      | 0v       |     |     |
|  10 |  24 |  GPIO.24 | ALT3 | 0 | 35 || 36 | 0 | ALT3 | CTS1     | 27  | 201 |
|  20 |  25 |  GPIO.25 | ALT3 | 0 | 37 || 38 | 0 | ALT3 | TxD1     | 28  | 198 |
|     |     |       0v |      |   | 39 || 40 | 0 | ALT3 | RxD1     | 29  | 199 |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+-Orange Pi+---+------+----------+-----+-----+

2nd attempt

Code:
gpio readall
+-----+-----+----------+------+---+-Orange Pi+---+---+------+---------+-----+--+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
|     |     |     3.3v |      |   |  1 || 2  |   |      | 5v       |     |     |
|  12 |   8 |    SDA.0 | ALT5 | 0 |  3 || 4  |   |      | 5V       |     |     |
|  11 |   9 |    SCL.0 | ALT5 | 0 |  5 || 6  |   |      | 0v       |     |     |
|   6 |   7 |   GPIO.7 | ALT3 | 0 |  7 || 8  | 0 | ALT3 | TxD3     | 15  | 13  |
|     |     |       0v |      |   |  9 || 10 | 0 | ALT3 | RxD3     | 16  | 14  |
|   1 |   0 |     RxD2 |  OUT | 1 | 11 || 12 | 0 | ALT3 | GPIO.1   | 1   | 110 |
|   0 |   2 |     TxD2 | ALT3 | 1 | 13 || 14 |   |      | 0v       |     |     |
|   3 |   3 |     CTS2 | ALT3 | 0 | 15 || 16 | 0 | ALT3 | GPIO.4   | 4   | 68  |
|     |     |     3.3v |      |   | 17 || 18 | 0 | ALT3 | GPIO.5   | 5   | 71  |
|  64 |  12 |     MOSI | ALT4 | 0 | 19 || 20 |   |      | 0v       |     |     |
|  65 |  13 |     MISO | ALT4 | 0 | 21 || 22 | 0 | ALT3 | RTS2     | 6   | 2   |
|  66 |  14 |     SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0      | 10  | 67  |
|     |     |       0v |      |   | 25 || 26 | 0 | ALT3 | GPIO.11  | 11  | 21  |
|  19 |  30 |    SDA.1 | ALT4 | 0 | 27 || 28 | 0 | ALT4 | SCL.1    | 31  | 18  |
|   7 |  21 |  GPIO.21 | ALT3 | 0 | 29 || 30 |   |      | 0v       |     |     |
|   8 |  22 |  GPIO.22 | ALT3 | 0 | 31 || 32 | 0 | ALT3 | RTS1     | 26  | 200 |
|   9 |  23 |  GPIO.23 | ALT3 | 0 | 33 || 34 |   |      | 0v       |     |     |
|  10 |  24 |  GPIO.24 | ALT3 | 0 | 35 || 36 | 0 | ALT3 | CTS1     | 27  | 201 |
|  20 |  25 |  GPIO.25 | ALT3 | 0 | 37 || 38 | 0 | ALT3 | TxD1     | 28  | 198 |
|     |     |       0v |      |   | 39 || 40 | 0 | ALT3 | RxD1     | 29  | 199 |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+-Orange Pi+---+------+----------+-----+-----+

did you see the difference? Huh

1st check the mode on Phisical pin 11
you will notice the difference from initial reading as pin is now OUT and value should alternate from 0 to 1 every 500 ms as per our blink example.

SO THATS WORKS Cool 

we are ready to compile our own program
Exclamation


as we are using the console or ssh, we will use vi as editor
if you are on a Xterm you can use any other graphic editor as pluma

if you don't know how to use vi, start with
man vi

some basic keys are
i -> where on command mode change to insert mode
esc -> where on insert mode return to command mode
: -> where on command mode change to execution mode
wq -> where on execution mode write our file and quit from vi
for a more extended keys please read the manual for vi (man vi)

so we will create our own file apagado.c with vi
Code:
vi apagado.c


and we will see a new file created on the vi  like this one


Code:
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"apagado.c" [Archivo nuevo]                                                       0,0-1        Todo

my system is on spanish so on the status bar you we wil see somethink like
" [New file] " instead [archivo nuevo]" 
we click on the key i and we will see as status bar changed to insert mode
--- INSERT ---

and we are ready to c and paste from this code
Code:
/* Apagado de la consoloa con el pin 25 by Spirax
*/

#include <stdio.h>
#include <wiringPi.h>

#define button_shutdown 25

int system(const char *command);

int main (void)
{
wiringPiSetup () ;
pinMode (button_shutdown, INPUT);
pullUpDnControl (button_shutdown, PUD_UP) ;

for (;;)
{ if (digitalRead (button_shutdown) == LOW) // boton pulsado = ejecutar shutdown
system("shutdown -h now");
sleep (1);
}
return 0 ;
}

now press the ESC key to return to command mode on vi
now press : to start the execution mode on vi
now write on the status bar
wq
to write and exit

and you will have the file apagado.c created on your folder ready to be compiled.
but 1st we will take a look on the source code
Code:
#include <wiringPi.h>

we are including the wiringPi library to use its functions


Code:
#define button_shutdown 25

we are using WiringPI 25 that is Phisycal pin 37

Code:
pinMode (button_shutdown, INPUT);

we stablish the pin mode as Input

Code:
pullUpDnControl (button_shutdown, PUD_UP) ;

we are starting the pin as UP

Code:
if (digitalRead (button_shutdown) == LOW)

we are continuosly checking on a loop if the Pin Change to LOW and ejecute next command  if LOW

Code:
system("shutdown -h now");

so when the pin is LOW we are doing a shutdown -h now (power off system)

so now that we are supposed we know what it will do, we are ready to compile it

I you remember the final note when we installed the library, in order to compile we will need to use our gcc with some parameters to link to the library on this case this are -lwiringPi -lwiringPiDev

Code:
gcc -lwiringPi -lwiringPiDev -o apagado apagado.c

this will compile our apagado.c source with an outoput file named as apagado

so thats all, we have our program compiled and we are ready to test
  note save all your work as we are ready to shutdown the system  

as we want our program be run on backgground and we want to shutdown it must be run as root
Code:
sudo ./apagado &

we will be provided with a number for the process but no needed this time

we are ready to check.
1st think we can do is read the gpio to check if there any difference on pin phisycal 37
Code:
readall
+-----+-----+----------+------+---+-Orange Pi+---+---+------+---------+-----+--+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
|     |     |     3.3v |      |   |  1 || 2  |   |      | 5v       |     |     |
|  12 |   8 |    SDA.0 | ALT5 | 0 |  3 || 4  |   |      | 5V       |     |     |
|  11 |   9 |    SCL.0 | ALT5 | 0 |  5 || 6  |   |      | 0v       |     |     |
|   6 |   7 |   GPIO.7 | ALT3 | 0 |  7 || 8  | 0 | ALT3 | TxD3     | 15  | 13  |
|     |     |       0v |      |   |  9 || 10 | 0 | ALT3 | RxD3     | 16  | 14  |
|   1 |   0 |     RxD2 | ALT3 | 0 | 11 || 12 | 0 | ALT3 | GPIO.1   | 1   | 110 |
|   0 |   2 |     TxD2 | ALT3 | 1 | 13 || 14 |   |      | 0v       |     |     |
|   3 |   3 |     CTS2 | ALT3 | 0 | 15 || 16 | 0 | ALT3 | GPIO.4   | 4   | 68  |
|     |     |     3.3v |      |   | 17 || 18 | 0 | ALT3 | GPIO.5   | 5   | 71  |
|  64 |  12 |     MOSI | ALT4 | 0 | 19 || 20 |   |      | 0v       |     |     |
|  65 |  13 |     MISO | ALT4 | 0 | 21 || 22 | 0 | ALT3 | RTS2     | 6   | 2   |
|  66 |  14 |     SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0      | 10  | 67  |
|     |     |       0v |      |   | 25 || 26 | 0 | ALT3 | GPIO.11  | 11  | 21  |
|  19 |  30 |    SDA.1 | ALT4 | 0 | 27 || 28 | 0 | ALT4 | SCL.1    | 31  | 18  |
|   7 |  21 |  GPIO.21 | ALT3 | 0 | 29 || 30 |   |      | 0v       |     |     |
|   8 |  22 |  GPIO.22 | ALT3 | 0 | 31 || 32 | 0 | ALT3 | RTS1     | 26  | 200 |
|   9 |  23 |  GPIO.23 | ALT3 | 0 | 33 || 34 |   |      | 0v       |     |     |
|  10 |  24 |  GPIO.24 | ALT3 | 0 | 35 || 36 | 0 | ALT3 | CTS1     | 27  | 201 |
|  20 |  25 |  GPIO.25 |   IN | 1 | 37 || 38 | 0 | ALT3 | TxD1     | 28  | 198 |
|     |     |       0v |      |   | 39 || 40 | 0 | ALT3 | RxD1     | 29  | 199 |
+-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
| BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+-----+-----+----------+------+---+-Orange Pi+---+------+----------+-----+-----+

you will see as GPIO.25 on WiringPi (pin Phisycal 37) now is mode IN and is activated 1

this mean that when we short this pin with ground (Phisical Pin 39) it will be 0, and we will execute the command as it is LOW.

now we can join pin 37 and 39 for a second, using a wire, a button, a coin, screwdriver or something metalic
and ohh surprise !!! the system is going down and poweroff

ok our program works, but after system is powered on again we need to have our program running again on background.
to do it automaticly we need to include it on the system startup process to run it as root.

I suppose you remember where our program was saved when compiled...
if no, check
~Downloads/WiringOPI/examples

we are copying it to the system path for later use
so go do this folder and copied it to /bin
Code:
cd Downloads/WiringOPI/examples
Code:
sudo cp apagado /bin

now in order to have it executed when boot the system we will modify the file /etc/rc.local with vi
as this is a system file it need to be mofidied by root only (or with sudo)

Code:
sudo vi /etc/rc.local

now we use the cursors to go down until the before last line exit 0 and add a new file
you remember from previous?
use key i to insert and add our apagago & as a new line before exit 0 line.

then click on ESC and : wq to save and exit

so now our file /etc/rc.local bin should be like this one
Code:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
apagado &

exit 0

and ready to restart the system to see if all changes are ok
Code:
sudo reboot

after the sytems restart we can check if our program is running on several ways.

1st we can check the process running to see if our apagado is running with ps -aux|grep apagado
Code:
ps -aux |grep apagado
root       362  0.0  0.0   1700   468 ?        S    10:44   0:00 apagado
pi         953  0.0  0.0   3684   744 pts/0    S+   10:45   0:00 grep apagado

we should see 2 lines one apagado running as root and the grep agagado running as pi

2nd we can check with the gpio readall as see status of pin 37 , it shoud be mode IN and activated to 1

and the 3rd and efective one is to push the button we should install between pin 37 and 39
this is the best one but if works as expected this will power off the system 

as I say at the begginig use this guide as your own risk.
I am not the library creator, just an user that share how to use it .
hope this help
Angel
Reply
#2
Cool!
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
Question HOWTO - GPIO Controller ericktarzia 18 6,850 10-15-2017, 12:28 AM
Last Post: hessamghani
  HOWTO - Dreamcast reconfigure controls - Reicast alexkidd 7 4,460 10-07-2017, 04:49 PM
Last Post: Fulmore
  HOWTO - eMMC install script for Ropi Harryzimm 30 10,770 08-10-2017, 07:24 AM
Last Post: Lamerjack
  HOWTO - display from 3.5mm/RCA CVBS audio/video alexkidd 18 12,035 07-20-2017, 05:09 PM
Last Post: lesshiy
  HOW TO - install Amiga emulator and run games? Vadim1972 1 513 06-18-2017, 06:47 PM
Last Post: alexkidd
  HOWTO upgrade system without crash it with sudo apt-get upgrade Spirax 1 807 06-08-2017, 10:24 AM
Last Post: alexkidd
  HOWTO - Run some ROMs from network share (per emulator) chupo_cro 9 2,190 05-18-2017, 10:35 PM
Last Post: chupo_cro
  HOWTO - Using DPad as analog control (N64 emulation) alexkidd 8 3,709 04-11-2017, 08:04 PM
Last Post: alexkidd
  HOWTO - execute permissions in NTFS storage (useful to change ROMS folder) Sikotik 2 1,569 12-30-2016, 01:25 AM
Last Post: niabi
  HOWTO: Set time from RetroPie Menu Sikotik 0 1,189 12-19-2016, 02:09 AM
Last Post: Sikotik

Forum Jump:


Users browsing this thread: 1 Guest(s)