【Arduino】W5100S-EVB-Pico, W5500-EVB-Picoを使う

it

Raspberry Pi PicoにWiznetのW5100S, W5500 LANコネクタが搭載したW5100S-EVB-PicoとW5500-EVB-Picoを使ってみます。
マイコン基板同士のLAN通信(Telnet Saver, Telnet Client)、WebServerにしたLチカ操作などLANを使ったスケッチを試してみます。

マイコン名

簡単紹介

Raspberry Pi PicoにLAN通信チップW5100, W5500を搭載し、手軽にLAN通信できる基板です。

Wiznet W5100S-EVB-Pico : 外部サイト
Wiznet W5500-EVB-Pico : 外部サイト

◆入手

国内電子部品オンラインショップで\2,500~\3,000(送料込み)が目安になると思います。

◆梱包

WIZnetの箱に入っています。
(ピンヘッダまで同梱されていたか覚えていません)

ピン配置

◆W5100S-EVB-Pico

◆W5500-EVB-Pico

外観

◆W5100S-EVB-Pico

◆W5500-EVB-Pico

使ってみた

◆開発環境

Arduino環境で開発しました。
ボードライブラリに「Raspberry Pi Pico/RP2040 by Earle F. Philhower, III」を使います。
LAN通信用モジュールライブラリは「Ethernet Library for Arduino」を使います。

Raspberry Pi Picoと高い互換性があります。
GPIOを使った基本的なスケッチはこちらを参照。

◆W5100S-EVB-PicoとW5500-EVB-Pico

どちらもRaspberry Pi PicoをベースにLAN通信チップ(W5100S / W5500)とRJ45コネクタを搭載したモデルです。
LAN通信部を除いた性能や使い方にはRaspberry Pi Picoと互換性があります。

LAN通信通信チップとの通信はRP2040で使用できるSPIのうち、SPI0を使います。
LANと併用してSPIを使う場合はSPI1側を使用するのが安全かと思います(主観です)

W5100SとW5500との違いはこちら(Wiznetサイト)を参照。
使用テキストベースの電文ではほとんど差異は見られませんでした。

W5100S-EVB-PicoとW5500-EVB-Picoのスケッチは今回の動作確認の範囲では同じスケッチでどちらも動作しました。

◆Telnet

W5100S-EVB-Picoをサーバにしたパソコン(USB-LANアダプタ経由)との通信ができました。
W5100S-EVB-PicoとW5500-EVB-Picoとの通信ができました。

同じスケッチで動作するので単純なコマンドの交換をするのであればW5100SとW5500との差は感じません。

◆WebServer

ブラウザにボタンを配置し、LAN経由のリモートで基板のLEDのOFF/ONができました。
ブラウザを使うことでパソコンからデバイス側を制御するための専用の通信ソフト不要でデバイス内の状態確認や制御ができます。

HTMLの記述は煩わしいですが、AIを活用することでコーディングを省略できます。

まとめ

有線LANを使ったマイコンとPC、マイコン同士の通信を簡単に実装できました。
今回はマイコンとLAN通信チップが1枚の基板に搭載された基板を使うことで親和性の高さを感じました。

これまでのRaspberry Pi Picoの資産を活かし、LANを使った通信によりパソコンからの電文やマイコン同士のデータ交換を市販のケーブルで行うことができます。

W5100S-EVB-PicoとW5500-EVB-Picoを試してみましたが、軽微な用途では差はみられないので価格や入手しやすい方を選択するのが良いと思います。

準備

Arduino環境の作成

◆開発環境

Arduino環境を使用します。
Arduino環境の作成についてはこちらで紹介しています。

◆ボードライブラリ

ボードライブラリに「Raspberry Pi Pico/RP2040 by Earle F. Philhower, III」を使います。

追加のボードマネージャのURLhttps://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
検索RP2040
ボードライブラリRaspberry Pi RP2040 Boards(x.x.x)※
W5100S-EVB-PicoWIZnet W5100S-EVB-Pico
W5500-EVB-PicoWIZnet W5500-EVB-Pico
※動作確認はバージョン 4.4.3です

