Last active
April 4, 2017 00:28
-
-
Save na0AaooQ/bd60448cdac9a2930ae2d5e1947814f5 to your computer and use it in GitHub Desktop.
Raspberry Pi 3 と DSUN-PIR を使って人感センサーを作る + 人感センサー検知状況を表示する簡易Webアプリケーションを作る ref: http://qiita.com/na0AaooQ/items/a73e1fc944f818fd104d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'sinatra/reloader' | |
require 'json' | |
PIR_STATUS_FILE = "/tmp/example-pir-status.log" | |
PIR_STATUS_FILE.freeze | |
get '/' do | |
if File.exist?(PIR_STATUS_FILE) | |
status = File.read(PIR_STATUS_FILE, :encoding => Encoding::UTF_8) | |
status = status.chomp | |
if status == "1" then | |
room_status = "[リビングに人がいます] " + status + "" | |
else | |
room_status = "[リビングに人はいません] " + status + "" | |
end | |
else | |
room_status = "[リビングの人感センサー検知状況を表示できません]" | |
end | |
room_status | |
end | |
post '/put' do | |
body = request.body.read | |
File.open(PIR_STATUS_FILE, "w") do |f| | |
f.puts(body); | |
end | |
if body == '' | |
status 400 | |
else | |
body.to_json | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# wpa_passphrase 無線LANのSSID SSIDのパスフレーズ >> /etc/wpa_supplicant/wpa_supplicant.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# cat /etc/wpa_supplicant/wpa_supplicant.conf | |
country=GB | |
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev | |
update_config=1 | |
network={ | |
ssid="SSID" | |
psk=パスフレーズが暗号化されて設定される | |
} | |
root@raspberrypi:~# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# cat /sys/class/gpio/gpio18/value | |
0 | |
root@raspberrypi:~# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MacBookProPC001:~ user$ ssh -i EC2インスタンスログイン用のSSH秘密鍵 ec2-user@198.51.100.20 | |
Last login: Sun Apr 2 23:29:03 2017 from i121-115-95-232.s42.a013.ap.plala.or.jp | |
__| __|_ ) | |
_| ( / Amazon Linux AMI | |
___|\___|___| | |
https://aws.amazon.com/amazon-linux-ami/2016.09-release-notes/ | |
[ec2-user@example-ruby-sinatra-server ~]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@example-ruby-sinatra-server ~]$ sudo su - | |
最終ログイン: 2017/04/02 (日) 23:30:13 JST日時 pts/0 | |
[root@example-ruby-sinatra-server ~]# groupadd sinatra | |
[root@example-ruby-sinatra-server ~]# useradd sinatra -g sinatra -d /home/sinatra -s /bin/bash | |
[root@example-ruby-sinatra-server ~]# | |
[root@example-ruby-sinatra-server ~]# su - sinatra | |
[sinatra@example-ruby-sinatra-server ~]$ | |
[sinatra@example-ruby-sinatra-server ~]$ id | |
uid=501(sinatra) gid=501(sinatra) groups=501(sinatra) | |
[sinatra@example-ruby-sinatra-server ~]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server ~]$ /sbin/ifconfig eth0 | grep inet | |
inet addr:198.51.100.20 Bcast:198.51.100.255 Mask:255.255.255.0 | |
inet6 addr: XXXX::XXX:XXXX:XXXX:XXXX/64 Scope:Link | |
[sinatra@example-ruby-sinatra-server ~]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[root@example-ruby-sinatra-server ~]# echo "sinatra ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/sinatra | |
[root@example-ruby-sinatra-server ~]# chown root:root /etc/sudoers.d/sinatra | |
[root@example-ruby-sinatra-server ~]# chmod 644 /etc/sudoers.d/sinatra | |
[root@example-ruby-sinatra-server ~]# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server ~]$ ruby -v | |
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux] | |
[sinatra@example-ruby-sinatra-server ~]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server ~]$ ls -lrta /home/sinatra/ | |
total 24 | |
-rw-r--r-- 1 sinatra sinatra 124 Aug 16 2016 .bashrc | |
-rw-r--r-- 1 sinatra sinatra 193 Aug 16 2016 .bash_profile | |
-rw-r--r-- 1 sinatra sinatra 18 Aug 16 2016 .bash_logout | |
drwxr-xr-x 4 root root 4096 Apr 2 23:31 .. | |
-rw------- 1 sinatra sinatra 8 Apr 2 23:31 .bash_history | |
drwx------ 2 sinatra sinatra 4096 Apr 2 23:31 . | |
[sinatra@example-ruby-sinatra-server ~]$ | |
[sinatra@example-ruby-sinatra-server ~]$ mkdir /home/sinatra/example-pir-api | |
[sinatra@example-ruby-sinatra-server ~]$ | |
[sinatra@example-ruby-sinatra-server ~]$ cd /home/sinatra/example-pir-api/ | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ pwd | |
/home/sinatra/example-pir-api | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server example-pir-api]$ ls -lrta /home/sinatra/example-pir-api/ | |
total 8 | |
drwx------ 3 sinatra sinatra 4096 Apr 2 23:37 .. | |
drwxrwxr-x 2 sinatra sinatra 4096 Apr 2 23:37 . | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ bundle init | |
Writing new Gemfile to /home/sinatra/example-pir-api/Gemfile | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ ls -lrta /home/sinatra/example-pir-api/Gemfile | |
-rw-r--r-- 1 sinatra sinatra 75 Apr 2 23:37 /home/sinatra/example-pir-api/Gemfile | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ echo "gem 'sinatra'" >> /home/sinatra/example-pir-api/Gemfile | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ echo "gem 'sinatra-contrib'" >> /home/sinatra/example-pir-api/Gemfile | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ echo "gem 'json'" >> /home/sinatra/example-pir-api/Gemfile | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server example-pir-api]$ cat /home/sinatra/example-pir-api/Gemfile | |
# frozen_string_literal: true | |
source "https://rubygems.org" | |
# gem "rails" | |
gem 'sinatra' | |
gem 'sinatra-contrib' | |
gem 'json' | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server example-pir-api]$ pwd | |
/home/sinatra/example-pir-api | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ ls -lrta /home/sinatra/example-pir-api/ | |
total 12 | |
drwx------ 3 sinatra sinatra 4096 Apr 2 23:37 .. | |
drwxrwxr-x 2 sinatra sinatra 4096 Apr 2 23:37 . | |
-rw-r--r-- 1 sinatra sinatra 122 Apr 2 23:39 Gemfile | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ bundle install --path vendor/bundle | |
Fetching gem metadata from https://rubygems.org/.......... | |
Fetching version metadata from https://rubygems.org/. | |
Resolving dependencies... | |
Installing backports 3.7.0 | |
Installing json 2.0.3 with native extensions | |
Installing multi_json 1.12.1 | |
Installing rack 1.6.5 | |
Installing tilt 2.0.7 | |
Using bundler 1.13.2 | |
Installing rack-protection 1.5.3 | |
Installing rack-test 0.6.3 | |
Installing sinatra 1.4.8 | |
Installing sinatra-contrib 1.4.7 | |
Bundle complete! 3 Gemfile dependencies, 10 gems now installed. | |
Bundled gems are installed into ./vendor/bundle. | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MacBookProPC001:~ user$ ssh pi@198.51.100.10 | |
pi@198.51.100.10's password: -> piユーザの初期パスワード raspberry を入力します。 | |
The programs included with the Debian GNU/Linux system are free software; | |
the exact distribution terms for each program are described in the | |
individual files in /usr/share/doc/*/copyright. | |
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent | |
permitted by applicable law. | |
Last login: Tue Mar 28 18:05:14 2017 | |
pi@raspberrypi:~ $ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server example-pir-api]$ ls -lrta /home/sinatra/example-pir-api/ | |
total 24 | |
-rw-r--r-- 1 sinatra sinatra 122 Apr 2 23:39 Gemfile | |
drwxrwxr-x 2 sinatra sinatra 4096 Apr 2 23:39 .bundle | |
drwxrwxr-x 3 sinatra sinatra 4096 Apr 2 23:39 vendor | |
drwx------ 4 sinatra sinatra 4096 Apr 2 23:39 .. | |
-rw-rw-r-- 1 sinatra sinatra 568 Apr 2 23:39 Gemfile.lock | |
drwxrwxr-x 4 sinatra sinatra 4096 Apr 2 23:39 . | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server ~]$ vi /home/sinatra/example-pir-api/example-pir-api.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server example-pir-api]$ sudo /usr/local/rbenv/shims/bundle exec ruby /home/sinatra/example-pir-api/example-pir-api.rb -o 0.0.0.0 -p 80 & | |
[1] 4965 | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ [2017-04-03 00:28:03] INFO WEBrick 1.3.1 | |
[2017-04-03 00:28:03] INFO ruby 2.3.1 (2016-04-26) [x86_64-linux] | |
== Sinatra (v1.4.8) has taken the stage on 80 for development with backup from WEBrick | |
[2017-04-03 00:28:03] INFO WEBrick::HTTPServer#start: pid=4966 port=80 | |
-> Enterキーを押します。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sinatra@example-ruby-sinatra-server example-pir-api]$ ps awux | grep -v grep | grep ruby | |
root 4965 0.0 0.4 186148 4452 pts/2 S 00:28 0:00 sudo /usr/local/rbenv/shims/bundle exec ruby /home/sinatra/example-pir-api/example-pir-api.rb -o 0.0.0.0 -p 80 | |
root 4966 1.7 2.7 360380 28236 pts/2 Sl 00:28 0:00 ruby /home/sinatra/example-pir-api/example-pir-api.rb -o 0.0.0.0 -p 80 | |
[sinatra@example-ruby-sinatra-server example-pir-api]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# vi /root/scripts/put_gpio_status.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# chmod 755 /root/scripts/put_gpio_status.sh | |
root@raspberrypi:~# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# /root/scripts/put_gpio_status.sh & | |
[1] 1833 | |
root@raspberrypi:~# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pi@raspberrypi:~ $ sudo su - | |
root@raspberrypi:~# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# mkdir /root/scripts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# chmod 755 /root/scripts/start_gpio.sh | |
root@raspberrypi:~# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# /root/scripts/start_gpio.sh | |
total 0 | |
drwxr-xr-x 50 root root 0 Jan 1 1970 .. | |
-rwxrwx--- 1 root gpio 4096 Mar 28 18:05 unexport | |
lrwxrwxrwx 1 root gpio 0 Mar 28 18:05 gpiochip100 -> ../../devices/platform/soc/soc:virtgpio/gpio/gpiochip100 | |
lrwxrwxrwx 1 root gpio 0 Mar 28 18:05 gpiochip0 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpiochip0 | |
-rwxrwx--- 1 root gpio 4096 Apr 2 13:56 export | |
drwxrwx--- 2 root gpio 0 Apr 2 13:56 . | |
lrwxrwxrwx 1 root root 0 Apr 2 13:56 gpio18 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio18 | |
root@raspberrypi:~# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# cat /sys/class/gpio/gpio18/value | |
0 | |
root@raspberrypi:~# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# cat /sys/class/gpio/gpio18/value | |
1 | |
root@raspberrypi:~# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
GPIO="18" | |
GPIO_VALUE="/sys/class/gpio/gpio$GPIO/value" | |
API="https://198.51.100.20/put" | |
SLEEP="2" | |
if [ ! -f $GPIO_VALUE ] ; then | |
echo "$GPIO_VALUE not found." | |
exit | |
fi | |
while : | |
do | |
curl -H "Accept: text/plain" -X POST -d `cat $GPIO_VALUE` $API | |
sleep $SLEEP | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@raspberrypi:~# vi /root/scripts/start_gpio.sh | |
#!/bin/sh | |
GPIO="18" | |
echo $GPIO > /sys/class/gpio/export | |
ls -lrta /sys/class/gpio/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment