ESP32_hotspot.ino 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /// import all of the common libs
  2. #include <WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <WiFiAP.h>
  5. #include <WebServer.h>
  6. #include <uri/UriBraces.h>
  7. #include <ESPmDNS.h>
  8. #include "esp_wifi.h" // common esp wifi methods
  9. #include "web.h" // our webpage contfigs
  10. #include "config.h" // out newtowrk configs
  11. WebServer server(port);
  12. void handleRoot() {
  13. String message = "<h1>Actions</h1>";
  14. String response = formatContent("Actions", message);
  15. server.send(200, "text/html", response);
  16. }
  17. void handleClients() {
  18. String message = "Client list\n\n";
  19. wifi_sta_list_t wifi_sta_list;
  20. tcpip_adapter_sta_list_t adapter_sta_list;
  21. esp_wifi_ap_get_sta_list(&wifi_sta_list);
  22. tcpip_adapter_get_sta_list(&wifi_sta_list, &adapter_sta_list);
  23. if (adapter_sta_list.num > 0) {
  24. for (uint8_t i = 0; i < adapter_sta_list.num; i++) {
  25. tcpip_adapter_sta_info_t station = adapter_sta_list.sta[i];
  26. ip_addr_t client_ip_addr;
  27. memcpy((char *)&client_ip_addr.u_addr.ip4, (char *)&station.ip, sizeof(ip4_addr));
  28. client_ip_addr.type = IPADDR_TYPE_V4;
  29. message += ((String) "[+] Device " + i + " ");
  30. message += ("%02X:%02X:%02X:%02X:%02X:%02X", station.mac[0], station.mac[1], station.mac[2], station.mac[3], station.mac[4], station.mac[5]);
  31. message += (" ");
  32. message += (ip4addr_ntoa(&(client_ip_addr.u_addr.ip4)));
  33. message += ("\n");
  34. }
  35. }
  36. String response = formatContent("Clients", "<pre>" + message + "</pre>");
  37. server.send(200, "text/html", response);
  38. }
  39. void handleStatus() {
  40. String message = "<h1>Online</h1>";
  41. String response = formatContent("Status", message);
  42. server.send(200, "text/html", response);
  43. }
  44. String formatContent(String title, String content) {
  45. String response = (String) page_header + (String) page_nav + (String) page_content + (String) page_footer;
  46. response.replace("__TITLE__", title);
  47. response.replace("__CONTENT__", content);
  48. server.send(200, "text/html", response);
  49. }
  50. void setup() {
  51. Serial.begin(baud);
  52. Serial.println("\n[+] Creating AP");
  53. WiFi.mode(WIFI_AP);
  54. WiFi.softAP(ssid, password, channel, hide_SSID, max_connection);
  55. Serial.println((String) " --> ssid: " + ssid);
  56. Serial.println((String) " --> password: " + password);
  57. Serial.println((String) " --> channel: " + channel);
  58. Serial.println((String) " --> hide ssid: " + hide_SSID);
  59. Serial.println((String) " --> max connections: " + max_connection);
  60. Serial.println(" --> ip address: 10.10.10.1");
  61. Serial.println(" --> gateway: 10.10.10.1");
  62. Serial.println(" --> subnet: 255.255.255.0");
  63. delay(100);
  64. WiFi.softAPConfig(ip, ip, subnet);
  65. delay(100);
  66. IPAddress myIP = WiFi.softAPIP();
  67. delay(100);
  68. MDNS.begin(host);
  69. server.on("/", handleRoot);
  70. server.on("/clients", handleClients);
  71. server.on("/status", handleStatus);
  72. server.onNotFound(handleRoot);
  73. server.begin();
  74. delay(100);
  75. MDNS.addService("http", "tcp", port);
  76. Serial.print("[+] HTTP server started: http://");
  77. Serial.print(host);
  78. Serial.print(", http://");
  79. Serial.println(myIP);
  80. }
  81. void loop() {
  82. server.handleClient();
  83. delay(2);
  84. }