◆モジュールライブラリ

Ethernetライブラリ

W5100S、W5500を使用するためのライブラリのダウンロードとインストールをします。
以下サイトからダウンロードします。
GitHub – WIZnet-ArduinoEthernet/Ethernet: [WIZnet Modified] Ethernet Library for Arduino

I2CのサンプルではSSD1306 OLED、SPIのサンプルではILI9341を使用します。
サンプルを使用しない場合はインストール不要です。

スケッチ

Telnet(TCP Sever)

説明

Telnetを使った電文の通信をします。
W5100S-EVB-PicoをTCP Serverにします。
パソコンとUSB LAN変換アダプタを使用してTCP ClientとしてServerとTCP/IPでTelnet通信します。

W5100S-EVB-Picoは起動後TCP ServerとしてClientからの接続を待機します。
接続後は、TCP Clientからのデータを受信するとUSBシリアルへ送信します。
USBシリアルからのデータはTCP Clientに送信します。

サーバー側(W5100S-EVB-Pico)設定
IP Address : 192.168.1.20
SubnetMask : 255.255.255.0
ポート番号 : 5000

クライアント側(パソコン)設定
IP Address : 192.168.1.10
SubnetMask : 255.255.255.0

配線

ジャンパワイヤでの配線は不要。
パソコンとUSBで接続、TCP Client側とLANケーブルで接続します。

スケッチ

#include <SPI.h>
#include <Ethernet.h>

#define SOCKET_PORT     (5000)
#define ETH_CS          (17)

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE
};

IPAddress ip(192, 168, 1, 20);
IPAddress myDns(192, 168, 1, 10);
IPAddress gateway(192, 168, 1, 10);
IPAddress subnet(255, 255, 255, 0);

EthernetServer server(SOCKET_PORT);

void setup()
{
  Ethernet.init(ETH_CS);
  Ethernet.begin(mac, ip, myDns, gateway, subnet);

  Serial.begin(115200);
  
  if (Ethernet.hardwareStatus() == EthernetNoHardware)
  {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1);
    }
  }

  Serial.println("ok....");
  Serial.print("localIP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("subnetMask: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("gatewayIP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("dnsServerIP: ");
  Serial.println(Ethernet.dnsServerIP());

  server.begin();
  Serial.println("Server Start");
}

void loop()
{
  EthernetClient client = server.available();

  if (client.available() != 0)
  {
    char cClient = client.read();
    Serial.print(cClient);
  }

  if(Serial.available() > 0)
  {
    char cCOM = Serial.read();
    server.print(cCOM);
  }
}

結果

結果を確認するためにTeratermを2つ起動します。
1つはW5100S-EVB-Pico のUSBシリアル通信用です。
今回”COM51″で認識しました。

もう一つはUSB-LANアダプタ用です。
接続するために、パソコンのネットワーク設定を変更する必要があります。
この変更にはアドミニストレータ権限が必要で、設定の変更は自身の責任で行います。
ネットワーク設定の変更と、接続先のTelnetServer(W5100S-EVB-Pico)の接続例を簡単に記載します。

Teratermの”COM51″側(W5100S-EVB-Pico)から”hello”と入力すると、Telnet Client側(USB-LANアダプタ)に”hello”と表示されました。

Telnet Client側(USB-LANアダプタ)から”world”(+ enter キー)と入力すると、”COM51″側(W5100S-EVB-Pico)に”world”と表示されました。

Telnet(TCP Client)

説明

Telnetを使った電文の通信をします。
このスケッチ時実行するためにはTelnet(TCP Server)側が必要です。

起動後TCP Serverに接続します。
接続後は、USBシリアルからの受信データをTCP Serverに送信します。
TCP Serverからの受信データをUSBシリアルに送信します。

サーバー側(W5100S-EVB-Pico)設定
IP Address : 192.168.1.20
SubnetMask : 255.255.255.0
ポート番号 : 5000

クライアント側(W5500-EVB-Pico)設定
IP Address : 192.168.1.30
SubnetMask : 255.255.255.0

配線

ジャンパワイヤでの配線は不要。
W5100S-EVB-PicoとW5500-EVB-PicoをLANケーブルで接続する。

スケッチ

#include <SPI.h>
#include <Ethernet.h>

#define   PORTNO    (5000)
#define   ETH_CS    (17)

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 30);

