Advertisement
pleasedontcode

**Web Server** rev_04

Oct 14th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Web Server**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-14 21:00:53
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* integrate the two codes adjusting everything with */
  21.     /* ESPAsyncWebServer and SPIFFS. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <WiFi.h>
  28. #include <ESPAsyncWebServer.h>
  29. #include <SPIFFS.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. // Create an instance of the server
  36. AsyncWebServer server(80);
  37.  
  38. // WiFi credentials
  39. const char* ssid = "YOUR_SSID";
  40. const char* password = "YOUR_PASSWORD";
  41.  
  42. void setup(void)
  43. {
  44.     // Start Serial communication for debugging
  45.     Serial.begin(115200);
  46.  
  47.     // Initialize SPIFFS
  48.     if (!SPIFFS.begin(true)) {
  49.         Serial.println("SPIFFS Mount Failed");
  50.         return;
  51.     }
  52.  
  53.     // Connect to Wi-Fi
  54.     WiFi.begin(ssid, password);
  55.     while (WiFi.status() != WL_CONNECTED) {
  56.         delay(1000);
  57.         Serial.println("Connecting to WiFi...");
  58.     }
  59.     Serial.println("Connected to WiFi");
  60.  
  61.     // Serve the index.html file from SPIFFS
  62.     server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  63.         request->send(SPIFFS, "/index.htm", "text/html");
  64.     });
  65.  
  66.     // Handle 404 Not Found
  67.     server.onNotFound([](AsyncWebServerRequest *request){
  68.         request->send(404, "text/plain", "Not found");
  69.     });
  70.  
  71.     // Start the server
  72.     server.begin();
  73. }
  74.  
  75. void loop(void)
  76. {
  77.     // Main loop does nothing, all handling is done in callbacks
  78. }
  79.  
  80. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement