Using pigpiod
The pigpio library described earlier (see Using pigpio) can be launched and used as a daemon.
Usage
To launch the daemon, use the following command:
ubuntu@rpi:~$ sudo pigpiod
To stop the daemon, stop the previous process:
ubuntu@rpi:~$ sudo killall pigpiod
To interface with pigpiod
you need to use a different set of functions, declared in pigpiod_if2.h
.
To compile code that interfaces with pigpiod
, link with libpigpiod_if2
instead of libpigpio
, as follows:
gcc -Wall -pthread -o io1212-4 io1212-4.c -lpigpiod_if2 -lrt
Example 1: Blink
An equivalent to the first example of the non-daemon pigpio library (see Example 1: Blink) can be created by calling the pigpiod_if2
interface instead of pigpio
.
1#include <unistd.h>
2
3#include <pigpiod_if2.h>
4
5#define Output_1 4
6#define Output_2 5
7#define Output_3 6
8#define Output_4 7
9#define Output_5 8
10#define Output_6 9
11#define Output_7 10
12#define Output_8 11
13#define Output_9 12
14#define Output_10 13
15#define Output_11 14
16#define Output_12 15
17
18#define Input_1 16
19#define Input_2 17
20#define Input_3 18
21#define Input_4 19
22#define Input_5 20
23#define Input_6 21
24#define Input_7 22
25#define Input_8 23
26#define Input_9 24
27#define Input_10 25
28#define Input_11 26
29#define Input_12 27
30
31char *optHost = NULL;
32char *optPort = NULL;
33
34int main(int argc, char *argv[])
35{
36 int pi;
37
38 pi = pigpio_start(optHost, optPort);
39
40 if (pi<0) return 1;
41
42 set_mode(pi, Output_1, PI_OUTPUT);
43 set_mode(pi, Output_2, PI_OUTPUT);
44 set_mode(pi, Output_3, PI_OUTPUT);
45 set_mode(pi, Output_4, PI_OUTPUT);
46 set_mode(pi, Output_5, PI_OUTPUT);
47 set_mode(pi, Output_6, PI_OUTPUT);
48 set_mode(pi, Output_7, PI_OUTPUT);
49 set_mode(pi, Output_8, PI_OUTPUT);
50 set_mode(pi, Output_9, PI_OUTPUT);
51 set_mode(pi, Output_10, PI_OUTPUT);
52 set_mode(pi, Output_11, PI_OUTPUT);
53 set_mode(pi, Output_12, PI_OUTPUT);
54
55 set_mode(pi, Input_1, PI_INPUT);
56 set_mode(pi, Input_2, PI_INPUT);
57 set_mode(pi, Input_3, PI_INPUT);
58 set_mode(pi, Input_4, PI_INPUT);
59 set_mode(pi, Input_5, PI_INPUT);
60 set_mode(pi, Input_6, PI_INPUT);
61 set_mode(pi, Input_7, PI_INPUT);
62 set_mode(pi, Input_8, PI_INPUT);
63 set_mode(pi, Input_9, PI_INPUT);
64 set_mode(pi, Input_10, PI_INPUT);
65 set_mode(pi, Input_11, PI_INPUT);
66 set_mode(pi, Input_12, PI_INPUT);
67
68 set_pull_up_down(pi, Input_1, PI_PUD_OFF);
69 set_pull_up_down(pi, Input_2, PI_PUD_OFF);
70 set_pull_up_down(pi, Input_3, PI_PUD_OFF);
71 set_pull_up_down(pi, Input_4, PI_PUD_OFF);
72 set_pull_up_down(pi, Input_5, PI_PUD_OFF);
73 set_pull_up_down(pi, Input_6, PI_PUD_OFF);
74 set_pull_up_down(pi, Input_7, PI_PUD_OFF);
75 set_pull_up_down(pi, Input_8, PI_PUD_OFF);
76 set_pull_up_down(pi, Input_9, PI_PUD_OFF);
77 set_pull_up_down(pi, Input_10, PI_PUD_OFF);
78 set_pull_up_down(pi, Input_11, PI_PUD_OFF);
79 set_pull_up_down(pi, Input_12, PI_PUD_OFF);
80
81 int count = 30;
82
83 while(count) {
84 gpio_write(pi, Output_1, 1);
85 sleep(1);
86 gpio_write(pi, Output_1, 0);
87 sleep(1);
88 count--;
89 }
90
91 pigpio_stop(pi);
92}
Example 2: Using inputs
An example of how to read an input using gpio_read()
can be tested by substituting lines 81-89 above with the following snippet.
int count = 20;
int state = gpio_read(pi, Input_1);
while(count) {
while(state == gpio_read(pi, Input_1));
state = gpio_read(pi, Input_1);
gpio_write(pi, Output_1, state);
sleep(1);
count--;
}
gpio_write(pi, Output_1, 0);