TolentinoCotesta

esp-fs-webserver-task

Oct 15th, 2021 (edited)
149
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <esp-fs-webserver.h>  // https://github.com/cotestatnt/esp-fs-webserver
  2. #include "FS.h"
  3. #include <LittleFS.h>   // Inclusa nel core ESP32 per Arduino nelle ultime versioni
  4.  
  5. #ifndef LED_BUILTIN
  6. #define LED_BUILTIN 2
  7. #endif
  8.  
  9. // In order to set SSID and password open the /setup webserver page
  10. // const char* ssid
  11. // const char* password;
  12.  
  13. // Il led built-in di solito si accende con l'uscita su LOW
  14. uint8_t ledPin = LED_BUILTIN;  
  15.  
  16. bool apMode = false;
  17. String hostname = "fsbrowser";
  18. IPAddress myIP;
  19.  
  20. WebServer server(80);
  21. FSWebServer myWebServer(LittleFS, server);
  22.  
  23. ////////////////////////////////  WiFi  /////////////////////////////////////////
  24. IPAddress startWiFi(){
  25.   //IPAddress myIP;
  26.   Serial.printf("Connecting to %s\n", WiFi.SSID().c_str());
  27.   WiFi.mode(WIFI_STA);
  28.   WiFi.begin();
  29.   // WiFi.begin(ssid, password);
  30.   uint32_t startTime = millis();
  31.   while (WiFi.status() != WL_CONNECTED ){
  32.     delay(500);
  33.     Serial.print(".");
  34.  
  35.     // If no connection (or specifically activated) go in Access Point mode
  36.     if( millis() - startTime > 10000 || apMode ) {
  37.       WiFi.mode(WIFI_AP);
  38.       WiFi.softAP("ESP_AP", "123456789");
  39.       myIP = WiFi.softAPIP();
  40.       Serial.print(F("\nAP mode.\nServer IP address: "));
  41.       Serial.println(myIP);
  42.       break;
  43.     }
  44.   }
  45.  
  46.   if(WiFi.status() == WL_CONNECTED) {
  47.     myIP = WiFi.localIP();
  48.     Serial.print(F("\nConnected! IP address: "));
  49.     Serial.println(myIP);
  50.  
  51.     // Set hostname
  52.     WiFi.setHostname(hostname.c_str());
  53.  
  54.     // Start MDNS responder
  55.     if (MDNS.begin(hostname.c_str())) {
  56.       Serial.println(F("MDNS responder started."));
  57.       Serial.printf("You should be able to connect with address\t http://%s.local/\n", hostname.c_str());
  58.       // Add service to MDNS-SD
  59.       MDNS.addService("http", "tcp", 80);
  60.     }
  61.   }
  62.   return myIP;
  63. }
  64.  
  65. ////////////////////////////////  Filesystem  /////////////////////////////////////////
  66. void startFilesystem(){
  67.    if(!LittleFS.begin(true)){
  68.       Serial.println("LittleFS Mount Failed");
  69.       return;
  70.    }
  71.        
  72.   File root = LittleFS.open("/", "r");
  73.   File file = root.openNextFile();
  74.   while (file){
  75.     const char* fileName = file.name();
  76.     size_t fileSize = file.size();
  77.     Serial.printf("FS File: %s, size: %lu\n", fileName, (long unsigned)fileSize);
  78.     file = root.openNextFile();
  79.   }
  80.   Serial.println();
  81. }
  82.  
  83.  
  84. void handleLed() {
  85.   // If new led state is specified - http://xxx.xxx.xxx.xxx/led?val=1
  86.   if(myWebServer.webserver->hasArg("val")) {
  87.     int value = myWebServer.webserver->arg("val").toInt();
  88.     digitalWrite(ledPin, value);
  89.   }
  90.   // else simple toggle the actual state
  91.   else {
  92.     digitalWrite(ledPin, !digitalRead(ledPin));
  93.   }
  94.   String reply = "LED is now ";
  95.   reply += digitalRead(ledPin) ? "OFF" : "ON";
  96.   myWebServer.webserver->send(200, "text/plain", reply);
  97. }
  98.  
  99.  
  100. void webServerTask(void * parameter){    
  101.  
  102.   Serial.print("webServerTask() running on core ");
  103.   Serial.println(xPortGetCoreID());
  104.  
  105.   // Add custom page handlers to webserver
  106.   myWebServer.addHandler("/led", HTTP_GET, handleLed);
  107.  
  108.   // Start webserver
  109.   if (myWebServer.begin()) {
  110.     Serial.print("ESP Web Server started on IP Address ");
  111.     Serial.println(WiFi.localIP());
  112.     Serial.println("Open /setup page to configure optional parameters");
  113.     Serial.println("Open /edit page to view and edit files");
  114.   }
  115.  
  116.   // Run webserver forever
  117.   for(;;) {
  118.     myWebServer.run();
  119.   }
  120.  
  121.   // When you're done, call vTaskDelete. Don't forget this!
  122.   vTaskDelete(NULL);
  123. }
  124.  
  125.  
  126. void setup(){
  127.   Serial.begin(115200);
  128.   pinMode(LED_BUILTIN, OUTPUT);
  129.  
  130.   // FILESYSTEM INIT
  131.   startFilesystem();
  132.  
  133.   // WiFi INIT
  134.   myIP = startWiFi();
  135.  
  136.   // Start webserver task
  137.   xTaskCreatePinnedToCore(
  138.     webServerTask,   // Function that should be called
  139.     "Web Server",    // Name of the task (for debugging)
  140.     8192,            // Stack size (bytes)
  141.     NULL,            // Parameter to pass
  142.     1,               // Task priority
  143.     NULL,            // Task handle
  144.     1                // Core number
  145.   );
  146.  
  147.   Serial.print("setup() running on core ");
  148.   Serial.println(xPortGetCoreID());
  149. }
  150.  
  151.  
  152. void loop() {
  153.  
  154. }
RAW Paste Data