sending debug over ws events (wip)

This commit is contained in:
Torsten 2021-07-01 23:46:32 +03:00
parent a0b01f4839
commit 3fad0f86f6
8 changed files with 90 additions and 9 deletions

View File

@ -34,6 +34,27 @@
load_chart("/weekly" , ".chart1"); load_chart("/weekly" , ".chart1");
load_chart("/monthly" , ".chart2"); load_chart("/monthly" , ".chart2");
} }
if (!!window.EventSource) {
var source = new EventSource('/events');
source.addEventListener('open', function(e) {
console.log("Events Connected");
}, false);
source.addEventListener('error', function(e) {
if (e.target.readyState != EventSource.OPEN) {
console.log("Events Disconnected");
}
}, false);
source.addEventListener('message', function(e) {
console.log("message", e.data);
}, false);
source.addEventListener('myevent', function(e) {
console.log("myevent", e.data);
}, false);
}
</script> </script>
<script onload="load_charts();" src="/chartist.min.js.gz"> </script> <script onload="load_charts();" src="/chartist.min.js.gz"> </script>

View File

@ -1,3 +1,27 @@
#include <Arduino.h> #include <Arduino.h>
#include <queue>
void serial_setup();
void serial_loop();
class WsSerial{
std::queue<String> debug;
public:
void print(const String& );
void println(const String& );
inline void println(const float num){ println(String(num)) ; };
inline const String& pop(){ const String& first = debug.front(); debug.pop(); return first;};
inline bool empty(){ return debug.empty();};
};
#if(false)
#define DEBUG_OUT Serial #define DEBUG_OUT Serial
#define DEBUG_SERIAL true
#else
#define DEBUG_SERIAL false
static WsSerial debug_out;
#define DEBUG_OUT debug_out
#endif

View File

@ -1,8 +1,10 @@
#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

@ -23,5 +23,5 @@ lib_deps =
monitor_speed = 115200 monitor_speed = 115200
upload_speed = 115200 upload_speed = 115200
;upload_port = myesp32.local upload_port = 192.168.4.1
;upload_protocol = espota upload_protocol = espota

View File

@ -9,17 +9,19 @@
const char* ssid = "fan_XXX"; const char* ssid = "fan_XXX";
void setup() { void setup() {
DEBUG_OUT.begin(115200); Serial.begin(115200);
ota_setup(ssid);
server_setup(); server_setup();
serial_setup();
ota_setup(ssid);
collector_setup(); collector_setup();
DEBUG_OUT.println(F("Setup done")); DEBUG_OUT.println(F("Setup done"));
DEBUG_OUT.print("IP address: "); DEBUG_OUT.print("IP address: ");
DEBUG_OUT.println(WiFi.softAPIP()); DEBUG_OUT.println(WiFi.softAPIP().toString());
} }
void loop() { void loop() {
ArduinoOTA.handle(); ArduinoOTA.handle();
server_loop(); server_loop();
serial_loop();
collector_loop(); collector_loop();
} }

View File

@ -10,7 +10,7 @@ void ota_setup(const char* ssid) {
delay(5000); delay(5000);
ESP.restart(); ESP.restart();
} }
DEBUG_OUT.println(WiFi.softAPIP()); DEBUG_OUT.println(WiFi.softAPIP().toString());
// Port defaults to 3232 // Port defaults to 3232
// ArduinoOTA.setPort(8266); // ArduinoOTA.setPort(8266);
@ -39,10 +39,10 @@ void ota_setup(const char* ssid) {
DEBUG_OUT.println("\nEnd"); DEBUG_OUT.println("\nEnd");
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
DEBUG_OUT.printf("Progress: %u%%\r", (progress / (total / 100))); DEBUG_OUT.println("Progress:" +String(progress / (total / 100)));
}); });
ArduinoOTA.onError([](ota_error_t error) { ArduinoOTA.onError([](ota_error_t error) {
DEBUG_OUT.printf("Error[%u]: ", error); DEBUG_OUT.print("Error[" + String(error) + "]: ");
if (error == OTA_AUTH_ERROR) DEBUG_OUT.println("Auth Failed"); if (error == OTA_AUTH_ERROR) DEBUG_OUT.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) DEBUG_OUT.println("Begin Failed"); else if (error == OTA_BEGIN_ERROR) DEBUG_OUT.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) DEBUG_OUT.println("Connect Failed"); else if (error == OTA_CONNECT_ERROR) DEBUG_OUT.println("Connect Failed");

View File

@ -0,0 +1,31 @@
#include "serial.hpp"
#include "server.hpp"
void WsSerial::print(const String& line){
debug.push(line);
}
void WsSerial::println(const String& line){
debug.push(line);
}
void serial_setup(){
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
Serial.printf("Client reconnected! Last message ID that it gat is: %u\n", client->lastId());
}
//send event with message "hello!", id current millis
// and set reconnect delay to 1 second
client->send("hello!",NULL,millis(),1000);
});
}
void serial_loop(){
#if DEBUG_SERIAL
return;
#else
if(debug_out.empty()) return ;
const String & line = debug_out.pop();
Serial.println(line.c_str());
events.send(line.c_str() , "message");
#endif
}

View File

@ -4,7 +4,6 @@
#include <FS.h> #include <FS.h>
#define DEBUG_OUT Serial
String getContentType(String filename){ String getContentType(String filename){
if(filename.indexOf(".htm") > 0) return "text/html"; if(filename.indexOf(".htm") > 0) return "text/html";
@ -106,6 +105,8 @@ void server_setup(){
server.onNotFound(notFound); server.onNotFound(notFound);
server.addHandler(&events);
server.begin(); server.begin();
} }