Ethernet3_WebClientRepeating

最終更新日:2023/3/13

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

オリジナルコード

/*
  Repeating Web client
 This sketch connects to a a web server and makes a request
 using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
 the Adafruit Ethernet shield, either one will work, as long as it's got
 a Wiznet Ethernet module on board.
 This example uses DNS, by assigning the Ethernet client with a MAC address,
 IP address, and DNS address.
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 created 19 Apr 2012
 by Tom Igoe
 modified 21 Jan 2014
 by Federico Vanzati
 http://arduino.cc/en/Tutorial/WebClientRepeating
 This code is in the public domain.
 */
#include <SPI.h>
#include <Ethernet3.h>
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192, 168, 1, 177);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 1, 1);
// fill in your Domain Name Server address here:
IPAddress myDns(1, 1, 1, 1);
// initialize the library instance:
EthernetClient client;
char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers
void setup() {
  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // give the ethernet module time to boot up:
  delay(1000);
  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip, subnet, gateway, myDns);
  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}
void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }
  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }
}
// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();
    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

説明

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にしておきます。SUBNET DNS等々実行環境に合わせてください。
このスケッチは Web サーバーに接続し、繰り返しリクエストを行うものです。
結果としてシリアルモニタを開くと以下のようなメッセージが返るかと思います。

My IP address: 192.168.0.210
connecting...
HTTP/1.1 200 OK
Date: Mon, 13 Mar 2023 02:09:13 GMT
Content-Type: text/plain
Content-Length: 6
Connection: close
x-amz-id-2: WDVZ7VZ42R/5W9GY3DsU5dxCqnf3r15kbDmBjz4b0HVM6UaTZoyLrSVOUum4R06flHhpBnkJya/SeQDfJ4TJuQ==
x-amz-request-id: TTJWCF0A7Z2D2AF2
x-amz-replication-status: COMPLETED
Last-Modified: Tue, 07 Sep 2021 08:10:09 GMT
x-amz-version-id: WBsZWT8zzegKJCAq_2P5OQ80QOW_Ejf7
Accept-Ranges: bytes
ETag: "362a8f1540fbd901c65f44e4662fc6d7"
X-Cache: RefreshHit from cloudfront
Via: 1.1 62c71b579b931f194fbc7abcc843d132.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: SFO53-C1
X-Amz-Cf-Id: mj_V1EF2OLHQP-6Un64vHTOxarSuHyNfNia8kBVCHLIUTllMmr2UEA==
CF-Cache-Status: DYNAMIC
Set-Cookie: __cf_bm=LCC3QvEgYTJfTKWCtaS2fOxd_Ofy1mxNjLrpYwAvEXQ-1678673353-0-AQ5XLVooS+B/xSmCFmw/bMP58lKZVuM5V7whgLsngHvw2U8PxjyD7N1Lfx/3R4srMm2q64CPtnDVpL17ExAAC+I=; path=/; expires=Mon, 13-Mar-23 02:39:13 GMT; domain=.arduino.cc; HttpOnly; SameSite=None
Server: cloudflare
CF-RAY: 7a70bbc76e428394-KIX
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400

10816    <---実行時間

レタッチコード

/*
 * 2023/03/13 T.Wanibe
 * Repeating Web client
 * このスケッチは Web サーバーに接続し、リクエストを行います
 * WiznetW5500-ENV-PICOをターゲットとします。
 * 回路:
 * ピン 16..21に接続されたイーサネットchip
 * 最大1044480バイトのフラッシュメモリのうち、スケッチが60900バイト(5%)を使っています。
 * 最大262144バイトのRAMのうち、グローバル変数が7968バイト(3%)を使っていて、ローカル変数で254176バイト使うことができます。
 */
#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[]       = {192, 168, 0, 1};
byte            subnet[]        = {255, 255, 255, 0};
// initialize the library instance:
EthernetClient  client;
char            server[]        = "www.arduino.cc";     //IPAddress server(64,131,82,241);
unsigned long   lastConnectionTime      = 0;            // last time you connected to the server, in milliseconds
const unsigned long     postingInterval = 10L * 1000L;  // delay between updates, in milliseconds
// the "L" is needed to use long type numbers
//----------------このメソッドは、サーバーへの HTTP 接続を確立します。
void httpRequest() {
        // 新しいリクエストを送信する前に、すべての接続を閉じてください。
        // これにより、WiFi シールドのソケットが解放されます
        client.stop();
        // 接続に成功した場合:
        if (client.connect(server, HTTPport)) {
                Serial.println("connecting...");
                // HTTP PUT リクエストを送信します。
                client.println("GET /latest.txt HTTP/1.1");
                client.println("Host: www.arduino.cc");
                client.println("User-Agent: arduino-ethernet");
                client.println("Connection: close");
                client.println();
                // 接続が確立された時刻に注意してください。
                lastConnectionTime = millis();
        }else {
                // 接続できなかった場合:
                Serial.println("connection failed");
        }
}
//----------------
void setup() {
         // start serial port:
         Serial.begin(SerialRate);
        while (!Serial) {
                ; // シリアルポートが接続されるのを待ちます。
        }
        // give the ethernet module time to boot up:
        delay(1000);
        // start the Ethernet connection using a fixed IP address and DNS server:
        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, dns_server);
        // print the Ethernet board/shield's IP address:
        Serial.print("My IP address: ");
        Serial.println(Ethernet.localIP());
}
//-------------
void loop() {
        // ネット接続からの着信データがある場合。 シリアルポートから送信します。 これはデバッグのみを目的としています。
        if (client.available()) {
                char c = client.read();
                Serial.write(c);
        }
        // 最後の接続から 10 秒が経過した場合は、再度接続してデータを送信します。
        if (millis() - lastConnectionTime > postingInterval) {
                httpRequest();
        }
}


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