IPAddress server(192, 168, 1, 20);

EthernetClient client;

void setup()
{
  Ethernet.init(ETH_CS);
  Ethernet.begin(mac, ip);
  
  Serial.begin(115200);
  while (!Serial);

  if (Ethernet.hardwareStatus() == EthernetNoHardware)
  {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true)
    {
      delay(1);
    }
  }

  while (Ethernet.linkStatus() == LinkOFF)
  {
    Serial.println("Ethernet cable is not connected.");
    delay(500);
  }

  delay(PORTNO);
  Serial.println("connecting...");

  if (client.connect(server, 5000))
  {
    Serial.println("connected");
  }
  else
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available() != 0)
  {
    char cClient = client.read();
    Serial.print(cClient);
  }

  while (Serial.available() > 0)
  {
    char cCOM = Serial.read();
    if (client.connected() == true)
    {
      client.print(cCOM);
    }
  }

  if (client.connected() == false) 
  {
    Serial.printf("\ndisconnecting.\n");
    client.stop();
    while (true)
    {
      delay(1);
    }
  }
}

結果

Web Server

説明

WebServerを作ります。

ブラウザ上に”LED on”と”LED off”ボタンを配置します。
“LED on”ボタンを押したら、W5100S-EVB-Picoの実装LEDを点灯させます。
“LED off”ボタンを押したら、実装LEDを消灯させます。

このスケッチは、
“Example > Ethernet > WebServer”を参考にしてアレンジしました。

サーバー側(W5100S-EVB-Pico)設定
IP Address : 192.168.1.177
SubnetMask : 255.255.255.0

配線

ジャンパワイヤでの配線は不要。
パソコンとUSBで接続、TCP Client側とLANケーブルで接続します。

スケッチ

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 177);

EthernetServer server(80);

char html[][128] =
{
  "HTTP/1.1 200 OK",
  "Content-Type: text/html",
  "<!DOCTYPE HTML>",
  "\n",
  "<html>",
  "<button onclick=\"location.href='/?LED=ON'\" type=\"button\">LED on</button>",
  "<button onclick=\"location.href='/?LED=OFF'\" type=\"button\">LED off</button>",
  "</html>",
  0
};


void setup()
{
  Ethernet.init(17);
  
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  while (!Serial) {
    ;
  }
  Serial.println("Ethernet WebServer Example");

  Ethernet.begin(mac, ip);

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1);
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() 
{
  EthernetClient client = server.available();
  if (client)
  {
    boolean currentLineIsBlank = true;
    String request = "";
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        request += c;

        if (c == '\n' && currentLineIsBlank)
        {
          int i = 0;
          while(html[i][0] != 0)
          {

            client.println(html[i]);
            i ++;
          }
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }

    if (request.indexOf("GET /?LED=ON") >= 0)
    {
      digitalWrite(LED_BUILTIN, HIGH);
    }
    if (request.indexOf("GET /?LED=OFF") >= 0)
    {
      digitalWrite(LED_BUILTIN, LOW);
    }

    delay(1);
    client.stop();
  }
}

結果

結果の確認にはTeratermとブラウザを開きます。

TeratermはUSBシリアル接続をしていないとサーバーを起動しないようになっているためです。
ブラウザのアドレスバーには”192.168.1.177″(+ Enter)を入力します。

ブラウザ上に”LED on”と”LED off”ボタンが表示されます。
“LED on”ボタンを押すとW5100S-EVB-Pico”の実装LED(緑)が点灯しました。
”LED off”ボタンを押すと実装LEDは消灯しました。

コメント

タイトルとURLをコピーしました