From 622c7d18c1ebd7c6af0462997e130b5a26f7886b Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Thu, 23 May 2019 21:18:59 +0200 Subject: [PATCH] mhz19: add logging of sent and received PDUs it should help with debbuging issues if debugging is enabled. Also it adds buffer dump function that will be used later to dump invalid responses. Signed-off-by: Igor Mammedov --- esphome/components/mhz19/mhz19.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/esphome/components/mhz19/mhz19.cpp b/esphome/components/mhz19/mhz19.cpp index 198ac01139..ead59a83c1 100644 --- a/esphome/components/mhz19/mhz19.cpp +++ b/esphome/components/mhz19/mhz19.cpp @@ -16,6 +16,15 @@ uint8_t mhz19_checksum(const uint8_t *command) { return 0xFF - sum + 0x01; } +static char hex_buf[MHZ19_PDU_LENGTH * 3 + 1]; +const char *dump_data_buf(const uint8_t *data) { + memset(hex_buf, '\0', sizeof(hex_buf)); + for (int i = 0; i < MHZ19_PDU_LENGTH; i++) { + sprintf(hex_buf, "%s%0x%s", hex_buf, data[i], i == MHZ19_PDU_LENGTH - 1 ? "" : " "); + } + return hex_buf; +} + void MHZ19Component::update() { uint8_t response[MHZ19_PDU_LENGTH]; if (!this->mhz19_write_command_(MHZ19_COMMAND_GET_PPM, response)) { @@ -50,6 +59,7 @@ void MHZ19Component::update() { } bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *response) { + ESP_LOGD(TAG, "cmd [%s]", dump_data_buf(command)); this->write_array(command, MHZ19_PDU_LENGTH); this->flush(); @@ -57,6 +67,7 @@ bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *respo return true; bool ret = this->read_array(response, MHZ19_PDU_LENGTH); + ESP_LOGD(TAG, "resp [%s]", dump_data_buf(response)); return ret; } float MHZ19Component::get_setup_priority() const { return setup_priority::DATA; }