Timer Start and stop on simple Relay

So, two widget can control same digital pin?
Do I have to use virtual pins?
Thank you

everything is really strange. Switching on the timer works, switching off no. Bah

try removing your button widget to see if that makes a difference.

Try loading the BASIC sketch, with just the relays connected to the NodeMCU, and see if works then. It may be something with your code/wiring that is causing the issue.

If the second option works, slowly add stuff back in and see where the issue occurs. This may help narrow down where you need to focus your attention.

You have a lot of code in your void loop(), and a delay(1000) both of which are a no-no for stable BLYNK operation.

1 Like

Thank you for reply.
I know that delay(1000) is raccomanded to calculate ina219 values.
I’ll try to remove buttons and I’ll let you know.
I believe that I will use virtual data anyway.

After doing some testing just now, I think I may have found a bug, at least in the iOS app - I don’t have an android device to test on.

It relates to the timer widget.

I will write up my findings a little later today in a new post.

Edit: Just read @PeteKnight’s reply - If you were to use virtual pins instead of digital pins in the apps settings, you can avoid the bug and have everything working correctly :slightly_smiling_face:

1 Like

@Andrea_Errico, the comments by @Toro_Blanco are correct, your void loop isn’t compatible with Blynk.

I didn’t pay attention to the state of your void loop earlier - I was trying to work-out what you were doing with the button that you’d said was connected to D6, which eventually turned-out to be a typo by you.

You should read this:
http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean
and restructure your void loop to remove the call to ina219values() - replacing it with a timer instead, and remove the delay.
I’d also remove the digitalWrite to your two LEDs if I were you.

When you move to virtual pins, you’ll need to do a pinMode declaration for the relays. I’d also recommend using GPIO pin references throughout, instead of this type of reference:

#define greenLED D7    
#define redLED D8 

with GPIO references, for a NodeMCU, this would look like this:

#define greenLED 13  // Pin D7 on the NodeMCU  
#define redLED   15  // Pin D8 on the NodeMCU

You should also read this, to ensure that you choose appropriate GPIO pins:

Pete.

Thank you
I understand what you mean. I ordered the code, but GPIO not yet. Now this is my code: It’s good?
I add SyncAll and other function to check connection
Thank you

#include <Blynk.h>

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


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

char auth[] = "XXXXXXXXXXXXX";

Adafruit_INA219 ina219;

char ssid[] = "ERRICIOFFI2.4";
char pass[] = "XXXX";

//char ssid[] = "FERRIDEA";
//char pass[] = "XXXXX?";

float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float current_h = 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 greenLED D7    
#define redLED D8    

BlynkTimer timer;



void setup(void) {
  
 
  Blynk.begin(auth, ssid, pass);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, 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();
      //stampaValori();      
    }  
  }  
}


boolean controllaConnessione(){

  if(Blynk.connected()){  

    digitalWrite(greenLED, HIGH);
    digitalWrite(redLED, LOW); 
    return true;
        
  }else{
    
    digitalWrite(greenLED, LOW);
    digitalWrite(redLED, HIGH);
    Blynk.connect();
    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
  current_h = current_h + current_mA / 3600;   // conteggio corrente
  Blynk.virtualWrite(V4, current_mA / 1000);
  Blynk.virtualWrite(V5, power_mW / 1000);
  Blynk.virtualWrite(V6, energy / 1000);
  Blynk.virtualWrite(V7, current_h / 1000);
}




BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}



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

Now I add a simple code to control Timer widget on Virtul PIN (V8 Faretto and V9 Madonna):
It’s right now?Thank you

BLYNK_WRITE(V8){  //controllo Timer Faretto  
  if(param.asInt()==1){
    digitalWrite(faretto, HIGH);  // START RELAY FARETTO
  }else{
    digitalWrite(faretto, LOW);   // STOP RELAY FARETTO
  }
}

BLYNK_WRITE(V9){  //controllo Timer Madonna  
  if(param.asInt()==1){
    digitalWrite(madonna, HIGH);  // START RELAY MAADONNA
  }else{
    digitalWrite(madonna, LOW);   // STOP RELAY MADONNA
  }
}

It’s impossible to tell from this snippet of code.
Don’t use Blynk.syncAll, it isn’t necessary with just a couple of virtual pins and will probably cause problems. Just sync the pins that are needed.

You’ve chosen to use some other type of formatting for your code other than triple backticks, which isn’t a good idea.

Pete.

I don’t understand , what’s wrong??

The block of code doesn’t have triple backticks at the beginning and end. Instead, it’s indented.
Whilst that works okay for small blocks of code like this, when you post more code, and people try to copy and paste it into the Arduino IDE to try to test or debug it, the code doesn’t always paste correctly.
It’s better to add the triple backticks instead.

Pete.

Sorry for my bad english, but maybe you mean, haven’t I copied the code well?
I’ll try to do it. I modify the call syncall to Blynk.syncVirtual(V4, V5, V6, V7, V8).
Let me know If I paste good code. Then I have a question: If NodeMcu offline, I can record my data in virtual pin and after reconnect NodeMcu send data to cloud as an array right?
If you read my code I send value only if is connected, so is it wrong? I’ll send data alway right? Let me know…this is my code:

#include <Blynk.h>

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


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

char auth[] = "XXXXXX";

Adafruit_INA219 ina219;

char ssid[] = "ERRICIOFFI2.4";
char pass[] = "XXXX";

//char ssid[] = "FERRIDEA";
//char pass[] = "XXXXX";

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 faretto D4  
#define madonna D5 
#define greenLED D7    
#define redLED D8    

BlynkTimer timer;



void setup(void) {
   
  Blynk.begin(auth, ssid, pass);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, 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();
      //stampaValori();      
    }else{
      Blynk.connect();  
    }
  }
}


boolean controllaConnessione(){

  if(Blynk.connected()){  

    digitalWrite(greenLED, HIGH);
    digitalWrite(redLED, LOW); 
    return true;
        
  }else{
    
    digitalWrite(greenLED, LOW);
    digitalWrite(redLED, 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
  Blynk.virtualWrite(V4, current_mA / 1000);
  Blynk.virtualWrite(V5, power_mW / 1000);
  Blynk.virtualWrite(V6, energy / 1000);
  Blynk.virtualWrite(V7, current_h / 1000);
  Blynk.virtualWrite(V8, current_day / 1000);
}




BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncVirtual(V4, V5, V6, V7, V8);
    isFirstConnect = false;
  }
}


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

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



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

can you help me? thank you

Hi, I have a simple question becuase blynk doc. isn’t clear.
Generally If NodeMcu lost connection, it tries to reconnect automatically? Or I must add a command in the code?
Thank you