loop für ve.direct

This commit is contained in:
helgeerbe 2022-08-15 10:56:37 +02:00
parent 70136e20aa
commit 2ff8f84387
5 changed files with 55 additions and 30 deletions

View File

@ -20,7 +20,6 @@ public:
void loop(); void loop();
private: private:
std::map<String, String> _kv_map; std::map<String, String> _kv_map;
VeDirectFrameHandler _myve;
uint32_t _lastPublish; uint32_t _lastPublish;
}; };

View File

@ -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. // The name of the record that contains the checksum.
static constexpr char checksumTagName[] = "CHECKSUM"; static constexpr char checksumTagName[] = "CHECKSUM";
VeDirectFrameHandler VeDirect;
VeDirectFrameHandler::VeDirectFrameHandler() : VeDirectFrameHandler::VeDirectFrameHandler() :
//mStop(false), // don't know what Victron uses this for, not using //mStop(false), // don't know what Victron uses this for, not using
veName(), veName(),
@ -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 * rxData
* This function is called by the application which passes a byte of serial data * This function is called by the application which passes a byte of serial data

View File

@ -7,9 +7,7 @@
* 2021.02.23 - 0.3 - change frameLen to 22 per VE.Direct Protocol version 3.30 * 2021.02.23 - 0.3 - change frameLen to 22 per VE.Direct Protocol version 3.30
* *
*/ */
#pragma once
#ifndef FRAMEHANDLER_H_
#define FRAMEHANDLER_H_
#include <Arduino.h> #include <Arduino.h>
@ -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 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. 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 { class VeDirectFrameHandler {
public: public:
VeDirectFrameHandler(); 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 veName[buffLen][nameLen] = { }; // public buffer for received names
char veValue[buffLen][valueLen] = { }; // public buffer for received values char veValue[buffLen][valueLen] = { }; // public buffer for received values
@ -54,10 +61,12 @@ private:
char tempName[frameLen][nameLen]; // private buffer for received names char tempName[frameLen][nameLen]; // private buffer for received names
char tempValue[frameLen][valueLen]; // private buffer for received values 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 textRxEvent(char *, char *);
void frameEndEvent(bool); void frameEndEvent(bool);
void logE(const char *, const char *); void logE(const char *, const char *);
bool hexRxEvent(uint8_t); bool hexRxEvent(uint8_t);
}; };
#endif // FRAMEHANDLER_H_ extern VeDirectFrameHandler VeDirect;

View File

@ -2,25 +2,24 @@
/* /*
* Copyright (C) 2022 Helge Erbe and others * Copyright (C) 2022 Helge Erbe and others
*/ */
#include "MqttVedirectPublishing.h"
#include "VeDirectFrameHandler.h" #include "VeDirectFrameHandler.h"
#include "MqttVedirectPublishing.h"
#include "MqttSettings.h" #include "MqttSettings.h"
MqttVedirectPublishingClass MqttVedirectPublishing; MqttVedirectPublishingClass MqttVedirectPublishing;
void MqttVedirectPublishingClass::init() void MqttVedirectPublishingClass::init()
{ {
Serial2.begin(19200, SERIAL_8N1, VICTRON_PIN_RX, VICTRON_PIN_TX);
Serial2.flush();
} }
void MqttVedirectPublishingClass::loop() void MqttVedirectPublishingClass::loop()
{ {
CONFIG_T& config = Configuration.get(); CONFIG_T& config = Configuration.get();
if (!MqttSettings.getConnected() && !config.Vedirect_Enabled) { if (!MqttSettings.getConnected() || !config.Vedirect_Enabled) {
return; return;
} }
@ -28,27 +27,12 @@ void MqttVedirectPublishingClass::loop()
String key; String key;
String value; String value;
bool bChanged; bool bChanged;
unsigned long now = millis();
while ( Serial2.available()) {
if ((millis() - now) > 100) {
now = millis();
yield();
}
_myve.rxData(Serial2.read());
}
yield();
String topic = ""; String topic = "";
for ( int i = 0; i < _myve.veEnd; i++ ) { for ( int i = 0; i < VeDirect.veEnd; i++ ) {
key = _myve.veName[i]; key = VeDirect.veName[i];
value = _myve.veValue[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. // Add new key, value pairs to map and update changed values.
// Mark changed values // Mark changed values
auto a = _kv_map.find(key); auto a = _kv_map.find(key);

View File

@ -4,6 +4,7 @@
*/ */
#include "Configuration.h" #include "Configuration.h"
#include "Hoymiles.h" #include "Hoymiles.h"
#include "VeDirectFrameHandler.h"
#include "MqttHassPublishing.h" #include "MqttHassPublishing.h"
#include "MqttPublishing.h" #include "MqttPublishing.h"
#include "MqttVedirectPublishing.h" #include "MqttVedirectPublishing.h"
@ -98,6 +99,11 @@ void setup()
} }
} }
Serial.println(F("done")); Serial.println(F("done"));
// Initialize ve.direct communication
Serial.print(F("Initialize ve.direct interface... "));
VeDirect.init();
Serial.println(F("done"));
} }
void loop() void loop()
@ -106,10 +112,16 @@ void loop()
yield(); yield();
Hoymiles.loop(); Hoymiles.loop();
yield(); yield();
if (Configuration.get().Vedirect_Enabled) {
VeDirect.loop();
yield();
}
MqttPublishing.loop(); MqttPublishing.loop();
yield(); yield();
MqttVedirectPublishing.loop(); if (Configuration.get().Vedirect_Enabled) {
yield(); MqttVedirectPublishing.loop();
yield();
}
MqttHassPublishing.loop(); MqttHassPublishing.loop();
yield(); yield();
WebApi.loop(); WebApi.loop();