From 52af52eb3ae043daf101f2ad1499228ae4e6e7aa Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 24 May 2023 09:17:38 +0200 Subject: [PATCH] Pylontech enhancement (#239) --- include/Configuration.h | 2 +- include/PylontechCanReceiver.h | 4 +- src/PylontechCanReceiver.cpp | 94 ++++++++++++++++++++++++++-------- src/WebApi_battery.cpp | 2 + 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/include/Configuration.h b/include/Configuration.h index 6915869b7..8c0aefeb6 100644 --- a/include/Configuration.h +++ b/include/Configuration.h @@ -36,7 +36,7 @@ #define POWERMETER_MAX_HTTP_JSON_PATH_STRLEN 256 #define POWERMETER_HTTP_TIMEOUT 1000 -#define JSON_BUFFER_SIZE 12288 +#define JSON_BUFFER_SIZE 15360 struct CHANNEL_CONFIG_T { uint16_t MaxChannelPower; diff --git a/include/PylontechCanReceiver.h b/include/PylontechCanReceiver.h index 25121c3dd..726a1af4c 100644 --- a/include/PylontechCanReceiver.h +++ b/include/PylontechCanReceiver.h @@ -19,6 +19,7 @@ class PylontechCanReceiverClass { public: void init(int8_t rx, int8_t tx); void enable(); + void disable(); void loop(); void parseCanPackets(); void mqtt(); @@ -31,9 +32,10 @@ class PylontechCanReceiverClass { float scaleValue(int16_t value, float factor); bool getBit(uint8_t value, uint8_t bit); + bool _isEnabled = false; uint32_t _lastPublish; twai_general_config_t g_config; - + esp_err_t twaiLastResult; }; extern PylontechCanReceiverClass PylontechCanReceiver; diff --git a/src/PylontechCanReceiver.cpp b/src/PylontechCanReceiver.cpp index 5d574050b..4cb35c431 100644 --- a/src/PylontechCanReceiver.cpp +++ b/src/PylontechCanReceiver.cpp @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0-or-later #include "PylontechCanReceiver.h" #include "Battery.h" #include "Configuration.h" @@ -13,34 +14,78 @@ PylontechCanReceiverClass PylontechCanReceiver; void PylontechCanReceiverClass::init(int8_t rx, int8_t tx) { CONFIG_T& config = Configuration.get(); - if (!config.Battery_Enabled) { - return; + g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)tx, (gpio_num_t)rx, TWAI_MODE_NORMAL); + if (config.Battery_Enabled) { + enable(); } - - g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)tx, (gpio_num_t)rx, TWAI_MODE_NORMAL); - enable(); - } void PylontechCanReceiverClass::enable() { - + if (_isEnabled) { + return; + } // Initialize configuration structures using macro initializers twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS(); twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); // Install TWAI driver - if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) { - MessageOutput.printf("[Pylontech] Twai driver installed\n"); - } else { - MessageOutput.printf("[Pylontech] Failed to install Twai driver\n"); + twaiLastResult = twai_driver_install(&g_config, &t_config, &f_config); + switch (twaiLastResult) { + case ESP_OK: + MessageOutput.println(F("[Pylontech] Twai driver installed")); + break; + case ESP_ERR_INVALID_ARG: + MessageOutput.println(F("[Pylontech] Twai driver install - invalid arg")); + break; + case ESP_ERR_NO_MEM: + MessageOutput.println(F("[Pylontech] Twai driver install - no memory")); + break; + case ESP_ERR_INVALID_STATE: + MessageOutput.println(F("[Pylontech] Twai driver install - invalid state")); + break; } // Start TWAI driver - if (twai_start() == ESP_OK) { - MessageOutput.printf("[Pylontech] Twai driver started\n"); - } else { - MessageOutput.printf("[Pylontech] Failed to start Twai driver\n"); + twaiLastResult = twai_start(); + switch (twaiLastResult) { + case ESP_OK: + MessageOutput.println(F("[Pylontech] Twai driver started")); + _isEnabled = true; + break; + case ESP_ERR_INVALID_STATE: + MessageOutput.println(F("[Pylontech] Twai driver start - invalid state")); + break; + } +} + +void PylontechCanReceiverClass::disable() +{ + if (!_isEnabled) { + return; + } + + // Stop TWAI driver + twaiLastResult = twai_stop(); + switch (twaiLastResult) { + case ESP_OK: + MessageOutput.println(F("[Pylontech] Twai driver stopped")); + break; + case ESP_ERR_INVALID_STATE: + MessageOutput.println(F("[Pylontech] Twai driver stop - invalid state")); + break; + } + + // Uninstall TWAI driver + twaiLastResult = twai_driver_uninstall(); + switch (twaiLastResult) { + case ESP_OK: + MessageOutput.println(F("[Pylontech] Twai driver uninstalled")); + _isEnabled = false; + break; + case ESP_ERR_INVALID_STATE: + MessageOutput.println(F("[Pylontech] Twai driver uninstall - invalid state")); + break; } } @@ -98,11 +143,18 @@ void PylontechCanReceiverClass::mqtt() void PylontechCanReceiverClass::parseCanPackets() { - - // Check for messages. twai_recive is blocking when there is no data so we return if there are no frames in the buffer + // Check for messages. twai_receive is blocking when there is no data so we return if there are no frames in the buffer twai_status_info_t status_info; - if (twai_get_status_info(&status_info) != ESP_OK) { - MessageOutput.printf("[Pylontech]Failed to get Twai status info\n"); + twaiLastResult = twai_get_status_info(&status_info); + if (twaiLastResult != ESP_OK) { + switch (twaiLastResult) { + case ESP_ERR_INVALID_ARG: + MessageOutput.println(F("[Pylontech] Twai driver get status - invalid arg")); + break; + case ESP_ERR_INVALID_STATE: + MessageOutput.println(F("[Pylontech] Twai driver get status - invalid state")); + break; + } return; } if (status_info.msgs_to_rx == 0) { @@ -112,7 +164,7 @@ void PylontechCanReceiverClass::parseCanPackets() // Wait for message to be received, function is blocking twai_message_t rx_message; if (twai_receive(&rx_message, pdMS_TO_TICKS(100)) != ESP_OK) { - MessageOutput.printf("[Pylontech] Failed to receive message\n"); + MessageOutput.println(F("[Pylontech] Failed to receive message")); return; } @@ -235,14 +287,12 @@ void PylontechCanReceiverClass::parseCanPackets() } } - uint16_t PylontechCanReceiverClass::readUnsignedInt16(uint8_t *data) { uint8_t bytes[2]; bytes[0] = *data; bytes[1] = *(data + 1); return (bytes[1] << 8) + bytes[0]; - } int16_t PylontechCanReceiverClass::readSignedInt16(uint8_t *data) diff --git a/src/WebApi_battery.cpp b/src/WebApi_battery.cpp index a11d14fbe..c9589df4a 100644 --- a/src/WebApi_battery.cpp +++ b/src/WebApi_battery.cpp @@ -109,5 +109,7 @@ void WebApiBatteryClass::onAdminPost(AsyncWebServerRequest* request) if (config.Battery_Enabled) { PylontechCanReceiver.enable(); + } else { + PylontechCanReceiver.disable(); } }