sending debug over ws events (wip)
This commit is contained in:
parent
a0b01f4839
commit
3fad0f86f6
@ -34,6 +34,27 @@
|
||||
load_chart("/weekly" , ".chart1");
|
||||
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 onload="load_charts();" src="/chartist.min.js.gz"> </script>
|
||||
|
||||
|
@ -1,3 +1,27 @@
|
||||
#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_SERIAL true
|
||||
#else
|
||||
#define DEBUG_SERIAL false
|
||||
static WsSerial debug_out;
|
||||
#define DEBUG_OUT debug_out
|
||||
#endif
|
||||
|
@ -1,8 +1,10 @@
|
||||
#include <ESPAsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <AsyncEventSource.h>
|
||||
#include <LittleFS.h>
|
||||
|
||||
static AsyncWebServer server(80);
|
||||
static AsyncEventSource events("/events");
|
||||
|
||||
void server_setup();
|
||||
void server_loop();
|
||||
|
@ -23,5 +23,5 @@ lib_deps =
|
||||
monitor_speed = 115200
|
||||
upload_speed = 115200
|
||||
|
||||
;upload_port = myesp32.local
|
||||
;upload_protocol = espota
|
||||
upload_port = 192.168.4.1
|
||||
upload_protocol = espota
|
||||
|
@ -9,17 +9,19 @@
|
||||
const char* ssid = "fan_XXX";
|
||||
|
||||
void setup() {
|
||||
DEBUG_OUT.begin(115200);
|
||||
ota_setup(ssid);
|
||||
Serial.begin(115200);
|
||||
server_setup();
|
||||
serial_setup();
|
||||
ota_setup(ssid);
|
||||
collector_setup();
|
||||
DEBUG_OUT.println(F("Setup done"));
|
||||
DEBUG_OUT.print("IP address: ");
|
||||
DEBUG_OUT.println(WiFi.softAPIP());
|
||||
DEBUG_OUT.println(WiFi.softAPIP().toString());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
ArduinoOTA.handle();
|
||||
server_loop();
|
||||
serial_loop();
|
||||
collector_loop();
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ void ota_setup(const char* ssid) {
|
||||
delay(5000);
|
||||
ESP.restart();
|
||||
}
|
||||
DEBUG_OUT.println(WiFi.softAPIP());
|
||||
DEBUG_OUT.println(WiFi.softAPIP().toString());
|
||||
|
||||
// Port defaults to 3232
|
||||
// ArduinoOTA.setPort(8266);
|
||||
@ -39,10 +39,10 @@ void ota_setup(const char* ssid) {
|
||||
DEBUG_OUT.println("\nEnd");
|
||||
});
|
||||
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) {
|
||||
DEBUG_OUT.printf("Error[%u]: ", error);
|
||||
DEBUG_OUT.print("Error[" + String(error) + "]: ");
|
||||
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_CONNECT_ERROR) DEBUG_OUT.println("Connect Failed");
|
||||
|
31
fan_control/src/serial.cpp
Normal file
31
fan_control/src/serial.cpp
Normal 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
|
||||
}
|
@ -4,7 +4,6 @@
|
||||
|
||||
#include <FS.h>
|
||||
|
||||
#define DEBUG_OUT Serial
|
||||
|
||||
String getContentType(String filename){
|
||||
if(filename.indexOf(".htm") > 0) return "text/html";
|
||||
@ -106,6 +105,8 @@ void server_setup(){
|
||||
|
||||
server.onNotFound(notFound);
|
||||
|
||||
server.addHandler(&events);
|
||||
|
||||
server.begin();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user