LoginSignup
3
0

More than 3 years have passed since last update.

自作IoT機器を作る

Posted at

自作IoT機器を作る

image.png

Raspberry PIからwifi経由でリレーのスウィッチをOn Off させる。

image.png

どうやって家のwifiに参加させるか?

モード1:このボードがアクセスポイントになり周辺機器にIP-addressを配る。
緑色のLEDが点滅してから4秒後に点滅が2秒になると設定が完了する。
192.168.4.1 、ポート番号8080 設定される。
モード2:ルータに参加しDHCPからIP-Addressをもらう。
下記のappがsmartConfigを行ってくれる。
家庭内ネットワークに参加しているiPhoneなどを用いてSmartConfigを実施する。
image.png

image.png

最初の一歩(Getting Start)

>arp -a
インターフェイス: 192.168.1.10 --- 0x13
  インターネット アドレス 物理アドレス           種類
  192.168.1.1           58-52-8a-3f-8a-21     動的
  192.168.1.90          b0-e8-92-10-3c-f6     動的
  192.168.1.104         e4-e4-ab-58-7e-36     動的
  192.168.1.111         b8-27-eb-43-fa-bf     動的
  192.168.1.150         78-0f-77-1a-31-a0     動的
  192.168.1.151         78-0f-77-17-5d-70     動的
  192.168.1.152         84-0d-8e-a7-d8-3a     動的
  192.168.1.255         ff-ff-ff-ff-ff-ff     静的
  224.0.0.22            01-00-5e-00-00-16     静的
  224.0.0.251           01-00-5e-00-00-fb     静的
  224.0.0.252           01-00-5e-00-00-fc     静的
  239.255.255.250       01-00-5e-7f-ff-fa     静的
  255.255.255.255       ff-ff-ff-ff-ff-ff     静的

無事192.168.1.152に接続できた。

wifiリレーに送信するコマンド

コマンド value(値)
Turn OFF the first relay A0 01 00 A1
Turn ON the first relay A0 01 01 A2
Turn OFF the second relay A0 02 00 A2
Turn ON the second relay A0 02 01 A3
Turn OFF the third relay A0 03 00 A3
Turn ON the third relay A0 03 01 A4
Turn OFF the fourth relay A0 04 00 A4
Turn ON the fourth relay A0 04 01 A5

デバック用サンプル

send.py
import sys,binascii,time,socket
HOST, PORT = "192.168.1.152", 8080
cmd=[('A00100A1','A00101A2'),('A00200A2','A00201A3'),('A00300A3','A00301A4'), ('A00400A4','A00401A5')]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
send=lambda x: sock.send(bytearray.fromhex(x))
for i,x in enumerate(cmd):
    print("Channel %d on"%i)
    send(x[1]) #コマンド 送信 on
    time.sleep(1)
    print("Channel %d off"%i)
    send(x[0]) #コマンド 送信 off
    time.sleep(1)
sock.close()

リレーに魔法瓶やコーヒーメーカを接続するとwifi経由でon/offできる。

結果

>send.py
Channel 0 on
Channel 0 off
Channel 1 on
Channel 1 off
Channel 2 on
Channel 2 off
Channel 3 on
Channel 3 off
3
0
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
3
0