ESP8266 VE.Direct serial problems


Since I am banging my head for a few days now i was wondering if there is someone that can help me with my code or at least see what is wrong with it.

Long story short.

ESP stops responding sometime after one loop and sometimes it is working a few days,hours.....

Adding link to my github with code and everything else.


There is no reason not to publish your sketch here directly. Many people will not look at a link.
I think you need to verify that you are connected to MQTT (not just to wifi) before attempting to publish.
Also you are not using the String class correctly, though on an ESP that can hardly be an issue.

Thank you for your answer, but i do check for connection on every loop before reading from victron. What about that string class?


You check for a 'WiFi' connection. That is not the same as checking if MQTT is also connected. Of course no Wifi no MQTT, but You can have WiFi but no MQTT.

Well, it is a bit of a long story, and if you google a bit you can find stuff about it all over the forum. The issue is that 'String' objects are declared on the heap, and particularly increasing, decreasing String can cause memory fragmentation. In your case, there is only 1 function that uses the String class, unfortunately you did declare 1 String variable globally. I am going to post your whole code here now, so it is easier to explain.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>
#include <SPI.h>
const char* ssid = "*";
const char* password =  "*";
const char* mqttServer = "*";
const int mqttPort = 1883;
const char* mqttUser = "*";
const char* mqttPassword = "*";

SoftwareSerial Victron(D7,D8); // RX, TX 
WiFiClient espClient;
PubSubClient client(espClient);

char p_buffer[80];
#define P(str) (strcpy_P(p_buffer, PSTR(str)), p_buffer)
char c;
String V_buffer;  // Buffer to hold data from the Victron monitor

char Current[6];
char Voltage[6];
char SOC[7];
char CE[7];

String stringOne;

void reconnect() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");  // As long as it is trying to connect print a . each 500ms to debugger
  Serial.println("WiFi re-connected");

void readVictronData() {
  if (Victron.available()) {
    c =;
    if (V_buffer.length() < 40) {
      V_buffer += c;
    if (c == '\n') {  // New line.
      if (V_buffer.startsWith("I")) {
        String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
        temp_string.toCharArray(Current, temp_string.length() + 1);
       client.publish("BMV/Current", Current); 
       Serial.print("Current: ");
      if (V_buffer.startsWith("V")) {
        String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
        temp_string.toCharArray(Voltage, temp_string.length() + 1);
        client.publish("BMV/Voltage", Voltage); 
      if (V_buffer.startsWith("SOC")) {
        String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
        temp_string.toCharArray(SOC, temp_string.length() + 1);
        client.publish("BMV/SOC", SOC); 
      if (V_buffer.startsWith("CE")) {
        String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
        temp_string.toCharArray(CE, temp_string.length() + 1);
        client.publish("BMV/CE", CE); 
     V_buffer=""; // Empty V_Buffer


void connect_mqtt() {
  client.setServer(mqttServer, mqttPort);
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
    if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
    } else {
      Serial.print("failed with state ");
    client.publish("BMV/Welcome", "Welcome from Victron");

void setup() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi..");
  Serial.println("Connected to the WiFi network");

void loop() {
  if (WiFi.status() != WL_CONNECTED) { // If wifi is not connected run reconnect() function
  readVictronData();  // Read Victron Data

If you move

String V_buffer;  // Buffer to hold data from the Victron monitor

into readVictronData(), at least the whole heap is actually cleared (with exception of whatever the libraries are using but you can trust they clean up after themselves) at the end of the function when all the objects are destroyed, that should prevent fragmentation.

You see, connected or not, you still publish()

But while loop is repeated until expression is false. So if MQTT is not connected it will not exit loop.

Am I right?

Yes but after you .connect() again in an if statement.