回答編集履歴

1

実行例を追記

2018/07/22 07:32

投稿

mt08
mt08

スコア1825

test CHANGED
@@ -3,3 +3,137 @@
3
3
 
4
4
 
5
5
  コマンド呼び出しではなく、[rpi-gpio](https://www.npmjs.com/package/rpi-gpio)というようなライブラリを使うのはどうでしょうか?
6
+
7
+
8
+
9
+ (追記)実行例: npmのインストール~rpi-gpioインストール~サンプルコード実行
10
+
11
+
12
+
13
+ 環境: RaspberryPi3B, Raspbian:2018-06-27-raspbian-stretch.img、Raspbian付属のnode.js(v4.8.2)
14
+
15
+
16
+
17
+
18
+
19
+ ```
20
+
21
+ pi@raspberrypi:~ $ cat /proc/cpuinfo | grep Revision
22
+
23
+ Revision : a02082
24
+
25
+ pi@raspberrypi:~ $ lsb_release -a
26
+
27
+ No LSB modules are available.
28
+
29
+ Distributor ID: Raspbian
30
+
31
+ Description: Raspbian GNU/Linux 9.4 (stretch)
32
+
33
+ Release: 9.4
34
+
35
+ Codename: stretch
36
+
37
+ pi@raspberrypi:~ $ uname -a
38
+
39
+ Linux raspberrypi 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l GNU/Linux
40
+
41
+ pi@raspberrypi:~ $
42
+
43
+ pi@raspberrypi:~ $ node -v
44
+
45
+ v4.8.2
46
+
47
+ pi@raspberrypi:~ $ npm i rpi-gpio
48
+
49
+ -bash: npm: command not found
50
+
51
+ pi@raspberrypi:~ $ sudo apt-get update && sudo apt-get -qq -y install npm # npmをインストール
52
+
53
+ Extracting templates from packages: 100%
54
+
55
+ Selecting previously unselected package libc-ares2:armhf.
56
+
57
+ (Reading database ... 136942 files and directories currently installed.)
58
+
59
+ Preparing to unpack .../libc-ares2_1.14.0-1~bpo9+1_armhf.deb ...
60
+
61
+ .
62
+
63
+ .
64
+
65
+ Setting up node-gyp (3.4.0-1) ...
66
+
67
+ Setting up npm (1.4.21+ds-2) ...
68
+
69
+ Processing triggers for libc-bin (2.24-11+deb9u3) ...
70
+
71
+ pi@raspberrypi:~ $
72
+
73
+ pi@raspberrypi:~ $ npm i rpi-gpio
74
+
75
+ (node:4821) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
76
+
77
+ /
78
+
79
+ > epoll@0.1.22 install /home/pi/node_modules/rpi-gpio/node_modules/epoll
80
+
81
+ > node-gyp rebuild
82
+
83
+
84
+
85
+ make: Entering directory '/home/pi/node_modules/rpi-gpio/node_modules/epoll/build'
86
+
87
+ CXX(target) Release/obj.target/epoll/src/epoll.o
88
+
89
+ SOLINK_MODULE(target) Release/obj.target/epoll.node
90
+
91
+ COPY Release/epoll.node
92
+
93
+ make: Leaving directory '/home/pi/node_modules/rpi-gpio/node_modules/epoll/build'
94
+
95
+ rpi-gpio@1.0.0 node_modules/rpi-gpio
96
+
97
+ ├── async@1.5.2
98
+
99
+ ├── debug@2.6.9 (ms@2.0.0)
100
+
101
+ ├── promise@7.1.1 (asap@2.0.6)
102
+
103
+ └── epoll@0.1.22 (bindings@1.2.1, nan@2.6.2)
104
+
105
+ pi@raspberrypi:~ $
106
+
107
+ pi@raspberrypi:~ $ cat example.js # サンプルコード
108
+
109
+ var gpio = require('rpi-gpio');
110
+
111
+
112
+
113
+ gpio.setup(7, gpio.DIR_IN, readInput);
114
+
115
+
116
+
117
+ function readInput(err) {
118
+
119
+ if (err) throw err;
120
+
121
+ gpio.read(7, function(err, value) {
122
+
123
+ if (err) throw err;
124
+
125
+ console.log('The value is ' + value);
126
+
127
+ });
128
+
129
+ }
130
+
131
+ pi@raspberrypi:~ $ node example.js
132
+
133
+ The value is true
134
+
135
+ ^C
136
+
137
+ pi@raspberrypi:~ $
138
+
139
+ ```