NodeMCU + Blynk + OTA disconnect all!

Hi, in this moment I have active 2 esp8266: One for solar panel and one test.
ip esp solar panel 192.168.1.217
ip esp test 192.168.1.30

ip esp test I can ping without problems. ip esp solar panel “Destination Host Unreachable”
Why???
Obviously in arduino ide I see only ip test for OTA!!
Please help me!!
Thank you

p.s. I move position of router and now I gained a few decibels. Now is -73dB

YESS! Now it works!
After moving my router, I restart router and now I can ping my solar esp and now I SEE in my IDE!!
So, also in this case, signal strength was crucial!!

1 Like

hi, for your knowledge I would like to paste the code so it can help someone.
My project control energy created by my solar panel and control my garden illumination. My hardware is divided:

Solar panel > INA219 current sensor > NodeMCU > 2 Relay > Illumination.

The whole is controlled by Blynk Application that can I update with OTA system.
Before paste my code I would like to have confirmation from you…in my code I use Blynk.config and not Blynk.begin because, I read somewhere that when Blynk goes offline and if set Blynk.begin, NodeMCU stop running the code unlike Blynk.config, right?
So in this case, if Blynk goes offline NodeMCU goes on to read power from solar panel right?in my code read power of WIFI trasmission (RSSI), that was important to work OTA. Thank you

#include <Blynk.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.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;
float current_h = 0;
float current_day = 0;

long rssi = 0;

  
#define relayFaretto10WD4 D4  
#define relayMadonnaD5 D5 
#define greenLEDD7 D7    
#define redLEDD8 D8 

#define buttonFaretto10W V0  // >> su D4
#define buttonMadonna V1     // >> su D5
#define timerFaretto V2      
#define timerMadonna V3      
#define correnteIstantanea V4
#define potenzaIstantanea V5
#define energiaTotale V6
#define correnteTotale V7
#define correnteTotaleDay V8
#define correnteTotaleDayGraph V9
#define virtualRSSI V10

boolean isFirstConnect = true;
boolean isConnesso;
boolean canSendCurrentDay = false;

BlynkTimer timer;


void setup(void) {

  Serial.begin(9600);
  ina219.begin();
  
  Blynk.config(auth);
  Blynk.connectWiFi(ssid, pass);
  
  pinMode(greenLEDD7, OUTPUT);
  pinMode(redLEDD8, OUTPUT);
  pinMode(relayFaretto10WD4, OUTPUT);
  pinMode(relayMadonnaD5, OUTPUT);

  digitalWrite(relayFaretto10WD4, LOW);
  digitalWrite(relayMadonnaD5, LOW);

  ArduinoOTA.begin();  // For OTA

  // Setup a function to be called 
  timer.setInterval(1000L, letturaPannello); 
  timer.setInterval(1500L, sendInaValues);  
  timer.setInterval(3000L, controllaConnessione); 
  timer.setInterval(3000L, sendRSSI);
  
}




void loop(void){

  timer.run(); // Initiates BlynkTimer
  ArduinoOTA.handle();  
  Blynk.run();
  
}




void letturaPannello() {

  if(!isFirstConnect){
    shuntvoltage = ina219.getShuntVoltage_mV();
    busvoltage = ina219.getBusVoltage_V();
    current_mA = (ina219.getCurrent_mA()/1000);
    if(current_mA<0){
      current_mA = current_mA*(-1);
    }
    power_mW = (ina219.getPower_mW()/1000);
    if(power_mW<0){
      power_mW = power_mW*(-1);
    }
    loadvoltage = busvoltage + (shuntvoltage / 1000);
    current_h = (current_h + current_mA / 3600);   // conteggio corrente totale
    current_day = (current_day + current_mA / 3600);   // conteggio corrente giornaliero
    energy = (energy + loadvoltage * current_mA / 3600);  // conteggio corrente energia 
  }
    
}


