Victron BMV 700 - WEMOS D1 Mini

Ok, I’ve spent some time re-learning some of this stuff and ended up reinstalling the whole software suite before I could managed to compile my original code.

So now I’ve got Arduino V1.8.9, ESP Core 2.5.2 & Blynk 0.6.1
I haven’t had time to upload anything new to my Wemos board and with that core number I’m sure it probably wouldn’t work anyway, I think the previous core version was 2.3.0 but I might just try it before downgrading back to 2.3.0.

I’ve got another board at my office and a BME280 so maybe I’ll recompile with that one to save breaking my operational board which is ok other than it crashes every few days.

The chap that was previously helping me did create another version of the code with the additional parts to check for internet connection and reboot if not available, I never tested this and it does not have the code for the temp sensor but I’ll post it here in case its any use.

#include <SoftwareSerial.h>
#include <SPI.h>
#define BLYNK_PRINT Serial //Jue
#include <ESP8266WiFi.h> //Jue
#include <BlynkSimpleEsp8266.h> //Jue
BlynkTimer timer;

char auth[] = ""; //Jue
char ssid[] = ""; //Jue
char pass[] = ""; //Jue
 
SoftwareSerial Victron(D7,D8); // RX, TX //Jue changed this
WidgetLED led1(V6);
 
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
 
float Current;
float Voltage;
float SOC;
float CE;

String stringOne;
 
void setup()
{
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.begin(19200);
  Victron.begin(19200);
  Blynk.begin(auth, ssid, pass); //Jue
  readVictronData(); // This will initially set Victron variables 
  timer.setInterval(5000, sendDatatoBlynk); // Send Data to blynk every 5 sec
}

// Main void loop that will continously be running
void loop() { 
  if (WiFi.status() != WL_CONNECTED) { // If wifi is not connected run reconnect() function
    reconnect();
  }

  if (Blynk.connected()) {  // Checking Blynk connection. If connected...
    led1.on();          // Turn on Virtual LED in app
    Blynk.run();        // Run Blynk
    readVictronData();  // Read Victron Data
    timer.run();        // Will make sure sendDatatoBlynk is called
   } else {
    Serial.println("Whoops blynk is not connected need to do something about that");
    led1.off();             // Turn of LED in app if connection to blynk is down
    CheckBlynkConnection(); // If Blynk connection is down, this will be called, should reconnect
   } 
  }

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

void sendDatatoBlynk() {
  // These if statements make sure only to send data if data has been set in the main loop already

  Serial.println("I am called within sendDatatoBlynk function");
  
  if(Current){
    Blynk.virtualWrite (V2,Current);  
  }
  if(Voltage){
    Blynk.virtualWrite (V1,Voltage);
  }
  if(SOC){
    Blynk.virtualWrite (V4,SOC);
  }
  if(CE){
    Blynk.virtualWrite (V3,CE);
  }
}

void readVictronData() {
  if (Victron.available()) {
    c = Victron.read();
 
    if (V_buffer.length() <32) {
      V_buffer += c;
    }
 
    if (c == '\n') {  // New line.
 
      if (V_buffer.startsWith("I")) {
        String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
        double temp_int = temp_string.toInt();
        Current = (float) temp_int/1000;
      }     
 
      if (V_buffer.startsWith("V")) {
        String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
        int temp_int = temp_string.toInt();
        Voltage = (float) temp_int/1000;
       
      }     
 
      if (V_buffer.startsWith("SOC")) {
        String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
        int temp_int = temp_string.toInt();
        SOC = (float) temp_int/10;
       
 
      }
      if (V_buffer.startsWith("CE")) {
        String temp_string = V_buffer.substring(V_buffer.indexOf("\t")+1);
        int temp_int = temp_string.toInt();
        CE = (float) temp_int/1000;
       
      }
  
     
      V_buffer=""; // Empty V_Buffer
    }
  } else {  // If Victron not available reset software
    Serial.println("Reset..");
    ESP.restart();
  }
}

void CheckBlynkConnection(){    // check if connected to Blynk server
  if(!Blynk.connected()){
    Serial.println("Not connected to Blynk server"); 
    Blynk.connect();  // try to connect to server with default timeout
  }
  else{
    Serial.println("Connected to Blynk server");     
  }
}

At least the Void Loop is a little bit tidier in this one.

Not much point in using WiFi.begin then Blynk.begin.
It would make more sense to use WiFi.begin, Blynk.config then Blynk.connect.

Pete.

1 Like