BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

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…

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

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.