Ethernet3_TelnetClient

最終更新日:2023/3/10

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

オリジナルコード

/*
  Telnet client
 This sketch connects to a a telnet server (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.  You'll need a telnet server
 to test this with.
 Processing's ChatServer example (part of the network library) works well,
 running on port 10002. It can be found as part of the examples
 in the Processing application, available at
 http://processing.org/
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 created 14 Sep 2010
 modified 9 Apr 2012
 by Tom Igoe
 */
#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:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
// Enter the IP address of the server you're connecting to:
IPAddress server(1, 1, 1, 1);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use  port 10002):
EthernetClient client;
void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 10002)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}
void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar);
    }
  }
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while (true);
  }
}

説明

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

MACアドレスはWIZNET系にしておきます。
IPは192.168.0.210にしておきます。
このスケッチはTELNETサーバとしてGoogleサーバにアクセスして結果をシリアルモニタに表示します。
とはいうもののGoogleサーバ(1.1.1.1)はほんとうなのか判りません。
別途telnetサーバが構築困難なのでこれで良しとします。

レタッチコード

/*
 * 2023/03/10 T.Wanibe
 * Telnet client
 * このスケッチは、Telnet サーバー (http://www.google.com) に接続します。
 * Arduino Wiznet イーサネット シールドを使用します。 telnet サーバーが必要です
 * これをテストします。  
 * Processing の ChatServer の例 (ネットワーク ライブラリの一部) は
 * うまく機能します。
 * ポート 10002 で実行されています。これは、Processing アプリケーションの例の
 * 一部として見つけることができます。
 * 最大1044480バイトのフラッシュメモリのうち、スケッチが60908バイト(5%)を使っています。
 * 最大262144バイトのRAMのうち、グローバル変数が7948バイト(3%)を使っていて、ローカル変数で254196バイト使うことができます。
 */
#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            gateway[]       = {192, 168, 0, 1};
byte            subnet[]        = {255, 255, 255, 0};           
byte            server[]        = {1, 1, 1, 1};                 //接続先のサーバーの IP アドレスを入力してください:
//接続先のサーバーの IP アドレスとポートでイーサネット クライアント ライブラリを初期化します (ポート 23 は telnet のデフォルトです。Processing の ChatServer を使用している場合は、ポート 10002 を使用します)。
EthernetClient  client;
//-------------
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);
        // シリアル通信を開き、ポートが開くのを待ちます。
        Serial.begin(SerialRate);
        while (!Serial) {
                ; // シリアルポートが接続されるのを待ちます。
        }
        // イーサネット シールドを初期化するために 1 秒待ちます。
        delay(1000);
        Serial.print("connecting...");
        Serial.println(server);
        // 接続が確立されたら、シリアル経由で報告します。
        if (client.connect(server, 10002)) {
                Serial.println("connected");
        }else {
                // サーバーに接続できなかった場合:
                Serial.println("connection failed");
        }
}
//-------------
void loop(){
        // サーバーから利用可能な着信バイトがある場合は、それらを読み取って出力します。
        if (client.available()) {
                char c = client.read();
                Serial.print(c);
        }
        // シリアル キューにバイトがある限り、それらを読み取り、開いている場合はソケットから送信します。
        while (Serial.available() > 0) {
                char inChar = Serial.read();
                if (client.connected()) {
                        client.print(inChar);
                }
        }
        // サーバーが切断されている場合は、クライアントを停止します。
        if (!client.connected()) {
                Serial.println();
                Serial.println("disconnecting.");
                client.stop();
                // do nothing:
                while (true);
        }
}


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