You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.7 KiB

  1. #include "Updater.h"
  2. #include <WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <WebServer.h>
  5. #include <ESPmDNS.h>
  6. #include <Update.h>
  7. void handleSetup(void);
  8. /** Function to check if the user wants to update the device.
  9. * This is signaled by pressing all C Buttons + Start
  10. * if so update process is initiated
  11. * @param controller pointer to struct containing presed buttons
  12. * @return none
  13. */
  14. void checkUpdateCombo(ControllerData *controller)
  15. {
  16. // if button combi is pressed
  17. if(controller->CUp && controller->CDown && controller->CLeft && controller->CRight && controller->buttonStart)
  18. {
  19. handleSetup();
  20. }
  21. }
  22. // ===== TAKEN FROM ARDUINO, slight modifications =====
  23. const char* host = "n64toique";
  24. WebServer server(80);
  25. /*
  26. * Login page
  27. */
  28. const char* loginIndex =
  29. "<form name='loginForm'>"
  30. "<table width='20%' bgcolor='A09F9F' align='center'>"
  31. "<tr>"
  32. "<td colspan=2>"
  33. "<center><font size=4><b>ESP32 Login Page</b></font></center>"
  34. "<br>"
  35. "</td>"
  36. "<br>"
  37. "<br>"
  38. "</tr>"
  39. "<td>Username:</td>"
  40. "<td><input type='text' size=25 name='userid'><br></td>"
  41. "</tr>"
  42. "<br>"
  43. "<br>"
  44. "<tr>"
  45. "<td>Password:</td>"
  46. "<td><input type='Password' size=25 name='pwd'><br></td>"
  47. "<br>"
  48. "<br>"
  49. "</tr>"
  50. "<tr>"
  51. "<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
  52. "</tr>"
  53. "</table>"
  54. "</form>"
  55. "<script>"
  56. "function check(form)"
  57. "{"
  58. "if(form.userid.value=='n64toique' && form.pwd.value=='n64toique')"
  59. "{"
  60. "window.open('/serverIndex')"
  61. "}"
  62. "else"
  63. "{"
  64. " alert('Error Password or Username')/*displays error message*/"
  65. "}"
  66. "}"
  67. "</script>";
  68. /*
  69. * Server Index Page
  70. */
  71. const char* serverIndex =
  72. "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
  73. "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
  74. "<input type='file' name='update'>"
  75. "<input type='submit' value='Update'>"
  76. "</form>"
  77. "<div id='prg'>progress: 0%</div>"
  78. "<script>"
  79. "$('form').submit(function(e){"
  80. "e.preventDefault();"
  81. "var form = $('#upload_form')[0];"
  82. "var data = new FormData(form);"
  83. " $.ajax({"
  84. "url: '/update',"
  85. "type: 'POST',"
  86. "data: data,"
  87. "contentType: false,"
  88. "processData:false,"
  89. "xhr: function() {"
  90. "var xhr = new window.XMLHttpRequest();"
  91. "xhr.upload.addEventListener('progress', function(evt) {"
  92. "if (evt.lengthComputable) {"
  93. "var per = evt.loaded / evt.total;"
  94. "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
  95. "}"
  96. "}, false);"
  97. "return xhr;"
  98. "},"
  99. "success:function(d, s) {"
  100. "console.log('success!')"
  101. "},"
  102. "error: function (a, b, c) {"
  103. "}"
  104. "});"
  105. "});"
  106. "</script>";
  107. /*
  108. * setup function
  109. */
  110. void handleSetup(void) {
  111. //Serial.begin(115200);
  112. // Connect to WiFi network
  113. WiFi.beginSmartConfig();
  114. Serial.println("");
  115. // Wait for connection
  116. while (WiFi.status() != WL_CONNECTED) {
  117. delay(500);
  118. Serial.print(".");
  119. }
  120. /*use mdns for host name resolution*/
  121. if (!MDNS.begin(host)) { //http://esp32.local
  122. Serial.println("Error setting up MDNS responder!");
  123. while (1) {
  124. delay(1000);
  125. }
  126. }
  127. Serial.println("mDNS responder started");
  128. /*return index page which is stored in serverIndex */
  129. server.on("/", HTTP_GET, []() {
  130. server.sendHeader("Connection", "close");
  131. server.send(200, "text/html", loginIndex);
  132. });
  133. server.on("/serverIndex", HTTP_GET, []() {
  134. server.sendHeader("Connection", "close");
  135. server.send(200, "text/html", serverIndex);
  136. });
  137. /*handling uploading firmware file */
  138. server.on("/update", HTTP_POST, []() {
  139. server.sendHeader("Connection", "close");
  140. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  141. ESP.restart();
  142. }, []() {
  143. HTTPUpload& upload = server.upload();
  144. if (upload.status == UPLOAD_FILE_START) {
  145. Serial.printf("Update: %s\n", upload.filename.c_str());
  146. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  147. Update.printError(Serial);
  148. }
  149. } else if (upload.status == UPLOAD_FILE_WRITE) {
  150. /* flashing firmware to ESP*/
  151. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  152. Update.printError(Serial);
  153. }
  154. } else if (upload.status == UPLOAD_FILE_END) {
  155. if (Update.end(true)) { //true to set the size to the current progress
  156. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  157. } else {
  158. Update.printError(Serial);
  159. }
  160. }
  161. });
  162. server.begin();
  163. while(true)
  164. {
  165. delay(50);
  166. server.handleClient();
  167. }
  168. }