ESP-IDFを使ってみる

esp32_nat_router


こちら
にESP-IDFを使ったRouterアプリが公開されています。
Console Componentを使って、コマンドラインで設定を行うことができます。
ファームを書き込むと、シリアルポートに以下が表示されます。
ESP32 NAT ROUTER
Type 'help' to get the list of commands.
Use UP/DOWN arrows to navigate through command history.
Press TAB when typing command name to auto-complete.

Unconfigured WiFi
Configure using 'set_sta' and 'set_ap' and restart.

showコマンドで現在の設定を確認することができます。
esp32> show
STA SSID: <undef> Password: <undef>
AP SSID: <undef> Password: <undef>
Uplink AP not connected
0 Stations connected

helpコマンドで使用できるコマンドの一覧を表示します。
esp32> help
help > help
  Print the list of registered commands

free
  Get the current size of free heap memory

heap
  Get minimum size of free heap memory that was available during program execu
  tion

version
  Get version of chip and SDK

restart
  Software reset of the chip

deep_sleep  [-t <t>] [--io=<n>] [--io_level=<0|1>]
  Enter deep sleep mode. Two wakeup modes are supported: timer and GPIO. If no
  wakeup option is specified, will sleep indefinitely.
  -t, --time=<t>  Wake up time, ms
      --io=<n>  If specified, wakeup using GPIO with given number
  --io_level=<0|1>  GPIO level to trigger wakeup

light_sleep  [-t <t>] [--io=<n>]... [--io_level=<0|1>]...
  Enter light sleep mode. Two wakeup modes are supported: timer and GPIO. Mult
  iple GPIO pins can be specified using pairs of 'io' and 'io_level' arguments
  . Will also wake up on UART input.
  -t, --time=<t>  Wake up time, ms
      --io=<n>  If specified, wakeup using GPIO with given number
  --io_level=<0|1>  GPIO level to trigger wakeup

tasks
  Get information about running tasks

nvs_set  <key> <type> -v <value>
  Set key-value pair in selected namespace.
Examples:
 nvs_set VarName i32 -v
  123
 nvs_set VarName str -v YourString
 nvs_set VarName blob -v 0123456789
  abcdef

         <key>  key of the value to be set
        <type>  type can be: i8, u8, i16, u16 i32, u32 i64, u64, str, blob
  -v, --value=<value>  value to be stored

nvs_get  <key> <type>
  Get key-value pair from selected namespace.
Example: nvs_get VarName i32
         <key>  key of the value to be read
        <type>  type can be: i8, u8, i16, u16 i32, u32 i64, u64, str, blob

nvs_erase  <key>
  Erase key-value pair from current namespace
         <key>  key of the value to be erased

nvs_namespace  <namespace>
  Set current namespace
   <namespace>  namespace of the partition to be selected

nvs_list  <partition> [-n <namespace>] [-t <type>]
  List stored key-value pairs stored in NVS.Namespace and type can be specifie
  d to print only those key-value pairs.
Following command list variables stor
  ed inside 'nvs' partition, under namespace 'storage' with type uint32_tExamp
  le: nvs_list nvs -n storage -t u32

   <partition>  partition name
  -n, --namespace=<namespace>  namespace name
  -t, --type=<type>  type can be: i8, u8, i16, u16 i32, u32 i64, u64, str, blob

nvs_erase_namespace  <namespace>
  Erases specified namespace
   <namespace>  namespace to be erased

set_sta  <ssid> <passwd>
  Set SSID and password of the STA interface
        <ssid>  SSID
      <passwd>  Password

set_ap  <ssid> <passwd>
  Set SSID and password of the SoftAP
        <ssid>  SSID of AP
      <passwd>  Password of AP

show
  Get status and config of the router




最初に以下のコマンドでAPのSSIDとパスワードを設定します。
パスワードが8文字未満の時はオープン(パスワード無し)ルーターの設定となります。
コマンドで指定した内容は、NVS領域に記録されます。
restartコマンドで最新の設定が有効になります。
esp32> set_ap ESP32 ESP32
AP will be open (no passwd needed).
I (24305) cmd_router: AP settings ESP32/ESP32 stored.

esp32> set_ap ESP32 ESP32PASS
I (111554) cmd_router: AP settings ESP32/ESP32PASS stored.

