当前位置:网站首页>通过Arduino IDE向闪存文件系统上传文件
通过Arduino IDE向闪存文件系统上传文件
2022-07-24 01:47:00 【挖煤的小李同学】
下载 Arduino-ESP8266闪存文件插件程序
通过点击此链接进入 Arduino-ESP8266官方GitHub页面下载。详情请见以下截图:

esp8266fs下载页面
具体操作看3-3-2 通过Arduino IDE向闪存文件系统上传文件 – 太极创客
完成后打开3-3-3 使用闪存文件系统建立功能更加丰富的网络服务器 – 太极创客
下载示例程序
/**********************************************************************
项目名称/Project : 零基础入门学用物联网
程序名称/Program name : 3_4_1_SPIFFS_File_server
团队/Team : 太极创客团队 / Taichi-Maker (www.taichi-maker.com)
作者/Author : CYNO朔
日期/Date(YYYYMMDD) : 20191109
程序目的/Purpose :
当用户访问NodeMCU地址时,NodeMCU将会检查访问地址是否指向SPIFFS系统中的文件,并且
将该文件显示于用户的浏览器中。如果访问地址所指向的文件无法在SPIFFS中找到,NodeMCU将会
向用户发送404信息。
-----------------------------------------------------------------------
修订历史/Revision History
日期/Date 作者/Author 参考号/Ref 修订说明/Revision Description
20200211 CYNO朔 0.01 修改了handleNotFound函数使其更直观
-----------------------------------------------------------------------
本示例程序为太极创客团队制作的《零基础入门学用物联网》中示例程序。
该教程为对物联网开发感兴趣的朋友所设计和制作。如需了解更多该教程的信息,请参考以下网页:
http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/
***********************************************************************/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <FS.h>
ESP8266WiFiMulti wifiMulti; // 建立ESP8266WiFiMulti对象
ESP8266WebServer esp8266_server(80); // 建立网络服务器对象,该对象用于响应HTTP请求。监听端口(80)
void setup() {
Serial.begin(9600); // 启动串口通讯
Serial.println("");
wifiMulti.addAP("CMCC-7093", "19881377093"); // 将需要连接的一系列WiFi ID和密码输入这里
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2"); // ESP8266-NodeMCU再启动后会扫描当前网络
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3"); // 环境查找是否有这里列出的WiFi ID。如果有
Serial.println("Connecting ..."); // 则尝试使用此处存储的密码进行连接。
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // 尝试进行wifi连接。
delay(1000);
Serial.print(i++); Serial.print(' ');
}
// WiFi连接成功后将通过串口监视器输出连接成功信息
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // 通过串口监视器输出连接的WiFi名称
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // 通过串口监视器输出ESP8266-NodeMCU的IP
if(SPIFFS.begin()){ // 启动闪存文件系统
Serial.println("SPIFFS Started.");
} else {
Serial.println("SPIFFS Failed to Start.");
}
esp8266_server.onNotFound(handleUserRequet); // 告知系统如何处理用户请求
esp8266_server.begin(); // 启动网站服务
Serial.println("HTTP server started");
}
void loop(void) {
esp8266_server.handleClient(); // 处理用户请求
}
// 处理用户浏览器的HTTP访问
void handleUserRequet() {
// 获取用户请求网址信息
String webAddress = esp8266_server.uri();
// 通过handleFileRead函数处处理用户访问
bool fileReadOK = handleFileRead(webAddress);
// 如果在SPIFFS无法找到用户访问的资源,则回复404 (Not Found)
if (!fileReadOK){
esp8266_server.send(404, "text/plain", "404 Not Found");
}
}
bool handleFileRead(String path) { //处理浏览器HTTP访问
if (path.endsWith("/")) { // 如果访问地址以"/"为结尾
path = "/index.html"; // 则将访问地址修改为/index.html便于SPIFFS访问
}
String contentType = getContentType(path); // 获取文件类型
if (SPIFFS.exists(path)) { // 如果访问的文件可以在SPIFFS中找到
File file = SPIFFS.open(path, "r"); // 则尝试打开该文件
esp8266_server.streamFile(file, contentType);// 并且将该文件返回给浏览器
file.close(); // 并且关闭文件
return true; // 返回true
}
return false; // 如果文件未找到,则返回false
}
// 获取文件类型
String getContentType(String filename){
if(filename.endsWith(".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";
}以上为一个示例代码段
边栏推荐
- Network type (notes on the third day)
- jmeter+influxdb+grafana压测实时监控平台搭建
- Hcip day 10 notes
- Rip (notes of the second day)
- Number of combinations....
- Database paradigm and schema decomposition
- 暑假第三周
- [code case] website confession wall & to do list (including complete source code)
- SCM learning notes 4--gpio (based on Baiwen STM32F103 series tutorials)
- Draw a two coordinate diagram with MATLAB (the simplest in the whole network)
猜你喜欢

Hcip network type, PPP session, data link layer protocol

Detailed explanation of php7 garbage collection mechanism

Arm architecture and programming 4 -- serial port (based on Baiwen arm architecture and programming tutorial video)

基于强化空间注意力的视网膜网络(ESA-Unet)

20220723 record an unexplained shutdown of SAP Oracle monitoring service

How to install, download and use the latest version of IDM software

Hcip day 11 notes

Structure the second operation of the actual combat battalion module

Exchange 2010通配符SSL证书安装文档

1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
随机推荐
Cartland number---
jenkins多任务并发构建
Notes - record a dynamic datasource please check the setting of primary problem solving
How to finally generate a file from saveastextfile in spark
Hcip day 6 notes
CANopen communication - PDO and SDO
Vessel Segmentation in Retinal Image Based on Retina-GAN
Hcip experiment
Introduction to digital signature technology
Detailed explanation of php7 garbage collection mechanism
Copying readable paths is not easy
How to synchronize MySQL database when easycvr platform is upgraded to the latest version v2.5.0?
jmeter+influxdb+grafana压测实时监控平台搭建
Customer first | domestic Bi leader, smart software completes round C financing
面试了二三十家公司所总结的问题,Android面试吃完这一套没有拿不到的Offer......
Perlin noise and random terrain
Rip --- routing information protocol
代码阅读方法与最佳实践
Mysql database authorization learning
Hospital network security architecture