Make Serial output optionnal

Save 2k of ROM
This commit is contained in:
Mathieu Maret 2016-03-26 15:07:15 +01:00
parent 7682a85982
commit a990ce0055
5 changed files with 80 additions and 59 deletions

View File

@ -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));

View File

@ -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));
}
}

View File

@ -9,8 +9,8 @@ void WebHandleRoot() {
"</fieldset>"
"<fieldset>"
"<legend>Pump</legend>"
"<a href=\"/gpio?gpio=13&amp;value=1\">Power PUMP ON</a><br/>"
"<a href=\"/gpio?gpio=13&value=0\">Power PUMP OFF</a><br/>"
"<a href=\"/gpio?gpio=2&amp;value=1\">Power PUMP ON</a><br/>"
"<a href=\"/gpio?gpio=2&value=0\">Power PUMP OFF</a><br/>"
"</fieldset>"
"<fieldset>"
"<legend>Settings</legend>"
@ -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", "<h1>OTA Mode set</h1><br/>"
"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", "<h1>Device Reboot</h1><br/>");
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");
}

View File

@ -23,6 +23,8 @@
#include <SFE_BMP180.h>
#include <Wire.h>
#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;

View File

@ -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