void controllaConnessione(){

  if(Blynk.connected()){  

    digitalWrite(greenLEDD7, HIGH);
    digitalWrite(redLEDD8, LOW); 
    isConnesso = true;
    rssi= WiFi.RSSI();
        
  }else{
    
    digitalWrite(greenLEDD7, LOW);
    digitalWrite(redLEDD8, HIGH);   
    isConnesso = false;
    Blynk.connect();
    //Blynk.begin(auth, ssid, pass);
    
  }
    
}  




void sendInaValues(){
  
  Blynk.virtualWrite(correnteIstantanea, current_mA);  // V4  
  Blynk.virtualWrite(potenzaIstantanea, power_mW);   // V5  
  Blynk.virtualWrite(energiaTotale, energy);  // V6   
  Blynk.virtualWrite(correnteTotale, current_h);   // V7   
  Blynk.virtualWrite(correnteTotaleDay, current_day);  // V8  
  if(canSendCurrentDay){ // ALLO START DEL TIMER FARETTO
    Blynk.virtualWrite(correnteTotaleDayGraph, current_day);  // MANDO IL VALORE TOTALE DELLA CORRENTE PRODOTTA
    current_day = 0;
    canSendCurrentDay = false;
  }  
  
}



void sendRSSI(){

  Blynk.virtualWrite(virtualRSSI, rssi);  // V10 RSSI in dB
  
}




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


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


BLYNK_WRITE(timerFaretto){  // timer V2 >>> buttonFaretto V0
  
  if (param.asInt() == 1) {
    Blynk.virtualWrite(buttonFaretto10W, 1); // button set to ON from timer
    digitalWrite(relayFaretto10WD4, HIGH); 
    canSendCurrentDay = true;
  }
  else{
    Blynk.virtualWrite(buttonFaretto10W, 0); // button set to OFF from timer
    digitalWrite(relayFaretto10WD4, LOW); 
  }
}


BLYNK_WRITE(timerMadonna){  // timer V3 >>> buttonFaretto V1
  
  if (param.asInt() == 1) {
    Blynk.virtualWrite(buttonMadonna, 1); // button set to ON from timer
    digitalWrite(relayMadonnaD5, HIGH); 
  }
  else{
    Blynk.virtualWrite(buttonMadonna, 0); // button set to OFF from timer
    digitalWrite(relayMadonnaD5, LOW);
  }
}



//////////////////     S Y N C  /////////////////////////
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncVirtual(energiaTotale);     // V6
    Blynk.syncVirtual(correnteTotale);    // V7
    Blynk.syncVirtual(correnteTotaleDay); // V8
    isFirstConnect = false;
  }
  isConnesso = true;
}



BLYNK_WRITE(energiaTotale){
  energy = param[0].asFloat();
}

BLYNK_WRITE(correnteTotale){
  current_h = param[0].asFloat();
}

BLYNK_WRITE(correnteTotaleDay){
  current_day = param[0].asFloat();
}


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

That’s correct, Blynk.begin is a fully blocking function and the code will stop at that point if a connection to Wi-Fi/Blynk isn’t possible.

Blynk.Config/Connect will only block the code execution for a short while, as it attempts to connect to Blynk.

However, I don’t see much benefit of that in your code. Serial output of the values will continue without a Blynk connection, and OTA updates will still be possible (provided a Wi-Fi connection exists), but as the device is in a remote location and presumably doesn’t have a PC connected to it then I don’t see the value in this.

If you added physical switches that are read by the code and used to manually control the relays in the event that there is no Wi-Fi/Blynk connection then I’d see the value in this approach.

Pete.

Thank you for reply.
First of all, I wanted to create something simple, the Serial.begin I forgot to remove. The method stampaValori() I used for only test, but Blynk, in this moment never call.
Do you mean this?

Do you mean set OTA password?

In this moment, I use physical switches and are set in AND with relay. I know, the best way is OR.

I don’t think you’ve understood any of what I was saying about the practical differences between using Blynk.begin and Blynk.config/connect in your particular setup.

Pete.

1 Like

yes, but you speak too enigmatic. I really want this, if the system is disconnected from the wifi, it must always continue to calculate the energy produced.
You are already unclear by saying that one completely stops and the other stops for a while. what does it mean?

If Blynk isn’t connected, how do you turn your relays on and off?

