diff --git a/fan_control/data/index.html b/fan_control/data/index.html
index 49faf0f..2796d8b 100644
--- a/fan_control/data/index.html
+++ b/fan_control/data/index.html
@@ -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);
+ }
diff --git a/fan_control/include/serial.hpp b/fan_control/include/serial.hpp
index 4318733..843990b 100644
--- a/fan_control/include/serial.hpp
+++ b/fan_control/include/serial.hpp
@@ -1,3 +1,27 @@
#include
+#include
+void serial_setup();
+void serial_loop();
+
+class WsSerial{
+ std::queue 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
diff --git a/fan_control/include/server.hpp b/fan_control/include/server.hpp
index 8bb09cf..5f6b52e 100644
--- a/fan_control/include/server.hpp
+++ b/fan_control/include/server.hpp
@@ -1,8 +1,10 @@
#include
#include
+#include
#include
static AsyncWebServer server(80);
+static AsyncEventSource events("/events");
void server_setup();
void server_loop();
diff --git a/fan_control/platformio.ini b/fan_control/platformio.ini
index 137847d..f3d353d 100644
--- a/fan_control/platformio.ini
+++ b/fan_control/platformio.ini
@@ -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
diff --git a/fan_control/src/main.cpp b/fan_control/src/main.cpp
index abec143..adc17c7 100644
--- a/fan_control/src/main.cpp
+++ b/fan_control/src/main.cpp
@@ -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();
}
diff --git a/fan_control/src/ota.cpp b/fan_control/src/ota.cpp
index 16519f9..ae9a1d5 100644
--- a/fan_control/src/ota.cpp
+++ b/fan_control/src/ota.cpp
@@ -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");
diff --git a/fan_control/src/serial.cpp b/fan_control/src/serial.cpp
new file mode 100644
index 0000000..b0ab958
--- /dev/null
+++ b/fan_control/src/serial.cpp
@@ -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
+}
diff --git a/fan_control/src/server.cpp b/fan_control/src/server.cpp
index fafbf79..fd4a516 100644
--- a/fan_control/src/server.cpp
+++ b/fan_control/src/server.cpp
@@ -4,7 +4,6 @@
#include
-#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();
}