Skip to content

Commit 639f3e7

Browse files
committed
node-wifi-mqtt: Silence debug output by using SerialDebugger
1 parent 3c32a0f commit 639f3e7

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed

libraries/SerialDebugger/SerialDebug.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#define WARNING B00000001
1616
#define ERROR B00000010
1717
#define NOTIFICATION B00000100
18+
// Alias for "NOTIFICATION"
19+
#define INFO B00000100
1820
#define STATUSUPDATE B00001000
1921

2022
class SerialDebug : public PrintCascade{

node-wifi-mqtt/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ LIBS = \
1414
$(ESP_LIBS)/Wire \
1515
$(ESP_LIBS)/ESP8266WiFi \
1616
$(ESP_LIBS)/ESP8266HTTPClient \
17+
../libraries/SerialDebugger/*.h \
18+
../libraries/SerialDebugger/*.cpp \
1719
../libraries/Adafruit_MQTT_Library/*.h \
1820
../libraries/Adafruit_MQTT_Library/*.cpp \
1921
../libraries/ArduinoJson/include \

node-wifi-mqtt/node-wifi-mqtt.ino

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
When SENSOR_DUMMY is enabled, don't use any real sensors. Thanks, Giuseppe!
2929
Add comment about connecting GPIO#16 to RST for waking up after deep sleep. Thanks, Giuseppe and Matthias!
3030
Add sensor ADS1231. Thanks, Clemens!
31+
Silence debug output by using SerialDebugger.
3132
3233
3334
GNU GPL v3 License
@@ -115,6 +116,9 @@
115116
// Compute sleep time in microseconds (*ms *us)
116117
#define DEEPSLEEP_TIME MEASUREMENT_INTERVAL * 1000UL
117118

119+
// Enable debug printing to UART
120+
#define DEBUG true
121+
118122

119123

120124
// ==========================
@@ -293,6 +297,13 @@ const int ds18b20_onewire_pin = 5;
293297
// Libraries
294298
// =========
295299

300+
// -------
301+
// General
302+
// -------
303+
// http://playground.arduino.cc/Code/SerialDebugger
304+
#include <SerialDebug.h>
305+
306+
296307
// ---------
297308
// Telemetry
298309
// ---------
@@ -434,6 +445,20 @@ void setup() {
434445
// ...
435446
Serial.begin(9600);
436447

448+
if (DEBUG) {
449+
450+
// If this line is commented out, the binary sketch size will decrease by 332 bytes
451+
SerialDebugger.begin(9600);
452+
453+
// Select which UART to use for debugging
454+
//SerialDebugger.begin(2, 9600);
455+
456+
SerialDebugger.enable(INFO);
457+
SerialDebugger.enable(WARNING);
458+
SerialDebugger.enable(ERROR);
459+
460+
}
461+
437462
// Setup WiFi
438463
wifi_setup();
439464

@@ -545,17 +570,17 @@ void setup_sensors() {
545570
// you would do this to initially discover addresses on the bus and then
546571
// use those addresses and manually assign them (see above) once you know
547572
// the devices on your bus (and assuming they don't change).
548-
//
573+
549574
// method 1: by index
550575
// change index to order divices as in nature
551576
// (an other approach can be to order physical devices ascending to device address on cable)
552577
for (int i=0; i < ds18b20_device_count; i++) {
553578

554579
// Get address of single device
555580
if (!ds18b20_sensor.getAddress(ds18b20_addresses[ds18b20_device_order[i]], i)) {
556-
Serial.print(F("Unable to find address for temperature array device "));
557-
Serial.print(i);
558-
Serial.println();
581+
if (SerialDebugger.debug(WARNING, "setup_sensors", "Unable to find address for temperature array device")) {
582+
SerialDebugger.println("Device #" + i);
583+
}
559584
}
560585

561586
// Give operating system / watchdog timer some breath
@@ -588,7 +613,7 @@ void read_temperature_array() {
588613

589614
#if SENSOR_DS18B20
590615

591-
Serial.println(F("Read temperature array (DS18B20)"));
616+
SerialDebugger.debug(INFO, "read_temperature_array", "Read temperature array (DS18B20)");
592617

593618
// Request temperature from all devices on the bus
594619

@@ -622,7 +647,7 @@ void read_humidity_temperature() {
622647

623648
#if SENSOR_DHTxx
624649

625-
Serial.println(F("Read humidity and temperature (DHTxx)"));
650+
SerialDebugger.debug(INFO, "read_humidity_temperature", "Read humidity and temperature (DHTxx)");
626651

627652
// Iterate all DHTxx devices
628653
for (int i=0; i < dht_device_count; i++) {
@@ -641,15 +666,21 @@ void read_humidity_temperature() {
641666
// Print debug messages when errors occur
642667

643668
case DHTLIB_ERROR_CHECKSUM:
644-
Serial.println("DHT checksum error. Device #" + i);
669+
if (SerialDebugger.debug(ERROR, "read_humidity_temperature", "DHT checksum error.")) {
670+
SerialDebugger.println("Device #" + i);
671+
}
645672
break;
646673

647674
case DHTLIB_ERROR_TIMEOUT:
648-
Serial.println("DHT timeout error. Device #" + i);
675+
if (SerialDebugger.debug(ERROR, "read_humidity_temperature", "DHT timeout error.")) {
676+
SerialDebugger.println("Device #" + i);
677+
}
649678
break;
650679

651680
default:
652-
Serial.println("DHT unknown error. Device #" + i);
681+
if (SerialDebugger.debug(ERROR, "read_humidity_temperature", "DHT unknown error.")) {
682+
SerialDebugger.println("Device #" + i);
683+
}
653684
break;
654685
}
655686

@@ -670,13 +701,15 @@ void read_weight() {
670701
// HX711 weight scale sensor
671702
#if SENSOR_HX711
672703

673-
Serial.println(F("Read weight (HX711)"));
704+
SerialDebugger.debug(INFO, "read_weight", "Read weight (HX711)");
674705

675706
hx711_sensor.power_up();
676707
weight = hx711_sensor.get_units(5);
677708

678709
// Debugging
679-
Serial.println(weight);
710+
if (SerialDebugger.debug(INFO, "read_weight", "Read weight (HX711)")) {
711+
Serial.println("Weight: " + String(weight));
712+
}
680713

681714
// Put the ADC to sleep mode
682715
hx711_sensor.power_down();
@@ -690,6 +723,8 @@ void read_weight() {
690723
// ADS1231 weight scale sensor
691724
#if SENSOR_ADS1231
692725

726+
SerialDebugger.debug(INFO, "read_weight", "Read weight (ADS1231)");
727+
693728
// Power ADS1231 and load cell
694729
digitalWrite(ADS1231_PIN_CELL_POWER, HIGH);
695730
ads1231_sensor.power(HIGH);
@@ -710,6 +745,7 @@ void read_weight() {
710745

711746
// Sensor isn't ready to read values, abort!
712747
if (!sensor_ready) {
748+
SerialDebugger.debug(ERROR, "read_weight", "ADS1231 not ready");
713749
// TODO: Introduce another magic value for signalling invalid readings?
714750
weight = -1;
715751
return;
@@ -740,14 +776,15 @@ void read_battery_level() {
740776
// This means our minimum analog read value should be 580 (3.14V)
741777
// and the maximum analog read value should be 774 (4.2V).
742778
int adc_level = analogRead(A0);
743-
Serial.print("Battery ADC Level: ");
744-
Serial.println(adc_level);
779+
if (SerialDebugger.debug(INFO, "read_battery_level", "Battery ADC: ")) {
780+
SerialDebugger.println(adc_level);
781+
}
745782

746783
// Convert battery level to percentage
747784
battery_level = map(adc_level, 535, 759, 0, 100);
748-
Serial.print("Battery level: ");
749-
Serial.print(battery_level);
750-
Serial.println("%");
785+
if (SerialDebugger.debug(INFO, "read_battery_level", "Battery level: ")) {
786+
SerialDebugger.print(battery_level).println("%");
787+
}
751788

752789
// Give operating system / watchdog timer some breath
753790
yield();
@@ -834,9 +871,9 @@ void transmit_readings() {
834871
// Publish data
835872
// TODO: Refactor to TerkinTelemetry
836873
if (mqtt_publisher.publish(payload)) {
837-
Serial.println(F("MQTT publish succeeded"));
874+
SerialDebugger.debug(INFO, "transmit_readings", "MQTT publish succeeded");
838875
} else {
839-
Serial.println(F("MQTT publish failed"));
876+
SerialDebugger.debug(ERROR, "transmit_readings", "MQTT publish failed");
840877
}
841878

842879
}
@@ -864,9 +901,7 @@ void wifi_setup() {
864901
bool wifi_connect() {
865902

866903
// Debugging
867-
Serial.println(); Serial.println();
868-
delay(10);
869-
Serial.println("Connecting to WiFi");
904+
SerialDebugger.debug(INFO, "wifi_connect", "Connecting to WiFi");
870905

871906
// Try connecting to WiFi
872907
for (int i = 0; i < WIFI_RETRY_COUNT; i++) {
@@ -875,17 +910,17 @@ bool wifi_connect() {
875910
if ((wifi_multi.run() == WL_CONNECTED)) {
876911

877912
// Debug WiFi
878-
Serial.println();
879-
Serial.print(F("WiFi connected! IP address: "));
880-
Serial.print(WiFi.localIP());
881-
Serial.println();
882-
913+
if (SerialDebugger.debug(INFO, "wifi_connect", "WiFi connected! IP address: ")) {
914+
SerialDebugger.println(WiFi.localIP());
915+
}
883916
return true;
884917

885918
// Not connected yet
886919
} else {
887920

888-
Serial.print(".");
921+
if (SerialDebugger.debug(INFO, "wifi_connect", "Retrying WiFi connection in a few seconds: ")) {
922+
SerialDebugger.println(WIFI_RETRY_DELAY);
923+
}
889924

890925
// Wait some time before retrying
891926
delay(WIFI_RETRY_DELAY * 1000);
@@ -902,34 +937,37 @@ bool wifi_connect() {
902937
bool mqtt_connect() {
903938

904939
if (mqtt.connected()) {
905-
Serial.println(F("MQTT connection already alive"));
940+
SerialDebugger.debug(INFO, "mqtt_connect", "MQTT connection already alive");
906941
return true;
907942
}
908943

909-
Serial.println(F("Connecting to MQTT broker"));
944+
SerialDebugger.debug(INFO, "mqtt_connect", "Connecting to MQTT broker");
910945

911946
// Reconnect loop
912947
uint8_t retries = MQTT_RETRY_COUNT;
913948
int8_t ret;
914949
while ((ret = mqtt.connect()) != 0) {
915950

916-
Serial.print("Error connecting to MQTT broker: ");
917-
Serial.println(mqtt.connectErrorString(ret));
951+
if (SerialDebugger.debug(WARNING, "mqtt_connect", "Error connecting to MQTT broker: ")) {
952+
SerialDebugger.println(String(mqtt.connectErrorString(ret)).c_str());
953+
}
918954

919955
retries--;
920956
if (retries == 0) {
921-
Serial.println(F("Giving up connecting to MQTT broker"));
957+
SerialDebugger.debug(ERROR, "mqtt_connect", "Giving up connecting to MQTT broker");
922958
return false;
923959
}
924960

925-
Serial.println("Retrying MQTT connection in 5 seconds");
961+
if (SerialDebugger.debug(INFO, "mqtt_connect", "Retrying MQTT connection in a few seconds: ")) {
962+
SerialDebugger.println(MQTT_RETRY_DELAY);
963+
}
926964

927965
// Wait some time before retrying
928966
delay(MQTT_RETRY_DELAY * 1000);
929967
}
930968

931969
if (mqtt.connected()) {
932-
Serial.println(F("Successfully connected to MQTT broker"));
970+
SerialDebugger.debug(INFO, "mqtt_connect", "Successfully connected to MQTT broker");
933971
return true;
934972
}
935973

0 commit comments

Comments
 (0)