redirecting serial

This commit is contained in:
Torsten Ruger 2021-06-28 11:36:43 +03:00
parent abe85144a1
commit e37fa901ab
5 changed files with 44 additions and 39 deletions

View File

@ -0,0 +1,3 @@
#include <Arduino.h>
#define DEBUG_OUT Serial

View File

@ -1,4 +1,5 @@
#include "collector.hpp" #include "collector.hpp"
#include "serial.hpp"
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <DHT.h> #include <DHT.h>
@ -33,14 +34,14 @@ void collector_setup(){
void collector_loop(){ void collector_loop(){
delay(delayMS); delay(delayMS);
// Get temperature event and print its value. // Get temperature event and print its value.
Serial.print(F("Temperature inside: ")); float in = dht_in.readTemperature();
Serial.print(dht_in.readTemperature()); float out = dht_out.readTemperature();
Serial.println(F("°C")); DEBUG_OUT.print(F("Temperature inside: "));
Serial.print(F("Temperature outside: ")); DEBUG_OUT.println(in);
Serial.print(dht_out.readTemperature()); DEBUG_OUT.print(F("Temperature outside: "));
Serial.println("°C"); DEBUG_OUT.println(out);
collector.add(in , out);
} }
void Collector::add(float in , float out) void Collector::add(float in , float out)
@ -48,12 +49,12 @@ void Collector::add(float in , float out)
minute += in; minute += in;
counter++ ; counter++ ;
if(counter % bucket){ if(counter % bucket){
int at_week = counter / bucket; int at_week = counter / bucket;
week_in[at_week] = minute / bucket; week_in[at_week] = minute / bucket;
minute = 0; minute = 0;
} }
if(counter % (bucket*bucket)){ if(counter % (bucket*bucket)){
int at_week = counter / bucket; int at_week = counter / bucket;
month_in[at_week] = minute / bucket; month_in[at_week] = minute / bucket;
counter = 0; counter = 0;
} }
@ -63,7 +64,7 @@ String Collector::one_week(float week[]){
String data = "["; String data = "[";
data += week[0]; data += week[0];
for( int a = 1; a < WEEK ; a = a + 1 ) { for( int a = 1; a < WEEK ; a = a + 1 ) {
data += "," ; data += "," ;
data += week[a] ; data += week[a] ;
} }
return data + "]"; return data + "]";
@ -78,7 +79,7 @@ String Collector::one_month(float month[]){
String data = "["; String data = "[";
data += month[0]; data += month[0];
for( int a = 1; a < MONTH ; a = a + 1 ) { for( int a = 1; a < MONTH ; a = a + 1 ) {
data += "," ; data += "," ;
data += month[a] ; data += month[a] ;
} }
return data + "]"; return data + "]";

View File

@ -3,18 +3,18 @@
#include "ota.hpp" #include "ota.hpp"
#include "server.hpp" #include "server.hpp"
#include "collector.hpp" #include "collector.hpp"
#include "serial.hpp"
const char* ssid = "fan_XXX"; const char* ssid = "fan_XXX";
void setup() { void setup() {
Serial.begin(115200); DEBUG_OUT.begin(115200);
ota_setup(ssid); ota_setup(ssid);
server_setup(); server_setup();
Serial.println(F("Setup done")); DEBUG_OUT.println(F("Setup done"));
Serial.print("IP address: "); DEBUG_OUT.print("IP address: ");
Serial.println(WiFi.softAPIP()); DEBUG_OUT.println(WiFi.softAPIP());
} }
void loop() { void loop() {

View File

@ -1,15 +1,16 @@
#include "ota.hpp" #include "ota.hpp"
#include "serial.hpp"
void ota_setup(const char* ssid) { void ota_setup(const char* ssid) {
Serial.println("Booting"); DEBUG_OUT.println("Booting");
boolean result = WiFi.softAP(ssid); boolean result = WiFi.softAP(ssid);
while (result == false) { while (result == false) {
Serial.println("Connection Failed! Rebooting..."); DEBUG_OUT.println("Connection Failed! Rebooting...");
delay(5000); delay(5000);
ESP.restart(); ESP.restart();
} }
Serial.println(WiFi.softAPIP()); DEBUG_OUT.println(WiFi.softAPIP());
// Port defaults to 3232 // Port defaults to 3232
// ArduinoOTA.setPort(8266); // ArduinoOTA.setPort(8266);
@ -32,21 +33,21 @@ void ota_setup(const char* ssid) {
type = "filesystem"; type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() // 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([]() { ArduinoOTA.onEnd([]() {
Serial.println("\nEnd"); DEBUG_OUT.println("\nEnd");
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { 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) { ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error); DEBUG_OUT.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); if (error == OTA_AUTH_ERROR) DEBUG_OUT.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_BEGIN_ERROR) DEBUG_OUT.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_CONNECT_ERROR) DEBUG_OUT.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_RECEIVE_ERROR) DEBUG_OUT.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed"); else if (error == OTA_END_ERROR) DEBUG_OUT.println("End Failed");
}); });
ArduinoOTA.begin(); ArduinoOTA.begin();
} }

View File

@ -1,8 +1,8 @@
#include "server.hpp" #include "server.hpp"
#include "collector.hpp" #include "collector.hpp"
#include <FS.h> #include "serial.hpp"
#define DBG_OUTPUT_PORT Serial #include <FS.h>
String getContentType(String filename){ String getContentType(String filename){
@ -20,11 +20,11 @@ String getContentType(String filename){
} }
bool handleFileRead(String path ){ bool handleFileRead(String path ){
DBG_OUTPUT_PORT.println("handleFileRead: " + path); DEBUG_OUT.println("handleFileRead: " + path);
if(path.endsWith("/")) path += "index.html"; if(path.endsWith("/")) path += "index.html";
String contentType = getContentType(path) + ";charset=utf-8"; String contentType = getContentType(path) + ";charset=utf-8";
if(LittleFS.exists(path)){ if(LittleFS.exists(path)){
DBG_OUTPUT_PORT.println("handle: " + path); DEBUG_OUT.println("handle: " + path);
File file = LittleFS.open(path, "r"); File file = LittleFS.open(path, "r");
if(path.endsWith(".gz")){ if(path.endsWith(".gz")){
//server.sendHeader("Content-Encoding" , "gzip"); //server.sendHeader("Content-Encoding" , "gzip");
@ -39,15 +39,15 @@ bool handleFileRead(String path ){
} }
void getWeekly() { void getWeekly() {
Serial.println("Weekly start"); DEBUG_OUT.println("Weekly start");
String data = collector.week_data(); String data = collector.week_data();
Serial.println("Weekly end"); DEBUG_OUT.println("Weekly end");
server.send(200, "text/html", data); server.send(200, "text/html", data);
} }
void getMonthly() { void getMonthly() {
Serial.println("Monthly start"); DEBUG_OUT.println("Monthly start");
String data = collector.month_data(); String data = collector.month_data();
Serial.println("Monthly end"); DEBUG_OUT.println("Monthly end");
server.send(200, "text/html", data); server.send(200, "text/html", data);
} }
@ -57,7 +57,7 @@ void server_setup(){
server.on("/monthly", getMonthly); server.on("/monthly", getMonthly);
server.begin(); server.begin();
Serial.println("HTTP server started"); DEBUG_OUT.println("HTTP server started");
server.onNotFound([](){ server.onNotFound([](){
if(!handleFileRead(server.uri())) if(!handleFileRead(server.uri()))
@ -67,4 +67,4 @@ void server_setup(){
void server_loop() { void server_loop() {
server.handleClient(); server.handleClient();
} }