Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <SPIFFS.h>
- #include <WebServer.h>
- #include <WiFi.h>
- // Class call of the webserver for server calls.
- WebServer _server;
- static char *_net_ssid;
- static char *_net_pass;
- // Initializes the WiFi with the default settings.
- // The values are set in the system environment.
- void init_wifi()
- {
- // If another library already established the connection
- // the function should not go further.
- if (WiFi.status() == WL_CONNECTED)
- return;
- // Start the network in WiFi mode.
- WiFi.begin(_net_ssid, _net_pass);
- // Retry count, and if it reaches 20 with the set SSID and
- // password it will make a soft AP as a local network.
- int retries = 0;
- // Loop to check the connection with the WiFi setup.
- while (WiFi.status() != WL_CONNECTED)
- {
- // 1 second delay between checks.
- delay(1000);
- // Printing if the loop is still looking for connection.
- Serial.println("Connecting to WiFi...");
- // Adds to the retries.
- retries++;
- // Check if the retries reached the 20 limit in the loop.
- if (retries >= 20)
- {
- // Soft AP network information.
- IPAddress local_ip(10, 0, 0, 1);
- IPAddress gateway(10, 0, 0, 254);
- IPAddress subnet(255, 255, 255, 0);
- // Start the soft AP with the current network information.
- WiFi.softAP(_net_ssid, _net_pass);
- WiFi.softAPConfig(local_ip, gateway, subnet);
- // Print that the soft AP started instead of the WiFI.
- Serial.println("Failed connecting to WiFi. Soft AP started.");
- // Break the loop.
- break;
- }
- }
- Serial.print("IP: "); // Start of the IP print
- if (WiFi.status() == WL_CONNECTED) // Check if there is WiFi connection...
- Serial.println(WiFi.localIP()); // Print the WiFi local IP.
- else // If there is no connection...
- Serial.println(WiFi.softAPIP()); // Print the soft AP local IP.
- }
- // Initializes the file-system (SPIFFS).
- void init_files()
- {
- // Start the SPIFFS if no other library or process started it yet.
- if (!SPIFFS.begin())
- return;
- }
- void add_file_to_page(File html_file, const char *path_to_file)
- {
- File file_to_add = SPIFFS.open(path_to_file, FILE_READ);
- if (!file_to_add)
- return;
- String s;
- while (file_to_add.available())
- {
- char file_read = file_to_add.read();
- s += file_read;
- }
- html_file.print(s);
- file_to_add.close();
- }
- void make_head(File html_file)
- {
- html_file.print("<head>");
- html_file.print("<style>");
- add_file_to_page(html_file, "style.css");
- html_file.print("</style>");
- html_file.print("</head>");
- }
- // Creates the body of the page.
- void make_body(File html_file)
- {
- html_file.print("<body>");
- html_file.print("<h1>Hello, World!</h1>");
- html_file.print("<form>");
- // Input for the SSID.
- html_file.print("<label for=' _net_ssid '>SSID:</label>");
- html_file.print("<input type=' text ' id=' _net_ssid ' name=' _net_ssid '><br><br>");
- // Input for the password.
- html_file.print("<label for=' _net_pass '>Password:</label>");
- html_file.print("<input type=' text ' id=' _net_pass ' name=' _net_pass '><br><br>");
- // A submit button that would apply the values into the variables.
- html_file.print("<input type=' submit ' value=' Submit '>");
- html_file.print("</form>");
- html_file.print("</body>");
- }
- // Creates the site for the webserver, for the network manager.
- void make_manager_page(File html_file)
- {
- // ? Should I call the fs init here?
- init_files();
- html_file.print("<!DOCTYPE html>"); // Init HTML.
- make_head(html_file); // Head = style (CSS)
- make_body(html_file); // Body = form/input
- Serial.println("Page created.");
- }
- // This will be called when somebody connects to the server.
- void handle_connect()
- {
- Serial.println("Client connected to server."); // Output upon connecting.
- File html_write = SPIFFS.open("/index.html", FILE_WRITE);
- if (!html_write) // Checks if the file creation was successful.
- return; // Return, if it failed.
- // Debug line to determine if the function successfully ran.
- Serial.println("Successfully opened index page.");
- // Make the manager page to `index.html.`
- make_manager_page(html_write);
- // Close the file after use.
- html_write.close();
- // Opens the HTML for reading.
- File html_read = SPIFFS.open("/index.html", FILE_READ);
- if (!html_read) // Check if file exist.
- return; // If it doesn't, return.
- // Stream the created file
- _server.streamFile(html_read, "text/html");
- // Close the read file.
- html_read.close();
- }
- // Initializes the webserver with the server calls.
- void init_webserver()
- {
- // Sets up a server call where the user can call the
- // network info manager page generation.
- _server.on("/", handle_connect); // ! Change this to "/manage" after testing.
- // Attempts to create the webserver, if no other module did.
- _server.begin();
- }
- // Initializes sub-modules.
- void setup()
- {
- init_wifi();
- init_files();
- init_webserver();
- }
- // Handles the process on every tick of the loop.
- void loop()
- {
- // Constantly handles the client.
- _server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement