Skip to content

Commit

Permalink
loop für ve.direct
Browse files Browse the repository at this point in the history
  • Loading branch information
helgeerbe committed Aug 15, 2022
1 parent 70136e2 commit 2ff8f84
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
1 change: 0 additions & 1 deletion include/MqttVedirectPublishing.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class MqttVedirectPublishingClass {
void loop();
private:
std::map<String, String> _kv_map;
VeDirectFrameHandler _myve;
uint32_t _lastPublish;
};

Expand Down
21 changes: 21 additions & 0 deletions lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ char MODULE[] = "VE.Frame"; // Victron seems to use this to find out where loggi
// The name of the record that contains the checksum.
static constexpr char checksumTagName[] = "CHECKSUM";

VeDirectFrameHandler VeDirect;

VeDirectFrameHandler::VeDirectFrameHandler() :
//mStop(false), // don't know what Victron uses this for, not using
veName(),
Expand All @@ -55,6 +57,25 @@ VeDirectFrameHandler::VeDirectFrameHandler() :
{
}

void VeDirectFrameHandler::init()
{
Serial2.begin(19200, SERIAL_8N1, VICTRON_PIN_RX, VICTRON_PIN_TX);
Serial2.flush();
}

void VeDirectFrameHandler::loop()
{
unsigned long now = millis();

while ( Serial2.available()) {
if ((millis() - now) > 500) {
now = millis();
yield();
}
rxData(Serial2.read());
}
}

/*
* rxData
* This function is called by the application which passes a byte of serial data
Expand Down
19 changes: 14 additions & 5 deletions lib/VeDirectFrameHandler/VeDirectFrameHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
* 2021.02.23 - 0.3 - change frameLen to 22 per VE.Direct Protocol version 3.30
*
*/

#ifndef FRAMEHANDLER_H_
#define FRAMEHANDLER_H_
#pragma once

#include <Arduino.h>

Expand All @@ -18,12 +16,21 @@ const byte nameLen = 9; // VE.Direct Protocol: max name
const byte valueLen = 33; // VE.Direct Protocol: max value size is 33 including /0
const byte buffLen = 40; // Maximum number of lines possible from the device. Current protocol shows this to be the BMV700 at 33 lines.

#ifndef VICTRON_PIN_TX
#define VICTRON_PIN_TX 21
#endif

#ifndef VICTRON_PIN_RX
#define VICTRON_PIN_RX 22
#endif

class VeDirectFrameHandler {

public:

VeDirectFrameHandler();
void rxData(uint8_t inbyte); // byte of serial data to be passed by the application
void init();
void loop();

char veName[buffLen][nameLen] = { }; // public buffer for received names
char veValue[buffLen][valueLen] = { }; // public buffer for received values
Expand Down Expand Up @@ -54,10 +61,12 @@ class VeDirectFrameHandler {
char tempName[frameLen][nameLen]; // private buffer for received names
char tempValue[frameLen][valueLen]; // private buffer for received values

void rxData(uint8_t inbyte); // byte of serial data to be passed by the application
void textRxEvent(char *, char *);
void frameEndEvent(bool);
void logE(const char *, const char *);
bool hexRxEvent(uint8_t);
};

#endif // FRAMEHANDLER_H_
extern VeDirectFrameHandler VeDirect;

28 changes: 6 additions & 22 deletions src/MqttVedirectPublishing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,37 @@
/*
* Copyright (C) 2022 Helge Erbe and others
*/
#include "MqttVedirectPublishing.h"
#include "VeDirectFrameHandler.h"
#include "MqttVedirectPublishing.h"
#include "MqttSettings.h"




MqttVedirectPublishingClass MqttVedirectPublishing;

void MqttVedirectPublishingClass::init()
{
Serial2.begin(19200, SERIAL_8N1, VICTRON_PIN_RX, VICTRON_PIN_TX);
Serial2.flush();
}

void MqttVedirectPublishingClass::loop()
{
CONFIG_T& config = Configuration.get();

if (!MqttSettings.getConnected() && !config.Vedirect_Enabled) {
if (!MqttSettings.getConnected() || !config.Vedirect_Enabled) {
return;
}

if (millis() - _lastPublish > (config.Mqtt_PublishInterval * 1000)) {
String key;
String value;
bool bChanged;
unsigned long now = millis();

while ( Serial2.available()) {
if ((millis() - now) > 100) {
now = millis();
yield();
}
_myve.rxData(Serial2.read());
}
yield();

String topic = "";
for ( int i = 0; i < _myve.veEnd; i++ ) {
key = _myve.veName[i];
value = _myve.veValue[i];
for ( int i = 0; i < VeDirect.veEnd; i++ ) {
key = VeDirect.veName[i];
value = VeDirect.veValue[i];

// just for debug
Serial.print(key.c_str());
Serial.print("= ");
Serial.println(value.c_str());

// Add new key, value pairs to map and update changed values.
// Mark changed values
auto a = _kv_map.find(key);
Expand Down
16 changes: 14 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#include "Configuration.h"
#include "Hoymiles.h"
#include "VeDirectFrameHandler.h"
#include "MqttHassPublishing.h"
#include "MqttPublishing.h"
#include "MqttVedirectPublishing.h"
Expand Down Expand Up @@ -98,6 +99,11 @@ void setup()
}
}
Serial.println(F("done"));

// Initialize ve.direct communication
Serial.print(F("Initialize ve.direct interface... "));
VeDirect.init();
Serial.println(F("done"));
}

void loop()
Expand All @@ -106,10 +112,16 @@ void loop()
yield();
Hoymiles.loop();
yield();
if (Configuration.get().Vedirect_Enabled) {
VeDirect.loop();
yield();
}
MqttPublishing.loop();
yield();
MqttVedirectPublishing.loop();
yield();
if (Configuration.get().Vedirect_Enabled) {
MqttVedirectPublishing.loop();
yield();
}
MqttHassPublishing.loop();
yield();
WebApi.loop();
Expand Down

0 comments on commit 2ff8f84

Please sign in to comment.