https://github.com/khoih-prog/Ethernet_Generic
I am using the library file to write a code that uses both webclient, webserver,
SF read and write,SD read and write simultaneously.
Webclient used for download
Webserver mounts LittleFS as a server
https://github.com/amcewen/HttpClient
I found an HttpClient that can be used in conjunction with the EthernetGeneric library
It passes in the HttpClient object to EthernetGeneric
// 引入闪存读写库
#include <FS.h>
#include <LittleFS.h>
// 引入以太网访问库
#include <EthernetClient.h>
#include <HTTPClient.h>
// 声明以太网客户端对象
EthernetClient client;
uint8_t buffsing[10240] = {0}; // 定义下载缓存,在全局变量,使用全局 RAM
void SplitURL(char szUrl[], char szHost[], char szPath[]) { // 分割URL
int iStart = 0;
int iEnd = 0;
int iLen = 0;
if (strncmp(szUrl, "http://", 7) == 0)
iStart = 7;
else if (strncmp(szUrl, "https://", 8) == 0)
iStart = 8;
while (szUrl[iStart + iLen] != '\0' && szUrl[iStart + iLen] != '/') {
iLen++;
}
memcpy(szHost, szUrl + iStart, iLen);
if (strlen(szUrl) - iStart - iLen == 0)
szPath[0] = '/';
else
memcpy(szPath, szUrl + iStart + iLen, strlen(szUrl) - iStart - iLen);
}
bool GetFile(fs::FS &fs, String file_url, const char *tf_url) { // 下载网络文件
Serial.println("准备下载文件:");
char szUrl[] = {0};
char szHost[100] = {0};
char szPath[1024] = {0};
strcpy(szUrl, file_url.c_str());
SplitURL(szUrl, szHost, szPath); // 分割URL
Serial.print(" - 地址:");
Serial.print(szHost);
Serial.print(" 路径:");
Serial.println(szPath);
// 声明HTTP访问端对象
HttpClient http(client);
http.get(szHost, szPath);
bool log = true;
int httpCode = http.responseStatusCode();
Serial.printf(" - [HTTP] GET访问结果: %d\n", httpCode);
if (httpCode == 200) {
http.skipResponseHeaders(); // 不用这个取不到Length
int Length = http.contentLength(); // 读取响应数据字节数
Serial.print(" - 文件大小 ≈ ");
Serial.println(GetBytes(Length));
if (Length > 0) {
int ms;
int Leng;
unsigned long mss;
unsigned long start; // 取启动时间
File fe = fs.open(tf_url, "wb+");
if (!fe) {
Serial.println(" - 创建下载文件失败");
log = false;
} else {
Serial.println(" - 开始下载文件...");
ms = 0;
Leng = 0;
mss = millis();
start = millis(); // 开始计时
while (http.connected() && (Length > 0 || Length == -1)) { // 判断有数据存在
size_t size = http.available(); // 获取数据流中字节数
if (size) {
int c = http.read(buffsing, ((size > sizeof(buffsing)) ? sizeof(buffsing) : size)); // 读取数据
if (!fe.write(buffsing, c)) { // 写入文件
Serial.println("- 数据写入失败");
log = false;
break;
}
Leng += c;
Length -= c;
ms = (millis() - mss) / 1000.0; // 计时每秒,当前时间减去获取时间
delay(1);
if ((Length > 0) && (ms >= 1)) { // 大约计算耗时
Serial.print(" - 剩余 ≈ ");
Serial.print(GetBytes(Length));
Serial.print("\t速度 ≈ ");
Serial.print(GetBytes(Leng));
Serial.println("/S");
ms = 0;
Leng = 0;
mss = millis();
}
delay(1);
}
}
start = millis() - start;
fe.close();
}
if (log) {
Serial.print(" - 文件下载完成, 耗时:");
Serial.print(start / 1000.0); // 计算耗时
Serial.println("s");
Serial.print(" - 文件保存路径:");
Serial.println(tf_url);
}
} else {
Serial.println(" - 无返回数据");
log = false;
}
http.stop();
} else {
Serial.println(" - [HTTP] GET访问失败");
log = false;
}
Serial.println(" - [HTTP] GET访问连接结束");
return log;
}
But I didn't get a WebServer that I could use with it,
I saw its integration with webserver in the following article
It can be used in conjunction with the EthernetWebServer library supported by
the EthernetGeneric library
But its webserver is used to connect to WIFI, and I want webserver. h to be used
through EthernetGeneric's Ethernet
Trigger wireless restart when the following code is not connected to WiFi
#include <WebServer.h>
#include <EthernetWebServer.h>
#include "Ethernet_Generic.h"
EthernetWebServer ethernetServer(80);
WebServer Server(80);
void handle() {
Server.send(200, "text/plain", "Hello!");
}
void setup() {
Serial.begin(115200);
delay(100);
Ethernet.init (15);
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01};
Ethernet.begin(mac);
Server.on("/", handle);
Server.begin();
Serial.print("HTTPServer is @ IP : ");
Serial.println(Server.localIP());
}
void loop() {
Server.handleClient();
}
I would like to use EthernetGeneric's Ethernet instead of WebServer's WiFi to
replace the EthernetWebServer library as it does not support LittleFS very much
I need LittleFS static files for the WebServer library
SPI2 connection used by EthernetGeneric,Defined in the Ethernet. h example
of the EthernetGeneric library
Because SD needs to be mounted, SPI2 is used
#define HSPI_IOMUX_PIN_NUM_MISO 12
#define HSPI_IOMUX_PIN_NUM_MOSI 13
#define HSPI_IOMUX_PIN_NUM_CLK 14
#define HSPI_IOMUX_PIN_NUM_CS 15
#define HSPI_IOMUX_PIN_NUM_WP 2
#define HSPI_IOMUX_PIN_NUM_HD 4
