added telemetry API endpoint
All checks were successful
Test compiling project / test (push) Successful in 1m44s
All checks were successful
Test compiling project / test (push) Successful in 1m44s
This commit is contained in:
76
src/main.cpp
76
src/main.cpp
@@ -56,8 +56,14 @@ struct NetworkData {
|
||||
String network_name;
|
||||
};
|
||||
|
||||
struct DeviceTelemetry {
|
||||
float heap_used_percent;
|
||||
int uptime_seconds;
|
||||
};
|
||||
|
||||
NetworkData wifi_data;
|
||||
NetworkData ethernet_data;
|
||||
DeviceTelemetry telemetry;
|
||||
|
||||
SensorData current_data = SensorData { -1, -1, -1, -1 };
|
||||
|
||||
@@ -74,7 +80,7 @@ void display_percentage(int percentage)
|
||||
{
|
||||
digitalWrite(LED_RED, 0);
|
||||
|
||||
if (percentage > 1) {
|
||||
if (percentage > 0) {
|
||||
digitalWrite(LED_1, 1);
|
||||
} else {
|
||||
digitalWrite(LED_1, 0);
|
||||
@@ -99,7 +105,17 @@ void display_percentage(int percentage)
|
||||
} else {
|
||||
digitalWrite(LED_5, 0);
|
||||
}
|
||||
delay(3000);
|
||||
|
||||
if (percentage > 10) {
|
||||
delay(3000);
|
||||
} else if (percentage > 0) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
delay(500);
|
||||
digitalWrite(LED_1, 0);
|
||||
delay(500);
|
||||
digitalWrite(LED_1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void display_error_code(byte err_code)
|
||||
@@ -137,7 +153,6 @@ void display_task(void* parameter)
|
||||
if (!is_error()) {
|
||||
// We have no error, refresh status display and wait half a second
|
||||
display_percentage(current_data.percentage);
|
||||
delay(1000);
|
||||
} else {
|
||||
Log.verbose("Error detected");
|
||||
// We have an error, display error code for 3 seconds and then water level for 3 seconds
|
||||
@@ -215,14 +230,12 @@ void ethernet_task(void* parameter)
|
||||
|
||||
void collect_internal_telemetry_task(void* parameter)
|
||||
{
|
||||
|
||||
while (true) {
|
||||
float heap_usage = (float(ESP.getFreeHeap()) / float(ESP.getHeapSize())) * 100;
|
||||
uint64_t uptime_seconds = millis() / 1000;
|
||||
|
||||
Log.verbose("Current heap usage: %F", heap_usage);
|
||||
Log.verbose("Current uptime: %d", uptime_seconds);
|
||||
|
||||
telemetry.heap_used_percent = heap_usage;
|
||||
telemetry.uptime_seconds = uptime_seconds;
|
||||
delay(60000);
|
||||
}
|
||||
}
|
||||
@@ -332,31 +345,40 @@ void setup()
|
||||
});
|
||||
|
||||
server.on("/sensor_data", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
StaticJsonDocument<128> doc;
|
||||
doc["percentage"] = current_data.percentage;
|
||||
doc["voltage"] = current_data.voltage;
|
||||
doc["current"] = current_data.current;
|
||||
doc["water_height"] = current_data.water_height;
|
||||
StaticJsonDocument<128> doc;
|
||||
doc["percentage"] = current_data.percentage;
|
||||
doc["voltage"] = current_data.voltage;
|
||||
doc["current"] = current_data.current;
|
||||
doc["water_height"] = current_data.water_height;
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
request->send(200, "application/json", output); });
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
request->send(200, "application/json", output); });
|
||||
|
||||
server.on("/telemetry", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
StaticJsonDocument<128> doc;
|
||||
doc["uptime_seconds"] = telemetry.uptime_seconds;
|
||||
doc["heap_percent"] = telemetry.heap_used_percent;
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
request->send(200, "application/json", output); });
|
||||
|
||||
server.on("/network_info", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
StaticJsonDocument<256> doc;
|
||||
doc["wifi"]["ip"] = wifi_data.ip_address;
|
||||
doc["wifi"]["rssi"] = wifi_data.rssi;
|
||||
doc["wifi"]["link"] = wifi_data.link;
|
||||
doc["wifi"]["ssid"] = wifi_data.network_name;
|
||||
StaticJsonDocument<256> doc;
|
||||
doc["wifi"]["ip"] = wifi_data.ip_address;
|
||||
doc["wifi"]["rssi"] = wifi_data.rssi;
|
||||
doc["wifi"]["link"] = wifi_data.link;
|
||||
doc["wifi"]["ssid"] = wifi_data.network_name;
|
||||
|
||||
doc["ethernet"]["ip"] = ethernet_data.ip_address;
|
||||
doc["ethernet"]["rssi"] = ethernet_data.rssi;
|
||||
doc["ethernet"]["link"] = ethernet_data.link;
|
||||
doc["ethernet"]["ip"] = ethernet_data.ip_address;
|
||||
doc["ethernet"]["rssi"] = ethernet_data.rssi;
|
||||
doc["ethernet"]["link"] = ethernet_data.link;
|
||||
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
request->send(200, "application/json", output); });
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
request->send(200, "application/json", output);
|
||||
});
|
||||
|
||||
server.on("/chota.css", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/chota.css", "text/css", false); });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user