Ethernet3_WebServer

最終更新日:2023/3/9

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

オリジナルコード

/*
  Web Server
 A simple web server that shows the value of the analog input pins.
 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
 */
#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);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

説明

W5500-EVB-PICO単体で使用するEthernetスケッチです。今回はWebserverです。
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提供している
のでレタッチしています。

レタッチコード

/*
 * 2023/03/09 T.Wanibe
 * Web 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         SerialRate      115200
// コントローラの MAC アドレスと IP アドレスを以下に入力します。
// IP アドレスは、ローカル ネットワークによって異なります。
byte            mac[]           = {0x00,0x08,0xDC,0x54,0x4D,0xE0};              //WIZNET
byte            ip[]            = {192, 168, 0, 210};
int             analogChannel[] = {26,27,28};
// イーサネット サーバー ライブラリの初期化
// 使用する IP アドレスとポート(ポート 80 は HTTP のデフォルトです):
EthernetServer  server(HTTPport);
//-------------
void setup() {
        pinMode(SPI_CS,OUTPUT);
        pinMode(NICReset,OUTPUT);
        // シリアル通信を開き、ポートが開くのを待ちます。
        Serial.begin(SerialRate);
        //while (!Serial) {
        //        ; // シリアルポートが接続されるのを待ちます。 レオナルドにのみ必要
        //}
        // イーサネット接続とサーバーを開始します。
        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);
        server.begin();
        Serial.print("server is at ");
        Serial.println(Ethernet.localIP());
}
//-------------
void loop() {
        // 着信クライアントをリッスンする
        EthernetClient client = server.available();
        if (client) {
                Serial.println("new client");
                // http リクエストは空白行で終了します
                boolean currentLineIsBlank = true;
                while (client.connected()) {
                        if (client.available()) {
                                char c = client.read();
                                Serial.write(c);
                                // 行末まで到達し (改行文字を受信)、行が空白の場合、http 要求は終了しているので、応答を送信できます
                                if (c == '\n' && currentLineIsBlank) {
                                        // 標準の http 応答ヘッダーを送信する
                                        client.println("HTTP/1.1 200 OK");
                                        client.println("Content-Type: text/html");
                                        client.println("Connection: close");            // 接続は応答の完了後に閉じられます
                                        client.println("Refresh: 5");                   // ページは 5 秒ごとに自動的に更新されます。
                                        client.println();
                                        client.println("<!DOCTYPE HTML>");
                                        client.println("<html>");
                                        // output the value of each analog input pin
                                        for (int i = 0; i < 3; i++) {
                                                int sensorReading = analogRead(analogChannel[i]);
                                                client.print("analog input ");
                                                client.print(analogChannel[i]);
                                                client.print(" is ");
                                                client.print(sensorReading);
                                                client.println("<br />");
                                        }
                                        client.println("</html>");
                                        break;
                                }
                                if (c == '\n') {
                                        // あなたは新しい行を始めています
                                        currentLineIsBlank = true;
                                }else if (c != '\r') {
                                        // 現在の行の文字を取得しました
                                        currentLineIsBlank = false;
                                }
                        }
                }
                // Web ブラウザにデータを受信する時間を与える
                delay(1);
                // 接続を閉じます:
                client.stop();
                Serial.println("client disconnected");
        }
}


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