2018-02-26 00:11:31 +00:00
|
|
|
#include <WiFiStatus.h>
|
|
|
|
|
2019-09-28 20:29:46 +00:00
|
|
|
WiFiStatus::WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager) {
|
|
|
|
server->on(WIFI_STATUS_SERVICE_PATH, HTTP_GET,
|
|
|
|
securityManager->wrapRequest(std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)
|
|
|
|
);
|
2018-11-11 17:47:44 +00:00
|
|
|
#if defined(ESP8266)
|
2018-02-26 00:11:31 +00:00
|
|
|
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
|
|
|
|
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
|
|
|
|
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP);
|
2018-11-11 17:47:44 +00:00
|
|
|
#elif defined(ESP_PLATFORM)
|
|
|
|
WiFi.onEvent(onStationModeConnected, WiFiEvent_t::SYSTEM_EVENT_STA_CONNECTED);
|
|
|
|
WiFi.onEvent(onStationModeDisconnected, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
|
|
|
|
WiFi.onEvent(onStationModeGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
|
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 17:47:44 +00:00
|
|
|
#if defined(ESP8266)
|
2018-02-26 00:11:31 +00:00
|
|
|
void WiFiStatus::onStationModeConnected(const WiFiEventStationModeConnected& event) {
|
|
|
|
Serial.print("WiFi Connected. SSID=");
|
|
|
|
Serial.println(event.ssid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiStatus::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
|
|
|
|
Serial.print("WiFi Disconnected. Reason code=");
|
|
|
|
Serial.println(event.reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiStatus::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
|
|
|
|
Serial.print("WiFi Got IP. localIP=");
|
|
|
|
Serial.print(event.ip);
|
|
|
|
Serial.print(", hostName=");
|
|
|
|
Serial.println(WiFi.hostname());
|
|
|
|
}
|
2018-11-11 17:47:44 +00:00
|
|
|
#elif defined(ESP_PLATFORM)
|
|
|
|
void WiFiStatus::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2019-04-27 22:41:11 +00:00
|
|
|
Serial.println("WiFi Connected.");
|
2018-11-11 17:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiStatus::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|
|
|
Serial.print("WiFi Disconnected. Reason code=");
|
|
|
|
Serial.println(info.disconnected.reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|
|
|
Serial.print("WiFi Got IP. localIP=");
|
|
|
|
Serial.print(WiFi.localIP().toString());
|
|
|
|
Serial.print(", hostName=");
|
|
|
|
Serial.println(WiFi.getHostname());
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
void WiFiStatus::wifiStatus(AsyncWebServerRequest *request) {
|
2019-04-14 07:52:40 +00:00
|
|
|
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_WIFI_STATUS_SIZE);
|
|
|
|
JsonObject root = response->getRoot();
|
2018-02-26 00:11:31 +00:00
|
|
|
wl_status_t status = WiFi.status();
|
|
|
|
root["status"] = (uint8_t) status;
|
|
|
|
if (status == WL_CONNECTED){
|
|
|
|
root["local_ip"] = WiFi.localIP().toString();
|
2019-04-29 21:15:07 +00:00
|
|
|
root["mac_address"] = WiFi.macAddress();
|
2018-02-26 00:11:31 +00:00
|
|
|
root["rssi"] = WiFi.RSSI();
|
|
|
|
root["ssid"] = WiFi.SSID();
|
|
|
|
root["bssid"] = WiFi.BSSIDstr();
|
|
|
|
root["channel"] = WiFi.channel();
|
|
|
|
root["subnet_mask"] = WiFi.subnetMask().toString();
|
|
|
|
root["gateway_ip"] = WiFi.gatewayIP().toString();
|
|
|
|
IPAddress dnsIP1 = WiFi.dnsIP(0);
|
|
|
|
IPAddress dnsIP2 = WiFi.dnsIP(1);
|
2018-11-11 17:47:44 +00:00
|
|
|
if (dnsIP1 != INADDR_NONE){
|
2018-02-26 00:11:31 +00:00
|
|
|
root["dns_ip_1"] = dnsIP1.toString();
|
|
|
|
}
|
2018-11-11 17:47:44 +00:00
|
|
|
if (dnsIP2 != INADDR_NONE){
|
2018-02-26 00:11:31 +00:00
|
|
|
root["dns_ip_2"] = dnsIP2.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response->setLength();
|
|
|
|
request->send(response);
|
|
|
|
}
|