Skip to content

Commit 9be9040

Browse files
committed
Adjustable serial output verbosity
1 parent 07a1956 commit 9be9040

9 files changed

Lines changed: 69 additions & 31 deletions

File tree

ESPEssentials.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#include "ESPEssentials.h"
22

3+
#include "Wifi.h"
4+
#include "OTA.h"
5+
#include "SerialOut.h"
6+
#include "WebServer.h"
7+
38
void handleESPEssentials()
49
{
510
Wifi.process();
@@ -10,7 +15,7 @@ void handleESPEssentials()
1015
void initESPEssentials(String projectName, int baudRate, String otaPassword)
1116
{
1217
Serial.begin(baudRate);
13-
Serial.println("");
18+
PRINTLN("");
1419

1520
if(Wifi.autoConnect((projectName + " Setup").c_str()))
1621
{

ESPEssentials.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#ifndef ESPESSENTIALS_H
22
#define ESPESSENTIALS_H
33

4-
#include "Wifi.h"
54
#include "OTA.h"
65
#include "WebServer.h"
6+
#include "Wifi.h"
7+
8+
#include <Arduino.h>
79

810
#define ESSENTIALS_BAUD 115200
911

1012
void handleESPEssentials();
1113
void initESPEssentials(String projectName, int baudRate = ESSENTIALS_BAUD, String otaPassword = "");
1214
void initESPEssentials(String projectName, String otaPassword);
1315

14-
#endif
16+
#endif

OTA.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "OTA.h"
2+
#include "SerialOut.h"
23

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

1213
onStart([]()
1314
{
14-
Serial.println("[OTA] Start");
15+
PRINTLN("[OTA] Start");
1516
});
1617
onEnd([]()
1718
{
18-
Serial.println("\n[OTA] End");
19+
PRINTLN("\n[OTA] End");
1920
});
2021
onProgress([](unsigned int progress, unsigned int total)
2122
{
22-
Serial.printf("[OTA] Progress: %u%%\r", (progress / (total / 100)));
23+
PRINTF("[OTA] Progress: %u%%\r", (progress / (total / 100)));
2324
});
2425
onError([](ota_error_t error)
2526
{
26-
Serial.printf("[OTA] Error[%u]: ", error);
27+
PRINTF("[OTA] Error[%u]: ", error);
2728

2829
switch(error)
2930
{
3031
case OTA_AUTH_ERROR:
31-
Serial.println("Auth Failed");
32+
PRINTLN("Auth Failed");
3233
break;
3334
case OTA_BEGIN_ERROR:
34-
Serial.println("Begin Failed");
35+
PRINTLN("Begin Failed");
3536
break;
3637
case OTA_CONNECT_ERROR:
37-
Serial.println("Connect Failed");
38+
PRINTLN("Connect Failed");
3839
break;
3940
case OTA_RECEIVE_ERROR:
40-
Serial.println("Receive Failed");
41+
PRINTLN("Receive Failed");
4142
break;
4243
case OTA_END_ERROR:
43-
Serial.println("End Failed");
44+
PRINTLN("End Failed");
4445
break;
4546
}
4647
});
4748

4849
begin();
49-
Serial.println("[OTA] OTA started");
50+
PRINTLN("[OTA] OTA started");
5051
}
5152

52-
OTAClass OTA;
53+
OTAClass OTA;

OTA.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ class OTAClass : public ArduinoOTAClass
1313

1414
extern OTAClass OTA;
1515

16-
#endif
16+
#endif

SerialOut.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <Arduino.h>
2+
3+
#ifdef SERIAL_OUT_SILENT
4+
5+
#define PRINT(s)
6+
#define PRINT_VERBOSE(s)
7+
#define PRINTLN(s)
8+
#define PRINTLN_VERBOSE(s)
9+
#define PRINTF(s, ...)
10+
#define PRINTF_VERBOSE(s, ...)
11+
12+
#else
13+
14+
#define PRINT(s) Serial.print(s)
15+
#define PRINTLN(s) Serial.println(s)
16+
#define PRINTF(s, ...) Serial.printf(s, ##__VA_ARGS__)
17+
18+
#ifdef SERIAL_OUT_VERBOSE
19+
#define PRINT_VERBOSE(s) Serial.print(s)
20+
#define PRINTLN_VERBOSE(s) Serial.println(s)
21+
#define PRINTF_VERBOSE(s, ...) Serial.printf(s, ##__VA_ARGS__)
22+
#else
23+
#define PRINT_VERBOSE(s)
24+
#define PRINTLN_VERBOSE(s)
25+
#define PRINTF_VERBOSE(s, ...)
26+
#endif
27+
28+
#endif

WebServer.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#include "WebServer.h"
2+
#include "SerialOut.h"
3+
4+
#include <ESP8266WiFi.h>
5+
#include <FS.h>
6+
#include <WiFiUdp.h>
27

38
WebServerClass::WebServerClass(int port = 80) : ESP8266WebServer(port)
49
{
@@ -13,7 +18,7 @@ void WebServerClass::init()
1318
{
1419
if (!SPIFFS.begin())
1520
{
16-
Serial.println("[Storage] Couldn't mount file system.");
21+
PRINTLN("[Storage] Couldn't mount file system.");
1722
return;
1823
}
1924

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

106111
bool WebServerClass::handleFileRead(String path)
107112
{
108-
Serial.println("[Storage] File read: " + path);
113+
PRINTLN_VERBOSE("[Storage] File read: " + path);
109114

110115
webserverBusy = true;
111116
if(path.endsWith("/")) path += "index.htm";
@@ -141,7 +146,7 @@ void WebServerClass::handleFileUpload()
141146
String filename = _upload.filename;
142147
if(!filename.startsWith("/"))
143148
filename = "/" + filename;
144-
Serial.println("[Storage] Uploading file: " + filename);
149+
PRINTLN("[Storage] Uploading file: " + filename);
145150

146151
fsUploadFile = SPIFFS.open(filename, "w");
147152
filename = String();
@@ -155,7 +160,7 @@ void WebServerClass::handleFileUpload()
155160
{
156161
if(fsUploadFile)
157162
fsUploadFile.close();
158-
Serial.println("[Storage] Received total: " + formatBytes(_upload.totalSize));
163+
PRINTLN("[Storage] Received total: " + formatBytes(_upload.totalSize));
159164
}
160165
}
161166

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

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

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

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

189194
if(path == "/")
190195
return send(500, "text/plain", "BAD PATH");
@@ -240,7 +245,7 @@ void WebServerClass::handleUpdate()
240245
{
241246
Serial.setDebugOutput(true);
242247
WiFiUDP::stopAll();
243-
Serial.printf("Update: %s\n", _upload.filename.c_str());
248+
PRINTF("Update: %s\n", _upload.filename.c_str());
244249
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
245250
if(!Update.begin(maxSketchSpace))
246251
{
@@ -257,7 +262,7 @@ void WebServerClass::handleUpdate()
257262
{
258263
if(Update.end(true))
259264
{
260-
Serial.printf("Update Success: %u\nRebooting...\n", _upload.totalSize);
265+
PRINTF("Update Success: %u\nRebooting...\n", _upload.totalSize);
261266
}
262267
else
263268
{
@@ -268,4 +273,4 @@ void WebServerClass::handleUpdate()
268273
yield();
269274
}
270275

271-
WebServerClass WebServer(80);
276+
WebServerClass WebServer(80);

WebServer.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
#include <Arduino.h>
55
#include <ESP8266WebServer.h>
6-
#include <ESP8266WiFi.h>
7-
#include <FS.h>
8-
#include <WiFiUdp.h>
96

107
class WebServerClass : public ESP8266WebServer
118
{
@@ -35,4 +32,4 @@ class WebServerClass : public ESP8266WebServer
3532

3633
extern WebServerClass WebServer;
3734

38-
#endif
35+
#endif

Wifi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ WifiClass::WifiClass()
55
setConfigPortalBlocking(false);
66
}
77

8-
WifiClass Wifi;
8+
WifiClass Wifi;

Wifi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ class WifiClass : public WiFiManager
1111

1212
extern WifiClass Wifi;
1313

14-
#endif
14+
#endif

0 commit comments

Comments
 (0)