Blynk2.0 not connecting to cloud unless I comment irrelevant parts of the code

Hello,
I am using gsm800l with arduino nano to connect my project with blynk2.0 the connection is not completed unless I make irrelevant changes such as commenting some BLYNK_WRITE() functions or change the string to be printed on the monitor; for example when I use:
Serial.print(“pulses=”); Serial.println( pulses);
the cloud could not connect, the serial monitor keep printing the following for ever:

Connecting to blynk.cloud:80
Ready (ping: 1123ms).
Connecting to blynk.cloud:80
Ready (ping: 704ms).

but when I remove some characters as the following it connects:
Serial.print(“pulse=”); Serial.println( pulses);
I tried it many times and the phenomenon is still there, I know this is very strange, I hope there is an explanation, the code is below, thank you in advance:

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "yyyyy"
#define BLYNK_AUTH_TOKEN "abc" 
#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>                
#include <BlynkSimpleTinyGSM.h>            
#include <SoftwareSerial.h>
#include <Adafruit_SleepyDog.h>
SoftwareSerial mySerial(7, 6);
#include <EEPROM.h>                        // 
#include <avr/wdt.h> //Watch dog timer functions
#include <elapsedMillis.h> 

char auth[] = BLYNK_AUTH_TOKEN;
char apn[]  = "internet";
char user[] = "Telenor";
char pass[] = "Telenor";
TinyGsm modem(mySerial);
BlynkTimer timer;
#define INTERVAL 1000L

#define     LS1                3          // last box up
#define     LS2                4          // last box down
#define     LS3                5          // box 11 down
#define     LS4                10         // box 11 up
#define     LS5                8          // box 10
#define     Pump1              9          // 
#define     Pump2              16         // 
#define     Drain              15         // 
#define     Pump4              13         // 
#define     Flow_meter         2          // 
#define     Trubidty           17         // 
#define     UV                 19         // 

bool LS1_state;                           // /***********************************************************************/
bool LS2_state;                           // /*  These variables are used to store the state of the limit swiches,  */
bool LS3_state;                           // /*  and it can be obtained from the hardwer directly.                  */
bool LS4_state;                           // /*                                                                     */
bool LS5_state;                           // /***********************************************************************/
bool Pump1_manual = 0;                    // /***********************************************************************/
bool Pump2_manual = 0;                    // /*  These variables are used to store the desired state of the pumps,  */
bool Drain_pump_manual = 0;               // /*  and it must be obtained just from the blynk dashboard.             */
bool Pump4_manual = 0;                    // /*                                                                     */
bool All_pumps = 0;                       // /***********************************************************************/
byte drain = 0;                           // 
byte count = 0;                           // 
byte full = 0;                            // 
byte on = 0;                              // 
byte EEPROM_Location;                     // 
unsigned int fullPeriod = 0;              // 
unsigned int drainTime = 0;               // 
unsigned int ONPeriod = 0;                // 
unsigned int Writing_times = 0;           // 
float total;                              // 
float averg;                              // 
float turbidity;                          // 
float flowRate;                           // 
float calibrationFactor = 4.5;            // can be removed
float totalMilliLitres = 0;               //
float outletVolume = 0;                   //
int pulses = 0;                  //
int i=0;
byte reset_times=0;
int reset_times_location=1000;
int online_mode=1;
elapsedMillis Pump1_timer;
elapsedMillis drainTimer;
elapsedMillis EEPROMTime;
elapsedMillis flowTime;
WidgetLED Pump1_indicater(V0);
WidgetLED Pump2_indicater(V1);
WidgetLED Drain_pump(V2);
WidgetLED Pump4_indicater(V3);

//EEPROM_Location = EEPROM.read(0);
//  EEPROM_Location = EEPROM_Location * 9;
//  outletVolume = EEPROM.get(EEPROM_Location + 3, outletVolume);
//  full = EEPROM.read(EEPROM_Location + 7);
//  fullPeriod = full * 1000;
//  on = EEPROM.read(EEPROM_Location + 8);
//  ONPeriod = on * 1000;
//  drain = EEPROM.read(EEPROM_Location + 9);
//  if(drain > 127){
//    drain = drain & 127;
//    All_pumps = 1;
//    }else All_pumps = 0;
//  drainTime = drain * 1000;