Pete.

I had thought of this, I should put my physical switches in OR with relays (and not in AND how I keep it set now), so if I had no connection, I could go into the garden and turn on the lighting.
But why could I turn on the relays even when I’m offline?

@PeteKnight is very clear :slight_smile:

You can use blynk connect in two ways:
Blynk.begin -> nodeMcu stops working until the connection is down

Blynk.config -> nodeMcu stops working as long as the connection is down, but only for 20 seconds, when trying to reconnect.
You can try to reconnect every 60 seconds, or every hour, you can calculate between “try to reconnect”.
You can also extrapolate the result when blynk tries to reconnect, that is only, for 20 seconds of waiting.

Maybe you would grasp the issues better if you did some real-life tests. To understand fully what is happening you’ll need to re- enable your serial prints and enhance them some that you’re obtaining data about the status of your Wi-Fi and Blynk connections, and have a laptop connected to your MCU.

Change your Blynk Auth code slightly so that it never connects to Blynk, and see what happens. Can you turn your relays on and off?

Change your Blynk Auth code back again and reboot, then after a while switch off your router. Can you turn your relays on and off?

Power your router back up again.
Does your MCU re-connect to your WiFi and to Blynk?
Can you turn your relays on and off?

If it works the way you want then that’s great, and you should ignore my comments.

Pete.

1 Like

Let’s go straight to the point, my NodeMCU normally connects when it finds the connection and I always knew that when blynk is offline I can’t command anything. But are you suggesting that instead you can do it?

use

void setup()
{
 Blynk.config(auth, server_ip, port);
 Blynk.connect();`


}

void loop() {
  timer.run();

  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
   
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
    ReCnctFlag = 0;  // Reset reconnection Flag
    ReCnctCount++;  // Increment reconnection Counter
    Serial.print("Attempting reconnection #");
    Serial.println(ReCnctCount);
    Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
}

But wasn’t it said that the loop has to stay clean?In my code I use this checkConnection:

void controllaConnessione(){

  if(Blynk.connected()){  

    digitalWrite(greenLEDD7, HIGH);
    digitalWrite(redLEDD8, LOW); 
    isConnesso = true;
    rssi= WiFi.RSSI();
        
  }else{
    
    digitalWrite(greenLEDD7, LOW);
    digitalWrite(redLEDD8, HIGH);   
    isConnesso = false;
    Blynk.connect();
    
  }
    
}  

Aren’t you still the same?This method call in timer.setInterval(3000L, controllaConnessione);

@Andrea_Errico I’d suggest that you don’t ask for advice or feedback if you don’t want it.

Have fun with your project.

Pete.

But I’m glad to get advice, especially when it’s clear. If I had to give them, I would do it differently, without going around it, but I always find myself disagreeing with the rules of blynk. As in this case of the loop clear.,in all these your suggestions, still not understood what is the problem of my code! My system reconnects perfectly every time the connection fails, now you are making me think about the operation of the relays … offline> no relay!

Pete, you raised me these issues and I reply:

Without Blynk connection I want my NodeMCU calculate data from solar panel.Is that what you wanted to know?

I have phisical switch (in OR with relay)… NO CONNECTION >>> MANUAL SWITCH, I don’t see alternatives, without a connection I will never be able to omit the relays, it seems obvious to me.

So everything is clarified, is it right then how is my system managed?
Don’t get me wrong, I like advice, but your approach to the answers makes me think that my project has problems.

The loop stay clean …
There’s only the reconnect blynk function
Nothing else.

I find that when the internet has a weak connection using Blynk.begin is more useful than Blynk.Config, is that true?

No.
What is true is that Blynk.begin will prevent the code from executing until a WiFi and Blynk server connection is established, whereas Blynk.Connect or Blynk.run following a Blynk.config will make one attempt to connect to the Blynk server. If this fails (times-out) then code execution will continue without the Blynk connection.

With this approach, it’s entirely up to you to manage the WiFi connection and decide how many WiFi connection attempts, and how many Blynk connection attempts you want to have before proceeding in ‘offline mode’, and how long to wait before trying again.

Pete.

1 Like