fixing logging

This commit is contained in:
Torsten Ruger 2021-07-03 20:49:43 +03:00
parent 1785f07706
commit 9c6b39fd50
5 changed files with 32 additions and 24 deletions

View File

@ -23,7 +23,6 @@
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
var series = JSON.parse(this.responseText); var series = JSON.parse(this.responseText);
var data = { series: series }; var data = { series: series };
chart = new Chartist.Line(clazz, data , {high: 30 , low: -25}); chart = new Chartist.Line(clazz, data , {high: 30 , low: -25});
@ -38,19 +37,17 @@
} }
var log = document.getElementById("log") var log = document.getElementById("log")
function myCallback() {
function get_log( ){ function get_log( ){
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText); console.log(this.responseText);
log.innerHTML += this.responseText; log.innerHTML = this.responseText + log.innerHTML;
} }
} }
xhttp.open("GET", "/log", true); xhttp.open("GET", "/log", true);
xhttp.send(); xhttp.send();
}; };
}
var intervalID = window.setInterval(get_log, 1500); var intervalID = window.setInterval(get_log, 1500);
</script> </script>
<script onload="load_charts();" src="/chartist.min.js.gz"> </script> <script onload="load_charts();" src="/chartist.min.js.gz"> </script>

View File

@ -5,15 +5,22 @@ class WsSerial{
std::queue<String> debug; std::queue<String> debug;
public: public:
inline void print(const String& line){ debug.push(line); } inline void print(const String& line){
inline void println(const String& line){ debug.push(line); } // enable debug the debugging Serial.println(line.c_str());
debug.push(line);
}
inline void println(const String& line){
// enable debug the debugging Serial.println(line.c_str());
debug.push(line);
}
inline void println(const float num){ println(String(num)) ; }; inline void println(const float num){ println(String(num)) ; };
inline const String& pop(){ const String& first = debug.front(); debug.pop(); return first;}; inline const String& first(){ return debug.front();};
inline void pop(){ debug.pop(); };
inline bool empty(){ return debug.empty();}; inline bool empty(){ return debug.empty();};
}; };
static WsSerial debug_out; extern WsSerial debug_out;
#if(false) #if(false)
#define DEBUG_OUT Serial #define DEBUG_OUT Serial

View File

@ -1,10 +1,8 @@
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <AsyncEventSource.h>
#include <LittleFS.h> #include <LittleFS.h>
static AsyncWebServer server(80); static AsyncWebServer server(80);
static AsyncEventSource events("/events");
void server_setup(); void server_setup();
void server_loop(); void server_loop();

View File

@ -5,6 +5,8 @@
#include <DHT.h> #include <DHT.h>
#include <DHT_U.h> #include <DHT_U.h>
WsSerial debug_out;
// DHT Temperature & Humidity Sensor // DHT Temperature & Humidity Sensor
// Unified Sensor Library Example // Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries // Written by Tony DiCola for Adafruit Industries

View File

@ -83,8 +83,14 @@ void server_setup(){
DEBUG_OUT.println("log start"); DEBUG_OUT.println("log start");
AsyncWebServerResponse *response = request->beginChunkedResponse("text/html", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { AsyncWebServerResponse *response = request->beginChunkedResponse("text/html", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
if(debug_out.empty()) return 0; if(debug_out.empty()) return 0;
String to_send = debug_out.pop(); String to_send = String(maxLen);
to_send += "<br/>"; while(!debug_out.empty()){
String add_next = debug_out.first();
if( (add_next.length() + 5 + to_send.length() ) > maxLen ) break ; // dont overflow buffer
to_send.concat(add_next);
to_send.concat("<br/>");
debug_out.pop();
}
to_send.getBytes(buffer, maxLen); to_send.getBytes(buffer, maxLen);
return to_send.length(); return to_send.length();
}); });
@ -116,8 +122,6 @@ void server_setup(){
server.onNotFound(notFound); server.onNotFound(notFound);
server.addHandler(&events);
server.begin(); server.begin();
} }