Skip to content

Commit

Permalink
ESP-32: Support set TX, RX pin in the web UI SETUP->OTHERS, you can s…
Browse files Browse the repository at this point in the history
…et any compatible pin.
  • Loading branch information
dzungpv committed Dec 13, 2023
1 parent 3b57da0 commit 7c41b8f
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 31 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ The software and hardware has been tested by the author for 5 years on the follo

This circuit using by the author, here is some [boards](https://github.com/dzungpv/mitsubishi2MQTT/blob/master/hardware/Before_Install.jpg) before install.

You can also use ESP-32 module for more processing power. If using ESP-32 you can change default pins in config.h, by default app use TX pin 26 and RX pin 27
You can also use ESP-32 module for more processing power. If using ESP-32 by default app use Serial (serial 0 port).
You can assign any compatible pin in the SETUP->OTHERS to use custom pin, example TX: 26, RX: 27, check ESP-32 manual for pin, when TX and RX set it will use Serial2 port.
You can see more pictures in [`hardware`](https://github.com/dzungpv/mitsubishi2MQTT/tree/master/hardware) folder.
***

Expand Down
4 changes: 2 additions & 2 deletions main/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ unsigned lastWifiScanMillis;
const PROGMEM char *m2mqtt_version = "2023.12.2";

// Define global variables for files
int HP_TX = 0; // variable for the ESP32 custom TX pin, 0 is the defautl and it use hardware serial 0
int HP_RX = 0; // variable for the ESP32 custom RX pin, 0 is the defautl and it use hardware serial 0
#ifdef ESP32
#define HP_TX 26 // define the ESP32 custom TX pin
#define HP_RX 27 // define the ESP32 custom RX pin
const PROGMEM char *wifi_conf = "/wifi.json";
const PROGMEM char *mqtt_conf = "/mqtt.json";
const PROGMEM char *unit_conf = "/unit.json";
Expand Down
8 changes: 8 additions & 0 deletions main/html_pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ const char html_page_others[] PROGMEM =
"<option value='OFF' _DEBUG_PCKTS_OFF_>_TXT_F_OFF_</option>"
"</select>"
"</p>"
"<p><b>_TXT_TX_PIN_</b>"
"<br/>"
"<input type='number' id='tx_pin' step='1' min='0' max='33' name='tx_pin' placeholder=' ' value='_TX_PIN_'>"
"</p>"
"<p><b>_TXT_RX_PIN_</b>"
"<br/>"
"<input type='number' id='rx_pin' step='1' min='0' max='33' name='rx_pin' placeholder=' ' value='_RX_PIN_'>"
"</p>"
"<br/>"
"<button name='save' type='submit' class='button bgrn'>_TXT_SAVE_</button>"
"</form>"
Expand Down
3 changes: 2 additions & 1 deletion main/language_translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ MAKE_WORD_TRANSLATION(txt_others_haauto, en::txt_others_haauto, vi::txt_others_h
MAKE_WORD_TRANSLATION(txt_others_hatopic, en::txt_others_hatopic, vi::txt_others_hatopic, da::txt_others_hatopic, de::txt_others_hatopic, es::txt_others_hatopic, fr::txt_others_hatopic, it::txt_others_hatopic, ja::txt_others_hatopic, zh::txt_others_hatopic, ca::txt_others_hatopic) // TODO translate
MAKE_WORD_TRANSLATION(txt_others_debug_packets, en::txt_others_debug_packets, vi::txt_others_debug_packets, da::txt_others_debug_packets, de::txt_others_debug_packets, es::txt_others_debug_packets, fr::txt_others_debug_packets, it::txt_others_debug_packets, ja::txt_others_debug_packets, zh::txt_others_debug_packets, ca::txt_others_debug_packets) // TODO translate
MAKE_WORD_TRANSLATION(txt_others_debug_log, en::txt_others_debug_log, vi::txt_others_debug_log, da::txt_others_debug_log, de::txt_others_debug_log, es::txt_others_debug_log, fr::txt_others_debug_log, it::txt_others_debug_log, ja::txt_others_debug_log, zh::txt_others_debug_log, ca::txt_others_debug_log) // TODO translate

MAKE_WORD_TRANSLATION(txt_others_tx_pin, en::txt_others_tx_pin, vi::txt_others_tx_pin, da::txt_others_tx_pin, de::txt_others_tx_pin, es::txt_others_tx_pin, fr::txt_others_tx_pin, it::txt_others_tx_pin, ja::txt_others_tx_pin, zh::txt_others_tx_pin, ca::txt_others_tx_pin) // TODO translate
MAKE_WORD_TRANSLATION(txt_others_rx_pin, en::txt_others_rx_pin, vi::txt_others_rx_pin, da::txt_others_rx_pin, de::txt_others_rx_pin, es::txt_others_rx_pin, fr::txt_others_rx_pin, it::txt_others_rx_pin, ja::txt_others_rx_pin, zh::txt_others_rx_pin, ca::txt_others_rx_pin) // TODO translate
// Page Status
MAKE_WORD_TRANSLATION(txt_status_title, en::txt_status_title, vi::txt_status_title, da::txt_status_title, de::txt_status_title, es::txt_status_title, fr::txt_status_title, it::txt_status_title, ja::txt_status_title, zh::txt_status_title, ca::txt_status_title) // TODO translate
MAKE_WORD_TRANSLATION(txt_status_hvac, en::txt_status_hvac, vi::txt_status_hvac, da::txt_status_hvac, de::txt_status_hvac, es::txt_status_hvac, fr::txt_status_hvac, it::txt_status_hvac, ja::txt_status_hvac, zh::txt_status_hvac, ca::txt_status_hvac) // TODO translate
Expand Down
2 changes: 2 additions & 0 deletions main/languages/ca-ES.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace ca
const char txt_others_hatopic[] PROGMEM = "HA tema d'autodescobriment";
const char txt_others_debug_packets[] PROGMEM = "MQTT topic debug packets";
const char txt_others_debug_log[] PROGMEM = "MQTT topic debug logs";
const char txt_others_tx_pin[] PROGMEM = "TX pin (ESP32, 0 use Serial)";
const char txt_others_rx_pin[] PROGMEM = "RX pin (ESP32, 0 use Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "Estat";
Expand Down
4 changes: 3 additions & 1 deletion main/languages/da-DA.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ namespace da
const char txt_others_hatopic[] PROGMEM = "HA Autodiscovery topic";
const char txt_others_debug_packets[] PROGMEM = "MQTT topic debug packets";
const char txt_others_debug_log[] PROGMEM = "MQTT topic debug logs";

const char txt_others_tx_pin[] PROGMEM = "TX pin (ESP32, 0 use Serial)";
const char txt_others_rx_pin[] PROGMEM = "RX pin (ESP32, 0 use Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "Status";
const char txt_status_hvac[] PROGMEM = "HVAC Status";
Expand Down
4 changes: 3 additions & 1 deletion main/languages/de-DE.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ namespace de
const char txt_others_hatopic[] PROGMEM = "HA Autodiscovery topic";
const char txt_others_debug_packets[] PROGMEM = "MQTT topic debug packets";
const char txt_others_debug_log[] PROGMEM = "MQTT topic debug logs";

const char txt_others_tx_pin[] PROGMEM = "TX pin (ESP32, 0 use Serial)";
const char txt_others_rx_pin[] PROGMEM = "RX pin (ESP32, 0 use Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "Status";
const char txt_status_hvac[] PROGMEM = "HVAC Status";
Expand Down
2 changes: 2 additions & 0 deletions main/languages/en-GB.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace en
const char txt_others_hatopic[] PROGMEM = "Autodiscovery topic";
const char txt_others_debug_packets[] PROGMEM = "MQTT debug packets";
const char txt_others_debug_log[] PROGMEM = "MQTT debug logs";
const char txt_others_tx_pin[] PROGMEM = "TX pin (ESP32, 0 use Serial)";
const char txt_others_rx_pin[] PROGMEM = "RX pin (ESP32, 0 use Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "Status";
Expand Down
4 changes: 3 additions & 1 deletion main/languages/es-ES.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ namespace es
const char txt_others_hatopic[] PROGMEM = "HA Autodiscovery topic";
const char txt_others_debug_packets[] PROGMEM = "MQTT topic debug packets";
const char txt_others_debug_log[] PROGMEM = "MQTT topic debug logs";

const char txt_others_tx_pin[] PROGMEM = "TX pin (ESP32, 0 use Serial)";
const char txt_others_rx_pin[] PROGMEM = "RX pin (ESP32, 0 use Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "Estado";
const char txt_status_hvac[] PROGMEM = "Estado HVAC";
Expand Down
4 changes: 3 additions & 1 deletion main/languages/fr-FR.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ namespace fr
const char txt_others_hatopic[] PROGMEM = "HA Topic Découverte automatique";
const char txt_others_debug_packets[] PROGMEM = "MQTT topic debug packets";
const char txt_others_debug_log[] PROGMEM = "MQTT topic debug logs";

const char txt_others_tx_pin[] PROGMEM = "TX pin (ESP32, 0 use Serial)";
const char txt_others_rx_pin[] PROGMEM = "RX pin (ESP32, 0 use Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "Etats";
const char txt_status_hvac[] PROGMEM = "Etat HVAC";
Expand Down
4 changes: 3 additions & 1 deletion main/languages/it-IT.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ namespace it
const char txt_others_hatopic[] PROGMEM = "HA Autodiscovery topic";
const char txt_others_debug_packets[] PROGMEM = "MQTT topic debug packets";
const char txt_others_debug_log[] PROGMEM = "MQTT topic debug logs";

const char txt_others_tx_pin[] PROGMEM = "TX pin (ESP32, 0 use Serial)";
const char txt_others_rx_pin[] PROGMEM = "RX pin (ESP32, 0 use Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "Stato";
const char txt_status_hvac[] PROGMEM = "Stato HVAC";
Expand Down
4 changes: 3 additions & 1 deletion main/languages/ja-JP.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ namespace ja
const char txt_others_hatopic[] PROGMEM = "HA自動検出トピック";
const char txt_others_debug_packets[] PROGMEM = "MQTT topic debug packets";
const char txt_others_debug_log[] PROGMEM = "MQTT topic debug logs";

const char txt_others_tx_pin[] PROGMEM = "TX pin (ESP32, 0 use Serial)";
const char txt_others_rx_pin[] PROGMEM = "RX pin (ESP32, 0 use Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "ステータス";
const char txt_status_hvac[] PROGMEM = "エアコン本体";
Expand Down
4 changes: 3 additions & 1 deletion main/languages/vi-VN.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ namespace vi
const char txt_others_hatopic[] PROGMEM = "Chủ đề tự động khám phá";
const char txt_others_debug_packets[] PROGMEM = "MQTT gỡ lỗi gói tin";
const char txt_others_debug_log[] PROGMEM = "MQTT gỡ lỗi nhật ký";

const char txt_others_tx_pin[] PROGMEM = "Chân TX (ESP32, 0 dùng Serial)";
const char txt_others_rx_pin[] PROGMEM = "Chân RX (ESP32, 0 dùng Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "Trạng thái";
const char txt_status_hvac[] PROGMEM = "Trạng thái ĐH";
Expand Down
4 changes: 3 additions & 1 deletion main/languages/zh-CN.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ namespace zh
const char txt_others_hatopic[] PROGMEM = "HA 自动发现主题";
const char txt_others_debug_packets[] PROGMEM = "MQTT topic debug packets";
const char txt_others_debug_log[] PROGMEM = "MQTT topic debug logs";

const char txt_others_tx_pin[] PROGMEM = "TX pin (ESP32, 0 use Serial)";
const char txt_others_rx_pin[] PROGMEM = "RX pin (ESP32, 0 use Serial)";

// Page Status
const char txt_status_title[] PROGMEM = "状态";
const char txt_status_hvac[] PROGMEM = "空调状态";
Expand Down
52 changes: 33 additions & 19 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ bool loadMqtt();
bool loadUnit();
bool loadOthers();
void saveMqtt(String mqttFn, const String& mqttHost, String mqttPort, const String& mqttUser, const String& mqttPwd, String mqttTopic);
void saveUnit(String tempUnit, String supportMode, String loginPassword, String tempStep, String languageIndex);
void saveUnit(String tempUnit, String supportMode, String supportFanMode, String loginPassword, String tempStep, String languageIndex);
void saveWifi(String apSsid, const String& apPwd, String hostName, const String& otaPwd);
void saveOthers(const String& haa, const String& haat, const String& debugPckts, const String& debugLogs);
void saveOthers(const String& haa, const String& haat, const String& debugPckts, const String& debugLogs, const String& txPin, const String& rxPin);
void saveCurrentOthers();
void initCaptivePortal();
void initMqtt();
Expand Down Expand Up @@ -257,8 +257,15 @@ void setup()
// Allow Remote/Panel
hp.enableExternalUpdate();
hp.enableAutoUpdate();
#if defined(ESP32) && defined(HP_TX) && defined(HP_RX)
hp.connect(&Serial2, HP_RX, HP_TX);
#if defined(ESP32)
if (HP_TX > 0 && HP_RX > 0)
{
hp.connect(&Serial2, HP_RX, HP_TX);
}
else
{
hp.connect(&Serial);
}
#else
hp.connect(&Serial);
#endif
Expand Down Expand Up @@ -522,7 +529,7 @@ bool loadOthers()
return false;
}
// Allocate document capacity.
const size_t capacity = JSON_OBJECT_SIZE(4) + 200;
const size_t capacity = JSON_OBJECT_SIZE(6) + 300;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, configFile);
// unit
Expand All @@ -545,6 +552,17 @@ bool loadOthers()
{
_debugModeLogs = true;
}
// custom tx rx pin
if (doc.containsKey("txPin") && doc.containsKey("rxPin")) // check key to prevent data is "null" if not exist
{
String txPin = doc["txPin"].as<String>();
String rxPin = doc["rxPin"].as<String>();
if (!txPin.isEmpty() && !rxPin.isEmpty())
{
HP_TX = atoi(txPin.c_str());
HP_RX = atoi(rxPin.c_str());
}
}
return true;
}

