From e37fa901abaa8b314dc51800d1db9a7f642c3082 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 28 Jun 2021 11:36:43 +0300 Subject: [PATCH] redirecting serial --- fan_control/include/serial.hpp | 3 +++ fan_control/src/collector.cpp | 23 ++++++++++++----------- fan_control/src/main.cpp | 10 +++++----- fan_control/src/ota.cpp | 27 ++++++++++++++------------- fan_control/src/server.cpp | 20 ++++++++++---------- 5 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 fan_control/include/serial.hpp diff --git a/fan_control/include/serial.hpp b/fan_control/include/serial.hpp new file mode 100644 index 0000000..4318733 --- /dev/null +++ b/fan_control/include/serial.hpp @@ -0,0 +1,3 @@ +#include + +#define DEBUG_OUT Serial diff --git a/fan_control/src/collector.cpp b/fan_control/src/collector.cpp index 151c2a4..7a428eb 100644 --- a/fan_control/src/collector.cpp +++ b/fan_control/src/collector.cpp @@ -1,4 +1,5 @@ #include "collector.hpp" +#include "serial.hpp" #include #include @@ -33,14 +34,14 @@ void collector_setup(){ void collector_loop(){ delay(delayMS); - // Get temperature event and print its value. - Serial.print(F("Temperature inside: ")); - Serial.print(dht_in.readTemperature()); - Serial.println(F("°C")); - Serial.print(F("Temperature outside: ")); - Serial.print(dht_out.readTemperature()); - Serial.println("°C"); + float in = dht_in.readTemperature(); + float out = dht_out.readTemperature(); + DEBUG_OUT.print(F("Temperature inside: ")); + DEBUG_OUT.println(in); + DEBUG_OUT.print(F("Temperature outside: ")); + DEBUG_OUT.println(out); + collector.add(in , out); } void Collector::add(float in , float out) @@ -48,12 +49,12 @@ void Collector::add(float in , float out) minute += in; counter++ ; if(counter % bucket){ - int at_week = counter / bucket; + int at_week = counter / bucket; week_in[at_week] = minute / bucket; minute = 0; } if(counter % (bucket*bucket)){ - int at_week = counter / bucket; + int at_week = counter / bucket; month_in[at_week] = minute / bucket; counter = 0; } @@ -63,7 +64,7 @@ String Collector::one_week(float week[]){ String data = "["; data += week[0]; for( int a = 1; a < WEEK ; a = a + 1 ) { - data += "," ; + data += "," ; data += week[a] ; } return data + "]"; @@ -78,7 +79,7 @@ String Collector::one_month(float month[]){ String data = "["; data += month[0]; for( int a = 1; a < MONTH ; a = a + 1 ) { - data += "," ; + data += "," ; data += month[a] ; } return data + "]"; diff --git a/fan_control/src/main.cpp b/fan_control/src/main.cpp index f9346e0..08d32b1 100644 --- a/fan_control/src/main.cpp +++ b/fan_control/src/main.cpp @@ -3,18 +3,18 @@ #include "ota.hpp" #include "server.hpp" #include "collector.hpp" +#include "serial.hpp" const char* ssid = "fan_XXX"; - void setup() { - Serial.begin(115200); + DEBUG_OUT.begin(115200); ota_setup(ssid); server_setup(); - Serial.println(F("Setup done")); - Serial.print("IP address: "); - Serial.println(WiFi.softAPIP()); + DEBUG_OUT.println(F("Setup done")); + DEBUG_OUT.print("IP address: "); + DEBUG_OUT.println(WiFi.softAPIP()); } void loop() { diff --git a/fan_control/src/ota.cpp b/fan_control/src/ota.cpp index 9698ad6..16519f9 100644 --- a/fan_control/src/ota.cpp +++ b/fan_control/src/ota.cpp @@ -1,15 +1,16 @@ #include "ota.hpp" +#include "serial.hpp" void ota_setup(const char* ssid) { - Serial.println("Booting"); + DEBUG_OUT.println("Booting"); boolean result = WiFi.softAP(ssid); while (result == false) { - Serial.println("Connection Failed! Rebooting..."); + DEBUG_OUT.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } - Serial.println(WiFi.softAPIP()); + DEBUG_OUT.println(WiFi.softAPIP()); // Port defaults to 3232 // ArduinoOTA.setPort(8266); @@ -32,21 +33,21 @@ void ota_setup(const char* ssid) { type = "filesystem"; // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() - Serial.println("Start updating " + type); + DEBUG_OUT.println("Start updating " + type); }); ArduinoOTA.onEnd([]() { - Serial.println("\nEnd"); + DEBUG_OUT.println("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + DEBUG_OUT.printf("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { - Serial.printf("Error[%u]: ", error); - if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); - else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); - else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); - else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); - else if (error == OTA_END_ERROR) Serial.println("End Failed"); + DEBUG_OUT.printf("Error[%u]: ", 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"); + else if (error == OTA_RECEIVE_ERROR) DEBUG_OUT.println("Receive Failed"); + else if (error == OTA_END_ERROR) DEBUG_OUT.println("End Failed"); }); ArduinoOTA.begin(); -} \ No newline at end of file +} diff --git a/fan_control/src/server.cpp b/fan_control/src/server.cpp index dc0de89..7d6d4d9 100644 --- a/fan_control/src/server.cpp +++ b/fan_control/src/server.cpp @@ -1,8 +1,8 @@ #include "server.hpp" #include "collector.hpp" -#include +#include "serial.hpp" -#define DBG_OUTPUT_PORT Serial +#include String getContentType(String filename){ @@ -20,11 +20,11 @@ String getContentType(String filename){ } bool handleFileRead(String path ){ - DBG_OUTPUT_PORT.println("handleFileRead: " + path); + DEBUG_OUT.println("handleFileRead: " + path); if(path.endsWith("/")) path += "index.html"; String contentType = getContentType(path) + ";charset=utf-8"; if(LittleFS.exists(path)){ - DBG_OUTPUT_PORT.println("handle: " + path); + DEBUG_OUT.println("handle: " + path); File file = LittleFS.open(path, "r"); if(path.endsWith(".gz")){ //server.sendHeader("Content-Encoding" , "gzip"); @@ -39,15 +39,15 @@ bool handleFileRead(String path ){ } void getWeekly() { - Serial.println("Weekly start"); + DEBUG_OUT.println("Weekly start"); String data = collector.week_data(); - Serial.println("Weekly end"); + DEBUG_OUT.println("Weekly end"); server.send(200, "text/html", data); } void getMonthly() { - Serial.println("Monthly start"); + DEBUG_OUT.println("Monthly start"); String data = collector.month_data(); - Serial.println("Monthly end"); + DEBUG_OUT.println("Monthly end"); server.send(200, "text/html", data); } @@ -57,7 +57,7 @@ void server_setup(){ server.on("/monthly", getMonthly); server.begin(); - Serial.println("HTTP server started"); + DEBUG_OUT.println("HTTP server started"); server.onNotFound([](){ if(!handleFileRead(server.uri())) @@ -67,4 +67,4 @@ void server_setup(){ void server_loop() { server.handleClient(); -} \ No newline at end of file +}