From a990ce0055d4bba5275ab3cf8d49c82f775f1803 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Sat, 26 Mar 2016 15:07:15 +0100 Subject: [PATCH] Make Serial output optionnal Save 2k of ROM --- WifiControlSensor/EEPROM.ino | 2 +- WifiControlSensor/MQTT.ino | 14 ++-- WifiControlSensor/WebServer.ino | 10 +-- WifiControlSensor/WifiControlSensor.ino | 95 +++++++++++++------------ WifiControlSensor/debug_sketch.h | 18 +++++ 5 files changed, 80 insertions(+), 59 deletions(-) create mode 100644 WifiControlSensor/debug_sketch.h diff --git a/WifiControlSensor/EEPROM.ino b/WifiControlSensor/EEPROM.ino index bc27126..4d03e9e 100644 --- a/WifiControlSensor/EEPROM.ino +++ b/WifiControlSensor/EEPROM.ino @@ -13,7 +13,7 @@ int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host if (eeprom.length() > EEPROM_SIZE ) return -EMSGSIZE; - Serial.println("Saving " + eeprom); + SKETCH_DEBUG_PRINTLN("Saving " + eeprom); for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++) { EEPROM.write(i, eeprom.charAt(i)); diff --git a/WifiControlSensor/MQTT.ino b/WifiControlSensor/MQTT.ino index d8f5d21..e031434 100644 --- a/WifiControlSensor/MQTT.ino +++ b/WifiControlSensor/MQTT.ino @@ -63,8 +63,8 @@ int MqttConnect() { uint8_t retries = 3; while ((ret = mqtt->connect()) != 0) { // connect will return 0 for connected - Serial.println(mqtt->connectErrorString(ret)); - Serial.println("Retrying MQTT connection ..."); + SKETCH_DEBUG_PRINTLN(mqtt->connectErrorString(ret)); + SKETCH_DEBUG_PRINTLN("Retrying MQTT connection ..."); mqtt->disconnect(); delay(100); // wait retries--; @@ -77,7 +77,7 @@ int MqttConnect() { int MqttPublish(double temp, double pressure) { if (MqttConnect() == 0) { - Serial.println("publishing !"); + SKETCH_DEBUG_PRINTLN("publishing !"); mqtt_temp->publish(temp); mqtt_pressure->publish(pressure); } @@ -120,12 +120,12 @@ void MqttCheckSubscription() { Adafruit_MQTT_Subscribe *subscription; while (subscription = mqtt->readSubscription(0)) { int gpio = getGpioFromSubscription(subscription); - Serial.print("Got Subscription for gpio "); - Serial.println(gpio); + SKETCH_DEBUG_PRINT("Got Subscription for gpio "); + SKETCH_DEBUG_PRINTLN(gpio); if (gpio > 0 && getGpioWatchedIndex(gpio) >= 0) { char *value = (char *) subscription->lastread; - Serial.print("Receive data: "); - Serial.println(value); + SKETCH_DEBUG_PRINT("Receive data: "); + SKETCH_DEBUG_PRINTLN(value); MqttChangeGpioValue(gpio, atoi(value)); } } diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index c8ae975..590253f 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -9,8 +9,8 @@ void WebHandleRoot() { "" "
" "Pump" - "Power PUMP ON
" - "Power PUMP OFF
" + "Power PUMP ON
" + "Power PUMP OFF
" "
" "
" "Settings" @@ -87,7 +87,7 @@ void WebHandleSave() { } void WebHandleOTA() { - Serial.println("Boot mode Set to OTA"); + SKETCH_DEBUG_PRINTLN("Boot mode Set to OTA"); EepromSaveBootMode(BOOTMODE_OTA); server.send(200, "text/html", "

OTA Mode set


" "You can reboot now" @@ -110,7 +110,7 @@ void WebHandleNotFound() { } void WebHandleReboot() { - Serial.println("HTTP request to reboot"); + SKETCH_DEBUG_PRINTLN("HTTP request to reboot"); server.send(200, "text/html", "

Device Reboot


"); ESP.restart(); } @@ -125,7 +125,7 @@ void WebSetupServer(int bootmode) { server.onNotFound(WebHandleNotFound); server.begin(); - Serial.println("HTTP server started"); + SKETCH_DEBUG_PRINTLN("HTTP server started"); } diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index 013d085..223e9da 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -23,6 +23,8 @@ #include #include +#include "debug_sketch.h" + #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" @@ -83,32 +85,32 @@ void MqttChangeGpioValue(int gpio, int value); void WifiSetup(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) { IPAddress myIP; if (bootmode == BOOTMODE_SETUP || forceSetup) { - Serial.println("Configuring access point..."); + SKETCH_DEBUG_PRINTLN("Configuring access point..."); /* You can set a password to the AP here */ WiFi.softAP(ssid); myIP = WiFi.softAPIP(); } else { - Serial.println("Connecting to Wifi..."); + SKETCH_DEBUG_PRINTLN("Connecting to Wifi..."); WiFi.begin(confSsid, confPassword); while (WiFi.status() != WL_CONNECTED) { delay(500); - Serial.print("."); + SKETCH_DEBUG_PRINT("."); } - Serial.println(""); - Serial.println("WiFi connected"); + SKETCH_DEBUG_PRINTLN(""); + SKETCH_DEBUG_PRINTLN("WiFi connected"); if (!MDNS.begin(confHost)) { - Serial.println("Error setting up MDNS responder!"); + SKETCH_DEBUG_PRINTLN("Error setting up MDNS responder!"); while (1) { delay(1000); } } - Serial.println("mDNS responder started"); + SKETCH_DEBUG_PRINTLN("mDNS responder started"); myIP = WiFi.localIP(); } - Serial.print("My IP address: "); - Serial.println(myIP); + SKETCH_DEBUG_PRINT("My IP address: "); + SKETCH_DEBUG_PRINTLN(myIP); } void OTASetup() { @@ -126,28 +128,28 @@ void OTASetup() { EepromSaveBootMode(BOOTMODE_SETUP); ArduinoOTA.onStart([]() { - Serial.println("Start"); + SKETCH_DEBUG_PRINTLN("Start"); }); ArduinoOTA.onEnd([]() { - Serial.println("\nEnd"); + SKETCH_DEBUG_PRINTLN("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + SKETCH_DEBUG_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"); + SKETCH_DEBUG_PRINTF("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR){ SKETCH_DEBUG_PRINTLN("Auth Failed");} + else if (error == OTA_BEGIN_ERROR){ SKETCH_DEBUG_PRINTLN("Begin Failed");} + else if (error == OTA_CONNECT_ERROR){ SKETCH_DEBUG_PRINTLN("Connect Failed");} + else if (error == OTA_RECEIVE_ERROR){ SKETCH_DEBUG_PRINTLN("Receive Failed");} + else if (error == OTA_END_ERROR){ SKETCH_DEBUG_PRINTLN("End Failed");} }); ArduinoOTA.begin(); - Serial.println("Ready"); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); - Serial.print("Free Space: "); - Serial.println(ESP.getFreeSketchSpace()); + SKETCH_DEBUG_PRINTLN("Ready"); + SKETCH_DEBUG_PRINT("IP address: "); + SKETCH_DEBUG_PRINTLN(WiFi.localIP()); + SKETCH_DEBUG_PRINT("Free Space: "); + SKETCH_DEBUG_PRINTLN(ESP.getFreeSketchSpace()); } void setup() { @@ -162,14 +164,15 @@ void setup() { int mqttPort; delay(1000); - Serial.begin(115200); - Serial.println(); + SKETCH_DEBUG_INIT(115200); + SKETCH_DEBUG_PRINTLN(); +#ifndef ENABLE_EXTRA_GPIO // Get GPIO 3 Status Serial.swap(); //Switch Serial on GPIO 13 & 15 pinMode(3, INPUT_PULLUP); int txStatus = digitalRead(3); -#ifndef ENABLE_EXTRA_GPIO + Serial.swap(); // Switch back on GPIO 1 & 3 #endif @@ -178,23 +181,23 @@ void setup() { hostName = confHost; if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) { - Serial.println("Configuration Found !:"); - Serial.println(mode); - Serial.println(confSsid); - Serial.println(confPassword); - Serial.println(confHost); - Serial.println(mqttServer); - Serial.println(mqttUser); - Serial.println(mqttPasswd); - Serial.println(mqttPort); - Serial.println(); + SKETCH_DEBUG_PRINTLN("Configuration Found !:"); + SKETCH_DEBUG_PRINTLN(mode); + SKETCH_DEBUG_PRINTLN(confSsid); + SKETCH_DEBUG_PRINTLN(confPassword); + SKETCH_DEBUG_PRINTLN(confHost); + SKETCH_DEBUG_PRINTLN(mqttServer); + SKETCH_DEBUG_PRINTLN(mqttUser); + SKETCH_DEBUG_PRINTLN(mqttPasswd); + SKETCH_DEBUG_PRINTLN(mqttPort); + SKETCH_DEBUG_PRINTLN(); } else { - Serial.println("No configuration saved"); + SKETCH_DEBUG_PRINTLN("No configuration saved"); } - Serial.print("Force Setup Mode ? :"); - Serial.print(txStatus ? "No" : "Yes"); - Serial.println(); + SKETCH_DEBUG_PRINT("Force Setup Mode ? :"); + SKETCH_DEBUG_PRINT(txStatus ? "No" : "Yes"); + SKETCH_DEBUG_PRINTLN(); WifiSetup(mode, txStatus == 0, confSsid, confPassword, confHost); MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost); @@ -203,7 +206,7 @@ void setup() { OTASetup(); } else { if (BMP180Setup()) - Serial.println("BMP180 init success"); + SKETCH_DEBUG_PRINTLN("BMP180 init success"); WebSetupServer(mode); } } @@ -220,13 +223,13 @@ void loop() { nbCycle++; if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) { if (BMP180IsConnected() && BMP180GetTempAndPressure(temp, pressure) == 0) { - Serial.print("Current T°C "); - Serial.print(temp); - Serial.print( " Pressure mB "); - Serial.println(pressure); + SKETCH_DEBUG_PRINT("Current T°C "); + SKETCH_DEBUG_PRINT(temp); + SKETCH_DEBUG_PRINT( " Pressure mB "); + SKETCH_DEBUG_PRINTLN(pressure); MqttPublish(temp, pressure); } else { - Serial.println("Cannot get T°C"); + SKETCH_DEBUG_PRINTLN("Cannot get T°C"); } nbCycle = 0; diff --git a/WifiControlSensor/debug_sketch.h b/WifiControlSensor/debug_sketch.h new file mode 100644 index 0000000..69f3d27 --- /dev/null +++ b/WifiControlSensor/debug_sketch.h @@ -0,0 +1,18 @@ +#pragma once +//#define SKETCH_DEBUG + +// Set where debug messages will be printed. +#define DEBUG_PRINTER Serial + +#ifdef SKETCH_DEBUG + #define SKETCH_DEBUG_INIT(speed){ DEBUG_PRINTER.begin(speed); } + #define SKETCH_DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); } + #define SKETCH_DEBUG_PRINTF(...) { DEBUG_PRINTER.printf(__VA_ARGS__); } + #define SKETCH_DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); } +#else + #define SKETCH_DEBUG_INIT(speed) + #define SKETCH_DEBUG_PRINT(...) {} + #define SKETCH_DEBUG_PRINTF(...) {} + #define SKETCH_DEBUG_PRINTLN(...) {} +#endif +