LoginSignup
7
8

More than 5 years have passed since last update.

RaspberryPi:Pi_PiperでPermission Errorが起きる

Last updated at Posted at 2015-09-17

最近のRaspbianでは、GPIOに出力するときにsudoが必要なくなりました。

例えば、GPIO17で出力をするとき。

echo 17 >/sys/class/gpio/export
echo out >/sys/class/gpio/gpio17/direction
echo 1 > /sys/class/gpio/gpio17/value

こんな感じで、上記のコマンドを一行づつシェルで実行すると、以前のようにsudoをつけなくてもGPIOの読み書きができるはずです。

Pi_Piperを使うがエラー

ここで、RubyのPi_Piperを使ってGPIOを出力してみます。

test.rb
require 'pi_piper'

pin = PiPiper::Pin.new(:pin => 17, :direction => :out)
pin.on
sleep 1
pin.off

これをsudoなしで実行してみます。

しかし、Pin.newの時点で

2.2.0/gems/pi_piper-1.3.2/lib/pi_piper/pin.rb:24:in 
initialize': Permission denied @ rb_sysopen - /sys/class/gpio/gpio17/direction (Errno::EACCES)

と怒られてしまいます。

しかし、このときの/sys/class/gpio/gpio17/ls -laしてみると、

drwxrwx--- 3 root gpio    0  9月 17 14:56 .
drwxrwx--- 5 root gpio    0  9月 17 14:56 ..
-rwxrwx--- 1 root gpio 4096  9月 17 14:56 active_low
-rwxrwx--- 1 root gpio 4096  9月 17 14:56 direction
-rwxrwx--- 1 root gpio 4096  9月 17 14:56 edge
drwxrwx--- 2 root gpio    0  9月 17 14:56 power
lrwxrwxrwx 1 root gpio    0  9月 17 14:56 subsystem -> ../../../../class/gpio
-rwxrwx--- 1 root gpio 4096  9月 17 14:56 uevent
-rwxrwx--- 1 root gpio 4096  9月 17 14:56 value

となり、それぞれのペリフェラルファイルはgpioグループになっており、権限もグループにたいしてrwxである事が確認できます。(ユーザー:piもgpioグループが付与されています)

Pi_Piperのpin.rbをみてみると、23,24行目で

File.open("/sys/class/gpio/export", "w") { |f| f.write("#{@pin}") }
File.open(direction_file, "w") { |f| f.write(@direction == :out ? "out" : "in") }

となっています。

どうやら、RaspbianでGPIOをexportしたときに、/sys/class/gpio/gpioXX/以下のファイルは、一旦rootグループとなり、そのあとgpioになるようで、今回の問題は、gpioグループになる前にdirectionファイルに書き込もうとするので、Permission Errorになるようです。

結論

pin.rbを編集し、

File.open("/sys/class/gpio/export", "w") { |f| f.write("#{@pin}") }

sleep 0.1

File.open(direction_file, "w") { |f| f.write(@direction == :out ? "out" : "in") }

と、sleepを入れることで解決。

Versions:

Pi_Piper (1.3.2) https://github.com/jwhitehorn/>pi_piper
ruby 2.2.3p173 (2015-08-18 revision 51636) [armv7l-linux-eabihf]
RASPBIAN Debian Wheezy 2015-05-05(Linux kernel 3.18)

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8