Samples_WLAN2 - gfd-dennou-club/mrubyc-esp32 GitHub Wiki

WiFi 注意事項

起動時に AP に接続する必要があることに注意. プログラム開始時に AP に接続していれば,WiFi の電波が切れても自動的に再接続する.

サンプル1

  • 起動時に AP に接続できないと,それ以降のコードは実行されない.
    • マルチタスクなプログラムを書いてもその状況は変わらない.
wlan = WLAN.new
wlan.connect("SSID", "PASS")

puts "Hello"   # <-- WiFi につながらないと,この行は実行されない

サンプル2

  • 一度 AP に接続していれば,AP の電波が届かない状態になってもしないと,プログラムが実行され続ける.
  • 再び AP の電波が届くようになれば自動的に AP に再接続する.

master.rb の例

num = 0      # 変数の初期化
loop do
  puts num   # ループ回数を表示
  num += 1   # 変数の値を更新
  sleep 1
end

slave.rb の例

wlan = WLAN.new
wlan.connect("SSID", "PASS")

puts "ifconfig: #{wlan.ifconfig}"
puts "mac: #{wlan.mac}"
puts "ip: #{wlan.ip}"

loop do
  if ( wlan.connected? ) #通信が確立していたら
    puts "Connection OK"
    # break
  end
  sleep 1
end

出力の例 (make menuconfig でログを出力するよう設定変更済み)

I (2916) wifi_init: tcp mss: 1440
I (2916) wifi_init: WiFi IRAM OP enabled
I (2916) wifi_init: WiFi RX IRAM OP enabled
I (3026) WiFi: WiFi setting up : WPA2 Personal
W (3026) wifi:Password length matches WPA2 standards, authmode threshold changes from OPEN to WPA2
I (3026) WiFi: WiFi started.
I (3026) phy_init: phy_version 4860,6b7a6e5,Feb  6 2025,14:47:07
I (3096) wifi:mode : sta (78:e3:6d:f0:d8:5c)
I (3096) wifi:enable tsf
I (4486) wifi:new:<11,2>, old:<1,0>, ap:<255,255>, sta:<11,2>, prof:1, snd_ch_cfg:0x0
I (4486) wifi:state: init -> auth (0xb0)
I (5486) wifi:state: auth -> init (0x200)
I (5496) wifi:new:<11,0>, old:<11,2>, ap:<255,255>, sta:<11,2>, prof:1, snd_ch_cfg:0x0
I (5496) WiFi: retry to connect to the AP
I (5496) WiFi: connect to the AP fail
I (7916) WiFi: retry to connect to the AP
I (7916) WiFi: connect to the AP fail
I (7916) wifi:new:<11,2>, old:<11,0>, ap:<255,255>, sta:<11,2>, prof:1, snd_ch_cfg:0x0
I (7926) wifi:state: init -> auth (0xb0)
I (7936) wifi:state: auth -> assoc (0x0)
I (7946) wifi:state: assoc -> run (0x10)
I (7956) wifi:connected with SugiyamaLab, aid = 1, channel 11, 40D, bssid = c0:83:c9:90:3a:54
I (7956) wifi:security: WPA2-PSK, phy: bgn, rssi: -32
I (7966) wifi:pm start, type: 1

I (7966) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us
I (7996) wifi:AP's beacon interval = 102400 us, DTIM period = 1
I (9016) esp_netif_handlers: sta ip: 192.168.3.75, mask: 255.255.255.0, gw: 192.168.3.1
I (9016) WiFi: got ip:192.168.3.75
1
ifconfig: {"ip"=>"192.168.3.75", "netmask"=>"255.255.255.0", "gw"=>"192.168.3.1", "dns"=>"192.168.3.1"}
mac: 78:E3:6D:F0:D8:5C
ip: 192.168.3.75
Connection OK
2
Connection OK
Connection OK
3
4
Connection OK
Connection OK
5
6
Connection OK
Connection OK
7
8
....(略).....
                                                                             <---- ****ここで AP の電源を切る****
....(略).....
Connection OK
Connection OK
57
58
Connection OK
I (68066) wifi:bcn_timeout,ap_probe_send_start
Connection OK
59
60
Connection OK
61
Connection OK
I (70566) wifi:ap_probe_send over, reset wifi status to disassoc
I (70566) wifi:state: run -> init (0xc800)
I (70576) wifi:pm stop, total sleep time: 50228241 us / 62606602 us

I (70576) wifi:new:<11,0>, old:<11,2>, ap:<255,255>, sta:<11,2>, prof:1, snd_ch_cfg:0x0
I (70586) WiFi: retry to connect to the AP
I (70586) WiFi: connect to the AP fail
62
63                                                                           <-- WiFi が切れてもループは回っている.
I (73006) WiFi: retry to connect to the AP
I (73006) WiFi: connect to the AP fail
64
65
66
I (75416) WiFi: retry to connect to the AP
I (75416) WiFi: connect to the AP fail
67
68
I (77826) WiFi: retry to connect to the AP
I (77836) WiFi: connect to the AP fail
69
....(略).....
                                                                             <---- ****ここで AP の電源を入れる****
....(略).....
110
111
I (121296) WiFi: retry to connect to the AP
I (121296) WiFi: connect to the AP fail
112
113
I (123706) WiFi: retry to connect to the AP
I (123706) WiFi: connect to the AP fail
114
115
116
I (126126) WiFi: retry to connect to the AP
I (126126) WiFi: connect to the AP fail
I (126246) wifi:new:<11,2>, old:<11,0>, ap:<255,255>, sta:<11,2>, prof:1, snd_ch_cfg:0x0
I (126246) wifi:state: init -> auth (0xb0)
I (126246) wifi:state: auth -> assoc (0x0)
I (126256) wifi:state: assoc -> run (0x10)
I (126296) wifi:connected with SugiyamaLab, aid = 1, channel 11, 40D, bssid = c0:83:c9:90:3a:54
I (126296) wifi:security: WPA2-PSK, phy: bgn, rssi: -36
I (126306) wifi:pm start, type: 1

I (126346) wifi:AP's beacon interval = 102400 us, DTIM period = 1
117
I (127446) esp_netif_handlers: sta ip: 192.168.3.75, mask: 255.255.255.0, gw: 192.168.3.1
I (127446) WiFi: got ip:192.168.3.75
118
Connection OK                                                                   <-- WiFi に再接続したことが分かる
Connection OK
119
120
Connection OK
Connection OK
121
122
Connection OK
Connection OK
123
124
Connection OK
Connection OK
125
126
Connection OK
Connection OK
127
128
⚠️ **GitHub.com Fallback** ⚠️