For me Blynk is contradictory!

Hi, sorry for my title but I’m going crazy for this:
I create a system that measure instantly by INA219, power generated from my solar panel. So, all data I can visualize on my phone with graphic ecc ecc and I can active my 2 relay (relay is not important beacuse don’t change the code, Blynk provide to set button on relay), that active 2 lights.
According for this, I have two version of my code. If you read my first version, I’m sure you won’t like this, but it’s is only version without problems!

This is my first version:
#include <Blynk.h>

#include <Wire.h>
#include <Adafruit_INA219.h>
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "++++++";

Adafruit_INA219 ina219;

char ssid[] = "****";
char pass[] = "****";

float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
float energy = 0;

unsigned long previousMillis = 0;
unsigned long interval = 100;
unsigned long currentMillis = 0;

#define greenLED D7    
#define redLED D8    


BlynkTimer timer;

void setup(void) {
  
 Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  Serial.begin(115200);
  
  while (!Serial) {
       delay(1);
  }

  uint32_t currentFrequency;
     
  ina219.begin();    
}

void loop(void){
 
  if(Blynk.connected()){  

    digitalWrite(greenLED, HIGH);
    digitalWrite(redLED, LOW);

    currentMillis = millis();

    if (currentMillis - previousMillis >= interval){
      previousMillis = currentMillis;
      ina219values();
     // stampaValori();
      Blynk.run();
      timer.run(); // Initiates BlynkTimer
    }  
    
  }else{
    
    digitalWrite(greenLED, LOW);
    digitalWrite(redLED, HIGH);
    
    Blynk.begin(auth, ssid, pass);
  }
  
  delay(1000);
}


void ina219values() {
  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  if (current_mA<0){
    current_mA = current_mA*(-1);
  }
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  energy = energy + loadvoltage * current_mA / 3600;
}

void stampaValori(){
  Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");
  Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
  Serial.print("Energia:         "); Serial.print(energy); Serial.println(" mWh");
  Serial.println("");
}




void myTimerEvent(){
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V4, current_mA / 1000);
  Blynk.virtualWrite(V5, power_mW / 1000);
  Blynk.virtualWrite(V6, energy / 1000);
}

If you read, surely you will not agree to 1)delay 2)Blynk.begin on reconnect 3)loop not clean ecc ecc.
But this version, I repeat is a bomb! Never disconnection and nothing problem!

After this, I wanted to evolve my version, cleaning loop, setting method to provide connection and manage two timer of my two lights. The result is this: In this code you can read setting of 4 button with 4 timer, because my set relay is composid of 4 parts and I set just know for future illumination:
#include <Blynk.h>

#include <Wire.h>
#include <Adafruit_INA219.h>
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "++++++";

Adafruit_INA219 ina219;

char ssid[] = "++++++++++";
char pass[] = "++++++++++";

float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float current_h = 0;
float current_day = 0;
float loadvoltage = 0;
float power_mW = 0;
float energy = 0;

boolean isFirstConnect = true;
boolean isConnesso;

unsigned long previousMillis = 0;
unsigned long interval = 100;
unsigned long currentMillis = 0;

#define relayFaretto10WD4 D4  
#define relayMadonnaD5 D5 
#define relayD9 D9  
#define relayD10 D10 
#define greenLEDD7 D7    
#define redLEDD8 D8    

#define buttonFaretto10W V0  // >> D4
#define buttonMadonna V1     // >> D5
#define buttonD9 V2          // >> D9
#define buttonD10 V3         // >> D10
#define correnteIstantanea V4
#define potenzaIstantanea V5
#define energiaTotale V6
#define correnteTotale V7
#define correnteTotaleDay V8


BlynkTimer timer;



void setup(void) {
   
  Blynk.begin(auth, ssid, pass);
  pinMode(greenLEDD7, OUTPUT);
  pinMode(redLEDD8, OUTPUT);
  timer.setInterval(1000L, letturaPannello);    
  ina219.begin();
  
}


