Ethernet3_AdvancedChatServer

最終更新日:2023/3/10

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

オリジナルコード

/*
 Advanced Chat Server
 A more advanced server that distributes any incoming messages
 to all connected clients but the client the message comes from.
 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.
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 redesigned to make use of operator== 25 Nov 2013
 by Norbert Truchsess
 */
#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[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
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);
EthernetClient clients[4];
void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, subnet, gateway);
  // start listening for clients
  server.begin();
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.print("Chat server address:");
  Serial.println(Ethernet.localIP());
}
void loop() {
  // wait for a new client:
  EthernetClient client = server.available();
  // when the client sends the first byte, say hello:
  if (client) {
    boolean newClient = true;
    for (byte i=0;i<4;i++) {
      //check whether this client refers to the same socket as one of the existing instances:
      if (clients[i]==client) {
        newClient = false;
        break;
      }
    }
    if (newClient) {
      //check which of the existing clients can be overridden:
      for (byte i=0;i<4;i++) {
        if (!clients[i] && clients[i]!=client) {
          clients[i] = client;
          // clead out the input buffer:
          client.flush();
          Serial.println("We have a new client");
          client.print("Hello, client number: ");
          client.print(i);
          client.println();
          break;
        }
      }
    }
    if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      // echo the bytes back to all other connected clients:
      for (byte i=0;i<4;i++) {
        if (clients[i] && (clients[i]!=client)) {
          clients[i].write(thisChar);
        }
      }
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
  for (byte i=0;i<4;i++) {
    if (!(clients[i].connected())) {
      // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
      clients[i].stop();
    }
  }
}

説明

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にしておきます。
このスケッチはAI0..5の値をブラウザで表示するようにしていますが、RP2040はユーザに3ch提供している
のでレタッチしています。
このサンプルスケッチはシリアルモニタを開かないとSetupのコードが途中で止まったままです。
TELENETツールを別途用意してください。PuTTYでよいかと思います。
TELENETを選択してIPアドレスを入力して接続すると、シリアルモニタにクライアントが接続された旨、
メッセージが届きます。入力した文字列がそのまま表示されるはずです。

レタッチコード

/*
 * 2023/03/10 T.Wanibe
 * Advanced Chat Server
 * アナログ入力ピンの値を表示する単純な Web サーバー。
 * W5500-EVB-PICOをターゲットとしています
 * 回路:
 * ピン 16..21に接続されたイーサネットchip
 * ピン A0(26) 〜 A2(28) に接続されたアナログ入力
 * 最大1044480バイトのフラッシュメモリのうち、スケッチが62172バイト(5%)を使っています。
 * 最大262144バイトのRAMのうち、グローバル変数が7944バイト(3%)を使っていて、ローカル変数で254200バイト使うことができます。
 */
#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};
int             analogChannel[] = {26,27,28};
// telnet のデフォルトはポート 23 です
EthernetServer  server(TELNETport);
EthernetClient  clients[4];
//--------------
void setup() {
        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);
        // イーサネット デバイスを初期化する
        Ethernet.begin(mac, ip, subnet, gateway);
        // クライアントのリスニングを開始する
        server.begin();
        // シリアル通信を開き、ポートが開くのを待ちます。
        Serial.begin(SerialRate);
        while (!Serial) {
                ; // シリアルポートが接続されるのを待ちます。 
        }
        Serial.print("Chat server address:");
        Serial.println(Ethernet.localIP());
}
//--------------
void loop() {
        // 新しいクライアントを待ちます:
        EthernetClient client = server.available();
        // クライアントが最初のバイトを送信したら、こんにちはと言ってください:
        if (client) {
                boolean newClient = true;
                for (byte i=0;i<4;i++) {
                        //このクライアントが既存のインスタンスの 1 つと同じソケットを参照しているかどうかを確認します。
                        if (clients[i]==client) {
                                newClient = false;
                                break;
                        }
                }
                if (newClient) {
                        //オーバーライドできる既存のクライアントを確認します。
                        for (byte i=0;i<4;i++) {
                                if (!clients[i] && clients[i]!=client) {
                                        clients[i] = client;
                                        // 入力バッファをクリアします:
                                        client.flush();
                                        Serial.println("We have a new client");
                                        client.print("Hello, client number: ");
                                        client.print(i);
                                        client.println();
                                        break;
                                }
                        }
                }
                if (client.available() > 0) {
                        // クライアントから受信したバイトを読み取ります。
                        char thisChar = client.read();
                        // 接続されている他のすべてのクライアントにバイトをエコー バックします。
                        for (byte i=0;i<4;i++) {
                                if (clients[i] && (clients[i]!=client)) {
                                        clients[i].write(thisChar);
                                }
                        }
                        // バイトをサーバーにもエコーします。
                        Serial.write(thisChar);
                }
        }
        for (byte i=0;i<4;i++) {
                if (!(clients[i].connected())) {
                        // client.stop() 内部ソケット記述子を無効にするため、次に == を使用すると常に false が返されます;
                        clients[i].stop();
                }
        }
}


戯言(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社の登録商標です。
その他の企業名ならびに製品名は、それぞれの会社の商標もしくは登録商標です。
すべての商標および登録商標は、それぞれの所有者に帰属します。