Help us understand the problem. What is going on with this article?

100均の扉センサーとRaspberryPi3で、HUE扉センサーをつくる

More than 3 years have passed since last update.

準備するもの

IMG_8853.jpg

  • RaspberryPi3
  • ブレッドボード
  • 抵抗(1KΩ)2つ
  • LED
  • ジャンパー線 3本

RaspberryPi3 ピン配列

https://matome.naver.jp/odai/2146277598114754901

配線

下記を参考にしました。
https://tool-lab.com/make/raspberrypi-startup-22/

今回行った配線は下記の通りです。

Rpi_doorsensor.png

RaspberryPiでのGPIO値の読み方

下記を参考にしました。
https://tool-lab.com/2013/12/raspi-gpio-controlling-command-1/
https://tool-lab.com/2013/12/raspi-gpio-controlling-command-2/

gpio readallして、gpioとwinringpi gemで指定する値の対応を確認します。
RPi3のGPIO26なので、それに対応するwPi25を使用します。
(最初よくわかりませんでしたが試行錯誤の結果、25を使うことにします)

$ gpio readall
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 0 | IN   | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | OUT  | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+

ruby gem

gemとしてwiringpiを利用しました。

コード

複数のbridge, HUE idから、bridgeおよびHUE idを指定しています。

#!/usr/bin/ruby

require 'wiringpi'
require "faraday"

# HUE Color code
# https://developers.meethue.com/documentation/core-concepts

# to check wPi_num and GPIO, execute 'gpio readall'
GPIO26 = 25

# init
bridge = 3
id = 3

# for API
bridge_ip = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
api_key   = ["hogehoge1", "hogehoge2", "hogehoge3"]

def api_put(bridge_ip, api_key, id, key, value)
  res = Faraday.put "http://" + bridge_ip + "/api/" + api_key + "/lights/" + id.to_s + "/state", "{\"hue\": #{value.to_s}}"
end


io = WiringPi::GPIO.new
io.pin_mode(GPIO26, WiringPi::INPUT )

loop do
  state = io.digital_read(GPIO26)

  if state == 0
    api_put(bridge_ip[bridge - 1], api_key[bridge - 1], id, "hue", 25500) # Green
  elsif state == 1
    api_put(bridge_ip[bridge - 1], api_key[bridge - 1], id, "hue", 65280) # Red
  else
    api_put(bridge_ip[bridge - 1], api_key[bridge - 1], id, "hue", 46920) # Blue
  end

  sleep(1)
end

その他参考情報

http://yoheikoga.github.io/2016/04/15/Ruby%E3%81%A7WiringPi%E3%82%92%E4%BD%BF%E3%81%86/
http://shokai.org/blog/archives/8460
http://pi.gadgetoid.com/article/wiringpi-as-a-ruby-gem
https://www.homegear.eu/index.php/Philips_hue_Color_Light_Reference

snumano
少年野球・草野球のスコアを家族や知人で共有できるサービス「BaseballScore」を運営しています。 https://baseballscore.herokuapp.com/
http://nushu123.blogspot.jp/
idcf
未来をささえる、Your Innovative Partner
http://www.idcf.jp/cloud/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした