File 从SD_MMC转换为SD库
我正在尝试将此代码从ESP32AIThinker Cam转换为在带护罩的Wemos D1R1上运行。File 从SD_MMC转换为SD库,file,File,我正在尝试将此代码从ESP32AIThinker Cam转换为在带护罩的Wemos D1R1上运行。 最大的问题是从SD_MMC.h到SD.h的变化。一些文件命令也是一个谜。这段代码的第一行我完全搞不懂: ` fs::FS &fs = SD_MMC; String path = server.uri(); //saves the to a string server uri ex.(192.168.100.110/edit server uri is "/edit&quo
最大的问题是从SD_MMC.h到SD.h的变化。一些文件命令也是一个谜。这段代码的第一行我完全搞不懂:
` fs::FS &fs = SD_MMC;
String path = server.uri(); //saves the to a string server uri ex.(192.168.100.110/edit server uri is "/edit")
Serial.print("path "); Serial.println(path);
//To send the index.html when the serves uri is "/"
if (path.endsWith("/")) {
path += "index.html";
`
这一个有更多的文件访问代码:
```File file = fs.open(path, "r"); //Open the File with file name = to path with intention to read it. For other modes see <a href="https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html" style="font-size: 13.5px;"> https://arduino-esp8266.readthedocs.io/en/latest/...</a>
size_t sent = server.streamFile(file, contentType); //sends the file to the server references from <a href="https://github.com/espressif/arduino-esp32/blob/master/libraries/WebServer/src/WebServer.h" style="font-size: 13.5px;"> https://arduino-esp8266.readthedocs.io/en/latest/...</a>
``File File=fs.open(路径“r”)//打开文件名为to path的文件,并打算读取它。有关其他模式,请参见
发送的大小=server.streamFile(文件,contentType)//将文件从发送到服务器引用
拒绝了我所有的改变他们信仰的尝试。
以下是完整代码,它在ESP32 Cam板上工作:
```//从中加载必要的库https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/src/WiFi.h
//https://www.instructables.com/How-to-Serve-Html-Files-From-SD-Card-on-ESP32/
#包括//添加Wifi功能
#include//创建一个只容纳一个同时客户端的简单Web服务器
#包括“FS.h”//以访问文件系统
#包括“SD_MMC.h”//以访问SD卡
//替换为您的网络凭据
#ifndef STASSID
#定义STASSID“用您的ssid替换”
定义STAPSK“用您的ssid替换”
#恩迪夫
常量字符*ssid=STASSID;
const char*password=STAPSK;
网络服务器(80)//将服务器设置为端口80
无效设置(){
Serial.begin(115200);//设置与ESP串行通信的波特率
/////////////////SD卡
如果(!SD_MMC.begin()){//初始化SD卡
Serial.println(“SD卡安装失败”);
返回;
}
uint8_t cardType=SD_MMC.cardType();//获取SD卡类型
如果(cardType==CARD_NONE){//检查sd卡是否存在
Serial.println(“未附SD卡”);
返回;
}
////////////////无线网络
WiFi.mode(WiFi_STA);//将其连接到WiFi网络
WiFi.begin(ssid,密码);//使用前面定义的ssid和密码
Serial.println(“”);
while(WiFi.status()!=WL_已连接){//等待连接
延迟(500);
连续打印(“.”);
}
Serial.println(“”);
串行打印(“连接到”);
序列号println(ssid);
串行打印(“IP地址:”);
Serial.println(WiFi.localIP());//打印路由器提供给esp的本地ip
////////////////服务器
server.onNotFound(handleRoot);//调用函数handleRoot,而不考虑服务器uri ex(192.168.100.110/edit服务器uri为“/edit”)
server.begin();//启动服务器
Serial.println(“HTTP服务器已启动”);
}
void handleRoot(){
/*SDU MMC属于SD卡“内存”。它另存为
与fs库中给定给fs的地址相同的变量
使用“FS”类使文件系统包装器
sd卡内存上的更改*/
fs::fs&fs=SD\u MMC;
String path=server.uri();//将保存到字符串服务器uri ex(192.168.100.110/edit服务器uri为“/edit”)
Serial.print(“路径”);Serial.println(路径);
//当服务uri为“/”时发送index.html
if(路径endsWith(“/”){
path+=“index.html”;
}
//获取扩展名及其对应的内容类型
字符串contentType=getContentType(路径);
串行打印(“内容类型”);
Serial.println(contentType);
File File=fs.open(path,“r”);//打开文件名为to path的文件,以便读取。有关其他模式,请参阅
size_t sent=server.streamFile(file,contentType);//将文件从发送到服务器引用
file.close();//关闭文件
}
//此函数返回内容类型的字符串
字符串getContentType(字符串文件名){
if(server.hasArg(“download”){//检查参数“download”是否存在
返回“应用程序/八位字节流”;
}else if(filename.endsWith(“.htm”){//检查字符串文件名是否以“.htm”结尾
返回“text/html”;
}else if(filename.endsWith(“.html”)){
返回“text/html”;
}else if(filename.endsWith(“.css”)){
返回“text/css”;
}else if(filename.endsWith(“.js”)){
返回“application/javascript”;
}else if(filename.endsWith(“.png”)){
返回“image/png”;
}else if(filename.endsWith(“.gif”)){
返回“image/gif”;
}else if(filename.endsWith(“.jpg”)){
返回“图像/jpeg”;
}else if(filename.endsWith(“.ico”)){
返回“图像/x图标”;
}else if(filename.endsWith(“.xml”)){
返回“text/xml”;
}else if(filename.endsWith(“.pdf”)){
返回“应用程序/x-pdf”;
}else if(filename.endsWith(“.zip”)){
返回“应用程序/x-zip”;
}else if(filename.endsWith(“.gz”)){
返回“application/x-gzip”;
}
返回“文本/纯文本”;
}
void循环(){
/*等待服务器上的连接并负责
从客户端发送和接收服务器uri上的数据请求*/
server.handleClient();
}
/*将此html代码包含在SD卡上名为“index.html”的文件中!!!
*图像标签。
*将所有源标记更改为与路径相似。
*
*
温室
.中心{
保证金:自动;
文本对齐:居中;
}
.左{
浮动:左;
}
.对{
浮动:对;
}
.卡片{
边框样式:实心;
边框宽度:1px;
边框颜色:#F7DC6F;
填充物:5px;
保证金:2
Have resisted all my attempts to convert them.
The following is the full code and it works on a ESP32 Cam board:
```//Load necessaries libraries from https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/src/WiFi.h
//https://www.instructables.com/How-to-Serve-Html-Files-From-SD-Card-on-ESP32/
#include <WiFi.h> // Add Wifi Capabilities
#include <WebServer.h> //To make a simple webserver that accomodates only one simultaneous client
#include "FS.h" //To access the file system
#include "SD_MMC.h" //To access the SD Card
//replace with your network credentials
#ifndef STASSID
#define STASSID "replace_with_your_ssid"<br>#define STAPSK "replace_with_your_ssid_pass"<br>
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
WebServer server(80); //Set the server at port 80
void setup() {
Serial.begin(115200); //Set the baud rate for serial communication with ESP
/////////////////sdcard
if (!SD_MMC.begin()) { //initialize SD Card
Serial.println("SD Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType(); //get sd card type
if (cardType == CARD_NONE) { //check if the sd card presence
Serial.println("No SD Card attached");
return;
}
////////////////Wifi
WiFi.mode(WIFI_STA); //Connect it to a wifi Network
WiFi.begin(ssid, password); //with the ssid and password defined earlier
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {// Wait for connection
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //prints the local ip given to the esp by the router
////////////////Server
server.onNotFound(handleRoot); //Calls the function handleRoot regardless of the server uri ex.(192.168.100.110/edit server uri is "/edit")
server.begin();//starts the server
Serial.println("HTTP server started");
}
void handleRoot() {
/* SD_MMC pertains to the sd card "memory". It is save as a
variable at the same address given to fs in the fs library
with "FS" class to enable the file system wrapper to make
changes on the sd cards memory */
fs::FS &fs = SD_MMC;
String path = server.uri(); //saves the to a string server uri ex.(192.168.100.110/edit server uri is "/edit")
Serial.print("path "); Serial.println(path);
//To send the index.html when the serves uri is "/"
if (path.endsWith("/")) {
path += "index.html";
}
//gets the extension name and its corresponding content type
String contentType = getContentType(path);
Serial.print("contentType ");
Serial.println(contentType);
File file = fs.open(path, "r"); //Open the File with file name = to path with intention to read it. For other modes see <a href="https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html" style="font-size: 13.5px;"> https://arduino-esp8266.readthedocs.io/en/latest/...</a>
size_t sent = server.streamFile(file, contentType); //sends the file to the server references from <a href="https://github.com/espressif/arduino-esp32/blob/master/libraries/WebServer/src/WebServer.h" style="font-size: 13.5px;"> https://arduino-esp8266.readthedocs.io/en/latest/...</a>
file.close(); //Close the file
}
//This functions returns a String of content type
String getContentType(String filename) {
if (server.hasArg("download")) { // check if the parameter "download" exists
return "application/octet-stream";
} else if (filename.endsWith(".htm")) { //check if the string filename ends with ".htm"
return "text/html";
} else if (filename.endsWith(".html")) {
return "text/html";
} else if (filename.endsWith(".css")) {
return "text/css";
} else if (filename.endsWith(".js")) {
return "application/javascript";
} else if (filename.endsWith(".png")) {
return "image/png";
} else if (filename.endsWith(".gif")) {
return "image/gif";
} else if (filename.endsWith(".jpg")) {
return "image/jpeg";
} else if (filename.endsWith(".ico")) {
return "image/x-icon";
} else if (filename.endsWith(".xml")) {
return "text/xml";
} else if (filename.endsWith(".pdf")) {
return "application/x-pdf";
} else if (filename.endsWith(".zip")) {
return "application/x-zip";
} else if (filename.endsWith(".gz")) {
return "application/x-gzip";
}
return "text/plain";
}
void loop() {
/*Waits For connection on the server and is responsible for
sending and receivingdata request on server uri from the client*/
server.handleClient();
}
/*INCLUDE THIS html code IN A FILE NAMED "index.html" ON THE SD CARD!!!
* The image tag <img> source (src) should be "/" + path.
* For example, to show the image named "plants.jpg":
* <img class="picture" style="max-width:100%;" src="/plants.jpg">.
* Change all source tags to be similar to your path.
*
*
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Greenhouse</title>
<meta name="description" content="Greenhouse">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.center {
margin: auto;
text-align: center;
}
.left {
float: left;
}
.right {
float: right;
}
.card {
border-style: solid;
border-width: 1px;
border-color: #F7DC6F;
padding: 5px;
margin: 20px;
}
.container {
position: relative;
}
.top-left {
position: absolute;
top: 8px;
left: 16px;
text-align: left;
}
.hide {
position: absolute;
opacity: 0;
}
input[type=checkbox]+label {
color: rgb(129, 129, 129);
font-style: italic;
padding: 5px;
}
input[type=checkbox]:checked+label {
color: #000000;
font-style: normal;
padding: 5px;
}
</style>
</head>
<body class="center" onload="onload()" style="font-family: Verdana, Geneva, Tahoma, sans-serif;">
<h1 class="center">Pulilan Greenhouse</h1>
<div class=card style="border-color: #ffffff;">
<img class="picture" id="slideshow" src="/picture/1.jpg" style="max-width:100%;">
<p> </p>
<button class="left" id="prev">❮ Prev</button>
<button class="right" id="next">❯ Next</button>
<button href="/picture/1.jpg" download id="download">Download</button>
<button id="take_photo">Take Photo</button>
</div>
<div class=card>
<div style="background-color:#F7DC6F;">
STATUS
</div>
<div class="container">
<img class="picture" style="max-width:100%;" src="/plants.jpg">
<div class="top-left">
<p id="temp"> Temp: 0 ℃ </p>
<p id="RH"> RH: 0 % </p>
</div>
</div>
</div>
<div class=card>
<div style="background-color:#F7DC6F;">
CONTROL
</div>
<table style="width: 100%;">
<tbody>
<tr>
<td>
<input class="hide" type="checkbox" onchange="relay(this.id)" id="1">
<label for="1">Relay 1</label>
</td>
<td>
<input class="hide" type="checkbox" onchange="relay(this.id)" id="2">
<label for="2">Relay 2</label>
</td>
<td>
<input class="hide" type="checkbox" onchange="relay(this.id)" id="3">
<label for="3">Relay 3</label>
</td>
</tr>
<tr>
<td>
<input class="hide" type="checkbox" onchange="relay(this.id)" id="4">
<label for="4">Relay 4</label>
</td>
<td>
<input class="hide" type="checkbox" onchange="relay(this.id)" id="5">
<label for="5">Relay 5</label>
</td>
<td>
<input class="hide" type="checkbox" onchange="relay(this.id)" id="6">
<label for="6">Relay 6</label>
</td>
</tr>
</tbody>
</table>
</div>
<div class=card>
<div style="background-color:#F7DC6F;text-align: center;">
SCHEDULE
</div>
<div class="container" style="text-align: left;">
Time: <input type="time"><br>
Duration: <input type="number" style=" width:50px;"> mins <br>
Days: <span class="days">
<input class="hide" onchange="day_selection(this.id)" id="sun" name="sun" type="checkbox" value="0">
<label for="sun">S</label>
<input class="hide" onchange="day_selection(this.id)" id="mon" type="checkbox" value="0">
<label for="mon">M</label>
<input class="hide" onchange="day_selection(this.id)" id="tue" type="checkbox" value="0">
<label for="tue">T</label>
<input class="hide" onchange="day_selection(this.id)" id="wed" type="checkbox" value="0">
<label for="wed">W</label>
<input class="hide" onchange="day_selection(this.id)" id="thur" type="checkbox" value="0">
<label for="thur">T</label>
<input class="hide" onchange="day_selection(this.id)" id="fri" type="checkbox" value="0">
<label for="fri">F</label>
<input class="hide" onchange="day_selection(this.id)" id="sat" type="checkbox" value="0">
<label for="sat">S</label>
</span>
</div>
</div>
<script>
function onload() {
console.log("loaded");}
var picture = 1;
var next = document.getElementById("next");
next.addEventListener("click", function () {
picture += 1;
console.log("picture", picture);
document.getElementById("slideshow").src = "/picture/" + picture + ".jpg";
});
var download = document.getElementById("download");
download.addEventListener("click", function () {
console.log("downlaod", document.getElementById("slideshow").src);
window.open(document.getElementById("slideshow").src)
});
var prev = document.getElementById("prev");
prev.addEventListener("click", function () {
picture -= 1;
console.log("picture", picture);
document.getElementById("slideshow").src = "/picture/" + picture + ".jpg";;
});
var take_photo = document.getElementById("take_photo");
take_photo.addEventListener("click",function(){
});
function relay(id) {
console.log(id, document.getElementById(id).checked);
}
function day_selection(id) {
console.log("id", id, document.getElementById(id).value);
}
</script>
</body>
</html>
*/
```