App not updating while in background

I am making a Raspberry Pi weather station that uploads its data using Blynk. I recently discovered that the weather station is only uploading data to the Blank app when the app is running. Shortly after I close the app (hit the home button), the weather station stops uploading data. This leads to my Supercharts missing large chunks of data. I also have a lightning detector set up to send notifications with a time stamp when it detects a lightning strike; however, I only receive the notification when I open the app and the time stamp isn’t when the strike occurred but rather when I opened the app. I am using the python Blynk library. I am also using the latest iOS app. I would greatly appreciate any help as I am new to coding and Blynk. Here is my code for the Raspberry Pi.


import BlynkLib_Outdoor
import time
import Adafruit_TCS34725
from Adafruit_BME280 import *
from RPi_AS3935 import RPi_AS3935
import SI1145.SI1145 as SI1145
import RPi.GPIO as GPIO
from datetime import datetime
import smbus


BLYNK_AUTH = 'This is where my token normally is'

# Initialize Blynk
blynk = BlynkLib_Outdoor.Blynk(BLYNK_AUTH)

# Initialize Lightning detector
as3935 = RPi_AS3935(address=0x03, bus=1)
as3935.set_indoors(True)
as3935.set_noise_floor(0)
time.sleep(0.005)
as3935.calibrate(tun_cap=2)
time.sleep(0.005)
as3935LastInterrupt = 0
as3935LightningCount = 0
as3935LastDistance = 0
as3935LastStatus = ""
GPIO.setmode(GPIO.BCM)
as3935Interrupt = False


def process_as3935_interrupt():

    global as3935Interrupt
    global as3935, as3935LastInterrupt, as3935LastDistance, as3935LastStatus

    as3935Interrupt = False

    reason = as3935.get_interrupt()

    time.sleep(0.003)

    as3935LastInterrupt = reason

    if reason == 0x00:
        now = datetime.now().strftime('%H:%M:%S - %m/%d')
        as3935LastStatus = "Spurious Interrupt %s" % now
    elif reason == 0x01:
        now = datetime.now().strftime('%H:%M:%S - %m/%d')
        as3935LastStatus = "Noise Floor too low - adjusting (%s)" % now
        as3935.raise_noise_floor()
    elif reason == 0x04:
        now = datetime.now().strftime('%H:%M:%S - %m/%d')
        as3935LastStatus = "Disturber detected - masking (%s)" % now
        as3935.set_mask_disturber(True)
    elif reason == 0x08:
        now = datetime.now().strftime('%H:%M:%S - %m/%d')
        distance = as3935.get_distance()
        as3935LastDistance = distance
        as3935LastStatus = "Lightning Detected " + str(distance) + "km away. (%s)" % now

    blynk.virtual_write(6, as3935LastStatus)
    blynk.notify(as3935LastStatus)


def handle_as3935_interrupt(channel):
    global as3935Interrupt

    blynk.virtual_write(6, "as3935 Interrupt")

    as3935Interrupt = True


as3935pin = 17

GPIO.setup(as3935pin, GPIO.IN)
GPIO.add_event_detect(as3935pin, GPIO.RISING, callback=handle_as3935_interrupt)

# Other sensors 


@blynk.VIRTUAL_READ(1)
def v1_read_handler():
    sensor_1 = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8, h_mode=BME280_OSAMPLE_8)
    degrees = sensor_1.read_temperature_f()
    blynk.virtual_write(1, '{0:0.1f}'.format(degrees))


@blynk.VIRTUAL_READ(2)
def v2_read_handler():
    sensor_2 = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8, h_mode=BME280_OSAMPLE_8)
    # atm = sensor.read_pressure_atm()
    pascals = sensor_2.read_pressure()
    hectopascals = pascals / 100
    blynk.virtual_write(2, '{0:0.0f}'.format(hectopascals))


@blynk.VIRTUAL_READ(3)
def v3_read_handler():
    sensor_3 = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8, h_mode=BME280_OSAMPLE_8)
    humidity = sensor_3.read_humidity()
    blynk.virtual_write(3, '{0:0.1f}'.format(humidity))


@blynk.VIRTUAL_READ(4)
def v4_read_handler():
    tcs = Adafruit_TCS34725.TCS34725(integration_time=Adafruit_TCS34725.TCS34725_INTEGRATIONTIME_154MS,
                                     gain=Adafruit_TCS34725.TCS34725_GAIN_4X)
    tcs.set_interrupt(False)
    r, g, b, c = tcs.get_raw_data()
    lux = Adafruit_TCS34725.calculate_lux(r, g, b)
    blynk.virtual_write(4, lux)


@blynk.VIRTUAL_READ(5)
def v5_read_handler():
    sensor_4 = SI1145.SI1145()
    vis = sensor_4.readVisible()
    IR = sensor_4.readIR()
    uv = sensor_4.readUV()
    uvindex = uv / 100.0
    blynk.virtual_write(5, uvindex)


@blynk.VIRTUAL_READ(6)
def v6_read_handler():
    if as3935Interrupt == True:
        process_as3935_interrupt()


# Start Blynk 


blynk.run()

Do you use Blynk Cloud?

Yes

In that case this is expected. VIRTUAL_READ handlers are triggered only when the app is active. You may change this with the local server with allow.reading.widget.without.active.app=true option. Otherwise, you need to use some kind of timer within your code that will trigger VIRTUAL_READ.

At some point we will also enable this on the server side on the Blynk Cloud.

Thank you so much for helping me out. This was really bugging me. So how could I use a timer to trigger the VIRTUAL_READ handler?