loop für ve.direct
This commit is contained in:
parent
70136e20aa
commit
2ff8f84387
@ -20,7 +20,6 @@ public:
|
||||
void loop();
|
||||
private:
|
||||
std::map<String, String> _kv_map;
|
||||
VeDirectFrameHandler _myve;
|
||||
uint32_t _lastPublish;
|
||||
};
|
||||
|
||||
|
||||
@ -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(),
|
||||
@ -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
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -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
|
||||
@ -54,10 +61,12 @@ private:
|
||||
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;
|
||||
|
||||
|
||||
@ -2,25 +2,24 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
@ -28,27 +27,12 @@ void MqttVedirectPublishingClass::loop()
|
||||
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);
|
||||
|
||||
16
src/main.cpp
16
src/main.cpp
@ -4,6 +4,7 @@
|
||||
*/
|
||||
#include "Configuration.h"
|
||||
#include "Hoymiles.h"
|
||||
#include "VeDirectFrameHandler.h"
|
||||
#include "MqttHassPublishing.h"
|
||||
#include "MqttPublishing.h"
|
||||
#include "MqttVedirectPublishing.h"
|
||||
@ -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()
|
||||
@ -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();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user