Advertisement
balatech

LittleFS testcode

Feb 11th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.89 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include <ArduinoJson.h>
  3. #include <WiFi.h>
  4. #include <ESPmDNS.h>
  5. #include <AsyncTCP.h>
  6. #include <ESPAsyncWebServer.h>
  7. #include <PubSubClient.h>
  8. /* You only need to format LittleFS the first time you run a
  9.    test or else use the LITTLEFS plugin to create a partition
  10.    https://github.com/lorol/arduino-esp32littlefs-plugin */
  11. WiFiClient wifiClient;
  12. PubSubClient client(wifiClient);
  13. uint8_t pin_led = 27;
  14. StaticJsonDocument<200> doc;
  15. StaticJsonDocument<300> mqtt;
  16. IPAddress APaddr(192,168,4,1);
  17. IPAddress gateway(192,168,4,1);
  18. IPAddress netmask(255,255,255,0);
  19. String hostname = "ESP32RF433";
  20. const char* APssid = "Balatech_AP";
  21. const char* APpassword = "password";
  22. String networksJson;
  23. bool connectedToServer = false;
  24. #include "fileFunctions.h"  
  25. #include "wifiFunctions.h"
  26. #define FORMAT_LITTLEFS_IF_FAILED true
  27.  
  28. AsyncWebServer server(80);
  29.  
  30.  
  31.  
  32. void setup(){
  33.     Serial.begin(115200);
  34.     pinMode(pin_led,OUTPUT);
  35.     if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)){
  36.         Serial.println("LittleFS Mount Failed");
  37.         return;
  38.     }    
  39.     Serial.println( "Test complete" );
  40.     autoConnectWiFi();
  41.     if(!connectedToServer){
  42.         if (!MQTTConnect()){
  43.             Serial.println("MQTT Connection failed!");
  44.             return;
  45.         }          
  46.     }
  47.     else
  48.     {
  49.         Serial.println("MQTT Connected!");
  50.        
  51.     }
  52.    
  53.    
  54.  /*    
  55.  * Server should respond to these routes
  56.  *
  57.  *  if not connected to station send the settings page to client
  58.  */
  59.  
  60.     server.rewrite("/", "/settings.html").setFilter(ON_AP_FILTER);
  61.  
  62.     server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  63.     request->send(LittleFS, "/home.html", "text/html");});
  64.  
  65.     server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *request){
  66.         request->send(LittleFS, "/settings.html", "text/html",false,sendJson);
  67.     });  
  68.     server.on("/getNetworkList", HTTP_GET, [](AsyncWebServerRequest *request){
  69.         request->send(200,"text/plain", networksJson);
  70.     });
  71.    
  72.    server.on("/saveWiFiCredentials", HTTP_POST, wifiSave);
  73.    
  74.    server.on("/saveMQTTCredentials", HTTP_POST, saveMQTT);
  75.   // server.on("/reset", HTTP_POST, resetAll);
  76.    
  77.    server.on("/devices", HTTP_GET, [](AsyncWebServerRequest *request){
  78.     request->send(LittleFS, "/listdevices.html", "text/html");});
  79.  
  80.    server.on("/details", HTTP_GET, [](AsyncWebServerRequest *request){
  81.     request->send(LittleFS, "/details.html", "text/html",false, readSettings);});
  82.    
  83.    server.on("/delSW", HTTP_GET, [](AsyncWebServerRequest *request){
  84.     request->send(LittleFS, "/delSW.html", "text/html");});
  85.    
  86.    server.on("/edit", HTTP_GET, [](AsyncWebServerRequest *request){
  87.     request->send(LittleFS, "/addSW.html", "text/html");});
  88.  
  89.   server.serveStatic("/", LittleFS, "/"); /* this servers the css files and favicon file automatically */
  90.   server.begin();
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97. void loop(){
  98. }
Tags: ESP32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement