鱼C论坛

 找回密码
 立即注册
查看: 2971|回复: 0

[学习笔记] ESP8266:向闪存系统上传文件

[复制链接]
发表于 2021-9-3 14:25:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 划句顾 于 2021-9-4 09:41 编辑


                               
登录/注册后可看大图


在上传代码之前,要先将文件下载到esp8266中,再上传代码,不然会因为找不到资源而报错。
下载资源的方法.jpg


                               
登录/注册后可看大图


代码:
  1. /****
  2. * author:LaoGu
  3. * time  :2021/9/3
  4. * purpose;将data里面的文件下载到esp8266里面后,通过8266的网址,查看网页
  5. */
  6. #include<ESP8266WiFi.h>
  7. #include<ESP8266WiFiMulti.h>
  8. #include<ESP8266WebServer.h>
  9. #include<FS.h>

  10. ESP8266WiFiMulti wifiMulti;

  11. ESP8266WebServer esp8266_server(80);

  12. void setup(){
  13.   Serial.begin(9600);
  14.   Serial.println("");
  15.   
  16.   wifiMulti.addAP("小G的wife","g5201314");  //这里是你自己的WiFi和密码
  17.   wifiMulti.addAP("nanjishit","g5201314");

  18.   Serial.println("Connecting...");

  19.   int i = 0;
  20.   while(wifiMulti.run()!=WL_CONNECTED){
  21.     delay(1000);
  22.     Serial.print(i++); Serial.print(" ");
  23.     }

  24.   Serial.println("\n");
  25.   Serial.print("Connect to ");
  26.   Serial.println(WiFi.SSID());
  27.   Serial.print("IP address: ");
  28.   Serial.println(WiFi.localIP());

  29.   if(SPIFFS.begin()){
  30.     Serial.println("SPIFFS Started.");
  31.     }else{
  32.       Serial.println("SPIFFS Failed to start.");
  33.       }
  34.       
  35.   esp8266_server.begin();   //启动网络服务器
  36.   
  37.   esp8266_server.onNotFound(handleUserRequest);
  38.   
  39.   Serial.println("HTTP server started");
  40.   
  41.   
  42. }
  43. void loop(){
  44.   esp8266_server.handleClient();  //处理用户请求
  45. }

  46. void handleUserRequest(){
  47.   //获取用户请求网址信息
  48.   String webAddress = esp8266_server.uri();

  49.   //通过handleFileRead函数处理用户访问
  50.   bool fileReadOK = handleFileRead(webAddress);

  51.   //如果在SPIFFS无法找到用户访问的资源,则回复404
  52.   if(!fileReadOK){
  53.     esp8266_server.send(404,"text/plain","404: Not Found");
  54.   }
  55. }

  56. bool handleFileRead(String path){
  57.   
  58.   if(path.endsWith("/")){    //如果访问地址以"/"结尾                          
  59.     path = "/index.html";    //则将访问地址修改为/index.html,便于SPIFFS访问
  60.     }
  61.    
  62.    String contentType = getContentType(path);  //获取文件类型
  63.    
  64.    if(SPIFFS.exists(path)){
  65.     File file = SPIFFS.open(path,"r");
  66.     esp8266_server.streamFile(file,contentType);  //并且将该文件返回浏览器
  67.     file.close();
  68.     return true;  
  69.     }
  70.     return false;
  71.   }

  72. String getContentType(String filename){
  73.   if(filename.endsWith(".htm")) return "text/html";
  74.   else if(filename.endsWith(".html")) return "text/html";
  75.   else if(filename.endsWith(".css"))  return "text/css";
  76.   else if(filename.endsWith(".js")) return "application/javascript";
  77.   else if(filename.endsWith(".png")) return "image/png";
  78.   else if(filename.endsWith(".gif")) return "image/gif";
  79.   else if(filename.endsWith(".jpg")) return "image/jpeg";
  80.   else if(filename.endsWith(".ico")) return "image/x-icon";
  81.   else if(filename.endsWith(".xml")) return "text/xml";
  82.   else if(filename.endsWith(".pdf")) return "application/x-pdf";
  83.   else if(filename.endsWith(".zip")) return "application/x-zip";
  84.   else if(filename.endsWith(".gz"))  return "application/x-gzip";
  85.   return "text/plain";
  86.    }
复制代码



                               
登录/注册后可看大图


串口:
串口照片.jpg


                               
登录/注册后可看大图


复制网址:192.168.43.109 到和esp8266相同局域网中的手机或者电脑的浏览器 打开


                               
登录/注册后可看大图


效果如下:
楠楠.jpg


                               
登录/注册后可看大图

注:图片等资源必须用英文命名,用中文到时候浏览器显示不出来。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 00:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表