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();
}