diff --git a/src/mitsubishi2mqtt/mitsubishi2mqtt.ino b/src/mitsubishi2mqtt/mitsubishi2mqtt.ino index 3b323aa..a7aba7c 100644 --- a/src/mitsubishi2mqtt/mitsubishi2mqtt.ino +++ b/src/mitsubishi2mqtt/mitsubishi2mqtt.ino @@ -58,7 +58,11 @@ ESP8266WebServer server(80); // ESP8266 web #define HVAC_UART 0 #endif +#define DEBUG_PRINT(x) {if (SerialLog) SerialLog->print(x);} +#define DEBUG_LOG(x) {DEBUG_PRINT(x);DEBUG_PRINT("\n");} + HardwareSerial* SerialHvac = NULL; +typeof(Serial)* SerialLog = &Serial; // wifi, mqtt and heatpump client instances WiFiClient espClient; @@ -105,22 +109,28 @@ void setup() { #endif case 0: SerialHvac = &Serial0; + if ((Stream*)SerialLog == (Stream*)&Serial0) { + SerialLog = NULL; + } break; } - Serial.begin(115200); - // Serial.println(F("Starting Mitsubishi2MQTT")); + if (SerialLog != NULL) { + SerialLog->begin(115200); + } + + DEBUG_LOG(F("Starting Mitsubishi2MQTT")); // Mount SPIFFS filesystem if (SPIFFS.begin()) { - // Serial.println(F("Mounted file system")); + DEBUG_LOG(F("Mounted file system")); } else { - // Serial.println(F("Failed to mount FS -> formating")); + DEBUG_LOG(F("Failed to mount FS -> formating")); SPIFFS.format(); // if (SPIFFS.begin()) - // Serial.println(F("Mounted file system after formating")); + DEBUG_LOG(F("Mounted file system after formating")); } //set led pin as output pinMode(blueLedPin, OUTPUT); @@ -201,7 +211,7 @@ void setup() { else { //write_log("Not found MQTT config go to configuration page"); } - // Serial.println(F("Connection to HVAC. Stop serial log.")); + DEBUG_PRINT(F("Connecting to HVAC... ")); //write_log("Connection to HVAC"); hp.setSettingsChangedCallback(hpSettingsChanged); hp.setStatusChangedCallback(hpStatusChanged); @@ -210,6 +220,7 @@ void setup() { hp.enableExternalUpdate(); hp.enableAutoUpdate(); if (hp.connect(SerialHvac)) { + DEBUG_LOG(F("Success")); heatpumpStatus currentStatus = hp.getStatus(); heatpumpSettings currentSettings = hp.getSettings(); rootInfo["roomTemperature"] = convertCelsiusToLocalUnit(currentStatus.roomTemperature, useFahrenheit); @@ -221,6 +232,8 @@ void setup() { rootInfo["action"] = hpGetAction(currentStatus, currentSettings); rootInfo["compressorFrequency"] = currentStatus.compressorFrequency; lastTempSend = millis(); + } else { + DEBUG_LOG(F("Failed")); } } else { @@ -234,17 +247,17 @@ bool loadWifi() { ap_ssid = ""; ap_pwd = ""; if (!SPIFFS.exists(wifi_conf)) { - // Serial.println(F("Wifi config file not exist!")); + DEBUG_LOG(F("Wifi config file not exist!")); return false; } File configFile = SPIFFS.open(wifi_conf, "r"); if (!configFile) { - // Serial.println(F("Failed to open wifi config file")); + DEBUG_LOG(F("Failed to open wifi config file")); return false; } size_t size = configFile.size(); if (size > 1024) { - // Serial.println(F("Wifi config file size is too large")); + DEBUG_LOG(F("Wifi config file size is too large")); return false; } @@ -267,7 +280,7 @@ bool loadWifi() { } bool loadMqtt() { if (!SPIFFS.exists(mqtt_conf)) { - Serial.println(F("MQTT config file not exist!")); + DEBUG_LOG(F("MQTT config file not exist!")); return false; } //write_log("Loading MQTT configuration"); @@ -310,7 +323,7 @@ bool loadMqtt() { bool loadUnit() { if (!SPIFFS.exists(unit_conf)) { - // Serial.println(F("Unit config file not exist!")); + DEBUG_LOG(F("Unit config file not exist!")); return false; } File configFile = SPIFFS.open(unit_conf, "r"); @@ -348,7 +361,7 @@ bool loadUnit() { bool loadOthers() { if (!SPIFFS.exists(others_conf)) { - // Serial.println(F("Others config file not exist!")); + DEBUG_LOG(F("Others config file not exist!")); return false; } File configFile = SPIFFS.open(others_conf, "r"); @@ -399,7 +412,7 @@ void saveMqtt(String mqttFn, String mqttHost, String mqttPort, String mqttUser, doc["mqtt_topic"] = mqttTopic; File configFile = SPIFFS.open(mqtt_conf, "w"); if (!configFile) { - // Serial.println(F("Failed to open config file for writing")); + DEBUG_LOG(F("Failed to open config file for writing")); } serializeJson(doc, configFile); configFile.close(); @@ -429,7 +442,7 @@ void saveUnit(String tempUnit, String supportMode, String loginPassword, String doc["login_password"] = loginPassword; File configFile = SPIFFS.open(unit_conf, "w"); if (!configFile) { - // Serial.println(F("Failed to open config file for writing")); + DEBUG_LOG(F("Failed to open config file for writing")); } serializeJson(doc, configFile); configFile.close(); @@ -444,7 +457,7 @@ void saveWifi(String apSsid, String apPwd, String hostName, String otaPwd) { doc["ota_pwd"] = otaPwd; File configFile = SPIFFS.open(wifi_conf, "w"); if (!configFile) { - // Serial.println(F("Failed to open wifi file for writing")); + DEBUG_LOG(F("Failed to open wifi file for writing")); } serializeJson(doc, configFile); delay(10); @@ -460,7 +473,7 @@ void saveOthers(String haa, String haat, String debugPckts, String debugLogs) { doc["debugLogs"] = debugLogs; File configFile = SPIFFS.open(others_conf, "w"); if (!configFile) { - // Serial.println(F("Failed to open wifi file for writing")); + DEBUG_LOG(F("Failed to open wifi file for writing")); } serializeJson(doc, configFile); delay(10); @@ -469,7 +482,7 @@ void saveOthers(String haa, String haat, String debugPckts, String debugLogs) { // Initialize captive portal page void initCaptivePortal() { - // Serial.println(F("Starting captive portal")); + DEBUG_LOG(F("Starting captive portal")); server.on("/", handleInitSetup); server.on("/save", handleSaveWifi); server.on("/reboot", handleReboot); @@ -502,11 +515,11 @@ void initOTA() { }); ArduinoOTA.onError([](ota_error_t error) { // write_log("Error[%u]: ", error); - // if (error == OTA_AUTH_ERROR) Serial.println(F("Auth Failed")); - // else if (error == OTA_BEGIN_ERROR) Serial.println(F("Begin Failed")); - // else if (error == OTA_CONNECT_ERROR) Serial.println(F("Connect Failed")); - // else if (error == OTA_RECEIVE_ERROR) Serial.println(F("Receive Failed")); - // else if (error == OTA_END_ERROR) Serial.println(F("End Failed")); + if (error == OTA_AUTH_ERROR) {DEBUG_LOG(F("Auth Failed"));} + else if (error == OTA_BEGIN_ERROR) {DEBUG_LOG(F("Begin Failed"));} + else if (error == OTA_CONNECT_ERROR) {DEBUG_LOG(F("Connect Failed"));} + else if (error == OTA_RECEIVE_ERROR) {DEBUG_LOG(F("Receive Failed"));} + else if (error == OTA_END_ERROR) {DEBUG_LOG(F("End Failed"));} }); ArduinoOTA.begin(); } @@ -533,7 +546,7 @@ boolean initWifi() { } } - // Serial.println(F("\n\r \n\rStarting in AP mode")); + DEBUG_LOG(F("\n\r \n\rStarting in AP mode")); WiFi.mode(WIFI_AP); wifi_timeout = millis() + WIFI_RETRY_INTERVAL_MS; WiFi.persistent(false); //fix crash esp32 https://github.com/espressif/arduino-esp32/issues/2025 @@ -548,8 +561,8 @@ boolean initWifi() { } delay(2000); // VERY IMPORTANT - // Serial.print(F("IP address: ")); - // Serial.println(WiFi.softAPIP()); + DEBUG_LOG(F("IP address: ")); + DEBUG_LOG(WiFi.softAPIP()); //ticker.attach(0.2, tick); // Start LED to flash rapidly to indicate we are ready for setting up the wifi-connection (entered captive portal). wifi_config = false; return false; @@ -591,7 +604,7 @@ void handleNotFound() { void handleSaveWifi() { if (!checkLogin()) return; - // Serial.println(F("Saving wifi config")); + DEBUG_LOG(F("Saving wifi config")); if (server.method() == HTTP_POST) { saveWifi(server.arg("ssid"), server.arg("psk"), server.arg("hn"), server.arg("otapwd")); } @@ -1141,7 +1154,7 @@ void handleUpgrade() { } void handleUploadDone() { - //Serial.printl(PSTR("HTTP: Firmware upload done")); + DEBUG_LOG(F("HTTP: Firmware upload done")); bool restartflag = false; String uploadDonePage = FPSTR(html_page_upload); String content = F("