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 ) if (eeprom.length() > EEPROM_SIZE )
return -EMSGSIZE; return -EMSGSIZE;
Serial.println("Saving " + eeprom); SKETCH_DEBUG_PRINTLN("Saving " + eeprom);
for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++) { for (int i = 0; i < eeprom.length() && i < EEPROM_SIZE; i++) {
EEPROM.write(i, eeprom.charAt(i)); EEPROM.write(i, eeprom.charAt(i));

View File

@ -63,8 +63,8 @@ int MqttConnect() {
uint8_t retries = 3; uint8_t retries = 3;
while ((ret = mqtt->connect()) != 0) { // connect will return 0 for connected while ((ret = mqtt->connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt->connectErrorString(ret)); SKETCH_DEBUG_PRINTLN(mqtt->connectErrorString(ret));
Serial.println("Retrying MQTT connection ..."); SKETCH_DEBUG_PRINTLN("Retrying MQTT connection ...");
mqtt->disconnect(); mqtt->disconnect();
delay(100); // wait delay(100); // wait
retries--; retries--;
@ -77,7 +77,7 @@ int MqttConnect() {
int MqttPublish(double temp, double pressure) { int MqttPublish(double temp, double pressure) {
if (MqttConnect() == 0) { if (MqttConnect() == 0) {
Serial.println("publishing !"); SKETCH_DEBUG_PRINTLN("publishing !");
mqtt_temp->publish(temp); mqtt_temp->publish(temp);
mqtt_pressure->publish(pressure); mqtt_pressure->publish(pressure);
} }
@ -120,12 +120,12 @@ void MqttCheckSubscription() {
Adafruit_MQTT_Subscribe *subscription; Adafruit_MQTT_Subscribe *subscription;
while (subscription = mqtt->readSubscription(0)) { while (subscription = mqtt->readSubscription(0)) {
int gpio = getGpioFromSubscription(subscription); int gpio = getGpioFromSubscription(subscription);
Serial.print("Got Subscription for gpio "); SKETCH_DEBUG_PRINT("Got Subscription for gpio ");
Serial.println(gpio); SKETCH_DEBUG_PRINTLN(gpio);
if (gpio > 0 && getGpioWatchedIndex(gpio) >= 0) { if (gpio > 0 && getGpioWatchedIndex(gpio) >= 0) {
char *value = (char *) subscription->lastread; char *value = (char *) subscription->lastread;
Serial.print("Receive data: "); SKETCH_DEBUG_PRINT("Receive data: ");
Serial.println(value); SKETCH_DEBUG_PRINTLN(value);
MqttChangeGpioValue(gpio, atoi(value)); MqttChangeGpioValue(gpio, atoi(value));
} }
} }

View File

@ -9,8 +9,8 @@ void WebHandleRoot() {
"</fieldset>" "</fieldset>"
"<fieldset>" "<fieldset>"
"<legend>Pump</legend>" "<legend>Pump</legend>"
"<a href=\"/gpio?gpio=13&amp;value=1\">Power PUMP ON</a><br/>" "<a href=\"/gpio?gpio=2&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&value=0\">Power PUMP OFF</a><br/>"
"</fieldset>" "</fieldset>"
"<fieldset>" "<fieldset>"
"<legend>Settings</legend>" "<legend>Settings</legend>"
@ -87,7 +87,7 @@ void WebHandleSave() {
} }
void WebHandleOTA() { void WebHandleOTA() {
Serial.println("Boot mode Set to OTA"); SKETCH_DEBUG_PRINTLN("Boot mode Set to OTA");
EepromSaveBootMode(BOOTMODE_OTA); EepromSaveBootMode(BOOTMODE_OTA);
server.send(200, "text/html", "<h1>OTA Mode set</h1><br/>" server.send(200, "text/html", "<h1>OTA Mode set</h1><br/>"
"You can reboot now" "You can reboot now"
@ -110,7 +110,7 @@ void WebHandleNotFound() {
} }
void WebHandleReboot() { 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/>"); server.send(200, "text/html", "<h1>Device Reboot</h1><br/>");
ESP.restart(); ESP.restart();
} }
@ -125,7 +125,7 @@ void WebSetupServer(int bootmode) {
server.onNotFound(WebHandleNotFound); server.onNotFound(WebHandleNotFound);
server.begin(); server.begin();
Serial.println("HTTP server started"); SKETCH_DEBUG_PRINTLN("HTTP server started");
} }

View File

@ -23,6 +23,8 @@
#include <SFE_BMP180.h> #include <SFE_BMP180.h>
#include <Wire.h> #include <Wire.h>
#include "debug_sketch.h"
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.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) { void WifiSetup(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
IPAddress myIP; IPAddress myIP;
if (bootmode == BOOTMODE_SETUP || forceSetup) { 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 */ /* You can set a password to the AP here */
WiFi.softAP(ssid); WiFi.softAP(ssid);
myIP = WiFi.softAPIP(); myIP = WiFi.softAPIP();
} else { } else {
Serial.println("Connecting to Wifi..."); SKETCH_DEBUG_PRINTLN("Connecting to Wifi...");
WiFi.begin(confSsid, confPassword); WiFi.begin(confSsid, confPassword);
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print("."); SKETCH_DEBUG_PRINT(".");
} }
Serial.println(""); SKETCH_DEBUG_PRINTLN("");
Serial.println("WiFi connected"); SKETCH_DEBUG_PRINTLN("WiFi connected");
if (!MDNS.begin(confHost)) { if (!MDNS.begin(confHost)) {
Serial.println("Error setting up MDNS responder!"); SKETCH_DEBUG_PRINTLN("Error setting up MDNS responder!");
while (1) { while (1) {
delay(1000); delay(1000);
} }
} }
Serial.println("mDNS responder started"); SKETCH_DEBUG_PRINTLN("mDNS responder started");
myIP = WiFi.localIP(); myIP = WiFi.localIP();
} }
Serial.print("My IP address: "); SKETCH_DEBUG_PRINT("My IP address: ");
Serial.println(myIP); SKETCH_DEBUG_PRINTLN(myIP);
} }
void OTASetup() { void OTASetup() {
@ -126,28 +128,28 @@ void OTASetup() {
EepromSaveBootMode(BOOTMODE_SETUP); EepromSaveBootMode(BOOTMODE_SETUP);
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
Serial.println("Start"); SKETCH_DEBUG_PRINTLN("Start");
}); });
ArduinoOTA.onEnd([]() { ArduinoOTA.onEnd([]() {
Serial.println("\nEnd"); SKETCH_DEBUG_PRINTLN("\nEnd");
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { 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) { ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error); SKETCH_DEBUG_PRINTF("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); if (error == OTA_AUTH_ERROR){ SKETCH_DEBUG_PRINTLN("Auth Failed");}
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_BEGIN_ERROR){ SKETCH_DEBUG_PRINTLN("Begin Failed");}
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_CONNECT_ERROR){ SKETCH_DEBUG_PRINTLN("Connect Failed");}
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_RECEIVE_ERROR){ SKETCH_DEBUG_PRINTLN("Receive Failed");}
else if (error == OTA_END_ERROR) Serial.println("End Failed"); else if (error == OTA_END_ERROR){ SKETCH_DEBUG_PRINTLN("End Failed");}
}); });
ArduinoOTA.begin(); ArduinoOTA.begin();
Serial.println("Ready"); SKETCH_DEBUG_PRINTLN("Ready");
Serial.print("IP address: "); SKETCH_DEBUG_PRINT("IP address: ");
Serial.println(WiFi.localIP()); SKETCH_DEBUG_PRINTLN(WiFi.localIP());
Serial.print("Free Space: "); SKETCH_DEBUG_PRINT("Free Space: ");
Serial.println(ESP.getFreeSketchSpace()); SKETCH_DEBUG_PRINTLN(ESP.getFreeSketchSpace());
} }
void setup() { void setup() {
@ -162,14 +164,15 @@ void setup() {
int mqttPort; int mqttPort;
delay(1000); delay(1000);
Serial.begin(115200); SKETCH_DEBUG_INIT(115200);
Serial.println(); SKETCH_DEBUG_PRINTLN();
#ifndef ENABLE_EXTRA_GPIO
// Get GPIO 3 Status // Get GPIO 3 Status
Serial.swap(); //Switch Serial on GPIO 13 & 15 Serial.swap(); //Switch Serial on GPIO 13 & 15
pinMode(3, INPUT_PULLUP); pinMode(3, INPUT_PULLUP);
int txStatus = digitalRead(3); int txStatus = digitalRead(3);
#ifndef ENABLE_EXTRA_GPIO
Serial.swap(); // Switch back on GPIO 1 & 3 Serial.swap(); // Switch back on GPIO 1 & 3
#endif #endif
@ -178,23 +181,23 @@ void setup() {
hostName = confHost; hostName = confHost;
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) { if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
Serial.println("Configuration Found !:"); SKETCH_DEBUG_PRINTLN("Configuration Found !:");
Serial.println(mode); SKETCH_DEBUG_PRINTLN(mode);
Serial.println(confSsid); SKETCH_DEBUG_PRINTLN(confSsid);
Serial.println(confPassword); SKETCH_DEBUG_PRINTLN(confPassword);
Serial.println(confHost); SKETCH_DEBUG_PRINTLN(confHost);
Serial.println(mqttServer); SKETCH_DEBUG_PRINTLN(mqttServer);
Serial.println(mqttUser); SKETCH_DEBUG_PRINTLN(mqttUser);
Serial.println(mqttPasswd); SKETCH_DEBUG_PRINTLN(mqttPasswd);
Serial.println(mqttPort); SKETCH_DEBUG_PRINTLN(mqttPort);
Serial.println(); SKETCH_DEBUG_PRINTLN();
} else { } else {
Serial.println("No configuration saved"); SKETCH_DEBUG_PRINTLN("No configuration saved");
} }
Serial.print("Force Setup Mode ? :"); SKETCH_DEBUG_PRINT("Force Setup Mode ? :");
Serial.print(txStatus ? "No" : "Yes"); SKETCH_DEBUG_PRINT(txStatus ? "No" : "Yes");
Serial.println(); SKETCH_DEBUG_PRINTLN();
WifiSetup(mode, txStatus == 0, confSsid, confPassword, confHost); WifiSetup(mode, txStatus == 0, confSsid, confPassword, confHost);
MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost); MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost);
@ -203,7 +206,7 @@ void setup() {
OTASetup(); OTASetup();
} else { } else {
if (BMP180Setup()) if (BMP180Setup())
Serial.println("BMP180 init success"); SKETCH_DEBUG_PRINTLN("BMP180 init success");
WebSetupServer(mode); WebSetupServer(mode);
} }
} }
@ -220,13 +223,13 @@ void loop() {
nbCycle++; nbCycle++;
if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) { if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) {
if (BMP180IsConnected() && BMP180GetTempAndPressure(temp, pressure) == 0) { if (BMP180IsConnected() && BMP180GetTempAndPressure(temp, pressure) == 0) {
Serial.print("Current T°C "); SKETCH_DEBUG_PRINT("Current T°C ");
Serial.print(temp); SKETCH_DEBUG_PRINT(temp);
Serial.print( " Pressure mB "); SKETCH_DEBUG_PRINT( " Pressure mB ");
Serial.println(pressure); SKETCH_DEBUG_PRINTLN(pressure);
MqttPublish(temp, pressure); MqttPublish(temp, pressure);
} else { } else {
Serial.println("Cannot get T°C"); SKETCH_DEBUG_PRINTLN("Cannot get T°C");
} }
nbCycle = 0; 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