esp32> restart

esp32> show
I (53805) cmd_router: ap_ssid ESP32
I (53805) cmd_router: ap_passwd ESP32PASS
STA SSID: <undef> Password: <undef>
AP SSID: ESP32 Password: ESP32PASS
Uplink AP not connected
0 Stations connected

このアクセスポイントに接続すると、192.168.4.xのアドレスが払い出されます。
esp32> I (19566) wifi:new:<1,0>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1
I (19576) wifi:station: 60:01:94:3a:79:dd join, AID=1, bgn, 20
I (19586) ESP32 NAT router: 1. station connected
I (19606) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.2

DHCPで払い出されるアドレスを変更するコマンドは用意されていません。
直接ソース(esp32_nat_router.c)の以下の部分を変更する必要が有ります。
    //IP4_ADDR(&ipInfo.ip, 192,168,4,1);
    //IP4_ADDR(&ipInfo.gw, 192,168,4,1);
    IP4_ADDR(&ipInfo.ip, 192,168,11,1);
    IP4_ADDR(&ipInfo.gw, 192,168,11,1);



外部のサイトに接続するためには、以下のコマンドでUplink側のルーター(外部と接続しているルータ)を登録する必要が有ります。
Uplink側のルーターを登録するとESP32は中継機として動作します。
esp32> set_sta UplinkルータのSSID Uplinkルータのパスワード
I (5346545) cmd_router: STA settings aterm-d5a4ee-g/XXXXXXXXXX stored.

esp32> restart

esp32> show
I (9787) cmd_router: ssid aterm-d5a4ee-g
I (9787) cmd_router: passwd XXXXXXXXXXX
I (9787) cmd_router: ap_ssid ESP32
I (9797) cmd_router: ap_passwd ESP32PASS
STA SSID: aterm-d5a4ee-g Password: XXXXXXXXXX
AP SSID: ESP32 Password: ESP32PASS
Uplink AP connected
1 Stations connected

Linuxマシンを使用してWiFiのパフォーマンスを調べてみました。
使用したLinuxマシンはOrangePi-PCで、MT7601UのUSB-WiFiとspeedtestツールを使いました。
どちらも親機にはAterm PA-WG2600HSを使っています。
こちらがNECのAterm WR8165N(300Mbps)経由で、親機に接続している状態でのパフォーマンスです。
orangepi@orangepipc:~$ speedtest --simple
Ping: 57.313 ms
Download: 13.38 Mbit/s
Upload: 6.26 Mbit/s
orangepi@orangepipc:~$ speedtest --simple
Ping: 55.69 ms
Download: 13.85 Mbit/s
Upload: 6.55 Mbit/s
orangepi@orangepipc:~$ speedtest --simple
Ping: 67.225 ms
Download: 13.87 Mbit/s
Upload: 5.69 Mbit/s

こちらがesp32_nat_router経由で、親機に接続したときのパフォーマンスです。
orangepi@orangepipc:~$ speedtest --simple
Ping: 63.655 ms
Download: 4.08 Mbit/s
Upload: 5.61 Mbit/s
orangepi@orangepipc:~$ speedtest --simple
Ping: 56.882 ms
Download: 5.35 Mbit/s
Upload: 6.29 Mbit/s
orangepi@orangepipc:~$ speedtest --simple
Ping: 67.796 ms
Download: 4.99 Mbit/s
Upload: 5.05 Mbit/s

こちらがesp_wifi_repeater(ESP8266のルーター)経由で、親機に接続したときのパフォーマンスです。
ESP32(240MHz 2Core)とESP8266(160MHz 1Core)では、CPU周波数は1.5倍の差ですが、Downloadスピードはそれ以上の差が出ています。
orangepi@orangepipc:~$ speedtest --simple
Ping: 55.979 ms
Download: 1.52 Mbit/s
Upload: 4.77 Mbit/s
orangepi@orangepipc:~$ speedtest --simple
Ping: 54.665 ms
Download: 1.20 Mbit/s
Upload: 4.20 Mbit/s
orangepi@orangepipc:~$ speedtest --simple
Ping: 48.133 ms
Download: 1.19 Mbit/s
Upload: 4.14 Mbit/s

続く...