1235|4

862

帖子

2

TA的资源

纯净的硅(初级)

楼主
 

【得捷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>
  • // 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, 2, 177);
  • IPAddress myDns(192, 168, 2, 1);
  • // 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;
  • // Variables to measure the speed
  • unsigned long beginMicros, endMicros;
  • unsigned long byteCount = 0;
  • bool printWebData = true; // set to false for better speed measurement
  • uint8_t svrIp[4]= {0};
  • void setup() {
  • // Open serial communications and wait for port to open:
  • Serial.begin(115200);
  • while (!Serial) {
  • ; // wait for serial port to connect. Needed for native USB port only
  • }
  • Ethernet.setCsPin(17);
  • Ethernet.setRstPin(20);
  • // start the Ethernet connection:
  • Serial.println("Initialize Ethernet with DHCP:");
  • if (Ethernet.begin(mac) == 0) {
  • Serial.println("Failed to configure Ethernet using DHCP");
  • // Check for Ethernet hardware link status
  • if (Ethernet.link() == 0) {
  • Serial.println("Ethernet cable is not connected.");
  • }
  • // try to congifure using IP address instead of DHCP:
  • Ethernet.begin(mac, ip, myDns);
  • } else {
  • Serial.print(" DHCP assigned IP ");
  • Serial.println(Ethernet.localIP());
  • }
  • // give the Ethernet shield a second to initialize:
  • delay(1000);
  • Serial.print("connecting to ");
  • Serial.print(server);
  • Serial.println("...");
  • // if you get a connection, report back via serial:
  • 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();
  • // 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 {
  • // if you didn't get a connection to the server:
  • Serial.println("connection failed");
  • }
  • beginMicros = micros();
  • }
  • void loop() {
  • // if there are incoming bytes available
  • // from the server, read them and print them:
  • 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); // show in the serial monitor (slows some boards)
  • }
  • byteCount = byteCount + len;
  • }
  • // if the server's disconnected, stop the client:
  • 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();
  • // do nothing forevermore:
  • 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>
  • // 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, 2, 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);
  • uint8_t ClientIp[4]= {0};
  • void setup() {
  • // Open serial communications and wait for port to open:
  • Serial.begin(115200);
  • while (!Serial) {
  • ; // wait for serial port to connect. Needed for native USB port only
  • }
  • Ethernet.setCsPin(17);
  • Ethernet.setRstPin(20);
  • Serial.println("Ethernet WebServer Example");
  • // start the Ethernet connection and the server:
  • Ethernet.begin(mac, ip);
  • // start the server
  • 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>");
  • int sensorReading = analogRead(A3);
  • client.print("pico Voltage");
  • 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");
  • }
  • }

实现过程:

第一步:定义mac地址、W5500 静态ip地址。

第二步:定义EthernetServer,并使用默认80端口。

第三步:在初始化函数setup中完成cs引脚和rst引脚的配置,调用server.begin函数完成W5500网络参数初始化并启动Web服务器。

第四步:在循环函数Loop中,当有客户端连接请求,打印请求内容,之后,读取电源电压ADC转换值,并返回HTTP请求给客户端。

 

点赞 关注
个人签名水不撩不知深浅 人不拼怎知输赢
 
 

回复
举报

862

帖子

2

TA的资源

纯净的硅(初级)

沙发
 
成功从百度获取数据
个人签名水不撩不知深浅 人不拼怎知输赢
 
 
 

回复

862

帖子

2

TA的资源

纯净的硅(初级)

板凳
 

在pio中,rp2040的库没有ethernet或者有点乱,没找到。这里选用了外部第三方库,ethernet3为arduino生态中最新的库,并且适配了w5500,支持了rp2040(只需要“借用”一下硬件spi即可),通过测试,webclient成功从百度获取数据,说明移植没有问题。


Webserver例程应该也没有问题,限于时间关系,今天没有测试成功,明天测试一下…


 
 
 

回复

862

帖子

2

TA的资源

纯净的硅(初级)

4
 

web server客户端截屏:

 

个人签名水不撩不知深浅 人不拼怎知输赢
 
 
 

回复

862

帖子

2

TA的资源

纯净的硅(初级)

5
 
 
web server 服务器端串口打印信息:

 

个人签名水不撩不知深浅 人不拼怎知输赢
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
福禄克有奖直播:高精度测温赋能电子制造 报名中!
直播时间:2025年2月28日(周五)上午10:00
直播主题:高精度测温赋能电子制造
小伙伴们儿快来报名直播吧~好礼等你拿!

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表