Skip to content

Commit

Permalink
Adjustable serial output verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
stnkl committed May 26, 2021
1 parent 07a1956 commit 9be9040
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 31 deletions.
7 changes: 6 additions & 1 deletion ESPEssentials.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "ESPEssentials.h"

#include "Wifi.h"
#include "OTA.h"
#include "SerialOut.h"
#include "WebServer.h"

void handleESPEssentials()
{
Wifi.process();
Expand All @@ -10,7 +15,7 @@ void handleESPEssentials()
void initESPEssentials(String projectName, int baudRate, String otaPassword)
{
Serial.begin(baudRate);
Serial.println("");
PRINTLN("");

if(Wifi.autoConnect((projectName + " Setup").c_str()))
{
Expand Down
6 changes: 4 additions & 2 deletions ESPEssentials.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#ifndef ESPESSENTIALS_H
#define ESPESSENTIALS_H

#include "Wifi.h"
#include "OTA.h"
#include "WebServer.h"
#include "Wifi.h"

#include <Arduino.h>

#define ESSENTIALS_BAUD 115200

void handleESPEssentials();
void initESPEssentials(String projectName, int baudRate = ESSENTIALS_BAUD, String otaPassword = "");
void initESPEssentials(String projectName, String otaPassword);

#endif
#endif
23 changes: 12 additions & 11 deletions OTA.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "OTA.h"
#include "SerialOut.h"

void OTAClass::init(char const *hostname, char const *password, uint16_t port)
{
Expand All @@ -11,42 +12,42 @@ void OTAClass::init(char const *hostname, char const *password, uint16_t port)

onStart([]()
{
Serial.println("[OTA] Start");
PRINTLN("[OTA] Start");
});
onEnd([]()
{
Serial.println("\n[OTA] End");
PRINTLN("\n[OTA] End");
});
onProgress([](unsigned int progress, unsigned int total)
{
Serial.printf("[OTA] Progress: %u%%\r", (progress / (total / 100)));
PRINTF("[OTA] Progress: %u%%\r", (progress / (total / 100)));
});
onError([](ota_error_t error)
{
Serial.printf("[OTA] Error[%u]: ", error);
PRINTF("[OTA] Error[%u]: ", error);

switch(error)
{
case OTA_AUTH_ERROR:
Serial.println("Auth Failed");
PRINTLN("Auth Failed");
break;
case OTA_BEGIN_ERROR:
Serial.println("Begin Failed");
PRINTLN("Begin Failed");
break;
case OTA_CONNECT_ERROR:
Serial.println("Connect Failed");
PRINTLN("Connect Failed");
break;
case OTA_RECEIVE_ERROR:
Serial.println("Receive Failed");
PRINTLN("Receive Failed");
break;
case OTA_END_ERROR:
Serial.println("End Failed");
PRINTLN("End Failed");
break;
}
});

begin();
Serial.println("[OTA] OTA started");
PRINTLN("[OTA] OTA started");
}

OTAClass OTA;
OTAClass OTA;
2 changes: 1 addition & 1 deletion OTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class OTAClass : public ArduinoOTAClass

extern OTAClass OTA;

#endif
#endif
28 changes: 28 additions & 0 deletions SerialOut.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <Arduino.h>

#ifdef SERIAL_OUT_SILENT

#define PRINT(s)
#define PRINT_VERBOSE(s)
#define PRINTLN(s)
#define PRINTLN_VERBOSE(s)
#define PRINTF(s, ...)
#define PRINTF_VERBOSE(s, ...)

#else

#define PRINT(s) Serial.print(s)
#define PRINTLN(s) Serial.println(s)
#define PRINTF(s, ...) Serial.printf(s, ##__VA_ARGS__)

#ifdef SERIAL_OUT_VERBOSE
#define PRINT_VERBOSE(s) Serial.print(s)
#define PRINTLN_VERBOSE(s) Serial.println(s)
#define PRINTF_VERBOSE(s, ...) Serial.printf(s, ##__VA_ARGS__)
#else
#define PRINT_VERBOSE(s)
#define PRINTLN_VERBOSE(s)
#define PRINTF_VERBOSE(s, ...)
#endif

#endif
25 changes: 15 additions & 10 deletions WebServer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "WebServer.h"
#include "SerialOut.h"

#include <ESP8266WiFi.h>
#include <FS.h>
#include <WiFiUdp.h>

WebServerClass::WebServerClass(int port = 80) : ESP8266WebServer(port)
{
Expand All @@ -13,7 +18,7 @@ void WebServerClass::init()
{
if (!SPIFFS.begin())
{
Serial.println("[Storage] Couldn't mount file system.");
PRINTLN("[Storage] Couldn't mount file system.");
return;
}

Expand Down Expand Up @@ -60,7 +65,7 @@ void WebServerClass::init()
{
if(!handleFileRead(uri()))
{
Serial.println("[Storage] Couldn't find file at \'" + uri() + "\'" + ".");
PRINTLN("[Storage] Couldn't find file at \'" + uri() + "\'" + ".");
send(404, "text/plain", "Oops, file not found!");
}
});
Expand Down Expand Up @@ -105,7 +110,7 @@ String WebServerClass::getContentType(String filename)

bool WebServerClass::handleFileRead(String path)
{
Serial.println("[Storage] File read: " + path);
PRINTLN_VERBOSE("[Storage] File read: " + path);

webserverBusy = true;
if(path.endsWith("/")) path += "index.htm";
Expand Down Expand Up @@ -141,7 +146,7 @@ void WebServerClass::handleFileUpload()
String filename = _upload.filename;
if(!filename.startsWith("/"))
filename = "/" + filename;
Serial.println("[Storage] Uploading file: " + filename);
PRINTLN("[Storage] Uploading file: " + filename);

fsUploadFile = SPIFFS.open(filename, "w");
filename = String();
Expand All @@ -155,7 +160,7 @@ void WebServerClass::handleFileUpload()
{
if(fsUploadFile)
fsUploadFile.close();
Serial.println("[Storage] Received total: " + formatBytes(_upload.totalSize));
PRINTLN("[Storage] Received total: " + formatBytes(_upload.totalSize));
}
}

Expand All @@ -165,7 +170,7 @@ void WebServerClass::handleFileDelete()
return send(500, "text/plain", "BAD ARGS");

String path = arg(0);
Serial.println("[Storage] Deleting file: " + path);
PRINTLN("[Storage] Deleting file: " + path);

if(path == "/")
return send(500, "text/plain", "BAD PATH");
Expand All @@ -184,7 +189,7 @@ void WebServerClass::handleFileCreate()
return send(500, "text/plain", "BAD ARGS");

String path = arg(0);
Serial.println("[Storage] Creating file: " + path);
PRINTLN("[Storage] Creating file: " + path);

if(path == "/")
return send(500, "text/plain", "BAD PATH");
Expand Down Expand Up @@ -240,7 +245,7 @@ void WebServerClass::handleUpdate()
{
Serial.setDebugOutput(true);
WiFiUDP::stopAll();
Serial.printf("Update: %s\n", _upload.filename.c_str());
PRINTF("Update: %s\n", _upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if(!Update.begin(maxSketchSpace))
{
Expand All @@ -257,7 +262,7 @@ void WebServerClass::handleUpdate()
{
if(Update.end(true))
{
Serial.printf("Update Success: %u\nRebooting...\n", _upload.totalSize);
PRINTF("Update Success: %u\nRebooting...\n", _upload.totalSize);
}
else
{
Expand All @@ -268,4 +273,4 @@ void WebServerClass::handleUpdate()
yield();
}

WebServerClass WebServer(80);
WebServerClass WebServer(80);
5 changes: 1 addition & 4 deletions WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

#include <Arduino.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <FS.h>
#include <WiFiUdp.h>

class WebServerClass : public ESP8266WebServer
{
Expand Down Expand Up @@ -35,4 +32,4 @@ class WebServerClass : public ESP8266WebServer

extern WebServerClass WebServer;

#endif
#endif
2 changes: 1 addition & 1 deletion Wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ WifiClass::WifiClass()
setConfigPortalBlocking(false);
}

WifiClass Wifi;
WifiClass Wifi;
2 changes: 1 addition & 1 deletion Wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class WifiClass : public WiFiManager

extern WifiClass Wifi;

#endif
#endif

0 comments on commit 9be9040

Please sign in to comment.