【得捷Follow me第4期】W5500-EVB-Pico PIO下TCP协议实现Webclient和WebServer
[复制链接]
本帖最后由 wo4fisher 于 2024-3-2 16:25 编辑
1. 准备工作
新建基础工程,并添加Ethernet3库到工程中
2. WebClient测试
使用Tcp协议创建WebClient,连接远端服务器,并发送http请求,并打印返回数据
- #include <Arduino.h>
- #include <SPI.h>
- #include <Ethernet3.h>
-
-
-
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
-
-
-
-
- char server[] = "www.baidu.com";
-
-
- IPAddress ip(192, 168, 2, 177);
- IPAddress myDns(192, 168, 2, 1);
-
-
-
-
- EthernetClient client;
-
-
- unsigned long beginMicros, endMicros;
- unsigned long byteCount = 0;
- bool printWebData = true;
- uint8_t svrIp[4]= {0};
-
- void setup() {
-
-
- Serial.begin(115200);
- while (!Serial) {
- ;
- }
-
- Ethernet.setCsPin(17);
- Ethernet.setRstPin(20);
-
- Serial.println("Initialize Ethernet with DHCP:");
- if (Ethernet.begin(mac) == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
-
- if (Ethernet.link() == 0) {
- Serial.println("Ethernet cable is not connected.");
- }
-
- Ethernet.begin(mac, ip, myDns);
- } else {
- Serial.print(" DHCP assigned IP ");
- Serial.println(Ethernet.localIP());
- }
-
- delay(1000);
- Serial.print("connecting to ");
- Serial.print(server);
- Serial.println("...");
-
-
- if (client.connect(server, 80)) {
- Serial.print("connected to ");
- client.remoteIP(svrIp);
- Serial.print(svrIp[0]);Serial.print(',');
- Serial.print(svrIp[1]);Serial.print(',');
- Serial.print(svrIp[2]);Serial.print(',');
- Serial.print(svrIp[3]);Serial.println();
-
- client.println("GET /search?q=arduino HTTP/1.1");
- client.println("Host: www.baidu.com");
- client.println("Connection: close");
- client.println();
- } else {
-
- Serial.println("connection failed");
- }
- beginMicros = micros();
- }
-
- void loop() {
-
-
- int len = client.available();
- if (len > 0) {
- byte buffer[80];
- if (len > 80) len = 80;
- client.read(buffer, len);
- if (printWebData) {
- Serial.write(buffer, len);
- }
- byteCount = byteCount + len;
- }
-
-
- if (!client.connected()) {
- endMicros = micros();
- Serial.println();
- Serial.println("disconnecting.");
- client.stop();
- Serial.print("Received ");
- Serial.print(byteCount);
- Serial.print(" bytes in ");
- float seconds = (float)(endMicros - beginMicros) / 1000000.0;
- Serial.print(seconds, 4);
- float rate = (float)byteCount / seconds / 1000.0;
- Serial.print(", rate = ");
- Serial.print(rate);
- Serial.print(" kbytes/second");
- Serial.println();
-
-
- while (true) {
- delay(1);
- }
- }
- }
实现过程:
第一步:定义mac地址、W5500 静态ip地址、DNS地址和web服务器主机ip名称。
第二步:定义EthernetClient。
第三步:在初始化函数setup中完成cs引脚和rst引脚的配置,调用begin函数完成W5500网络参数初始化(首先尝试动态分配,动态分配失败的话,使用静态分配方式进行初始化),接下来调用connect方法连接远端服务器。
第四步:在循环函数Loop中,当接收到返回数据,通过串口打印;断开连接的话,停止。
3. WebServer测试
创建WebServer服务器,如果客户端连接访问,返回pico ADC模数转换结果
- #include <Arduino.h>
- #include <SPI.h>
- #include <Ethernet3.h>
-
-
-
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
- };
- IPAddress ip(192, 168, 2, 177);
-
-
-
-
- EthernetServer server(80);
- uint8_t ClientIp[4]= {0};
-
- void setup() {
-
-
- Serial.begin(115200);
- while (!Serial) {
- ;
- }
-
- Ethernet.setCsPin(17);
- Ethernet.setRstPin(20);
-
- Serial.println("Ethernet WebServer Example");
-
-
- 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");
-
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- Serial.write(c);
-
-
-
- if (c == '\n' && currentLineIsBlank)
- {
-
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close");
- client.println("Refresh: 5");
- client.println();
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
-
- int sensorReading = analogRead(A3);
- client.print("pico Voltage");
- client.print(" is ");
- client.print(sensorReading);
- client.println("<br />");
-
- client.println("</html>");
- break;
- }
- if (c == '\n') {
-
- currentLineIsBlank = true;
- } else if (c != '\r') {
-
- currentLineIsBlank = false;
- }
- }
- }
-
- delay(1);
-
- client.stop();
- Serial.println("client disconnected");
- }
- }
实现过程:
第一步:定义mac地址、W5500 静态ip地址。
第二步:定义EthernetServer,并使用默认80端口。
第三步:在初始化函数setup中完成cs引脚和rst引脚的配置,调用server.begin函数完成W5500网络参数初始化并启动Web服务器。
第四步:在循环函数Loop中,当有客户端连接请求,打印请求内容,之后,读取电源电压ADC转换值,并返回HTTP请求给客户端。
|