一,硬件:

  • USR-ES1模块这个淘宝上卖的比较火,所以我们用的这个。
  • 板子用mega2560,理论上可以用在任何arduino上,包括stm32,esp系列

二,软件:

三,接口:

1,RST是硬件复位引脚,一定要接线

2,我一共连了7根线,RST(5),CS(4),SCK(52),MISO(50),MOSI(51),3.3V,GND。

Arduino / Genuino BoardMOSIMISOSCKCS (slave)CS (master)Level
Uno or Duemilanove11 or ICSP-412 or ICSP-113 or ICSP-3105V
Mega1280 or Mega256051 or ICSP-450 or ICSP-152 or ICSP-3535V
LeonardoICSP-4ICSP-1ICSP-35V
DueSPI-4SPI-1SPI-34, 10, 523,3V
ZeroICSP-4ICSP-1ICSP-33,3V
10111 or ICSP-412 or ICSP-113 or ICSP-310103,3V
MKR100081093,3V
(上表是arduino的SPI引脚的官网引脚表,CS我们可以自己设置)

可以使用下面2句函数设置CS引脚和RST引脚

//这里Ethernet不改变,且放在实例如client.begin()前面
Ethernet.setCsPin(4);
Ethernet.setRstPin(5);

四,程序

#include <SPI.h>
#include <Ethernet3.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.baidu.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

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
  }
  Ethernet.setCsPin(4);
  Ethernet.setRstPin(5);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // 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, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.baidu.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf 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);
  }
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

五,串口收到的数据如下

connected
HTTP/1.1 301 Moved Permanently
Cache-Control: max-age=86400
Content-Length: 246
Content-Type: text/html; charset=iso-8859-1
Date: Thu, 16 Dec 2021 01:26:03 GMT
Expires: Fri, 17 Dec 2021 01:26:03 GMT
Location: http://www.baidu.com/search/?q=arduino
Server: Apache
Connection: close

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.baidu.com/search/?q=arduino">here</a>.</p>
</body></html>

disconnecting.