BlynkTimer crashes whole program

Hello, everybody!
Hardware: SIM800C+ArduinoUNO+Android.
Maybe, someone knows, how to solve this problem: When I add BlynkTimer in my program - blynk begins to disconnect. Without BlynkTimer everything works fine.

Here is the program:

#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM800
#define BLYNK_HEARTBEAT 60
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
#include <SoftwareSerial.h>
#define pinBOOT 9
//BlynkTimer timer;
SoftwareSerial SerialAT(7,8); // RX, TX (17,16)
TinyGsm modem(SerialAT);
char auth[] = "-M86LN2UMefPv2T";
char apn[]  = "internet.mts.ru";
char user[] = "";
char pass[] = "";
RF24 radio(4,5);
// Network uses that radio
float data[2];
float data1[2];
float data2[2];
float data3[2];
int state = 0;
WidgetLED ledTest(V5);
WidgetLED ledLight(V7);

BLYNK_WRITE(V4){  /////////TEST
  if (param.asInt())
  {
    ledTest.on();
  }
  else
  {
    ledTest.off();
  }
}
RF24Network network(radio);
void sendDirectiveOff(){
   RF24NetworkHeader header(03);    //кому отправляем
   state = 0;
   Serial.println("sending state...");
   Serial.println(state);
   network.write(header,&state,sizeof(state));
}
void sendDirectiveOn() {                
 RF24NetworkHeader header(03);    //кому отправляем
 int state = 1;
 Serial.println("sending state...");
 Serial.println(1);
 network.write(header,&state,sizeof(state)); 
}
BLYNK_WRITE(V6){  //////LIGHT
  if (param.asInt())
  {
    sendDirectiveOn();
    Serial.println("Light on!");
  }
  else
  {
    sendDirectiveOff();
    Serial.println("Light off!");
   
  }
}
void printValuesToBlynk(){
  Blynk.virtualWrite(V0, data1[0]);
  Blynk.virtualWrite(V1, data1[1]);
  Blynk.virtualWrite(V2, data2[0]);
  Blynk.virtualWrite(V3, data2[1]);
  if(data3[0] == 1){ledLight.on(); } else {ledLight.off();}
  Blynk.virtualWrite(V8, data3[1]);
}
//int timer_printBlynk = timer.setInterval(5000L, printValuesToBlynk); 
void setup() {
  Serial.begin(9600);
  delay(10);
  // Set GSM module baud rate
  SerialAT.begin(9600);
  delay(3000);
  digitalWrite(pinBOOT, HIGH);            
  delay(1000);
  digitalWrite(pinBOOT, LOW);
  Serial.println("Initializing modem...");
  modem.restart();
  Blynk.begin(auth, modem, apn, user, pass); 
  while (Blynk.connect() == false) { }
  SPI.begin();
  radio.begin();
  network.begin(120, 00);
 // timer.enable(timer_printBlynk);
  //timer.setInterval(5000,printValuesToBlynk);
}

void loop() {
  // put your main code here, to run repeatedly:
  Blynk.run();
 // timer.run();
  network.update();
      while ( network.available()){
      RF24NetworkHeader header;                            
      network.read(header,&data,sizeof(data));                  // читаем данные 
      Serial.println("From: ");
      Serial.println(header.from_node);
      if(header.from_node == 1){
       data1[0] = data[0];
       data1[1] = data[1];
      } else if (header.from_node == 2){
       data2[0] = data[0];
       data2[1] = data[1];
      }  else if (header.from_node == 3) {
       data3[0] = data[0];
       data3[1] = data[1];
      }
    }
}

Clean your loop.

1 Like

Ok, only with Blynk.run() in loop and with commented timer - works.
With Blynk.run() in loop and with uncommented timer - disconnects. Mean, that uncommented only one line at the begin of the programm: BlynkTimer timer;
The same with SimpleTimer library.

That’s a very different issue to the one that you reported when you began this topic.

This line of code is outside of any function, so I don’t think it will do anything. If that is the case then attempting to enable a non-existent timer isn’t likely to produce a good result.

And, as @Blynk_Coeur says, your void loop is incompatible with good Blynk operation.

Pete.

2 Likes

Thanks for your answer! But I solved this problem some other way, please, close this topic!

When you need help we try to help you.
When you solve your issue, we need to know how you did.
I think @PeteKnight agree with me.

4 Likes

Ok! I desided not to use BlynkTimer or SimpleTimer. I just added a counter in the void loop(). So, when the counter increases 10 times - it means that 10 seconds passed. (I receive one message per second from other nrf224l01). So we can call the method to send values to Blynk. Like this:

void loop() { 
  Blynk.run();
  network.update();
      while ( network.available()){
      RF24NetworkHeader header;                            
      network.read(header,&data,sizeof(data)); 
      Serial.println("From: ");
      Serial.println(header.from_node);
      if(header.from_node == 1){
        Serial.println("1");
       data1[0] = data[0];
       data1[1] = data[1];
      } else if (header.from_node == 2){
         Serial.println("2");
       data2[0] = data[0];
       data2[1] = data[1];
      }  else if (header.from_node == 3) {
         Serial.println("3");
       data3[0] = data[0];
       data3[1] = data[1];
      }
      counter=counter+1;
      if(counter==10){
        Serial.println("hello");
        counter = 0;
        printValuesToBlynk();
      }
    }
}

I did that because, unfortunately, I could not found out, how to use BlynkTimer correctly.

1 Like

It’s just my opinion, but I think you’ll be having much worse disconnection issues with that void loop.

Pete.

1 Like

Thanks, Pete, I undersatand, that it is better to keep loop() clean. But in this particular case to keep it clean I have to use interrupt pin in nrf24l01 (there is an irq pin there) ant it will be a great problems during implementation this way. So, for now, this program works fine without any disconnections so, maybe, one day, I will implement this with inetrrupts. If I do that, I will definetely share in this branch.