My sketch works only while Blynk app is open

Hi, I think that i need your help with my first project with Blynk…I admit I’m not an expert but I 'm writing a sketch for a simple alarm system; I’m using an Arduino MKR 1010 WiFi. The sketch runs perfectly when the Blynk app is open on my phone(Xiaomi Redme 8, Android 11)and I can see everything on the serial port and I receive the notifications… When I close the app (open in background) or lock the screen the system doesn’t works anymore, and the only thing that I can visualize on the serial is" current time"…
The MKR Is connectet to my home WiFi…
What am I doing wrong?
Thanks in advance, Luca.
Below, my sketch

#include <Blynk.h>
#define BLYNK_PRINT Serial
//#define BLYNK_TEMPLATE_ID   "YourTemplateID"
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

BlynkTimer timer;
WidgetRTC rtc;

int PIR = 3;
int RELAY = 1;
int REED = 2;
int buzzer_set = 4;
int buttonBlynk = V4;
int abilitazione_buzzer=0;


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXX";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXXXXXXXX";
char pass[] = "XXXXXXXXXXXX";
void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V5, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V6, currentDate);
}
BLYNK_CONNECTED() {
  // Synchronize time on connection
Blynk.syncAll();
  rtc.begin();
}

BLYNK_WRITE(V0){
int abilitazione_buzzer= param.asInt();
Serial.print("abilitazione_buzzer== ");
Serial.println(abilitazione_buzzer);
Blynk.virtualWrite(V0,abilitazione_buzzer);
if(abilitazione_buzzer== 0){
    abilitazione_buzzer=0;
     digitalWrite(4,LOW);
  }
 else{
  abilitazione_buzzer=1;
  digitalWrite(4,HIGH);
 }
}
      BLYNK_READ(V2){
        int REED = digitalRead(2);
        if(REED){
         Blynk.notify("allarme");
         Serial.println("allarme");
        delay(1000);
        }
        Blynk.virtualWrite(V2,REED);
            if(REED){           
            digitalWrite(1, HIGH);
            delay(5000);
            }
            else{
            digitalWrite(1,LOW);
            }
            }        
      BLYNK_READ(V3){
    int PIR =  digitalRead(3);
  if(PIR == HIGH){
    Blynk.notify("allarme pir");
    Serial.println("allarme pir");
    delay(1000);
  }
Blynk.virtualWrite(V3, PIR);
      if(PIR == HIGH ){
      digitalWrite(1, HIGH);
      delay(5000);
       }
      else{
      digitalWrite(1,LOW);
        }
  }
BLYNK_WRITE(V4){
   int buttonBlynK = param.asInt();
  Serial.println (buttonBlynk);
  if( buttonBlynk = 1){
  digitalWrite(1,HIGH);
  delay(10000);
}else{
  digitalWrite(1,LOW);
  }
}
void setup()
{
  pinMode(1, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3,INPUT);
  pinMode(4,OUTPUT);
  pinMode(V4,INPUT);
  pinMode(V0,INPUT);
  digitalWrite(3,HIGH);
      // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  setSyncInterval(10 * 60);
 timer.setInterval(10000L, clockDisplay);
}
void loop()
{
  Blynk.run();
  timer.run();
}

Well, you certainly make your code difficult to read with the crazy indentations that you’re using.

You need to explain what is what type of widget is connected to each virtual pin.

I’d be interested to hear what you believe the BLYNK_READ(vPin) functions do, and why you are using them in your sketch.

You also need to stop using delays in your sketch, but I can’t really suggest the best way to replace these unless I understand what the purpose of those BLYNK_READ functions are.

It appears that you are using Blynk Legacy, but this line of code:

implies that you are using a Blynk IoT example as your starting point.
What version of te Blynk library are you using?

Pete.

Thanks for your time… I told that I wasn’t a genius :sweat_smile: I try to explaine…
V0: Button widget which I use to controll a relay
V2: REED sensor/Value Display
V3: Pir sensor/Value Display
V4:Button widget which I use to controll another relay
V5/6: Value Display for hour and data.
I use Blynk Legacy and the Arduino library is “Blynk 1.0.1”.
I use the funtion BLYNK_READ to read the sensors
but tonight, scrolling through an old post, I understand that It works only when App Active, :grimacing: right?I thank and listen for any advice.
Luca.

The sensors are attached to your device, not to the Blynk cloud server. You should be using a timer to read the sensor values then push the results to the Blynk server using the Blynk.virtualWrite(vPin, value) command.

Take a look at the DHT example to see how this should be structured.

Pete.

Ok, thank you for your time, I replaced the Blynk Read function with

[Unformatted code removed by moderator]

It Works…but if I would to implement the notify with the current Time…

[Unformatted code removed by moderator]

Arduino disagrees…

Ok, thank you for your time, I replaced the Blynk Read function with

[Unformatted code removed by moderator]

It Works…but if I would to implement the notify with the current Time…

[Unformatted code removed by moderator]

Arduino disagrees…

@Luca you know how to use triple backticks correctly, and what correctly formatted code looks like.

I’ve removed your unformatted code. I’d suggest that you edit your post and correct the formatting.

Pete.

Ok, After your advice I replaced the Blynk.Read function with two timer like this

{ 
int REED = digitalRead(2);
Blynk.virtualWrite(V2,digitalRead(REED));  
    if(REED==1){ 
    Blynk.notify("allarme");  
    Serial.println("allarme");
    digitalWrite(1, HIGH);

It runs… But if I would to implement the notification with the time of the event… I try a this
Blynk.notify("allarme"+ hour(), minute(), second()+);

Arduino disagrees… Why?
As Always, thanks…

This piece of code looks like programming mumbo jumbo to me…

This piece of code will read the value if GPIO 2 and store the result in an integer variable called REED

int REED = digitalRead(2);

As GPIO2 is going to be either HIGH (1) or LOW (0) the value stored in the variable REED will be. 1 or a 0

This line of code…

Blynk.virtualWrite(V2,digitalRead(REED));

Will first of all do a digitalRead() on pin GPIO 0 to GPIO1, depending on what the result of th first line of code was, that resulted in either a 1 or a 0 being stored in the integer variable REED
The exult of this digitalRead() will then be sent to virtual pin V2 in Blynk.

I’m almost certain that this is not what you are wanting to achieve, as GPIO0 does not have a PinMode entry in void setup, and GPIO1 is declared as an output and if you are doing a digitalRead() then I’d expect it to be an input.

As for the rest of your recent posts, I don’t understand what it is that you are asking or stating.

Pete.

Thank you for your patience… What I was trying to ask was how to add time to the notification… I try with…Blynk.notify("allarme"+ hour(), minute(), second()+)

You need to build your string of hours, minutes and seconds first, then send this string to Blynk.
You probably need to use use sprintf to give you the correct HH:MM:SS format.

Pete.