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

Blynk doesn`t work on background

Hi! I have finished my project, but I have a problem with blynk. It works, but if I close the app or project, stops working. The blynk app is running in background, I think, or at least, I have configured on my smartphone. Im using a wemos mini pro board. Sorry about my english.

#include "DHT.h"                              // DHT Sensor
#include <ESP8266WiFi.h>
#include <Blynk.h>
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
//Setup connection of the sensor
Adafruit_BME280 bme; // I2C

    char auth[] = "xxxxxxxx";
    char ssid[] = "xxxxxxxx";
    char pass[] = "xxxxxxx";
BlynkTimer timer;

DHT dhtA(15, DHT22);                           // DHT instance named dhtA, D8 and sensor type

//Variables
    float pressure;     //To store the barometric pressure (Pa)
    float temperature;  //To store the temperature (oC)
    float humidity;     //To store the humidity (%)
    int altimeter;      //To store the humidity (%) (you can also use it as a float variable)

const int riego1 = D7;
const int riego2 = D0;

int riego1State = 0;
int riego2State = 0;

void setup() {
Serial.begin(9600);
Serial.println("ArduPlantas:");
Blynk.begin(auth, ssid, pass);
dhtA.begin();                               // Starting the DHT-22
bme.begin(0x76);    //Begin the sensor
pinMode(riego1, INPUT);
pinMode(riego2, INPUT);
digitalWrite(riego1, riego1State);
digitalWrite(riego2, riego2State);
digitalWrite(0,HIGH);
digitalWrite(2,HIGH);
digitalWrite(12,HIGH);
digitalWrite(14,HIGH);
timer.setInterval(5000L, ReadSensors);   // read sensor every 5s 
}

 void ReadSensors(){
      //Read values from the sensor:
      pressure = bme.readPressure();
      temperature = bme.readTemperature();
      humidity = bme.readHumidity ();
      altimeter = bme.readAltitude (1010);

      Blynk.virtualWrite(V2, pressure/100);     // write pressure to V1 value display widget
      Blynk.virtualWrite(V3, temperature);  // write temperature to V2 value display widget
      Blynk.virtualWrite(V4, altimeter);   //write altimeter to V3 value display widget
      Blynk.virtualWrite(V5, humidity);// write humidity to V4 value display widget
      
      //Print values to serial monitor:
      Serial.print(F("Pressure: "));
      Serial.print(pressure);
      Serial.print(" Mb");
      Serial.print("\t");
      Serial.print(("Temp: "));
      Serial.print(temperature);
      Serial.print(" °C");
      Serial.print("\t");
      Serial.print("Humidity: ");
      Serial.print(humidity); // this should be adjusted to your local forcase
      Serial.println(" %");    
      Serial.print("Altimeter: ");
      Serial.print(altimeter); // this should be adjusted to your local forcase
      Serial.println(" m");  
      //delay(2000); //Update every 5 sec  
    }

void climateRoutine() {
    byte h1 = dhtA.readHumidity();            // f1 and h1 are celsius and humidity readings
    // byte t1 = dhtA.readTemperature(true);  // for temperature in farenheits
    byte t1 = dhtA.readTemperature();         // from DHT/A
    Blynk.virtualWrite(V0, t1);               //  Set Virtual Pin 0 frequency to PUSH in Blynk app
    Blynk.virtualWrite(V1, h1);               //  Set Virtual Pin 1 frequency to PUSH in Blynk app
}

void loop() {
  Blynk.run();
  timer.run();
  climateRoutine();                           // Climate routine
riego1State = digitalRead(riego1);
riego2State = digitalRead(riego2);
}
BLYNK_READ(V7) 
{
  if (riego1State == HIGH){ 
  
     Blynk.virtualWrite(V7, 1);
  }
  else
  {
     Blynk.virtualWrite(V7, 0);
  }
}
BLYNK_READ(V8)
{
  if (riego2State == HIGH){ 
  
     Blynk.virtualWrite(V8, 1);
  }
  else
  {
     Blynk.virtualWrite(V8, 0);
  }
    delay(4700);                                // 4.7 sec between routines
}

What exactly do you mean by “stops working”?

Is this the project where you are using Eventor in the app?

Also, you should read this:

Pete.

Yeah this is the project where I´m using eventor.
Stop working, eventor for example doesn´t work with the app running in background.
Anyway tomorrow I’ll modify the code leaving “void loop” as clean as posible. Thanks for your attention. Best regards.

I think that if you want someone who is familiar with Eventor (I’m not, I’m an iOS user) to help you with this then you’ll need to share information about your Eventor setup and explain in detail exactly what ‘Doesn’t work’ means in precise detail.

Pete.

Not really.
You’re still trying to do two digitalReads and two Blynk.virtualWrites per void loop cycle, and have at least one blocking delay in your code.

The Blynk.virtualWrites will flood the server, and the delays will most likely cause disconnections.

Re-read the “keep your void loop clean” document until you fully understand the principals of how to use timers, avoid delays and prevent flooding then restructure your code accordingly.

While you’re at it, you’d be better off including your eventor code in your main sketch.

Pete.

Hi Pete, first of all Happy New Year! Finally after some hours/days I solved this problem. I was choosing in eventor settings “Vpin value” instead of simply virtual pin “V6”. I test it with a basic sketch, and it works.
Also I modified my project sketch leaving like this:

void setup()
{
  Serial.begin(9600);
 delay(10);

  Blynk.begin(auth, ssid, pass);

dhtA.begin();                               // Starting the DHT-22
bme.begin(0x76);    //Begin the sensor
pinMode(riego1, INPUT);
pinMode(riego2, INPUT);
digitalWrite(riego1, riego1State);
digitalWrite(riego2, riego2State);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);

timer.setInterval(5000L, ReadSensors);   // read sensor every 5s 
timer.setInterval(3000L, riego);

}
void loop() {
  Blynk.run();
  timer.run();
}

Thank you for your attention. Best regards.