84 lines
3.4 KiB
C++
84 lines
3.4 KiB
C++
/*
|
|
status.cpp
|
|
|
|
Copyright (c) 2020 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
#include "status.h"
|
|
#include "configure.h"
|
|
|
|
#include <SPIFFS.h>
|
|
#include <WiFi.h>
|
|
|
|
|
|
namespace status {
|
|
|
|
/*
|
|
Read the battery level
|
|
*/
|
|
float batteryVoltage() {
|
|
float v;
|
|
v = analogRead(battery_sense); // 12bit reading
|
|
v /= 4095.0f; // percent of range
|
|
v *= 2.0f; // doubled
|
|
v *= 3.3f; // percent of referance voltage
|
|
v *= 1.1f; // multiply by ADC resolution (V)
|
|
return v;
|
|
}
|
|
|
|
|
|
/*
|
|
Template keyiables
|
|
*/
|
|
String getString(const String& key) {
|
|
// status values
|
|
if (key == "STATUS_IRAM_TOTAL") return String(ESP.getHeapSize());
|
|
if (key == "STATUS_IRAM_FREE") return String(ESP.getFreeHeap());
|
|
if (key == "STATUS_IRAM_MAX") return String(ESP.getMinFreeHeap());
|
|
if (key == "STATUS_SPIFFS_TOTAL") return String(SPIFFS.totalBytes());
|
|
if (key == "STATUS_SPIFFS_FREE") return String(SPIFFS.totalBytes() - SPIFFS.usedBytes());
|
|
if (key == "STATUS_BATTERY_VOLTAGE") return String(batteryVoltage());
|
|
if (key == "STATUS_WIFI_MODE") return String(WiFi.getMode());
|
|
if (key == "STATUS_WIFI_POWER") return String(WiFi.getTxPower());
|
|
if (key == "STATUS_WIFI_SSID") return WiFi.SSID();
|
|
if (config::settings().mode == WIFI_AP) {
|
|
if (key == "STATUS_NETWORK_HOSTNAME") return String(WiFi.softAPgetHostname());
|
|
if (key == "STATUS_NETWORK_IPV4") return WiFi.softAPIP().toString();
|
|
if (key == "STATUS_NETWORK_IPV6") return WiFi.softAPIPv6().toString();
|
|
if (key == "STATUS_NETWORK_CIDR") return String(WiFi.softAPSubnetCIDR());
|
|
if (key == "STATUS_NETWORK_GATEWAY") return WiFi.gatewayIP().toString();
|
|
if (key == "STATUS_NETWORK_MAC") return WiFi.softAPmacAddress();
|
|
}
|
|
if (config::settings().mode == WIFI_STA) {
|
|
if (key == "STATUS_WIFI_STATUS") return String(WiFi.status());
|
|
if (key == "STATUS_WIFI_SLEEP") return String(WiFi.getSleep());
|
|
if (key == "STATUS_NETWORK_HOSTNAME") return String(WiFi.getHostname());
|
|
if (key == "STATUS_NETWORK_IPV4") return WiFi.localIP().toString();
|
|
if (key == "STATUS_NETWORK_IPV6") return WiFi.localIPv6().toString();
|
|
if (key == "STATUS_NETWORK_CIDR") return String(WiFi.subnetCIDR());
|
|
if (key == "STATUS_NETWORK_GATEWAY") return WiFi.gatewayIP().toString();
|
|
if (key == "STATUS_NETWORK_MAC") return WiFi.macAddress();
|
|
}
|
|
return String();
|
|
}
|
|
|
|
} // namespace status
|