void SendDhtData(){
  Serial.println("From SendDHtData");
  
  LS1_state = digitalRead(LS1);
  LS2_state = digitalRead(LS2);
  LS3_state = digitalRead(LS3);
  LS4_state = digitalRead(LS4);
  LS5_state = digitalRead(LS5);
  if(Pump1_timer > fullPeriod) Pump1_timer = 0;
  if(All_pumps){
    if(Pump1_manual || Pump1_timer < ONPeriod){
      Pump1_indicater.on();
      digitalWrite(Pump1, LOW);
      }else {
        Pump1_indicater.off();
        digitalWrite(Pump1, HIGH);
        }
    if(LS4_state || Pump2_manual){
      digitalWrite(Pump2, LOW);
      Pump2_indicater.on();
      }else if(!LS3_state){
        digitalWrite(Pump2, HIGH);
        Pump2_indicater.off();
        }
    if(LS5_state || Drain_pump_manual){
      digitalWrite(Drain, LOW);
      Drain_pump.on();
      drainTimer = 0;
      }else if((!LS5_state) && (drainTimer > drainTime)){
        digitalWrite(Drain, HIGH);
        Drain_pump.off();
        }
    if(LS1_state || Pump4_manual){
      digitalWrite(Pump4, LOW);
      digitalWrite(UV, LOW);
      Pump4_indicater.on();
      }else if((!LS2_state)){
        digitalWrite(Pump4, HIGH);
        digitalWrite(UV, HIGH);  
        Pump4_indicater.off();
        }
    }else{
      digitalWrite(Pump1, HIGH);
      digitalWrite(Pump2, HIGH);
      digitalWrite(Drain, HIGH);
      digitalWrite(Pump4, HIGH);
      digitalWrite(UV, HIGH);
      Pump1_indicater.off();
      Pump2_indicater.off();
      Drain_pump.off();
      Pump4_indicater.off();
      } 
      // detachInterrupt(digitalPinToInterrupt(2));
 // outletVolume = pulses / 486 + outletVolume;
   flowTime = 0;
  pulses = 0;
    Serial.print("pulse="); Serial.println( pulses);
      Serial.print("flow rate=");
  Serial.println(flowTime);
    
 
  flowRate = ((1000 / flowTime) * pulses * 0.705) / calibrationFactor;
   Serial.print("flow rate=");Serial.println(flowRate);
  totalMilliLitres = (flowRate / 60) * 1000000 + totalMilliLitres;
   Serial.print("totalMilliLitres=");Serial.println(totalMilliLitres);
  flowTime = 0;
  pulses = 0;
    //  //totalMilliLitres += flowMilliLitres;
  outletVolume = totalMilliLitres / 1000000;

//  //outletVolume = flowRate / 60 + outletVolume;
  //Serial.print("flow rate=");Serial.println(flowRate);
  Serial.print("outletVolume=");Serial.println(outletVolume);
  Blynk.virtualWrite(V12, outletVolume);
  Blynk.virtualWrite(V13, flowRate);
  if (count < 64){
    ++count;
    total = total + (float)(Trubidty * 0.0008056640625) * 1.52;
    }
  if (count >= 64){
    averg = (total / count);
    turbidity = -1 * 119.7 * (averg - 2.13) + 16.00;
    count = 0;
    total = 0;
    Blynk.virtualWrite(V14, turbidity);
    }
  if(EEPROMTime > 60000){
    EEPROMTime = 0;
    EEPROM_Location = EEPROM.read(0);
    if(EEPROM_Location > 112){
      
      }
    EEPROM_Location = EEPROM_Location * 9;
    Writing_times = EEPROM.get(EEPROM_Location + 1, Writing_times);
    if(Writing_times > 50000){
      ++EEPROM_Location;
      EEPROM.write(0, EEPROM_Location);
      Writing_times = 0;
      }
    EEPROM.put((EEPROM_Location + 3), outletVolume);
    EEPROM.put((EEPROM_Location + 7), full);
    EEPROM.put((EEPROM_Location + 8), on);
    if(All_pumps) EEPROM.put((EEPROM_Location + 9), (drain | 128));
      else EEPROM.put((EEPROM_Location + 9), drain);
    ++Writing_times;
    EEPROM.put((EEPROM_Location + 1), Writing_times);
    }
  }

BLYNK_WRITE(V4){
  Pump1_manual = param.asInt();
  if(!Pump1_manual){
    digitalWrite(Pump1, HIGH);
    Pump1_indicater.off();
    }
  }
BLYNK_WRITE(V5){
  Pump2_manual = param.asInt();
  if(!Pump2_manual){
    digitalWrite(Pump2, HIGH);
    Pump2_indicater.off();
    }
  }
BLYNK_WRITE(V6){
  Drain_pump_manual = param.asInt();
  if(!Drain_pump_manual){
    digitalWrite(Drain, HIGH);
    Drain_pump.off();
    }
  }
BLYNK_WRITE(V7){
  Pump4_manual = param.asInt();
  if(!Pump4_manual){
    digitalWrite(Pump4, HIGH);
    Pump4_indicater.off();
    }
  }
BLYNK_WRITE(V8){
  All_pumps = param.asInt();
  }
BLYNK_WRITE(V9){
  full = param.asInt();
  if(full <= on){
    Blynk.virtualWrite(V9, on + 1);
    fullPeriod = ONPeriod + 1000;
    full = on + 1;
    }else fullPeriod = full * 1000;
  }
BLYNK_WRITE(V10){
  on = param.asInt();
  ONPeriod = on * 1000;
  if(full <= on){
    Blynk.virtualWrite(V9, on + 1);
    fullPeriod = ONPeriod + 1000;
    full = on + 1;
    }
  }
