Advertisement
Guest User

SPIFFS Stops

a guest
Nov 12th, 2016
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.36 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266mDNS.h>
  3. #include <ArduinoOTA.h>
  4. #include <FS.h>
  5. #include <Hash.h>
  6. #include <ESPAsyncTCP.h>
  7. #include <ESPAsyncWebServer.h>
  8. #include <SPIFFSEditor.h>
  9.  
  10. // SKETCH BEGIN
  11. AsyncWebServer server(80);
  12. AsyncWebSocket ws("/ws");
  13. AsyncEventSource events("/events");
  14.  
  15. void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
  16.   if(type == WS_EVT_CONNECT){
  17.     Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
  18.     client->printf("Hello Client %u :)", client->id());
  19.     client->ping();
  20.   } else if(type == WS_EVT_DISCONNECT){
  21.     Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
  22.   } else if(type == WS_EVT_ERROR){
  23.     Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
  24.   } else if(type == WS_EVT_PONG){
  25.     Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
  26.   } else if(type == WS_EVT_DATA){
  27.     AwsFrameInfo * info = (AwsFrameInfo*)arg;
  28.     String msg = "";
  29.     if(info->final && info->index == 0 && info->len == len){
  30.       //the whole message is in a single frame and we got all of it's data
  31.       Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len);
  32.  
  33.       if(info->opcode == WS_TEXT){
  34.         for(size_t i=0; i < info->len; i++) {
  35.           msg += (char) data[i];
  36.  
  37.         }
  38.       } else {
  39.         char buff[3];
  40.         for(size_t i=0; i < info->len; i++) {
  41.           sprintf(buff, "%02x ", (uint8_t) data[i]);
  42.           msg += buff ;
  43.         }
  44.       }
  45.  
  46.  
  47.       if(msg=="RESET"){
  48.         ESP.reset();
  49.         ESP.restart();
  50.       }
  51.  
  52.       if(msg=="HEAP"){
  53.         client->text((String)ESP.getFreeHeap());
  54.       }
  55.  
  56.       if(msg=="CLOSE"){
  57.         ws.closeAll();
  58.       }
  59.       if(msg=="fsinfo()"){
  60.         fsinfo();
  61.         client->text("-----------------------------------------------");
  62.         client->text("Flash Real Size: " + (String) ESP.getFlashChipRealSize());
  63.         client->text("Flash Firmware Configured Size: " + (String) ESP.getFlashChipSize());
  64.  
  65.         FSInfo fsInfo;
  66.         SPIFFS.info(fsInfo);
  67.         client->text("FS Bytes: " + (String) fsInfo.usedBytes + " / " + (String) fsInfo.totalBytes);
  68.         uint8_t used = fsInfo.usedBytes;
  69.         uint8_t total = fsInfo.totalBytes;
  70.         uint8_t percent = (used/total);
  71.         client->text("FS % Used: " + (String)(percent));
  72.  
  73.       }
  74.  
  75.  
  76.  
  77.  
  78.       Serial.printf("%s\n",msg.c_str());
  79.  
  80.       if(info->opcode == WS_TEXT)
  81.           int null;
  82.       else
  83.  
  84.         client->binary("I got your binary message");
  85.     } else {
  86.       //message is comprised of multiple frames or the frame is split into multiple packets
  87.       if(info->index == 0){
  88.         if(info->num == 0)
  89.           Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
  90.         Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len);
  91.       }
  92.  
  93.       Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len);
  94.  
  95.       if(info->opcode == WS_TEXT){
  96.         for(size_t i=0; i < info->len; i++) {
  97.           msg += (char) data[i];
  98.         }
  99.       } else {
  100.         char buff[3];
  101.         for(size_t i=0; i < info->len; i++) {
  102.           sprintf(buff, "%02x ", (uint8_t) data[i]);
  103.           msg += buff ;
  104.         }
  105.       }
  106.       Serial.printf("%s\n",msg.c_str());
  107.  
  108.       if((info->index + len) == info->len){
  109.         Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len);
  110.         if(info->final){
  111.           Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
  112.           if(info->message_opcode == WS_TEXT)
  113.             client->text("I got your text message");
  114.           else
  115.             client->binary("I got your binary message");
  116.         }
  117.       }
  118.     }
  119.   }
  120. }
  121.  
  122.  
  123. const char* ssid = "***";
  124. const char* password = "****";
  125. const char * hostName = "esp8266";
  126. const char* http_username = "admin";
  127. const char* http_password = "admin";
  128.  
  129.  
  130.  
  131. void setup(){
  132.   Serial.begin(115200);
  133.   Serial.setDebugOutput(true);
  134.   WiFi.hostname(hostName);
  135.   WiFi.mode(WIFI_AP_STA);
  136.   WiFi.softAP(hostName);
  137.   WiFi.begin(ssid, password);
  138.   if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  139.     Serial.printf("STA: Failed!\n");
  140.     WiFi.disconnect(false);
  141.     delay(1000);
  142.     WiFi.begin(ssid, password);
  143.   }
  144.  
  145.   //Send OTA events to the browser
  146.   ArduinoOTA.onStart([]() { events.send("Update Start", "ota"); });
  147.   ArduinoOTA.onEnd([]() { events.send("Update End", "ota"); });
  148.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  149.     char p[32];
  150.     sprintf(p, "Progress: %u%%\n", (progress/(total/100)));
  151.     events.send(p, "ota");
  152.   });
  153.   ArduinoOTA.onError([](ota_error_t error) {
  154.     if(error == OTA_AUTH_ERROR) events.send("Auth Failed", "ota");
  155.     else if(error == OTA_BEGIN_ERROR) events.send("Begin Failed", "ota");
  156.     else if(error == OTA_CONNECT_ERROR) events.send("Connect Failed", "ota");
  157.     else if(error == OTA_RECEIVE_ERROR) events.send("Recieve Failed", "ota");
  158.     else if(error == OTA_END_ERROR) events.send("End Failed", "ota");
  159.   });
  160.   ArduinoOTA.setHostname(hostName);
  161.   ArduinoOTA.begin();
  162.  
  163.   MDNS.addService("http","tcp",80);
  164.  
  165.   SPIFFS.begin();
  166.  
  167.   ws.onEvent(onWsEvent);
  168.   server.addHandler(&ws);
  169.  
  170.   events.onConnect([](AsyncEventSourceClient *client){
  171.     client->send("hello!",NULL,millis(),1000);
  172.   });
  173.   server.addHandler(&events);
  174.  
  175.   server.addHandler(new SPIFFSEditor(http_username,http_password));
  176.  
  177.   server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
  178.     request->send(200, "text/plain", String(ESP.getFreeHeap()));
  179.   });
  180.  
  181.  
  182.  
  183.   server.on("/scan", HTTP_GET, [](AsyncWebServerRequest *request){
  184.   String json = "[";
  185.   int n = WiFi.scanComplete();
  186.   if(n == -2){
  187.     WiFi.scanNetworks(true);
  188.   } else if(n){
  189.     for (int i = 0; i < n; ++i){
  190.       if(i) json += ",";
  191.       json += "{";
  192.       json += "\"rssi\":"+String(WiFi.RSSI(i));
  193.       json += ",\"ssid\":\""+WiFi.SSID(i)+"\"";
  194.       json += ",\"bssid\":\""+WiFi.BSSIDstr(i)+"\"";
  195.       json += ",\"channel\":"+String(WiFi.channel(i));
  196.       json += ",\"secure\":"+String(WiFi.encryptionType(i));
  197.       json += ",\"hidden\":"+String(WiFi.isHidden(i)?"true":"false");
  198.       json += "}";
  199.     }
  200.     WiFi.scanDelete();
  201.     if(WiFi.scanComplete() == -2){
  202.       WiFi.scanNetworks(true);
  203.     }
  204.   }
  205.   json += "]";
  206.   request->send(200, "text/json", json);
  207.   json = String();
  208. });
  209.  
  210.   server.on("/reset", HTTP_GET, [](AsyncWebServerRequest *request){
  211.     ESP.reset();
  212.     ESP.restart();
  213.   });
  214.   server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
  215.   server.serveStatic("/counter", SPIFFS, "/").setDefaultFile("counter.txt");
  216.  
  217.   server.onNotFound([](AsyncWebServerRequest *request){
  218.     Serial.printf("NOT_FOUND: ");
  219.     if(request->method() == HTTP_GET)
  220.       Serial.printf("GET");
  221.     else if(request->method() == HTTP_POST)
  222.       Serial.printf("POST");
  223.     else if(request->method() == HTTP_DELETE)
  224.       Serial.printf("DELETE");
  225.     else if(request->method() == HTTP_PUT)
  226.       Serial.printf("PUT");
  227.     else if(request->method() == HTTP_PATCH)
  228.       Serial.printf("PATCH");
  229.     else if(request->method() == HTTP_HEAD)
  230.       Serial.printf("HEAD");
  231.     else if(request->method() == HTTP_OPTIONS)
  232.       Serial.printf("OPTIONS");
  233.     else
  234.       Serial.printf("UNKNOWN");
  235.     Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
  236.  
  237.     if(request->contentLength()){
  238.       Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str());
  239.       Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength());
  240.     }
  241.  
  242.     int headers = request->headers();
  243.     int i;
  244.     for(i=0;i<headers;i++){
  245.       AsyncWebHeader* h = request->getHeader(i);
  246.       Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str());
  247.     }
  248.  
  249.     int params = request->params();
  250.     for(i=0;i<params;i++){
  251.       AsyncWebParameter* p = request->getParam(i);
  252.       if(p->isFile()){
  253.         Serial.printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
  254.       } else if(p->isPost()){
  255.         Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
  256.       } else {
  257.         Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
  258.       }
  259.     }
  260.  
  261.     request->send(404);
  262.   });
  263.   server.onFileUpload([](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
  264.     if(!index)
  265.       Serial.printf("UploadStart: %s\n", filename.c_str());
  266.     Serial.printf("%s", (const char*)data);
  267.     if(final)
  268.       Serial.printf("UploadEnd: %s (%u)\n", filename.c_str(), index+len);
  269.   });
  270.   server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){
  271.     if(!index)
  272.       Serial.printf("BodyStart: %u\n", total);
  273.     Serial.printf("%s", (const char*)data);
  274.     if(index + len == total)
  275.       Serial.printf("BodyEnd: %u\n", total);
  276.   });
  277.   server.begin();
  278.  
  279. fsinfo();
  280. }
  281.  
  282. void fsinfo(){
  283.   File f = SPIFFS.open("/FSINFO.txt", "a+");
  284.   f.println("-----------------------------------------------");
  285.   f.print("Flash Real Size: ");
  286.   f.println(ESP.getFlashChipRealSize());
  287.   f.print("Flash Firmware Configured Size: ");
  288.   f.println(ESP.getFlashChipSize());
  289.  
  290.   FSInfo fsInfo;
  291.   SPIFFS.info(fsInfo);
  292.   f.print("FS Bytes: ");
  293.   f.print(fsInfo.usedBytes);
  294.   f.print(" / ");
  295.   f.println(fsInfo.totalBytes);
  296.   f.print("FS % Used: ");
  297.   uint8_t used = fsInfo.usedBytes;
  298.   uint8_t total = fsInfo.totalBytes;
  299.   f.println(used/total);
  300.   f.close();
  301. }
  302.  
  303.  
  304.  
  305.  
  306. long OLD = millis();
  307. long NEW;
  308.  
  309.  
  310. void loop(){
  311.   ArduinoOTA.handle();
  312.  
  313.   NEW = millis();
  314.   if(NEW - OLD > 1000){
  315.   File f = SPIFFS.open("/counter.txt", "a+");
  316.   if (!f) {
  317.       Serial.println("file open failed");
  318.   }
  319.   f.print((String)random(0,100));
  320.   f.print(" ");
  321.   f.println(round(NEW/1000));
  322.   //f.print("File Size: ");
  323.   //f.print(f.size());
  324.   //f.println("");
  325.   f.close();
  326.   OLD = NEW;
  327.  
  328. }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement