From dd50d61b2618856d361f6b2e16cccfa9890d40de Mon Sep 17 00:00:00 2001 From: cterwilliger Date: Fri, 10 Apr 2020 22:38:31 -0700 Subject: [PATCH 1/9] Add files via upload --- src/VEDirect.cpp | 53 ++++++++++++++++++++++++++++-------------------- src/VEDirect.h | 7 +++++-- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/VEDirect.cpp b/src/VEDirect.cpp index 68c720f..94708fd 100644 --- a/src/VEDirect.cpp +++ b/src/VEDirect.cpp @@ -10,12 +10,17 @@ - Implementation Updates: - 2019-07-14 See VEDirect.h + - 2020-04-10 Converted to SoftwareSerial, added checks for null pointers for ESP8266 ******************************************************************/ #include "VEDirect.h" -VEDirect::VEDirect(HardwareSerial& port): - VESerial(port) +// Serial variables +#define rxPin D7 +#define txPin D8 + +VEDirect::VEDirect(): + VESerial (*new SoftwareSerial(rxPin, txPin)) // Initialise the serial port that the // VE.Direct device is connected to and // store it for later use. @@ -28,7 +33,7 @@ VEDirect::~VEDirect() { uint8_t VEDirect::begin() { // Check connection the serial port - VESerial.begin(19200); + VESerial.begin(VED_BAUD_RATE); if (VESerial) { delay(500); if(VESerial.available()) { @@ -85,26 +90,30 @@ int32_t VEDirect::read(uint8_t target) { } label = strtok(line, "\t"); - if (strcmp_P(label, ved_labels[target]) == 0) { - value_str = strtok(0, "\t"); - if (value_str[0] == 'O') { //ON OFF type - if (value_str[1] == 'N') { - ret = 1; // ON - break; - } else { - ret = 0; // OFF - break; - } - } else { - sscanf(value_str, "%ld", &ret); - break; + if (label) { + if (strcmp_P(label, ved_labels[target]) == 0) { + value_str = strtok(0, "\t"); + if (value_str) { + if (value_str[0] == 'O') { //ON OFF type + if (value_str[1] == 'N') { + ret = 1; // ON + break; + } else { + ret = 0; // OFF + break; + } + } else { + sscanf(value_str, "%ld", &ret); + break; + } + } + } else { // Line not of interest + lines--; + loops = VED_MAX_READ_LOOPS; + line[0] = '\0'; + idx = 0; } - } else { // Line not of interest - lines--; - loops = VED_MAX_READ_LOOPS; - line[0] = '\0'; - idx = 0; - } + } } } return ret; diff --git a/src/VEDirect.h b/src/VEDirect.h index 3840a3b..d5ab2ff 100644 --- a/src/VEDirect.h +++ b/src/VEDirect.h @@ -15,12 +15,15 @@ - Target labels extendible with enum and PROGMEM strings - Retired copy_raw_to_serial0() code - use VE_DUMP on read - Added some tunable parameters see #defines + - 2020-04-10: + - Convert to SoftwareSerial so ESP8266 can use native usb serial for debug ******************************************************************/ #ifndef VEDIRECT_H_ #define VEDIRECT_H_ #include +#include // Tunable parameters - defaults tested on mega2560 R3 #define VED_LINE_SIZE 30 // Seems to be plenty. VE.Direct protocol could change @@ -52,13 +55,13 @@ const char ved_labels[VE_LAST_LABEL][VED_MAX_LEBEL_SIZE] PROGMEM = { class VEDirect { public: - VEDirect(HardwareSerial& port); + VEDirect(); virtual ~VEDirect(); uint8_t begin(); int32_t read(uint8_t target); void copy_raw_to_serial0(); // kept for backwards compatibility private: - HardwareSerial& VESerial; + SoftwareSerial& VESerial; }; #endif /* VEDIRECT_H_ */ From 012841ff23919dc37d3589fb3255220677bac4f0 Mon Sep 17 00:00:00 2001 From: cterwilliger Date: Fri, 10 Apr 2020 22:39:03 -0700 Subject: [PATCH 2/9] Add files via upload --- examples/ReadVEDirect/ReadVEDirect.ino | 128 +++++++++++++------------ 1 file changed, 65 insertions(+), 63 deletions(-) diff --git a/examples/ReadVEDirect/ReadVEDirect.ino b/examples/ReadVEDirect/ReadVEDirect.ino index 757353a..6d5a395 100644 --- a/examples/ReadVEDirect/ReadVEDirect.ino +++ b/examples/ReadVEDirect/ReadVEDirect.ino @@ -1,63 +1,65 @@ -/****************************************************************** - VEDirect Arduino - - Copyright 2018, 2019, Brendan McLearie - Distributed under MIT license - see LICENSE.txt - - See README.md - - File: ReadVEDirect.ino / ReadVEDirect.cpp - - Provides example use of the VEDirect library -******************************************************************/ - -#include "Arduino.h" -#include "VEDirect.h" - -// 32 bit ints to collect the data from the device -int32_t VE_soc, VE_power, VE_voltage, VE_current; -// Boolean to collect an ON/OFF value -uint8_t VE_alarm; - -// VEDirect instantiated with relevant serial object -VEDirect myve(Serial3); - -void setup() { - Serial.begin(9600); // Adjust as needed -} - -void loop() { - Serial.println("Reading values from Victron Energy device using VE.Direct text mode"); - Serial.println(); - - // Read the data - if(myve.begin()) { // test connection - VE_soc = myve.read(VE_SOC); - VE_power = myve.read(VE_POWER); - VE_voltage = myve.read(VE_VOLTAGE); - VE_current = myve.read(VE_CURRENT); - VE_alarm = myve.read(VE_ALARM); - } else { - Serial.println("Could not open serial port to VE device"); - while (1); - } - - // Print each of the values - Serial.print("State of Charge (SOC): "); - Serial.println(VE_soc, DEC); - Serial.print("Power: "); - Serial.println(VE_power, DEC); - Serial.print("Voltage "); - Serial.println(VE_voltage, DEC); - Serial.print("Current "); - Serial.println(VE_current, DEC); - Serial.print("Alarm "); - Serial.println(VE_alarm, DEC); - Serial.println(); - - // Copy the raw data stream (minus the \r) to Serial0 - // Call read() with a token that won't match any VE.Direct labels - Serial.println("All data from device:"); - myve.read(VE_DUMP); - Serial.println(); - delay(10000); -} +/****************************************************************** + VEDirect Arduino + + Copyright 2018, 2019, Brendan McLearie + Distributed under MIT license - see LICENSE.txt + + See README.md + + File: ReadVEDirect.ino / ReadVEDirect.cpp + - Provides example use of the VEDirect library + + 2020.04.10 - convert to SoftwareSerial +******************************************************************/ + +#include "Arduino.h" +#include "VEDirect.h" + +// 32 bit ints to collect the data from the device +int32_t VE_soc, VE_power, VE_voltage, VE_current; +// Boolean to collect an ON/OFF value +uint8_t VE_alarm; + +// VEDirect instantiated with relevant serial object +VEDirect myve; + +void setup() { + Serial.begin(19200); // Adjust as needed - DEBUG serial port +} + +void loop() { + Serial.println("Reading values from Victron Energy device using VE.Direct text mode"); + Serial.println(); + + // Read the data + if(myve.begin()) { // test connection + VE_soc = myve.read(VE_SOC); + VE_power = myve.read(VE_POWER); + VE_voltage = myve.read(VE_VOLTAGE); + VE_current = myve.read(VE_CURRENT); + VE_alarm = myve.read(VE_ALARM); + } else { + Serial.println("Could not open serial port to VE device"); + while (1); + } + + // Print each of the values + Serial.print("State of Charge (SOC): "); + Serial.println(VE_soc, DEC); + Serial.print("Power: "); + Serial.println(VE_power, DEC); + Serial.print("Voltage "); + Serial.println(VE_voltage, DEC); + Serial.print("Current "); + Serial.println(VE_current, DEC); + Serial.print("Alarm "); + Serial.println(VE_alarm, DEC); + Serial.println(); + + // Copy the raw data stream (minus the \r) to Serial0 + // Call read() with a token that won't match any VE.Direct labels + Serial.println("All data from device:"); + myve.read(VE_DUMP); + Serial.println(); + delay(10000); +} From 70df74fa2e4e18e17a3df75eadc3db483cedaac5 Mon Sep 17 00:00:00 2001 From: cterwilliger Date: Fri, 10 Apr 2020 22:55:08 -0700 Subject: [PATCH 3/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 28770f8..85322dd 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Provides: ```C #include "VEDirect.h" -VEDirect my_bmv(Serial3); +VEDirect my_bmv; my_int32 = my_bmv.read(VE_SOC); // VE_SOC, VE_VOLTAGE, VE_CURRENT, VE_POWER, VE_ALARM From dd8c6fec360e124dd17db62910e089e1a7e5f8e4 Mon Sep 17 00:00:00 2001 From: Chris Terwilliger Date: Mon, 13 Apr 2020 21:38:17 -0700 Subject: [PATCH 4/9] Update VEDirect.cpp --- src/VEDirect.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/VEDirect.cpp b/src/VEDirect.cpp index 94708fd..e79ad19 100644 --- a/src/VEDirect.cpp +++ b/src/VEDirect.cpp @@ -15,12 +15,9 @@ #include "VEDirect.h" -// Serial variables -#define rxPin D7 -#define txPin D8 - VEDirect::VEDirect(): - VESerial (*new SoftwareSerial(rxPin, txPin)) +VEDirect::VEDirect(byte rxPin, byte txPin): + VESerial(*new SoftwareSerial(rxPin, txPin)) // Initialise the serial port that the // VE.Direct device is connected to and // store it for later use. From dc9d2938b2098cd0ba1522a5454dd46a1fc91a9b Mon Sep 17 00:00:00 2001 From: Chris Terwilliger Date: Mon, 13 Apr 2020 21:40:24 -0700 Subject: [PATCH 5/9] Update VEDirect.h --- src/VEDirect.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/VEDirect.h b/src/VEDirect.h index d5ab2ff..0d16186 100644 --- a/src/VEDirect.h +++ b/src/VEDirect.h @@ -55,7 +55,7 @@ const char ved_labels[VE_LAST_LABEL][VED_MAX_LEBEL_SIZE] PROGMEM = { class VEDirect { public: - VEDirect(); + VEDirect(byte rxPin, byte txPin); virtual ~VEDirect(); uint8_t begin(); int32_t read(uint8_t target); From 2a2507a438cad8a66fcb325f3094d7f9ae07588b Mon Sep 17 00:00:00 2001 From: Chris Terwilliger Date: Mon, 13 Apr 2020 21:42:34 -0700 Subject: [PATCH 6/9] Update ReadVEDirect.ino --- examples/ReadVEDirect/ReadVEDirect.ino | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/ReadVEDirect/ReadVEDirect.ino b/examples/ReadVEDirect/ReadVEDirect.ino index 6d5a395..cc97fd4 100644 --- a/examples/ReadVEDirect/ReadVEDirect.ino +++ b/examples/ReadVEDirect/ReadVEDirect.ino @@ -15,13 +15,17 @@ #include "Arduino.h" #include "VEDirect.h" +// Serial pins +#define rxPin D7 +#define txPin D8 + // 32 bit ints to collect the data from the device int32_t VE_soc, VE_power, VE_voltage, VE_current; // Boolean to collect an ON/OFF value uint8_t VE_alarm; // VEDirect instantiated with relevant serial object -VEDirect myve; +VEDirect myve(rxPin, txPin); void setup() { Serial.begin(19200); // Adjust as needed - DEBUG serial port From 88cdee2b28bd358a2c822ecca88e7a751b829381 Mon Sep 17 00:00:00 2001 From: Chris Terwilliger Date: Mon, 13 Apr 2020 21:44:31 -0700 Subject: [PATCH 7/9] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 85322dd..02c14b7 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,11 @@ Provides: ```C #include "VEDirect.h" -VEDirect my_bmv; +// Serial pins +#define rxPin D7 +#define txPin D8 + +VEDirect my_bmv(rxPin, txPin); my_int32 = my_bmv.read(VE_SOC); // VE_SOC, VE_VOLTAGE, VE_CURRENT, VE_POWER, VE_ALARM From eb86527baaf8723f112323e89ffe7c7bd4933cfa Mon Sep 17 00:00:00 2001 From: Chris Terwilliger Date: Mon, 13 Apr 2020 21:47:11 -0700 Subject: [PATCH 8/9] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 02c14b7..2a9e90f 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ Provides: - Defaults set to read Volts, Power, Current, State of Charge (SOC), Alarm - Easily extendible by adding labels for any other stats and settings of interest - A diagnostic "full dump" of everything coming from the device + + This branch implements SoftwareSerial primarily for ESP8266 boards that need their native USB port for debug purposes. ### Usage: ```C From c8a63daa40b96859d042a856cc778a410c314118 Mon Sep 17 00:00:00 2001 From: Chris Terwilliger Date: Mon, 31 Aug 2020 17:31:27 -0600 Subject: [PATCH 9/9] errant constructor in VEDirect.cpp removed. --- src/VEDirect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/VEDirect.cpp b/src/VEDirect.cpp index e79ad19..133bb78 100644 --- a/src/VEDirect.cpp +++ b/src/VEDirect.cpp @@ -11,11 +11,11 @@ Updates: - 2019-07-14 See VEDirect.h - 2020-04-10 Converted to SoftwareSerial, added checks for null pointers for ESP8266 + - 2020-08-31 Removed errant constructor ******************************************************************/ #include "VEDirect.h" -VEDirect::VEDirect(): VEDirect::VEDirect(byte rxPin, byte txPin): VESerial(*new SoftwareSerial(rxPin, txPin)) // Initialise the serial port that the