Expand Down Expand Up @@ -646,15 +664,17 @@ void saveWifi(String apSsid, const String& apPwd, String hostName, const String&
configFile.close();
}

void saveOthers(const String& haa, const String& haat, const String& debugPckts, const String& debugLogs)
void saveOthers(const String& haa, const String& haat, const String& debugPckts, const String& debugLogs, const String& txPin, const String& rxPin)
{
// Allocate document capacity.
const size_t capacity = JSON_OBJECT_SIZE(4) + 130;
const size_t capacity = JSON_OBJECT_SIZE(6) + 300;
DynamicJsonDocument doc(capacity);
doc["haa"] = haa;
doc["haat"] = haat;
doc["debugPckts"] = debugPckts;
doc["debugLogs"] = debugLogs;
doc["txPin"] = txPin;
doc["rxPin"] = rxPin;
File configFile = SPIFFS.open(others_conf, "w");
if (!configFile)
{
Expand All @@ -669,7 +689,7 @@ void saveCurrentOthers()
String haa = others_haa ? "ON" : "OFF";
String debugPckts = _debugModePckts ? "ON" : "OFF";
String debugLogs = _debugModeLogs ? "ON" : "OFF";
saveOthers(haa, others_haa_topic, debugPckts, debugLogs);
saveOthers(haa, others_haa_topic, debugPckts, debugLogs, String(HP_TX), String(HP_RX));
}

// Initialize captive portal page
Expand Down Expand Up @@ -1061,7 +1081,7 @@ void handleOthers(AsyncWebServerRequest *request)
checkLogin(request);
if (request->hasArg("save"))
{
saveOthers(request->arg("HAA"), request->arg("haat"), request->arg("DebugPckts"), request->arg("DebugLogs"));
saveOthers(request->arg("HAA"), request->arg("haat"), request->arg("DebugPckts"), request->arg("DebugLogs"), request->arg("tx_pin"), request->arg("rx_pin"));
String saveRebootPage = FPSTR(html_page_save_reboot);
// localize
saveRebootPage.replace("_TXT_M_SAVE_", translatedWord(FL_(txt_m_save)));
Expand All @@ -1078,12 +1098,16 @@ void handleOthers(AsyncWebServerRequest *request)
othersPage.replace("_TXT_OTHERS_HATOPIC_", translatedWord(FL_(txt_others_hatopic)));
othersPage.replace("_TXT_OTHERS_DEBUG_PCKTS_", translatedWord(FL_(txt_others_debug_packets)));
othersPage.replace("_TXT_OTHERS_DEBUG_LOGS_", translatedWord(FL_(txt_others_debug_log)));
othersPage.replace("_TXT_TX_PIN_", translatedWord(FL_(txt_others_tx_pin)));
othersPage.replace("_TXT_RX_PIN_", translatedWord(FL_(txt_others_rx_pin)));
othersPage.replace("_TXT_F_ON_", translatedWord(FL_(txt_f_on)));
othersPage.replace("_TXT_F_OFF_", translatedWord(FL_(txt_f_off)));
othersPage.replace("_TXT_SAVE_", translatedWord(FL_(txt_save)));
othersPage.replace("_TXT_BACK_", translatedWord(FL_(txt_back)));
// set data
othersPage.replace("_HAA_TOPIC_", others_haa_topic);
othersPage.replace("_TX_PIN_", String(HP_TX));
othersPage.replace("_RX_PIN_", String(HP_RX));
if (others_haa)
{
othersPage.replace("_HAA_ON_", "selected");
Expand Down Expand Up @@ -2175,16 +2199,6 @@ void sendKeepAlive()
if (_debugModeLogs)
mqttClient.publish(ha_debug_logs_topic.c_str(), 1, false, (char *)"Failed to publish avialable status");
}
// send up time
rootInfo["upTime"] = getUpTime();
String mqttOutput;
serializeJson(rootInfo, mqttOutput);
if (!mqttClient.publish(ha_state_topic.c_str(), 1, false, mqttOutput.c_str()))
{
if (_debugModeLogs)
mqttClient.publish(ha_debug_logs_topic.c_str(), 1, false, (char *)("Failed to publish up time"));
}
mqttOutput = "";
}

void hpPacketDebug(byte *packet, unsigned int length, const char *packetDirection)
Expand Down

0 comments on commit 7c41b8f

Please sign in to comment.