Ethernet3_DhcpChatServer

最終更新日:2023/3/10

インクルードするライブラリですが、Ethernet3を使用します。このライブラリは安定していると思います。
TM1637用のポートは確保します。

オリジナルコード

/*
 DHCP Chat  Server
 A simple server that distributes any incoming messages to all
 connected clients.  To use telnet to  your device's IP address and type.
 You can see the client's input in the serial monitor as well.
 Using an Arduino Wiznet Ethernet shield.
 THis version attempts to get an IP address using DHCP
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 created 21 May 2011
 modified 9 Apr 2012
 by Tom Igoe
 Based on ChatServer example by David A. Mellis
 */
#include <SPI.h>
#include <Ethernet3.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
IPAddress ip(192, 168, 1, 177);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  Serial.println("Trying to get an IP address using DHCP");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // initialize the ethernet device not using DHCP:
    Ethernet.begin(mac, ip, subnet, gateway);
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  ip = Ethernet.localIP();
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(ip[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  // start listening for clients
  server.begin();
}
void loop() {
  // wait for a new client:
  EthernetClient client = server.available();
  // when the client sends the first byte, say hello:
  if (client) {
    if (!gotAMessage) {
      Serial.println("We have a new client");
      client.println("Hello, client!");
      gotAMessage = true;
    }
    // read the bytes incoming from the client:
    char thisChar = client.read();
    // echo the bytes back to the client:
    server.write(thisChar);
    // echo the bytes to the server as well:
    Serial.print(thisChar);
  }
}

説明

W5500-EVB-PICO単体で使用するEthernetスケッチです。今回はAdvancedChatServerです。
W5500-EVB-PICOはW5500とRP2040をSPI接続しています。GP16..21が占有されます。
汎用ライブラリの場合、自分でちゃんとPIN指定した方がいいです。
また、W5500のRESETピンがPULL_UPされていないようなので、自分でHIGHに固定する必要がありそうです。

MACアドレスはWIZNET系にしておきます。
IPは192.168.0.210にしておきます。
このサンプルスケッチはシリアルモニタを開かないとSetupのコードが途中で止まったままです。
TELENETツールを別途用意してください。PuTTYでよいかと思います。
TELENETを選択してIPアドレスを入力して接続すると、シリアルモニタにクライアントが接続された旨、
メッセージが届きます。入力した文字列がそのまま表示されるはずです。

レタッチコード

/*
 * 2023/03/14 T.Wanibe 
 * DHCP Chat  Server
 * 着信メッセージを接続されているすべてのクライアントに配信する単純なサーバー。 
 * デバイスの IP アドレスとタイプに telnet を使用するには。
 * シリアル モニターでもクライアントの入力を確認できます。
 * W5500-EVB-PICOを使用します。
 * このバージョンは、DHCP を使用して IP アドレスを取得しようとします
 * 最大1044480バイトのフラッシュメモリのうち、スケッチが62484バイト(5%)を使っています。
 * 最大262144バイトのRAMのうち、グローバル変数が7996バイト(3%)を使っていて、ローカル変数で254148バイト使うことができます。
 */
#include        <SPI.h>
#include        <Ethernet3.h>
#define         SPI_SCK         18
#define         SPI_RX          16
#define         SPI_TX          19
#define         SPI_CS          17
#define         NICReset        20
#define         HTTPport        80
#define         TELNETport      23
#define         SerialRate      115200
// コントローラの MAC アドレスと IP アドレスを以下に入力します。
// IP アドレスは、ローカル ネットワークによって異なります。
// ゲートウェイとサブネットはオプションです。
byte            mac[]           = {0x00,0x08,0xDC,0x54,0x4D,0xE0};              //WIZNET
byte            ip[]            = {192, 168, 0, 210};
byte            dns_server[]    = {192, 168, 0, 1};
byte            gateway[]       = {0, 0, 0, 0};
byte            subnet[]        = {255, 255, 255, 0};
boolean         gotAMessage     = false;                                        // クライアントからのメッセージをまだ受け取っているかどうか
// telnet のデフォルトはポート 23 です
EthernetServer  server(TELNETport);
//----------------
void setup() {
        // シリアル通信を開きます。
        Serial.begin(SerialRate);
        while (!Serial) {
                ;                               // シリアルポートが接続されるのを待ちます。
        }
        // イーサネット接続を開始します。
        pinMode(SPI_CS,OUTPUT);
        pinMode(NICReset,OUTPUT);
        // イーサネット接続とサーバーを開始します。
        SPI.setSCK(SPI_SCK);
        SPI.setRX(SPI_RX);
        SPI.setTX(SPI_TX);
        SPI.setCS(SPI_CS);
        SPI.begin();
        // Ethernet.init(pin)を使用してCSピンを設定できます
        Ethernet.setCsPin(SPI_CS);
        Ethernet.setRstPin(NICReset);
        digitalWrite(NICReset,LOW);
        delay(10);
        digitalWrite(NICReset,HIGH);
        Ethernet.init(SPI_CS);
        Serial.println("Trying to get an IP address using DHCP");
        if (Ethernet.begin(mac) == 0) {
                Serial.println("Failed to configure Ethernet using DHCP");
                // DHCP を使用せずにイーサネット デバイスを初期化します。
                Ethernet.begin(mac, ip, subnet, gateway);
        }
        // ローカル IP アドレスを印刷します。
        Serial.print("My IP address: ");
        Serial.println(Ethernet.localIP());
        Serial.println();
        // クライアントのリスニングを開始する
        server.begin();
}
//----------------
void loop() {
        // 新しいクライアントを待ちます:
        EthernetClient client = server.available();
        // クライアントが最初のバイトを送信したら、こんにちはと言ってください:
        if (client) {
                if (!gotAMessage) {
                        Serial.println("We have a new client");
                        client.println("Hello, client!");
                        gotAMessage = true;
                }
                // クライアントから受信したバイトを読み取ります。
                char thisChar = client.read();
                // バイトをクライアントにエコー バックします。
                server.write(thisChar);
                // バイトをサーバーにもエコーします。
                Serial.print(thisChar);
        }
}


戯言(nonsense)に戻る


問い合わせ頁の表示


免責事項

本ソフトウエアは、あなたに対して何も保証しません。本ソフトウエアの関係者(他の利用者も含む)は、あなたに対して一切責任を負いません。
あなたが、本ソフトウエアを利用(コンパイル後の再利用など全てを含む)する場合は、自己責任で行う必要があります。

本ソフトウエアの著作権はToolsBoxに帰属します。
本ソフトウエアをご利用の結果生じた損害について、ToolsBoxは一切責任を負いません。
ToolsBoxはコンテンツとして提供する全ての文章、画像等について、内容の合法性・正確性・安全性等、において最善の注意をし、作成していますが、保証するものではありません。
ToolsBoxはリンクをしている外部サイトについては、何ら保証しません。
ToolsBoxは事前の予告無く、本ソフトウエアの開発・提供を中止する可能性があります。

商標・登録商標

Microsoft、Windows、WindowsNTは米国Microsoft Corporationの米国およびその他の国における登録商標です。
Windows Vista、Windows XPは、米国Microsoft Corporation.の商品名称です。
LabVIEW、National Instruments、NI、ni.comはNational Instrumentsの登録商標です。
I2Cは、NXP Semiconductors社の登録商標です。
その他の企業名ならびに製品名は、それぞれの会社の商標もしくは登録商標です。
すべての商標および登録商標は、それぞれの所有者に帰属します。