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

Python virtual pin Read when App is closed

Hello Community

I am using a Raspberry Pi, Arduino combination do make kind of a self-made home Automation. There I want to control my lights and get some Sensor data. With the Arduino I control the Sensors and Actors. The Raspberry controls the communication with the Blynk Server. Arduino and Raspberry are connected via serial communication. I don’t only use the Blynk App to work with the incoming and outgoing data, I also use https requests to get the Data, and so I am able to Use it with Shortcuts in IOS.

Now I want to read data from a DHT11 sensor. I send the read data from my Arduino to the Raspberry where the Raspi sends it over a Virtual Pin to a Value Display which refreshes every 5s. Everything works fine when I have the app open I get the data from the Sensor.
Now the Problem is that when I close the app and want to get the data over a https request the data doesn’t refresh.

How do I get new data, even when the app is closed?

Here is the code of the Raspberry Pi:


import blynklib
import serial

BLYNK_AUTH = ''

# initialize blynk
blynk = blynklib.Blynk(BLYNK_AUTH)

READ_PRINT_MSG = "[READ_VIRTUAL_PIN_EVENT] Pin: V{}"


# register handler for virtual pin V11 reading
@blynk.handle_event('read V5')
def read_virtual_pin_handler(pin):
    print(READ_PRINT_MSG.format(pin))
    blynk.virtual_write(pin, a)
    blynk.virtual_write(6, b)


###########################################################
# infinite loop that waits for event
###########################################################

ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.flush()

while True:
    blynk.run()
    if ser.in_waiting > 0:
            line = ser.readline().decode('utf-8').rstrip()

            a = float(line[0:5])
            b = float(line[5:10])
            #print(line)
            #print(a)
            #print(b)

I’m not really sure what you mean by “App” in this case. Is it the script on the Pi? If so, you have to schedule it with cron or some other method to run in the background at startup or use a screen session or … .

You ought to give some serious thought to using WiFi based devices such as NodeMCUs and running MQTT and Node-Red on your Raspberry Pi.

This would remove your need for the serial connection and the need to use the rather limited Python library for Blynk.

Pete.