2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

夏休みの工作特別企画 壁のスイッチをwifiにする

Last updated at Posted at 2019-08-01

夏休みの工作特別企画 壁のスイッチをwifiにする

この季節になると引退後でも、夏休みの工作がしたくなる。また、「青春18きっぷ」、これも気になる。年齢にかかわらず、利用できる、老人でも使える学割である。

今回の記事は、「トラ技(とらわざ)的自作です。」私は、40年ぐらい前トランジスタ技術という雑誌のことを「虎わざ」と命名した。

#さて
#イメージは、下記の通りです。
###wifiに接続されているスマートフォンまたは、PCのブラウザーからコマンドを送ると壁のスイッチをonする。
image.png

材料
image.png

image.png

フラッシュメモリ書き込み用に
image.png
今回の記事に使う参考資料は、下記の通りです。
image.png

$ git clone https://github.com/IOT-MCU/ESP-01S-Relay-v1.0.git

資料によると
image.png

赤枠のGPIO0にリレーがつながっていることがわかる。この信号をLowにするとリレーがonになり部屋の電灯がつくようにする。適当でも大丈夫JP2で1-2につなぐか2-3につなぐかだから大丈夫。
jp2を壁のスイッチに接続すればOK!
image.png
感電するといけないから使い捨ての手術用手袋を忘れないで!
これでwifiでも手動でも使えるようになった。

こここで用いられるESP01は、Espressif Systemsによって製造されているTCP/IPスタックとマイクロコントローラを備えたWi-Fiモジュールです。
アメリカのドラマによくでるスマートフォンから爆弾起爆する部品でする。
フラッシュメモリーにプログラムを書いて使用します。
老人ボケでc言語めんどくさいのでluaを勉強して使います。

この製品は、中国製なのでまず受入検査します。
usb ideモジュールにESP01を差し、PCに接続します。
image.png
設定をします。
image.png
Rlogin等の端末で確認します。
image.png

AT
OK
AT+CWJAP="SSIDを入れる","パスワードを入れる"
WIFI CONNECTED
WIFI GOT IP
OK
AT+CIFSR
+CIFSR:STAIP,"192.168.1.13"
+CIFSR:STAMAC,"84:0d:8e:a7:d8:3a"
OK

AT+CIFSRコマンド入れてIPが出力されればOK。

AT+CIFSR
+CIFSR:STAIP,"192.168.1.13"
+CIFSR:STAMAC,"84:0d:8e:a7:d8:3a"

Rloginを閉じてESP8266Flasher.exeを起動します。
ESP8266Flasher.exeは、git clone https://github.com/nodemcu/nodemcu-flasher.git ダウンロードします。
image.png
image.png

私は、nodemcu_integer_0.9.6-dev_20150704.binを使っています。

次にLuaLoader.exeを起動します。
LuaLoader.exeは、ESP-01S-Relay-v1.0にあります。
image.png
下部のテキストボックスにprint('hello world')を入力します。

> print('hello world')
hello world

と出力されます。きちんと入りました。
下記のプログラムをアップロードします。
image.png
image.png

init.lua
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
gpio.write(led1, gpio.HIGH)
gpio.write(led2, gpio.HIGH)
print("Ready to start STATION")
wifi.setmode(wifi.STATION)
wifi.sta.config("ssid","password")
collectgarbage();
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        buf = buf.."HTTP/1.1 200 OK\n\n"
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
		print(vars);
        if (vars == 'sw=on')then
			gpio.write(led1, gpio.LOW);
			print("led1 on")
		 elseif(vars=='sw=off')then
		    gpio.write(led1, gpio.HIGH);
			print("led1 off");
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)
> = wifi.sta.getip()
192.168.1.13	255.255.255.0	192.168.1.1
> 

wifiに参加できていればOKです。
ping して確認します。

(base) C:\Users\hirat\esp01>ping 192.168.1.13
192.168.1.13 に ping を送信しています 32 バイトのデータ:
192.168.1.13 からの応答: バイト数 =32 時間 =28ms TTL=255
192.168.1.13 からの応答: バイト数 =32 時間 =50ms TTL=255
192.168.1.13 からの応答: バイト数 =32 時間 =3ms TTL=255
192.168.1.13 からの応答: バイト数 =32 時間 =3ms TTL=255
192.168.1.13 の ping 統計:
    パケット数: 送信 = 4、受信 = 4、損失 = 0 (0% の損失)、
ラウンド トリップの概算時間 (ミリ秒):
    最小 = 3ms、最大 = 50ms、平均 = 21ms

image.png
USBから取り外し配線します。
テストプログラムを実行します。

test.py
import time,requests
r = requests.post('http://192.168.1.13/?sw=off')
for i in range(5):
    r = requests.post('http://192.168.1.13/?sw=on')
    print(i,r,"On")
    time.sleep(2)
    r = requests.post('http://192.168.1.13/?sw=off')
    print(i,r,"Off")
    time.sleep(2)
result.txt
C:\Users\hirat\source\repos\py\000tests>test.py
0 <Response [200]> On
0 <Response [200]> Off
1 <Response [200]> On
1 <Response [200]> Off
2 <Response [200]> On
2 <Response [200]> Off
3 <Response [200]> On
3 <Response [200]> Off
4 <Response [200]> On
4 <Response [200]> Off

電源は、100円均一で入手した、充電器とケーブルを改造した。
image.png
Enjoy

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?