LoginSignup
13
13

More than 5 years have passed since last update.

Arduino x Ruby でSwitchを使う&ネットに状態を流す

Posted at

Switchを使ってみたい

結線の参照先
Switches and Arduino

結線はこんな感じ

ソースはDinoのサンプルから拝借

03_switch.rb
require "dino"

board = Dino::Board.new(Dino::TxRx.new)
button = Dino::Components::Button.new(pin: 2, board: board)

button.down do
  puts "button down"
end

button.up do
  puts "button up"
end

sleep

サンプルソースに付属の画像では、結線はこうなっている。
サンプルの方の結線

回路のことはよく解らないのだが、抵抗ってどんな役割をしているのだろうか。

Switchの状態をネットの向こうへ届けよう

面白いサンプルを発見した。
austinbv / dino_rails_example

  • サーバに接続されているArduinoのLEDをブラウザから光らせる
  • サーバのArduinoのスイッチを押すと、接続しているブラウザの背景が赤になる

という動きをするサンプルです。

最初の「LEDを光らせる」については、特定のURLにアクセスがあった場合、Arduinoへ信号を送ってLED光らせたり、消したりします。

もう一つの方についてはPusherというサービスを使って、サーバ側に接続されているArduinoのスイッチの状態をリアルタイムに配信するというものです。

スイッチの状態が変化したらサーバからPusherへ通知が行われ、Pusherから接続しているクライアントへサーバの状態が配信されるというもの。

Pusherの準備

dino_rails_exampleの準備

  • gitでソースをクローンする
/Users/yoshidayuuya/develop/dino% git clone https://github.com/austinbv/dino_rails_example.git
Cloning into 'dino_rails_example'...
remote: Counting objects: 41, done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 41 (delta 0), reused 41 (delta 0)
Unpacking objects: 100% (41/41), done.
  • 必要なところを修正する
    • GemfilesでRailsの最新バージョンで作成したプロジェクトのGemfileを参考に修正
13
13
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
13
13