void loop(void){
 
  Blynk.run();
  timer.run();
  
}


void letturaPannello(){
    
  isConnesso = controllaConnessione();

  if(isConnesso){
    
    currentMillis = millis();   

    if (currentMillis - previousMillis >= interval){
      previousMillis = currentMillis;
      ina219values();  //  LEGGO IL SENSORE 
      sendIna219Values();
      //stampaValori();      
    }
  }else{
    Blynk.connect(); 
  }
  //delay(1000);
}


boolean controllaConnessione(){

  if(Blynk.connected()){  

    digitalWrite(greenLEDD7, HIGH);
    digitalWrite(redLEDD8, LOW); 
    return true;
        
  }else{
    
    digitalWrite(greenLEDD7, LOW);
    digitalWrite(redLEDD8, HIGH);   
    return false;
    
  }
    
}  


void ina219values() {
  
  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = abs(ina219.getCurrent_mA());
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  energy = energy + loadvoltage * current_mA / 3600;  // conteggio energia totale
  current_h = current_h + current_mA / 3600;   // conteggio corrente totale
  current_day = current_day + current_mA / 3600;   // conteggio corrente giornaliero
  
}


void sendIna219Values(){
  Blynk.virtualWrite(correnteIstantanea, current_mA / 1000);  // VIRTUAL PIN   V4
  Blynk.virtualWrite(potenzaIstantanea, power_mW / 1000);  // VIRTUAL PIN   V5
  Blynk.virtualWrite(energiaTotale, energy / 1000);  // VIRTUAL PIN  V6
  Blynk.virtualWrite(correnteTotale, current_h / 1000);  // VIRTUAL PIN   V7
  Blynk.virtualWrite(correnteTotaleDay, current_day / 1000);  // VIRTUAL PIN  V8
}




BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncVirtual(buttonFaretto10W, buttonMadonna, correnteIstantanea, potenzaIstantanea, energiaTotale, correnteTotale, correnteTotaleDay, V9, V10, V11, V12);   //V9  V10 V11 V12  sono TIMER
    isFirstConnect = false;
  }
}


BLYNK_WRITE(buttonFaretto10W){  //controllo VIRTUAL Button Faretto
  
  if(param.asInt()==1){
    digitalWrite(relayFaretto10WD4, HIGH);  // START RELAY FARETTO
  }else{
    digitalWrite(relayFaretto10WD4, LOW);   // STOP RELAY FARETTO
  }
}


BLYNK_WRITE(buttonMadonna){  //controllo VIRTUAL Button Madonna
  
  if(param.asInt()==1){
    digitalWrite(relayMadonnaD5, HIGH);  // START RELAY MADONNA
  }else{
    digitalWrite(relayMadonnaD5, LOW);   // STOP RELAY MADONNA
  }
}



BLYNK_WRITE(V9){  //controllo Timer Faretto  
  
  if(param.asInt()==1){
    Blynk.virtualWrite(buttonFaretto10W, 1);  // START RELAY FARETTO
    //digitalWrite(relayFaretto10WD4, HIGH);  
  }else{
    Blynk.virtualWrite(buttonFaretto10W, 0);   // STOP RELAY FARETTO
    //digitalWrite(relayFaretto10WD4, LOW);   
    current_day = 0;
  }
}

BLYNK_WRITE(V10){  //controllo Timer Madonna  
  
  if(param.asInt()==1){
    Blynk.virtualWrite(buttonMadonna, 1);   // START RELAY MADONNA
    //digitalWrite(relayMadonnaD5, HIGH);  
  }else{
    Blynk.virtualWrite(buttonMadonna, 0);  // STOP RELAY MADONNA
    //digitalWrite(relayMadonnaD5, LOW);   
    current_day = 0;
  }
}


//////7//////////////////////////////////////////////////////////////////////////////////
///////////////////////CONTROLLO RELAY D9 e D10 CHE NON HAI ANCORA CONNESSO AL NODEMCU

