当前位置:网站首页>通过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";
}以上为一个示例代码段
边栏推荐
- 医院无线网络系统设计
- C byte array and class mutual conversion
- About rapidssl certificate
- 面试了二三十家公司所总结的问题,Android面试吃完这一套没有拿不到的Offer......
- SCM learning notes 6 -- interrupt system (based on Baiwen STM32F103 series tutorials)
- Hcip day 9 notes
- php7 垃圾回收机制详解
- jmeter+influxdb+grafana压测实时监控平台搭建
- Database design
- Study and use of burpsuite plug-in
猜你喜欢

医院综合布线

php7 垃圾回收机制详解

代码阅读方法与最佳实践

What is the Gantt chart function of Zen

OSPF (fifth day notes)

SCM learning notes 9 -- common communication methods (based on Baiwen STM32F103 series tutorials)

選址與路徑規劃問題(Lingo,Matlab實現)

How to synchronize MySQL database when easycvr platform is upgraded to the latest version v2.5.0?

Hcip third day notes

Introduction to digital signature technology
随机推荐
Try to run this command from the system terminal Make sure that you use the correct
Notes - record the solution to the failure of @refreshscope dynamic refresh configuration
SCM learning notes 5--stm32 clock system (based on Baiwen STM32F103 series tutorials)
数字签名技术简介
Jenkins multitask concurrent construction
Structure the second operation of the actual combat battalion module
Introduction to digital signature technology
How to solve the problem that the universal vision NVR device is connected to the easycvr platform and cannot be online after offline?
Ora-12899 error caused by nchar character
Summary of HCIA knowledge points
Hcip day 6 notes
Arm architecture and programming 3 -- key control LED (based on Baiwen arm architecture and programming tutorial video)
OSPF (sixth day notes)
After the interview with 20 or 30 companies, there is no offer that you can't get after the Android interview
CANopen communication - PDO and SDO
Cartland number---
Webshell management tool and its traffic characteristics analysis
Research on retinal vascular segmentation based on GAN using few samples
Jenkins multitâche construction simultanée
SCM learning notes 4--gpio (based on Baiwen STM32F103 series tutorials)