BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Problem with reading data form inverter when Blynk added

Hello everyone!
I have problem with my sketch when I add Blynk to it. What I’m trying to do is get data for solar hybrid inverter, for example, input grid AC voltage, output AC voltage, DC voltage coming for solar panels, charge current from solar panels and so on.
To ‘speak’ with inverter is has special protocol where we request data by phseudo-ASCII commands, for example, QPIGS, as can be seen from scetch below, and answers with a sting containing parameter.

Everything work as expected when I run sketch in standart way - in loop. Please see attached screenshoot.
But when I try to run the same sketch with Blynk (everything in separate function and run through timer every 5 sec), the data from inverter become wrong. Please see attached screenshoot.

I’m using :
• Arduino UNO with Ethernet Shield
• WaveShare RS232 board to connect an inverter and arduino
• Blynk Library version 0.5.4

I really do not understand what’s a reason for such behavious. What am I doing wrong?
Thanks.


#include <SoftwareSerial.h>
#include <string.h>
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

//*-- Global variables
char U_grid [6] ;        // Input Grid voltage 
char F_grid [6] ;        // Frequency of grid voltage 
char U_out [6] ;         // Output voltage
int S_out ;              // Output power full(VA)
int P_out ;              // Output power active (W)
int Load ;               // Load (%)
short U_bat_int ;        // Battery voltage intger part 
short U_bat_fl ;         // Battery voltage float part
float U_bat_dec ;        // Battery voltage 
int I_charge ;           // Battery charging current
int C_bat ;              // Battery charge level (%)
int T_inv ;              // Temperature of inverter (NTC)
int I_pv_bat ;           // Battery charging current (from solar panels)
char U_pv [6] ;          // Voltage of solar panels
int I_discharge ;        // battery discharge current

char QPIGS[8] = {81, 80, 73, 71, 83, 183, 169, 13};  // Request parameters

char auth[] = "12c3acd7a36b4e48b4f14538e03a045b";
#define arduino_bitrate 9600              // Arduino serial port bitrate 
                                      // (0 -> RX, 1 -> TX)
#define com_bitrate 2400              // Bitrate arduino-inverter
#define com_rxpin 7                  // RX software serial
#define com_txpin 8                  // TX software serial
#define W5100_CS  10
#define SDCARD_CS 4

BlynkTimer timer;

//*-- Software Serial
SoftwareSerial comSerial( com_rxpin, com_txpin ); // RX, TX

void setup() {
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card 
  Serial.begin(arduino_bitrate);
  
  Serial.setTimeout(2000);      // Serial  port timeout
  comSerial.begin(com_bitrate); //Start software serial to speak with inverter
    
  Serial.println("Start"); // Debug
  delay(2000);
    
//Send test request to inverter
  comSerial.println("Q");
  delay(2000);
  while (comSerial.available()) 
  {
    comSerial.read();
    }
//Blynk.begin(auth);
  // You can also specify server:
  Blynk.begin(auth, "blynk-cloud.com", 80);  
  //Blynk.begin(auth, IPAddress(192,168,1,44), 80);
  
  timer.setInterval(5000L, updateInvData); //Request parameters every 5 seconds
}

void updateInvData() {
    //*-- Request current parameters --*//
  Serial.println("Inv request"); // For debug
  comSerial.print(QPIRI); // Send request string 
  delay(1000);
  Serial.println(QPIRI);  // for debug, check what did we send
  delay(1000);
  int i = 0;
  char buffer[120]; //array to store reply

  // If there is a reply - read data
  if (comSerial.available()) {
    delay(1000);

    //Write data to buffer
    while ( comSerial.available() && i < 119) {      
      buffer[i++] = comSerial.read();      
    }
    buffer[i++] = '\0';  //close array
  }

  //if buffer is not empty
  if (i > 0) {
    Serial.println("Buffer:");
    Serial.println(buffer); // For debug
    //Parse the reply (string to separate values)
    sscanf(buffer, "(%s%s%s%*s%d%d%d%*d%d%*c%d%d%d%d%d%s%*s%d", &U_grid, &F_grid, &U_out, &S_out, &P_out, &Load, &U_bat_int, &U_bat_fl, &I_charge, &C_bat, &T_inv, &I_pv_bat, &U_pv, &I_discharge );
  }
  // U_bat in DEC 
  U_bat_dec = U_bat_int + .01*U_bat_fl; // For debug to check if data were parsed correctly 
  Serial.println(U_bat_dec);// Print to serial monitor U_bat_dec value
  Blynk.virtualWrite(V1, millis() / 1000); // Test write to virtual pin
  Blynk.virtualWrite(V2, F_grid); // Test write to virtual pin
  Blynk.virtualWrite(V3, U_out); // Test write to virtual pin   
}
void loop() {
  Blynk.run();
  timer.run();
  }


delay() and other blocking while() loops will cause Blynk disconnections.

Not to mention adding up the total of 3 seconds of delay inside a loop that is trying to run every 5 seconds… delay() stops the main loop from running, that includes the timer, thus skewing it as well.

You could try throwing some Blynk.run(); command in your updateInvData function, just before each of the delays. A bit of a messy solution, but it might work.

There are also a number of other threads about getting data from solar controllers. You might want to search the forum for those to get some inspiration.

Pete.

Gunner, thanks for your reply.

delay() and other blocking while() loops will cause Blynk disconnections.

I thought that delay() and while() will cause Blynk disconnections if they are only in main loop(). Right or I mistaken? I my sketch loop() I included only Blynk.run() and timer.run().
Also in function updateInvData() I added Blynk.virtualWrite(V1, millis() / 1000) and it shows correct time Blynk app on iPhone thus I concluded that Blynk works without disconnections.

I tried to delete all delay()s from function but it didn’t help.

Hi Pete.

Thanks for your recomendation. I searched the forum and found onlya post about getting data from EPSolar solar charge controller, but it works over RS485 (MubBus) and it is little bit another story.

Mistaken.
The Blynk process needs frequent processor time to keep it working in the background . Blocking routines like delay and while prevent this from happening.
One solution to this (as I suggested earlier) is put some Blynk.run commands in to your functions that are taking a long time to execute.

Pete.