Added API endpoint which shows network info

This commit is contained in:
Thomas Basler 2022-04-12 23:53:58 +02:00
parent 917a091bf2
commit b58788ded5
3 changed files with 32 additions and 0 deletions

View File

@ -15,6 +15,8 @@ private:
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);
void onNotFound(AsyncWebServerRequest* request);
void onNetworkStatus(AsyncWebServerRequest* request);
};
extern WebApiClass WebApi;

View File

@ -24,6 +24,7 @@ build_flags =
lib_deps =
https://github.com/lorol/LITTLEFS.git
https://github.com/me-no-dev/ESPAsyncWebServer.git
bblanchon/ArduinoJson @ ^6.19.4
board = esp32dev
board_build.partitions = partitions_custom.csv

View File

@ -1,4 +1,7 @@
#include "WebApi.h"
#include "ArduinoJson.h"
#include "AsyncJson.h"
#include "WiFiSettings.h"
#include "defaults.h"
#include <LittleFS.h>
@ -18,6 +21,8 @@ void WebApiClass::init()
_ws.onEvent(std::bind(&WebApiClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6));
_server.on("/api/network/status", HTTP_GET, std::bind(&WebApiClass::onNetworkStatus, this, _1));
_server.serveStatic("/", LITTLEFS, "/", "max-age=86400").setDefaultFile("index.html");
_server.onNotFound(std::bind(&WebApiClass::onNotFound, this, _1));
_server.begin();
@ -42,4 +47,28 @@ void WebApiClass::onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient*
}
}
void WebApiClass::onNetworkStatus(AsyncWebServerRequest* request)
{
AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
root[F("sta_status")] = ((WiFi.getMode() & WIFI_STA) != 0);
root[F("sta_ssid")] = WiFi.SSID();
root[F("sta_ip")] = WiFi.localIP().toString();
root[F("sta_netmask")] = WiFi.subnetMask().toString();
root[F("sta_gateway")] = WiFi.gatewayIP().toString();
root[F("sta_dns1")] = WiFi.dnsIP(0).toString();
root[F("sta_dns2")] = WiFi.dnsIP(1).toString();
root[F("sta_mac")] = WiFi.macAddress();
root[F("sta_rssi")] = WiFi.RSSI();
root[F("ap_status")] = ((WiFi.getMode() & WIFI_AP) != 0);
root[F("ap_ssid")] = WiFiSettings.getApName();
root[F("ap_ip")] = WiFi.softAPIP().toString();
root[F("ap_mac")] = WiFi.softAPmacAddress();
root[F("ap_stationnum")] = WiFi.softAPgetStationNum();
response->setLength();
request->send(response);
}
WebApiClass WebApi;