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

Keep running Blynk loop while other functions execute

So, I’m using a raspberry pi 3b+ with a dht22 sensor. I’m working with Python 3.7 using the blynk library.

I want to be able to keep the blynk loop running, but I also want to check independently the current humidity/temperature, to do other stuff with that (turn on/off fans, a humidifyer, etc).

I’ve faced some problems trying this:

  • I tried just checking if the humidity is over a given value in the same loop where blynk.run() is, and then call another function which does whatever it has to do, and check for humidity again, to return to the main loop when humidity is at desired value, but when this function is called, the blynk loop stops and the app hangs.

  • DHT22 sensor has a delay of 1.5s between readings. I don’t want to be checking it all the time. My intention was to normally check values like each 5 minutes, and when a minimum value is reached, call a function which would check it more frequently (1.5s) until it’s in the desired range.

  • I tried running two different processes, one for the blynk loop, and another one for ‘main’ loop (which doesnt interact with blynk). The problem is, as the two processes check the dht22 sensor, they might do it at the same time, and because the sensor has a response time of 1.5s it will glitch out

I hope I explained my issue. Thanks in advance

Have you looked at the Blynk Python library examples on GitHub?..

Pete.

Thanks. Figured that out. Now, I added a button on the app assigned to virtual pin 3.
I have this code:

@blynk.handle_event('write V3')
def controlFans(pin, value):
	print(WRITE_EVENT_PRINT_MSG.format(pin, value))
	if int(value) == 1:
		print('fans')
		GPIO.output(14, GPIO.LOW)
	else:
		GPIO.output(14, GPIO.HIGH)

@blynkTimer.register(interval=15)
def checkSensorData():
	hum, temp = DHT.read_retry(DHT_SENSOR, DHT_PIN)
	print('Temperature: {} ºC, Humidity: {} %'.format(temp,hum))
	blynk.virtual_write(TEMP_VPIN, temp)
	blynk.virtual_write(HUM_VPIN, hum)

	if  hum > 65.0 and hum < 95.0:
		print('Humidity OK')
	else:
		#do stuff to get humidity between 65 and 95%
		adjustHumidity()
		
		

while True:
	blynk.run()
	blynkTimer.run()

But when I press the button it doesn’t print anything. What am I doing wrong?

I know nothing about Python, but your method of handling the the widget value change looks nothing loke the event_VIRTUAL_WRITE.py example.

Pete.

For some reason, I was using the other repo, and on that repo it doesn’t install BlynkTimer