(I use RPi 3 Model BV1.2 and Thonny 3.2.0)
This tutorial should get you a basic understanding of how you can get data from the app to your rpi in a python program. I researched it as i couldn’t get the basic ‘Blynk direct to RPi physical pins’ set up to work as it was documented here.
I was expecting the following code to work on RPi, but it doesn’t :
import blynklib import time BLYNK_AUTH = 't7BQHE3PvBko6ql-HF_WzeO-JMsHb01-' blynk = blynklib.Blynk(BLYNK_AUTH) try: blynk.run() while True: time.sleep(.1) except KeyboardInterrupt: GPIO.cleanup()
I’m looking to open a hatch via relais connected by the RPi. that’s will be the use case for this code.
Make sure you import the libraries in Thonny via menu >Tools>Manage Packages…
So this is how i get a port controlled by a button widget on the app:
create a new project with 2 button widgets. one with pin V5 and the other with pin V6
the comments in the code are in Dutch / Nederlands
import RPi.GPIO as GPIO import time import blynklib #definieer variabelen Relais=[5,6] # lijst met poort nummers van de relais, zo kunnen we alle output poorten in 1 keer instellen #configureer de input output poorten GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(Relais,GPIO.OUT) global valueV5 valueV5=0 global valueV6 valueV6=0 BLYNK_AUTH = 'your auth code' # initialize Blynk blynk = blynklib.Blynk(BLYNK_AUTH) # register handler for virtual pin V5 write event @blynk.handle_event('write V5') def write_virtual_pin_handler(pin, value): global valueV5 valueV5 = int(value) print("V5 : ",valueV5) # register handler for virtual pin V6 write event @blynk.handle_event('write V6') def write_virtual_pin_handler(pin, value): global valueV6 valueV6 = int(value) print("V6 : ",valueV6) try: while True: blynk.run() if valueV5==1: GPIO.output(5,GPIO.HIGH) else: GPIO.output(5,GPIO.LOW) if valueV6==1: GPIO.output(6,GPIO.HIGH) else: GPIO.output(6,GPIO.LOW) except KeyboardInterrupt: GPIO.cleanup()
one comment: I read that the blynk.run() statement should not be in the While True: loop but it doesn’t work if it is not in that loop…
comments to improve/optimize are very welcome