BLYNK_WRITE(buttonD9){  //controllo VIRTUAL Button D9
  
  if(param.asInt()==1){
    digitalWrite(relayD9, HIGH);  // START RELAY D9
  }else{
    digitalWrite(relayD9, LOW);   // STOP RELAY D9
  }
}


BLYNK_WRITE(buttonD10){  //controllo Button D10
  
  if(param.asInt()==1){
    digitalWrite(relayD10, HIGH);  // START RELAY D10
  }else{
    digitalWrite(relayD10, LOW);   // STOP RELAY D10
  }
}



BLYNK_WRITE(V11){  //controllo Timer D9  
  
  if(param.asInt()==1){
    Blynk.virtualWrite(buttonD9, 1); 
    //digitalWrite(relayD9, HIGH);  // START RELAY D9
  }else{
    Blynk.virtualWrite(buttonD9, 0);
    //digitalWrite(relayD9, LOW);   // STOP RELAY D9
  }
}

BLYNK_WRITE(V12){  //controllo Timer D10  
  
  if(param.asInt()==1){
    Blynk.virtualWrite(buttonD10, 1); 
    //digitalWrite(relayD10, HIGH);  // START RELAY D10
  }else{
    Blynk.virtualWrite(buttonD10, 0); 
    //digitalWrite(relayD10, LOW);   // STOP RELAY D10
  }
}

///////////////////////////////////////////////////////////////////////////


void stampaValori(){
    Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
    Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
    Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
    Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");
    Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
    Serial.print("Energia:         "); Serial.print(energy); Serial.println(" mWh");
    Serial.print("CorrenteH:         "); Serial.print(current_h); Serial.println(" mAh");
    Serial.print("CorrenteHDay:         "); Serial.print(current_day); Serial.println(" mAh");
    Serial.println("");
}

This version doesn’t work correctly! Blynk bans me everytime!I clean loop and I order the cose so what I wrong?
Thank you for spending time for me.
Best regards

This gives us no clues about what is not working with your new version of the code, or how to help you.
You need to help us to help you.

Lines of code like this:

    Blynk.virtualWrite(buttonMadonna, 1);   // START RELAY MADONNA
    //digitalWrite(relayMadonnaD5, HIGH);  

almost certainly won’t achieve what you want unless you un-comment the lines of code that activate your relay.

Pete.

The second version works, but lost connection everytime! The problems are:

1)My app recieve data slowly from blynk server
2)the connection always falls.

I think I made a good order of my code, but it’s obvious there is a problem. What?
I kindly ask if I have well entered the connection and data sending settings.
Thank you

Does the connection fail when you do something in the app, after a random amount of time, or immediately after the connection to the server?

I’ll say this again - you’re not providing enough information for us to help you. When I get bored of asking questions and being given tiny pieces of information in reply then I’ll stop trying to help. Most other forum members seem to work i the same way.
Help us to help you, by giving us some proper information.

Pete.

when connected, I read the data correctly but not periodically. Let me explain better … in the first version, I received the data correctly every second, but now the reception of the data is messy and random, as if the reception was not marked by time. The second problem is that (I don’t know after how long, but very little maybe every minute) I lose the connection and then reconnect and so on.
Forget relays, that’s not the problem. Now I don’t know what directions to give more, the only things that happen to me with the second version are these. The first one does not give me any problem.
The second version was created to: order the code in the loop, but in general all the code and to set the timers.
Thanks

But can the problem be that in the timer method I have to insert only the sending of virtual data as in the first version?
In my second version, in Blynk.timer method I inserted the call to the main method (letturaPannello), which executes the various instructions.
What do you think?

Your code is very messy.
There is no need to check if Blynk is connected every time you take a power reading.
The millis() check is not needed in void letturaPannello()
You’re not being consistent with the way that you reference your virtual pins - sometimes you reference them directly, others you use variables to reference them.

I suggest that you start again, with a very simplified version of the code that does just one task, then get this working successfully. You can then add in morte features as needed.

Pete.

For each light I have two control: button or timer. Timer use to set start and end and button I use to stop when I want before stop.