Added debug logging to memory, and Web API to retrieve the log
This commit is contained in:
parent
7ada49cb5b
commit
127fd6176c
53
ESP32/src/DebugLog.cpp
Normal file
53
ESP32/src/DebugLog.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "DebugLog.h"
|
||||
|
||||
detail::DebugLog DebugLog;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
DebugLog::DebugLog()
|
||||
{
|
||||
buffer[sizeof(buffer)-1] = 0;
|
||||
}
|
||||
|
||||
DebugLog::~DebugLog()
|
||||
{
|
||||
}
|
||||
|
||||
void DebugLog::append(uint8_t b)
|
||||
{
|
||||
buffer[end] = b;
|
||||
end = (end + 1) % (sizeof(buffer)-1);
|
||||
if(end == start) start = (start + 1) % (sizeof(buffer)-1);
|
||||
}
|
||||
|
||||
size_t DebugLog::write(uint8_t b)
|
||||
{
|
||||
append(b);
|
||||
return Serial.write(b);
|
||||
}
|
||||
|
||||
size_t DebugLog::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
for(size_t i = 0; i < size; ++i)
|
||||
{
|
||||
append(buffer[i]);
|
||||
}
|
||||
return Serial.write(buffer, size);
|
||||
}
|
||||
|
||||
const char* DebugLog::get(int partIdx)
|
||||
{
|
||||
buffer[end] = 0;
|
||||
if(partIdx == 0)
|
||||
{
|
||||
return (const char*)&buffer[start];
|
||||
}
|
||||
else if(partIdx == 1)
|
||||
{
|
||||
if(end < start)
|
||||
return (const char*)&buffer[0];
|
||||
}
|
||||
|
||||
return (const char*)&buffer[end];
|
||||
}
|
||||
}
|
26
ESP32/src/DebugLog.h
Normal file
26
ESP32/src/DebugLog.h
Normal file
@ -0,0 +1,26 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace detail
|
||||
{
|
||||
class DebugLog : public Print
|
||||
{
|
||||
public:
|
||||
DebugLog();
|
||||
~DebugLog();
|
||||
|
||||
virtual size_t write(uint8_t b) override;
|
||||
virtual size_t write(const uint8_t *buffer, size_t size) override;
|
||||
|
||||
const char* get(int partIdx);
|
||||
|
||||
private:
|
||||
void append(uint8_t b);
|
||||
|
||||
private:
|
||||
uint8_t buffer[4096];
|
||||
int start = 0;
|
||||
int end = 0;
|
||||
};
|
||||
}
|
||||
|
||||
extern detail::DebugLog DebugLog;
|
@ -1,5 +1,11 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "DebugLog.h"
|
||||
#include "ADC.h"
|
||||
#include "OTA.h"
|
||||
#include "DataLogger.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <WiFiMulti.h>
|
||||
#include <FS.h>
|
||||
@ -8,11 +14,6 @@
|
||||
|
||||
#include <Dps310.h>
|
||||
|
||||
#include "ADC.h"
|
||||
#include "OTA.h"
|
||||
#include "DataLogger.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include "wifi-credentials.h"
|
||||
|
||||
#include <limits>
|
||||
@ -129,7 +130,7 @@ void connectWifi()
|
||||
const int numSSIDs = sizeof(wifi_STA_credentials)/sizeof(wifi_STA_credentials[0]);
|
||||
if(numSSIDs > 0)
|
||||
{
|
||||
Serial.println("Connecting to wifi...");
|
||||
DebugLog.println("Connecting to wifi...");
|
||||
|
||||
for(int idx = 0; idx < numSSIDs; ++idx)
|
||||
{
|
||||
@ -152,7 +153,7 @@ void setup()
|
||||
Serial.begin(115200);
|
||||
|
||||
if(!SPIFFS.begin(false)){
|
||||
Serial.println("SPIFFS Mount Failed");
|
||||
DebugLog.println("SPIFFS Mount Failed");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -162,13 +163,13 @@ void setup()
|
||||
// Create the WiFi Access Point
|
||||
if(wifi_AP_ssid != nullptr)
|
||||
{
|
||||
Serial.println("Creating wifi access point...");
|
||||
DebugLog.println("Creating wifi access point...");
|
||||
WiFi.softAP(wifi_AP_ssid, wifi_AP_password);
|
||||
|
||||
Serial.print("Wifi access point created, SSID=");
|
||||
Serial.print(wifi_AP_ssid);
|
||||
Serial.print(", IP=");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
DebugLog.print("Wifi access point created, SSID=");
|
||||
DebugLog.print(wifi_AP_ssid);
|
||||
DebugLog.print(", IP=");
|
||||
DebugLog.println(WiFi.softAPIP());
|
||||
}
|
||||
|
||||
// Also connect as a station (if the configured remote access point is in range)
|
||||
@ -180,6 +181,21 @@ void setup()
|
||||
|
||||
pressureSensor.begin(Wire);
|
||||
|
||||
server.on("/api/debug/log", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
AsyncResponseStream *response = request->beginResponseStream("text/plain");
|
||||
int logPartIdx = 0;
|
||||
while(true)
|
||||
{
|
||||
const char* text = DebugLog.get(logPartIdx);
|
||||
if(text[0] == 0) break;
|
||||
response->print(text);
|
||||
|
||||
++logPartIdx;
|
||||
}
|
||||
|
||||
request->send(response);
|
||||
});
|
||||
|
||||
server.on("/api/status", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
int v = batteryVoltage;
|
||||
int c = batteryOutputCurrent;
|
||||
@ -234,7 +250,7 @@ void setup()
|
||||
server.serveStatic("/", SPIFFS, "/www/").setCacheControl("max-age=5184000");
|
||||
|
||||
server.begin();
|
||||
Serial.println("HTTP server started");
|
||||
DebugLog.println("HTTP server started");
|
||||
|
||||
digitalWrite(debugLedPin, LOW);
|
||||
}
|
||||
@ -246,18 +262,18 @@ void handle_wifi_connection()
|
||||
{
|
||||
if(newWifiStatus == WL_CONNECTED)
|
||||
{
|
||||
Serial.print("Connected to wifi (");
|
||||
Serial.print(WiFi.SSID().c_str());
|
||||
Serial.print("), ip=");
|
||||
Serial.println(WiFi.localIP());
|
||||
DebugLog.print("Connected to wifi (");
|
||||
DebugLog.print(WiFi.SSID().c_str());
|
||||
DebugLog.print("), ip=");
|
||||
DebugLog.println(WiFi.localIP());
|
||||
}
|
||||
else if(newWifiStatus == WL_DISCONNECTED)
|
||||
{
|
||||
char codeStr[16];
|
||||
sprintf(codeStr, "%d", (int)newWifiStatus);
|
||||
Serial.print("Lost wifi connexion (");
|
||||
Serial.print(codeStr);
|
||||
Serial.println(")");
|
||||
DebugLog.print("Lost wifi connexion (");
|
||||
DebugLog.print(codeStr);
|
||||
DebugLog.println(")");
|
||||
|
||||
connectWifi();
|
||||
}
|
||||
@ -265,8 +281,8 @@ void handle_wifi_connection()
|
||||
{
|
||||
char codeStr[16];
|
||||
sprintf(codeStr, "%d", (int)newWifiStatus);
|
||||
Serial.print("Wifi state: ");
|
||||
Serial.println(codeStr);
|
||||
DebugLog.print("Wifi state: ");
|
||||
DebugLog.println(codeStr);
|
||||
}
|
||||
|
||||
wifi_STA_status = newWifiStatus;
|
||||
@ -323,7 +339,7 @@ void handle_pressure_measure()
|
||||
ret = pressureSensor.measureTempOnce(temp, oversampling);
|
||||
if(ret != 0)
|
||||
{
|
||||
Serial.print("Failed to measure temperature: "); Serial.println(ret);
|
||||
DebugLog.print("Failed to measure temperature: "); DebugLog.println(ret);
|
||||
return;
|
||||
}
|
||||
temperature = (int16_t)(temp * 10.0f + 0.5f);
|
||||
@ -332,7 +348,7 @@ void handle_pressure_measure()
|
||||
pressureSensor.measurePressureOnce(pressure, oversampling);
|
||||
if(ret != 0)
|
||||
{
|
||||
Serial.print("Failed to measure pressure: "); Serial.println(ret);
|
||||
DebugLog.print("Failed to measure pressure: "); DebugLog.println(ret);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -370,9 +386,9 @@ void handle_pressure_measure()
|
||||
}
|
||||
altitude = (int32_t)(alt * 1000.0f + 0.5f);
|
||||
|
||||
/*Serial.print("temperature="); Serial.print(temp); Serial.print("°C");
|
||||
Serial.print(" pressure="); Serial.print(pressure); Serial.print("Pa");
|
||||
Serial.print(" altitude="); Serial.print(altitude); Serial.println("mm");*/
|
||||
/*DebugLog.print("temperature="); DebugLog.print(temp); DebugLog.print("°C");
|
||||
DebugLog.print(" pressure="); DebugLog.print(pressure); DebugLog.print("Pa");
|
||||
DebugLog.print(" altitude="); DebugLog.print(altitude); DebugLog.println("mm");*/
|
||||
}
|
||||
|
||||
void loop()
|
||||
@ -395,7 +411,7 @@ void loop()
|
||||
stoppedSince = -1;
|
||||
if(!DataLogger::get().isOpen())
|
||||
{
|
||||
Serial.println("Starting DataLogger");
|
||||
DebugLog.println("Starting DataLogger");
|
||||
DataLogger::get().open();
|
||||
}
|
||||
}
|
||||
@ -409,7 +425,7 @@ void loop()
|
||||
{
|
||||
if(DataLogger::get().isOpen())
|
||||
{
|
||||
Serial.println("Stopping DataLogger");
|
||||
DebugLog.println("Stopping DataLogger");
|
||||
DataLogger::get().close();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user