Skip to content

Commit

Permalink
Pylontech enhancement (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
madmartin authored May 24, 2023
1 parent a3e7439 commit 52af52e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
2 changes: 1 addition & 1 deletion include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion include/PylontechCanReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
94 changes: 72 additions & 22 deletions src/PylontechCanReceiver.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include "PylontechCanReceiver.h"
#include "Battery.h"
#include "Configuration.h"
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/WebApi_battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,7 @@ void WebApiBatteryClass::onAdminPost(AsyncWebServerRequest* request)

if (config.Battery_Enabled) {
PylontechCanReceiver.enable();
} else {
PylontechCanReceiver.disable();
}
}

0 comments on commit 52af52e

Please sign in to comment.