Web.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef Web_h
  2. #define Web_h
  3. /*
  4. Code taken from espressif ESP32 OTA Update example
  5. */
  6. #include <WiFi.h>
  7. #include <WiFiClient.h>
  8. #include <WebServer.h>
  9. #include <ESPmDNS.h>
  10. #include <Update.h>
  11. #include "Assets.h"
  12. #include "Display.h"
  13. #include "WiFiScan.h"
  14. extern Display display_obj;
  15. extern WiFiScan wifi_scan_obj;
  16. class Web
  17. {
  18. private:
  19. PROGMEM const char* host = "esp32marauder";
  20. PROGMEM const char* ssid = "MarauderOTA";
  21. PROGMEM const char* password = "justcallmekoko";
  22. bool serving = false;
  23. int num_sta = 0;
  24. PROGMEM const char* loginIndex =
  25. "<form name='loginForm'>"
  26. "<table width='20%' bgcolor='A09F9F' align='center'>"
  27. "<tr>"
  28. "<td colspan=2>"
  29. "<center><font size=4><b>ESP32 Login Page</b></font></center>"
  30. "<br>"
  31. "</td>"
  32. "<br>"
  33. "<br>"
  34. "</tr>"
  35. "<td>Username:</td>"
  36. "<td><input type='text' size=25 name='userid'><br></td>"
  37. "</tr>"
  38. "<br>"
  39. "<br>"
  40. "<tr>"
  41. "<td>Password:</td>"
  42. "<td><input type='Password' size=25 name='pwd'><br></td>"
  43. "<br>"
  44. "<br>"
  45. "</tr>"
  46. "<tr>"
  47. "<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
  48. "</tr>"
  49. "</table>"
  50. "</form>"
  51. "<script>"
  52. "function check(form)"
  53. "{"
  54. "if(form.userid.value=='admin' && form.pwd.value=='admin')"
  55. "{"
  56. "window.open('/serverIndex')"
  57. "}"
  58. "else"
  59. "{"
  60. " alert('Error Password or Username')/*displays error message*/"
  61. "}"
  62. "}"
  63. "</script>";
  64. /*
  65. * Server Index Page
  66. */
  67. PROGMEM const char* serverIndex =
  68. "<script src='/jquery.min.js'></script>"
  69. "Because the lack of an asynchronous webserver in this Arduino sketch like 'ESPAsyncWebServer', <br/>"
  70. "both file 'serverIndex' and 'jquery.min.js' can't be read from the webserver at the same time. <br/><br/>"
  71. "Your web browser probably requests those two files simultaneously and therefore <br/>"
  72. "the javascript file failed to load. By a refresh of this page, the browser cash has already <br/>"
  73. "load 'serverIndex' file, the web browser will do a second attempt to only read the javascript file. <br/>"
  74. "This second attempt, with an idle webserver, will be processed.<br/><br/>"
  75. "Long story short, press F5 (refresh web browser) before uploading your firmware. <br/><br/>"
  76. "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
  77. "<input type='file' name='update'>"
  78. "<input type='submit' value='Update'>"
  79. "</form>"
  80. "<div id='prg'>progress: 0%</div>"
  81. "<script>"
  82. "$('form').submit(function(e){"
  83. "e.preventDefault();"
  84. "var form = $('#upload_form')[0];"
  85. "var data = new FormData(form);"
  86. " $.ajax({"
  87. "url: '/update',"
  88. "type: 'POST',"
  89. "data: data,"
  90. "contentType: false,"
  91. "processData:false,"
  92. "xhr: function() {"
  93. "var xhr = new window.XMLHttpRequest();"
  94. "xhr.upload.addEventListener('progress', function(evt) {"
  95. "if (evt.lengthComputable) {"
  96. "var per = evt.loaded / evt.total;"
  97. "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
  98. "}"
  99. "}, false);"
  100. "return xhr;"
  101. "},"
  102. "success:function(d, s) {"
  103. "console.log('success!')"
  104. "},"
  105. "error: function (a, b, c) {"
  106. "}"
  107. "});"
  108. "});"
  109. "</script>";
  110. public:
  111. Web();
  112. void main();
  113. PROGMEM static void onJavaScript();
  114. void setupOTAupdate();
  115. void shutdownServer();
  116. };
  117. #endif