PAGE_NetworkConfiguration.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // HTML PAGE
  3. //
  4. const char PAGE_NetworkConfiguration[] PROGMEM = R"=====(
  5. <meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <a href="admin.html" class="btn btn--s"><</a>&nbsp;&nbsp;<strong>Network Configuration</strong>
  8. <hr>
  9. Connect to Router with these settings:<br>
  10. <form action="" method="get">
  11. <table border="0" cellspacing="0" cellpadding="3" style="width:310px" >
  12. <tr><td align="right">SSID:</td><td><input type="text" id="ssid" name="ssid" value=""></td></tr>
  13. <tr><td align="right">Password:</td><td><input type="text" id="password" name="password" value=""></td></tr>
  14. <tr><td align="right">DHCP:</td><td><input type="checkbox" id="dhcp" name="dhcp"></td></tr>
  15. <tr><td align="right">IP: </td><td><input type="text" id="ip_0" name="ip_0" size="3">.<input type="text" id="ip_1" name="ip_1" size="3">.<input type="text" id="ip_2" name="ip_2" size="3">.<input type="text" id="ip_3" name="ip_3" value="" size="3"></td></tr>
  16. <tr><td align="right">Netmask:</td><td><input type="text" id="nm_0" name="nm_0" size="3">.<input type="text" id="nm_1" name="nm_1" size="3">.<input type="text" id="nm_2" name="nm_2" size="3">.<input type="text" id="nm_3" name="nm_3" size="3"></td></tr>
  17. <tr><td align="right">Gateway:</td><td><input type="text" id="gw_0" name="gw_0" size="3">.<input type="text" id="gw_1" name="gw_1" size="3">.<input type="text" id="gw_2" name="gw_2" size="3">.<input type="text" id="gw_3" name="gw_3" size="3"></td></tr>
  18. <tr><td colspan="2" align="center"><input type="submit" style="width:150px" class="btn btn--m btn--blue" value="Save"></td></tr>
  19. </table>
  20. </form>
  21. <hr>
  22. <strong>Connection State:</strong><div id="connectionstate">N/A</div>
  23. <hr>
  24. <strong>Networks:</strong><br>
  25. <table border="0" cellspacing="3" style="width:310px" >
  26. <tr><td><div id="networks">Scanning...</div></td></tr>
  27. <tr><td align="center"><a href="javascript:GetState()" style="width:150px" class="btn btn--m btn--blue">Refresh</a></td></tr>
  28. </table>
  29. <script>
  30. function GetState()
  31. {
  32. setValues("/admin/connectionstate");
  33. }
  34. function selssid(value)
  35. {
  36. document.getElementById("ssid").value = value;
  37. }
  38. window.onload = function ()
  39. {
  40. load("style.css","css", function()
  41. {
  42. load("microajax.js","js", function()
  43. {
  44. setValues("/admin/values");
  45. setTimeout(GetState,3000);
  46. });
  47. });
  48. }
  49. function load(e,t,n){if("js"==t){var a=document.createElement("script");a.src=e,a.type="text/javascript",a.async=!1,a.onload=function(){n()},document.getElementsByTagName("head")[0].appendChild(a)}else if("css"==t){var a=document.createElement("link");a.href=e,a.rel="stylesheet",a.type="text/css",a.async=!1,a.onload=function(){n()},document.getElementsByTagName("head")[0].appendChild(a)}}
  50. </script>
  51. )=====";
  52. const char PAGE_WaitAndReload[] PROGMEM = R"=====(
  53. <meta http-equiv="refresh" content="5; URL=config.html">
  54. Please Wait....Configuring and Restarting.
  55. )=====";
  56. //
  57. // SEND HTML PAGE OR IF A FORM SUMBITTED VALUES, PROCESS THESE VALUES
  58. //
  59. void send_network_configuration_html()
  60. {
  61. digitalWrite(Blue_Led, LOW);
  62. if (server.args() > 0 ) // Save Settings
  63. {
  64. String temp = "";
  65. config.dhcp = false;
  66. for ( uint8_t i = 0; i < server.args(); i++ ) {
  67. if (server.argName(i) == "ssid") config.ssid = urldecode(server.arg(i));
  68. if (server.argName(i) == "password") config.password = urldecode(server.arg(i));
  69. if (server.argName(i) == "ip_0") if (checkRange(server.arg(i))) config.IP[0] = server.arg(i).toInt();
  70. if (server.argName(i) == "ip_1") if (checkRange(server.arg(i))) config.IP[1] = server.arg(i).toInt();
  71. if (server.argName(i) == "ip_2") if (checkRange(server.arg(i))) config.IP[2] = server.arg(i).toInt();
  72. if (server.argName(i) == "ip_3") if (checkRange(server.arg(i))) config.IP[3] = server.arg(i).toInt();
  73. if (server.argName(i) == "nm_0") if (checkRange(server.arg(i))) config.Netmask[0] = server.arg(i).toInt();
  74. if (server.argName(i) == "nm_1") if (checkRange(server.arg(i))) config.Netmask[1] = server.arg(i).toInt();
  75. if (server.argName(i) == "nm_2") if (checkRange(server.arg(i))) config.Netmask[2] = server.arg(i).toInt();
  76. if (server.argName(i) == "nm_3") if (checkRange(server.arg(i))) config.Netmask[3] = server.arg(i).toInt();
  77. if (server.argName(i) == "gw_0") if (checkRange(server.arg(i))) config.Gateway[0] = server.arg(i).toInt();
  78. if (server.argName(i) == "gw_1") if (checkRange(server.arg(i))) config.Gateway[1] = server.arg(i).toInt();
  79. if (server.argName(i) == "gw_2") if (checkRange(server.arg(i))) config.Gateway[2] = server.arg(i).toInt();
  80. if (server.argName(i) == "gw_3") if (checkRange(server.arg(i))) config.Gateway[3] = server.arg(i).toInt();
  81. if (server.argName(i) == "dhcp") config.dhcp = true;
  82. }
  83. server.send ( 200, "text/html", PAGE_WaitAndReload );
  84. WriteConfig();
  85. ConfigureWifi();
  86. AdminTimeOutCounter=0;
  87. }
  88. else
  89. {
  90. server.send ( 200, "text/html", PAGE_NetworkConfiguration );
  91. }
  92. Serial.println(__FUNCTION__);
  93. digitalWrite(Blue_Led, HIGH);
  94. }
  95. //
  96. // FILL THE PAGE WITH VALUES
  97. //
  98. void send_network_configuration_values_html()
  99. {
  100. digitalWrite(Blue_Led, LOW);
  101. String values ="";
  102. values += "ssid|" + (String) config.ssid + "|input\n";
  103. values += "password|" + (String) config.password + "|input\n";
  104. values += "ip_0|" + (String) config.IP[0] + "|input\n";
  105. values += "ip_1|" + (String) config.IP[1] + "|input\n";
  106. values += "ip_2|" + (String) config.IP[2] + "|input\n";
  107. values += "ip_3|" + (String) config.IP[3] + "|input\n";
  108. values += "nm_0|" + (String) config.Netmask[0] + "|input\n";
  109. values += "nm_1|" + (String) config.Netmask[1] + "|input\n";
  110. values += "nm_2|" + (String) config.Netmask[2] + "|input\n";
  111. values += "nm_3|" + (String) config.Netmask[3] + "|input\n";
  112. values += "gw_0|" + (String) config.Gateway[0] + "|input\n";
  113. values += "gw_1|" + (String) config.Gateway[1] + "|input\n";
  114. values += "gw_2|" + (String) config.Gateway[2] + "|input\n";
  115. values += "gw_3|" + (String) config.Gateway[3] + "|input\n";
  116. values += "dhcp|" + (String) (config.dhcp ? "checked" : "") + "|chk\n";
  117. server.send ( 200, "text/plain", values);
  118. Serial.println(__FUNCTION__);
  119. digitalWrite(Blue_Led, HIGH);
  120. }
  121. //
  122. // FILL THE PAGE WITH NETWORKSTATE & NETWORKS
  123. //
  124. void send_connection_state_values_html()
  125. {
  126. digitalWrite(Blue_Led, LOW);
  127. String state = "N/A";
  128. String Networks = "";
  129. if (WiFi.status() == 0) state = "Idle";
  130. else if (WiFi.status() == 1) state = "NO SSID AVAILBLE";
  131. else if (WiFi.status() == 2) state = "SCAN COMPLETED";
  132. else if (WiFi.status() == 3) state = "CONNECTED";
  133. else if (WiFi.status() == 4) state = "CONNECT FAILED";
  134. else if (WiFi.status() == 5) state = "CONNECTION LOST";
  135. else if (WiFi.status() == 6) state = "DISCONNECTED";
  136. int n = WiFi.scanNetworks();
  137. if (n == 0)
  138. {
  139. Networks = "<font color='#FF0000'>No networks found!</font>";
  140. }
  141. else
  142. {
  143. Networks = "Found " +String(n) + " Networks<br>";
  144. Networks += "<table border='0' cellspacing='0' cellpadding='3'>";
  145. Networks += "<tr bgcolor='#DDDDDD' ><td><strong>Name</strong></td><td><strong>Quality</strong></td><td><strong>Enc</strong></td><tr>";
  146. for (int i = 0; i < n; ++i)
  147. {
  148. int quality=0;
  149. if(WiFi.RSSI(i) <= -100)
  150. {
  151. quality = 0;
  152. }
  153. else if(WiFi.RSSI(i) >= -50)
  154. {
  155. quality = 100;
  156. }
  157. else
  158. {
  159. quality = 2 * (WiFi.RSSI(i) + 100);
  160. }
  161. Networks += "<tr><td><a href='javascript:selssid(\"" + String(WiFi.SSID(i)) + "\")'>" + String(WiFi.SSID(i)) + "</a></td><td>" + String(quality) + "%</td><td>" + String((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*") + "</td></tr>";
  162. }
  163. Networks += "</table>";
  164. }
  165. String values ="";
  166. values += "connectionstate|" + state + "|div\n";
  167. values += "networks|" + Networks + "|div\n";
  168. server.send ( 200, "text/plain", values);
  169. Serial.println(__FUNCTION__);
  170. digitalWrite(Blue_Led, HIGH);
  171. }