WS_WIFI.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "WS_WIFI.h"
  2. // The name and password of the WiFi access point
  3. const char *ssid = APSSID;
  4. const char *password = APPSK;
  5. // IPAddress apIP(192, 168, 4, 1); // Set the IP address of the AP
  6. char ipStr[16];
  7. WebServer server(80);
  8. void handleRoot() {
  9. String myhtmlPage =
  10. String("") +
  11. "<html>"+
  12. "<head>"+
  13. " <meta charset=\"utf-8\">"+
  14. " <title>ESP32 S3 Relay 6CH</title>"+
  15. " <style>" +
  16. " body {" +
  17. " font-family: Arial, sans-serif;" +
  18. " background-color: #f0f0f0;" +
  19. " margin: 0;" +
  20. " padding: 0;" +
  21. " }" +
  22. " .header {" +
  23. " text-align: center;" +
  24. " padding: 20px 0;" +
  25. " background-color: #333;" +
  26. " color: #fff;" +
  27. " margin-bottom: 20px;" +
  28. " }" +
  29. " .container {" +
  30. " max-width: 600px;" +
  31. " margin: 0 auto;" +
  32. " padding: 20px;" +
  33. " background-color: #fff;" +
  34. " border-radius: 5px;" +
  35. " box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);" +
  36. " }" +
  37. " .input-container {//" +
  38. " display: flex;" +
  39. " align-items: center;" +
  40. " margin-bottom: 10px;" +
  41. " }" +
  42. " .input-container label {" +
  43. " width: 80px;" +
  44. " margin-right: 10px;" +
  45. " }" +
  46. " .input-container input[type=\"text\"] {" +
  47. " flex: 1;" +
  48. " padding: 5px;" +
  49. " border: 1px solid #ccc;" +
  50. " border-radius: 3px;" +
  51. " margin-right: 10px; "+
  52. " }" +
  53. " .input-container button {" +
  54. " padding: 5px 10px;" +
  55. " background-color: #333;" +
  56. " color: #fff;" +
  57. " font-size: 14px;" +
  58. " font-weight: bold;" +
  59. " border: none;" +
  60. " border-radius: 3px;" +
  61. " text-transform: uppercase;" +
  62. " cursor: pointer;" +
  63. " }" +
  64. " .button-container {" +
  65. " margin-top: 20px;" +
  66. " text-align: center;" +
  67. " }" +
  68. " .button-container button {" +
  69. " margin: 0 5px;" +
  70. " padding: 10px 15px;" +
  71. " background-color: #333;" +
  72. " color: #fff;" +
  73. " font-size: 14px;" +
  74. " font-weight: bold;" +
  75. " border: none;" +
  76. " border-radius: 3px;" +
  77. " text-transform: uppercase;" +
  78. " cursor: pointer;" +
  79. " }" +
  80. " .button-container button:hover {" +
  81. " background-color: #555;" +
  82. " }" +
  83. " </style>" +
  84. "</head>"+
  85. "<body>"+
  86. " <script defer=\"defer\">"+
  87. " function ledSwitch(ledNumber) {"+
  88. " var xhttp = new XMLHttpRequest();" +
  89. " xhttp.onreadystatechange = function() {" +
  90. " if (this.readyState == 4 && this.status == 200) {" +
  91. " console.log('LED ' + ledNumber + ' state changed');" +
  92. " }" +
  93. " };" +
  94. " if (ledNumber < 7) {" +
  95. " xhttp.open('GET', '/Switch' + ledNumber, true);" +
  96. " }" +
  97. " else if(ledNumber == 7){" +
  98. " xhttp.open('GET', '/AllOn', true);" +
  99. " }" +
  100. " else if(ledNumber == 8){" +
  101. " xhttp.open('GET', '/AllOff', true);" +
  102. " }" +
  103. " xhttp.send();" +
  104. " }" +
  105. " function updateData() {"
  106. " var xhr = new XMLHttpRequest();"
  107. " xhr.open('GET', '/getData', true);"
  108. " xhr.onreadystatechange = function() {"
  109. " if (xhr.readyState === 4 && xhr.status === 200) {"
  110. " var dataArray = JSON.parse(xhr.responseText);"
  111. " document.getElementById('ch1').value = dataArray[0];"
  112. " document.getElementById('ch2').value = dataArray[1];"
  113. " document.getElementById('ch3').value = dataArray[2];"
  114. " document.getElementById('ch4').value = dataArray[3];"
  115. " document.getElementById('ch5').value = dataArray[4];"
  116. " document.getElementById('ch6').value = dataArray[5];"
  117. // " // Remove the button's disabled attribute to make it clickable"+
  118. " document.getElementById('btn1').removeAttribute(\'disabled\');"+
  119. " document.getElementById('btn2').removeAttribute(\'disabled\');"+
  120. " document.getElementById('btn3').removeAttribute(\'disabled\');"+
  121. " document.getElementById('btn4').removeAttribute(\'disabled\');"+
  122. " document.getElementById('btn5').removeAttribute(\'disabled\');"+
  123. " document.getElementById('btn6').removeAttribute(\'disabled\');"+
  124. " document.getElementById('btn7').removeAttribute(\'disabled\');"+
  125. " document.getElementById('btn8').removeAttribute(\'disabled\');"+
  126. " }"+
  127. " };"+
  128. " xhr.send();"+
  129. " }"+
  130. " var refreshInterval = 10;"+ // Define a variable for timing, 10ms
  131. " setInterval(updateData, refreshInterval);"+ // The updateData function is executed periodically every 10ms
  132. " </script>" +
  133. "</head>"+
  134. "<body>"+
  135. " <div class=\"header\">"+
  136. " <h1>ESP32-S3-Relay-6CH</h1>"+
  137. " </div>"+
  138. " <div class=\"container\">"+
  139. " <div class=\"input-container\" style=\"margin-left: 140px;\">"+
  140. " <label for=\"input1\">CH1</label>"+
  141. " <input type=\"text\" id=\"ch1\" />"+
  142. " <button value=\"Switch1\" id=\"btn1\" disabled onclick=\"ledSwitch(1)\">Button 1</button>"+
  143. " </div>"+
  144. " <div class=\"input-container\" style=\"margin-left: 140px;\">"+
  145. " <label for=\"input2\">CH2</label>"+
  146. " <input type=\"text\" id=\"ch2\" />"+
  147. " <button value=\"Switch2\" id=\"btn2\" disabled onclick=\"ledSwitch(2)\">Button 2</button>"+
  148. " </div>"+
  149. " <div class=\"input-container\" style=\"margin-left: 140px;\">"+
  150. " <label for=\"input3\">CH3</label>"+
  151. " <input type=\"text\" id=\"ch3\" />"+
  152. " <button value=\"Switch3\" id=\"btn3\" disabled onclick=\"ledSwitch(3)\">Button 3</button>"+
  153. " </div>"+
  154. " <div class=\"input-container\" style=\"margin-left: 140px;\">"+
  155. " <label for=\"input4\">CH4</label>"+
  156. " <input type=\"text\" id=\"ch4\" />"+
  157. " <button value=\"Switch4\" id=\"btn4\" disabled onclick=\"ledSwitch(4)\">Button 4</button>"+
  158. " </div>"+
  159. " <div class=\"input-container\" style=\"margin-left: 140px;\">"+
  160. " <label for=\"input5\">CH5</label>"+
  161. " <input type=\"text\" id=\"ch5\" />"+
  162. " <button value=\"Switch5\" id=\"btn5\" disabled onclick=\"ledSwitch(5)\">Button 5</button>"+
  163. " </div>"+
  164. " <div class=\"input-container\" style=\"margin-left: 140px;\">"+
  165. " <label for=\"input6\">CH6</label>"+
  166. " <input type=\"text\" id=\"ch6\" />"+
  167. " <button value=\"Switch6\" id=\"btn6\" disabled onclick=\"ledSwitch(6)\">Button 6</button>"+
  168. " </div>"+
  169. " <div class=\"button-container\">"+
  170. " <button value=\"AllOn\" id=\"btn7\" disabled onclick=\"ledSwitch(7)\">All On</button>"+
  171. " <button value=\"AllOff\" id=\"btn8\" disabled onclick=\"ledSwitch(8)\">All Off</button>"+
  172. " </div>"+
  173. " </div>"+
  174. "</body>"+
  175. "</html>";
  176. server.send(200, "text/html", myhtmlPage);
  177. // Serial.println("用户访问了主页");
  178. printf("The user visited the home page\r\n");
  179. }
  180. void handleGetData() {
  181. String json = "[";
  182. for (int i = 0; i < sizeof(Relay_Flag) / sizeof(Relay_Flag[0]); i++) {
  183. json += String(Relay_Flag[i]);
  184. if (i < sizeof(Relay_Flag) / sizeof(Relay_Flag[0]) - 1) {
  185. json += ",";
  186. }
  187. }
  188. json += "]";
  189. server.send(200, "application/json", json);
  190. }
  191. void handleSwitch(uint8_t ledNumber) {
  192. uint8_t Data[1]={0};
  193. Data[0]=ledNumber+48;
  194. Relay_Analysis(Data,WIFI_Mode);
  195. server.send(200, "text/plain", "OK");
  196. }
  197. void handleSwitch1() { handleSwitch(1); }
  198. void handleSwitch2() { handleSwitch(2); }
  199. void handleSwitch3() { handleSwitch(3); }
  200. void handleSwitch4() { handleSwitch(4); }
  201. void handleSwitch5() { handleSwitch(5); }
  202. void handleSwitch6() { handleSwitch(6); }
  203. void handleSwitch7() { handleSwitch(7); }
  204. void handleSwitch8() { handleSwitch(8); }
  205. void WIFI_Init()
  206. {
  207. WiFi.mode(WIFI_AP);
  208. while(!WiFi.softAP(ssid, password)) {
  209. printf("Soft AP creation failed.\r\n");
  210. printf("Try setting up the WIFI again.\r\n");
  211. }
  212. // WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // Set the IP address and gateway of the AP
  213. delay(100);
  214. RGB_Light(0, 60, 0);
  215. delay(1000);
  216. RGB_Light(0, 0, 0);
  217. IPAddress myIP = WiFi.softAPIP();
  218. uint32_t ipAddress = WiFi.softAPIP();
  219. printf("AP IP address: ");
  220. sprintf(ipStr, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
  221. printf("%s\r\n", ipStr);
  222. server.on("/", handleRoot);
  223. server.on("/getData", handleGetData);
  224. server.on("/Switch1", handleSwitch1);
  225. server.on("/Switch2", handleSwitch2);
  226. server.on("/Switch3", handleSwitch3);
  227. server.on("/Switch4", handleSwitch4);
  228. server.on("/Switch5", handleSwitch5);
  229. server.on("/Switch6", handleSwitch6);
  230. server.on("/AllOn" , handleSwitch7);
  231. server.on("/AllOff" , handleSwitch8);
  232. server.begin();
  233. printf("Web server started\r\n");
  234. }
  235. void WIFI_Loop()
  236. {
  237. server.handleClient(); // Processing requests from clients
  238. }