Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
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.

75 lines
2.9 KiB

  1. #include <WiFiStatus.h>
  2. WiFiStatus::WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager) {
  3. server->on(WIFI_STATUS_SERVICE_PATH,
  4. HTTP_GET,
  5. securityManager->wrapRequest(std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1),
  6. AuthenticationPredicates::IS_AUTHENTICATED));
  7. #ifdef ESP32
  8. WiFi.onEvent(onStationModeConnected, WiFiEvent_t::SYSTEM_EVENT_STA_CONNECTED);
  9. WiFi.onEvent(onStationModeDisconnected, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  10. WiFi.onEvent(onStationModeGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
  11. #elif defined(ESP8266)
  12. _onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
  13. _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
  14. _onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP);
  15. #endif
  16. }
  17. #ifdef ESP32
  18. void WiFiStatus::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  19. Serial.println(F("WiFi Connected."));
  20. }
  21. void WiFiStatus::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  22. Serial.print(F("WiFi Disconnected. Reason code="));
  23. Serial.println(info.disconnected.reason);
  24. }
  25. void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
  26. Serial.printf_P(
  27. PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), WiFi.localIP().toString().c_str(), WiFi.getHostname());
  28. }
  29. #elif defined(ESP8266)
  30. void WiFiStatus::onStationModeConnected(const WiFiEventStationModeConnected& event) {
  31. Serial.print(F("WiFi Connected. SSID="));
  32. Serial.println(event.ssid);
  33. }
  34. void WiFiStatus::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
  35. Serial.print(F("WiFi Disconnected. Reason code="));
  36. Serial.println(event.reason);
  37. }
  38. void WiFiStatus::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
  39. Serial.printf_P(
  40. PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), event.ip.toString().c_str(), WiFi.hostname().c_str());
  41. }
  42. #endif
  43. void WiFiStatus::wifiStatus(AsyncWebServerRequest* request) {
  44. AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_WIFI_STATUS_SIZE);
  45. JsonObject root = response->getRoot();
  46. wl_status_t status = WiFi.status();
  47. root["status"] = (uint8_t)status;
  48. if (status == WL_CONNECTED) {
  49. root["local_ip"] = WiFi.localIP().toString();
  50. root["mac_address"] = WiFi.macAddress();
  51. root["rssi"] = WiFi.RSSI();
  52. root["ssid"] = WiFi.SSID();
  53. root["bssid"] = WiFi.BSSIDstr();
  54. root["channel"] = WiFi.channel();
  55. root["subnet_mask"] = WiFi.subnetMask().toString();
  56. root["gateway_ip"] = WiFi.gatewayIP().toString();
  57. IPAddress dnsIP1 = WiFi.dnsIP(0);
  58. IPAddress dnsIP2 = WiFi.dnsIP(1);
  59. if (dnsIP1 != INADDR_NONE) {
  60. root["dns_ip_1"] = dnsIP1.toString();
  61. }
  62. if (dnsIP2 != INADDR_NONE) {
  63. root["dns_ip_2"] = dnsIP2.toString();
  64. }
  65. }
  66. response->setLength();
  67. request->send(response);
  68. }