zoukankan      html  css  js  c++  java
  • ESP8266 使用 SPIFFS 进行文件的上传与保存

    首先需要下载包。网址https://github.com/esp8266/arduino-esp8266fs-plugin/releases  我下载的是最新的包。

    下载下来之后是个jar包,需要放到arduino根目录的tools文件夹中。

    不要放错位置。放错位置的话,你的arduino IDE是无法在工具栏看到这个的,切记。我一开始就是放错了位置,结果找不到

    然后呢,你需要在项目里创建一个data文件夹。然后将需要上传到falsh中的文件放到这个目录中。然后点击上面Data Upload就可以把这些文件上传到falsh中了

    注意:上传项目编译文件是编译文件,上传data目录文件是目录文件。两码事,千万不要混为一谈

    附代码

    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    #include <ESP8266mDNS.h>
    #include <FS.h>
    ESP8266WebServer server(80);
    void setup() {
      Serial.begin(115200);
      // put your setup code here, to run once:
      WiFi.begin("kangtine","87602261");
      SPIFFS.begin();//这个地方要开启
      while(WiFi.status()!=WL_CONNECTED){
          delay(500);
          Serial.println(".");
        }
              Serial.print("dns 配置中");
        if(WiFi.status() == WL_CONNECTED) //If WiFi connected to hot spot then start mDNS
      {
        if (MDNS.begin("lsq")) {  //Start mDNS with name esp8266
          Serial.println("MDNS started");
        }
      }
        Serial.print("Wi-Fi connected,IP:");
        Serial.println(WiFi.localIP());
        server.on("/",[](){
            server.send(200,"text/html","hello from <b>ESP8266</b>.");
          });
       server.on("/index.htm",rootRouter);
         server.onNotFound([](){
            server.send(404,"text/plain","File Not found!");
          });
        server.begin();
    
        MDNS.addService("http","tcp",80);
        Serial.println("HTTP server started.");
    
        int n = MDNS.queryService("http","tcp");
        if(n>0){
            for(int i=0;i<n;i++){
                Serial.print(i+1);
                Serial.print(MDNS.hostname(i));
                Serial.print(MDNS.IP(i));
                Serial.print(MDNS.port(i));
              }
          }else{
              Serial.print("no service found");
            }
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
        MDNS.update();
      server.handleClient();
    }
    
    void rootRouter(){
        File file=SPIFFS.open("/index.htm","r");//以只读模式打开index.htm,流模式为text/html。然后关闭该文件
        server.streamFile(file,"text/html");
        file.close();
      }

    至此就可以了。记得把index.htm放到data文件夹中,然后点击Data Upload上传

    注意,如果你的index.htm里边引用了别的文件或者图片。那必须在server.on()中设置

    例如,如果你的index.htm中引用了某个图片。

    server.on("/img/aaa.png",imgRouter);

    void imgRouter(){

      

     File file=SPIFFS.open("/img/aaa.png","r");//以只读模式打开index.htm,流模式为text/html。然后关闭该文件
        server.streamFile(file,"image/png");
        file.close();

    }

    这样来操作才可以。相当的麻烦好像。。。不过貌似有好的办法。。。

    接下来让我们认识SPIFFS文件系统

     

     Dir  目录对象

    用法

    next 下一个文件   fileName 读取文件名  openFile 打开文件

    Dir dir=SPIFFS.openDir("/data");

    while(dir.next)){

      Serial.print(dir.fileName());

      File f = dir.openFile("r");

      Serial.println(f.size());

    }

    File (文件)对象

     SPIFFS.open 和 dir.openFile 都会返回File对象,他支持stream的所有函数。你可以使用readBytes findUntil   parseInt  println及其他所有stream方法

      返回目前文件的位置,单位是byte

  • 相关阅读:
    【販売管理】「クレジットメモとデビットメモ」
    COALESCE [NULL でない最初の式を返す」
    【EXCEL】CONCAT(複数の文字列の連結)
    文字列内の検索 FIND
    テーブルコントロールTable Controls: スクロールを伴う場合の例
    SAP ABAP プログラム 選択画面定義 基本命令
    【転載】表示レイアウト実装方法
    php在linux中执行外部命令
    openldap+php-ldap操作
    MAC Ruby版本需要升级至2.2.2以上
  • 原文地址:https://www.cnblogs.com/Lonelychampion/p/12230368.html
Copyright © 2011-2022 走看看