BLYNK_WRITE(V11){
  drain = param.asInt();
  if(drain < 128){
    drainTime = drain * 1000;
    }else{
      Blynk.virtualWrite(V11, 127);
      drainTime = 127 * 1000;
      }
  }

void interrupt(){
  ++pulses;
  }

void setup(){
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("setup");
  reset_times= EEPROM.read(reset_times_location);
  if(reset_times>=1){
    reset_times=0;
     EEPROM.write(reset_times_location, reset_times);
     reset_times= EEPROM.read(reset_times_location);
     Serial.print("1reset_times="); Serial.println(reset_times);
  }else{
  reset_times=reset_times+1;
  EEPROM.write(reset_times_location, reset_times);
  reset_times= EEPROM.read(reset_times_location);
   Serial.print("2reset_times="); Serial.println(reset_times);
  }
 // wdt_disable();
  //noInterrupts();
  pinMode(LS1, INPUT);
  pinMode(LS2, INPUT);
  pinMode(LS3, INPUT);
  pinMode(LS4, INPUT);
  pinMode(LS5, INPUT);
  pinMode(Pump1, OUTPUT);
  pinMode(Pump2, OUTPUT);
  pinMode(Drain, OUTPUT);
  pinMode(Pump4, OUTPUT);
  pinMode(Flow_meter, INPUT);
  pinMode(Trubidty, INPUT);
  pinMode(UV, OUTPUT);
//-----------------------------------------------------------------------
  digitalWrite(Pump1, HIGH);
  digitalWrite(Pump2, HIGH);
  digitalWrite(Drain, HIGH);
  digitalWrite(Pump4, HIGH);
  digitalWrite(UV, HIGH);
 
if(reset_times<1){
  online_mode=1;
   Serial.print("online_mode="); Serial.println(online_mode);
    Serial.print("reset_times="); Serial.println(reset_times);
  modem.restart();

Serial.println("before Blynk.begin");

  Blynk.begin(auth, modem, apn, user, pass);
 
Serial.println("after Blynk.begin");


}else{
  online_mode=0;
   Serial.print("online_mode="); Serial.println(online_mode);
  reset_times=0;
  // EEPROM.write(reset_times, reset_times_location);
   Serial.print("reset_times="); Serial.println(reset_times);
}
//  if(Blynk.connect()){
//     wdt_enable(WDTO_8S);
//    }else{
//      wdt_disable();
//    }
  

  timer.setInterval(INTERVAL, SendDhtData);

}


/************************************************************************************
 *  loop() function
 **********************************************************************************/
void loop(){
   if(online_mode==1){//////////////online mode
      if (mySerial.available()) {
         Serial.write(mySerial.read());
        }
      if (Serial.available()) {
         mySerial.write(Serial.read());
      }
 
      if(Blynk.connected()){
        Watchdog.enable(8000);
         Watchdog.reset();
     //    Serial.println("Watchdog reset");
      // wdt_reset();
         Blynk.run();
        // Watchdog.disable();
      // wdt_disable();
     //  Serial.println("wdt disabled");
         i=0;
    }else{
         i++;
         Serial.print("i= "); Serial.println(i);     
         if (i==1000){
            Watchdog.enable(8000);
            Serial.println("watchdog enabled for 8 seconds");
    //      wdt_enable(WDTO_8S);
    //      Serial.println("wdt enabled");
         }
      }
   
       Blynk.run();
       timer.run();
   }else{////////offline mode
     if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
 
  if(Blynk.connected()){
     Watchdog.reset();
 // Serial.println("Watchdog reset");
 //   wdt_reset();
      Blynk.run();
   //    Watchdog.disable();
  //    wdt_disable();
   //   Serial.println("wdt disabled");
      i=0;
    }else{
      i++;
       Serial.print("i= "); Serial.println(i);     
      if (i==1000){
      Watchdog.enable(8000);
      Serial.println("watchdog enabled for 75 seconds");
//      wdt_enable(WDTO_8S);
//      Serial.println("wdt enabled");
      }
    }
   
 //   Blynk.run();
  timer.run();
   }
  }

When you compile the sketch, what does the IDE say about available memory?

Pete.

It says:
Sketch uses 29058 bytes (94%) of program storage space. Maximum is 30720 bytes.
Global variables use 1282 bytes (62%) of dynamic memory, leaving 766 bytes for local variables. Maximum is 2048 bytes.

I think that might be your problem.

Pete.

Lack of memory explains the phinomena indeed.

Thank you very much Pete.

You could use the F() macro when serial printing strings, but I’m not sure how much that would help.

I’m also concerned about the amount of EEPROM writing that seems to be taking place in the sketch, which could kill your memory quite quickly (100,000 wrote operations).
Maybe you’d be better with something like the TTGO T-CALL board which has more memory and allows LittleFS to be used instead of EEPROM.

You may also need to take a look at your void loop…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Pete.

I will consider that.
Thank you

1 Like