一,硬件:
- USR-ES1模块这个淘宝上卖的比较火,所以我们用的这个。
- 板子用mega2560,理论上可以用在任何arduino上,包括stm32,esp系列
二,软件:
- VSCODE+PlatformIO+arduino框架
- 使用的Ethernet3.0,地址:https://github.com/sstaub/Ethernet3
三,接口:
1,RST是硬件复位引脚,一定要接线
2,我一共连了7根线,RST(5),CS(4),SCK(52),MISO(50),MOSI(51),3.3V,GND。
Arduino / Genuino Board | MOSI | MISO | SCK | CS (slave) | CS (master) | Level |
Uno or Duemilanove | 11 or ICSP-4 | 12 or ICSP-1 | 13 or ICSP-3 | 10 | – | 5V |
Mega1280 or Mega2560 | 51 or ICSP-4 | 50 or ICSP-1 | 52 or ICSP-3 | 53 | – | 5V |
Leonardo | ICSP-4 | ICSP-1 | ICSP-3 | – | – | 5V |
Due | SPI-4 | SPI-1 | SPI-3 | – | 4, 10, 52 | 3,3V |
Zero | ICSP-4 | ICSP-1 | ICSP-3 | – | – | 3,3V |
101 | 11 or ICSP-4 | 12 or ICSP-1 | 13 or ICSP-3 | 10 | 10 | 3,3V |
MKR1000 | 8 | 10 | 9 | – | – | 3,3V |
可以使用下面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.
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
走着的小站
本文地址: arduino 使用w5500模块(USR-ES1)进行有线连接网口RJ45
本文地址: arduino 使用w5500模块(USR-ES1)进行有线连接网口RJ45