Advertisement
Guest User

main.cpp

a guest
Nov 4th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. #include <SPIFFS.h>
  4. #include <WebServer.h>
  5. #include <WiFi.h>
  6.  
  7. // Class call of the webserver for server calls.
  8. WebServer _server;
  9.  
  10. static char *_net_ssid;
  11. static char *_net_pass;
  12.  
  13. // Initializes the WiFi with the default settings.
  14. // The values are set in the system environment.
  15. void init_wifi()
  16. {
  17.     // If another library already established the connection
  18.     // the function should not go further.
  19.     if (WiFi.status() == WL_CONNECTED)
  20.         return;
  21.  
  22.     // Start the network in WiFi mode.
  23.     WiFi.begin(_net_ssid, _net_pass);
  24.  
  25.     // Retry count, and if it reaches 20 with the set SSID and
  26.     // password it will make a soft AP as a local network.
  27.     int retries = 0;
  28.  
  29.     // Loop to check the connection with the WiFi setup.
  30.     while (WiFi.status() != WL_CONNECTED)
  31.     {
  32.         // 1 second delay between checks.
  33.         delay(1000);
  34.  
  35.         // Printing if the loop is still looking for connection.
  36.         Serial.println("Connecting to WiFi...");
  37.  
  38.         // Adds to the retries.
  39.         retries++;
  40.  
  41.         // Check if the retries reached the 20 limit in the loop.
  42.         if (retries >= 20)
  43.         {
  44.             // Soft AP network information.
  45.             IPAddress local_ip(10, 0, 0, 1);
  46.             IPAddress gateway(10, 0, 0, 254);
  47.             IPAddress subnet(255, 255, 255, 0);
  48.  
  49.             // Start the soft AP with the current network information.
  50.             WiFi.softAP(_net_ssid, _net_pass);
  51.             WiFi.softAPConfig(local_ip, gateway, subnet);
  52.  
  53.             // Print that the soft AP started instead of the WiFI.
  54.             Serial.println("Failed connecting to WiFi. Soft AP started.");
  55.  
  56.             // Break the loop.
  57.             break;
  58.         }
  59.     }
  60.  
  61.     Serial.print("IP: ");                // Start of the IP print
  62.     if (WiFi.status() == WL_CONNECTED)   // Check if there is WiFi connection...
  63.         Serial.println(WiFi.localIP());  // Print the WiFi local IP.
  64.     else                                 // If there is no connection...
  65.         Serial.println(WiFi.softAPIP()); // Print the soft AP local IP.
  66. }
  67.  
  68. // Initializes the file-system (SPIFFS).
  69. void init_files()
  70. {
  71.     // Start the SPIFFS if no other library or process started it yet.
  72.     if (!SPIFFS.begin())
  73.         return;
  74. }
  75.  
  76. void add_file_to_page(File html_file, const char *path_to_file)
  77. {
  78.     File file_to_add = SPIFFS.open(path_to_file, FILE_READ);
  79.     if (!file_to_add)
  80.         return;
  81.  
  82.     String s;
  83.     while (file_to_add.available())
  84.     {
  85.         char file_read = file_to_add.read();
  86.         s += file_read;
  87.     }
  88.     html_file.print(s);
  89.  
  90.     file_to_add.close();
  91. }
  92.  
  93. void make_head(File html_file)
  94. {
  95.     html_file.print("<head>");
  96.     html_file.print("<style>");
  97.     add_file_to_page(html_file, "style.css");
  98.     html_file.print("</style>");
  99.     html_file.print("</head>");
  100. }
  101.  
  102. // Creates the body of the page.
  103. void make_body(File html_file)
  104. {
  105.     html_file.print("<body>");
  106.     html_file.print("<h1>Hello, World!</h1>");
  107.     html_file.print("<form>");
  108.  
  109.     // Input for the SSID.
  110.     html_file.print("<label for=' _net_ssid '>SSID:</label>");
  111.     html_file.print("<input type=' text ' id=' _net_ssid ' name=' _net_ssid '><br><br>");
  112.  
  113.     // Input for the password.
  114.     html_file.print("<label for=' _net_pass '>Password:</label>");
  115.     html_file.print("<input type=' text ' id=' _net_pass ' name=' _net_pass '><br><br>");
  116.  
  117.     // A submit button that would apply the values into the variables.
  118.     html_file.print("<input type=' submit ' value=' Submit '>");
  119.  
  120.     html_file.print("</form>");
  121.     html_file.print("</body>");
  122. }
  123.  
  124. // Creates the site for the webserver, for the network manager.
  125. void make_manager_page(File html_file)
  126. {
  127.     // ? Should I call the fs init here?
  128.     init_files();
  129.  
  130.     html_file.print("<!DOCTYPE html>"); // Init HTML.
  131.     make_head(html_file);               // Head = style (CSS)
  132.     make_body(html_file);               // Body = form/input
  133.  
  134.     Serial.println("Page created.");
  135. }
  136.  
  137. // This will be called when somebody connects to the server.
  138. void handle_connect()
  139. {
  140.     Serial.println("Client connected to server."); // Output upon connecting.
  141.  
  142.     File html_write = SPIFFS.open("/index.html", FILE_WRITE);
  143.  
  144.     if (!html_write) // Checks if the file creation was successful.
  145.         return;      // Return, if it failed.
  146.  
  147.     // Debug line to determine if the function successfully ran.
  148.     Serial.println("Successfully opened index page.");
  149.  
  150.     // Make the manager page to `index.html.`
  151.     make_manager_page(html_write);
  152.  
  153.     // Close the file after use.
  154.     html_write.close();
  155.  
  156.     // Opens the HTML for reading.
  157.     File html_read = SPIFFS.open("/index.html", FILE_READ);
  158.  
  159.     if (!html_read) // Check if file exist.
  160.         return;     // If it doesn't, return.
  161.  
  162.     // Stream the created file
  163.     _server.streamFile(html_read, "text/html");
  164.  
  165.     // Close the read file.
  166.     html_read.close();
  167. }
  168.  
  169. // Initializes the webserver with the server calls.
  170. void init_webserver()
  171. {
  172.     // Sets up a server call where the user can call the
  173.     // network info manager page generation.
  174.     _server.on("/", handle_connect); // ! Change this to "/manage" after testing.
  175.  
  176.     // Attempts to create the webserver, if no other module did.
  177.     _server.begin();
  178. }
  179.  
  180. // Initializes sub-modules.
  181. void setup()
  182. {
  183.     init_wifi();
  184.     init_files();
  185.     init_webserver();
  186. }
  187.  
  188. // Handles the process on every tick of the loop.
  189. void loop()
  190. {
  191.     // Constantly handles the client.
  192.     _server.handleClient();
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement