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

Button control GPIO state with python

Hello All,
Pretty new in the blynk world which sound good on what I want to do for a DIY project to make my garden more smart …
I have a raspberry which control 12v lights and flooding valves and i want to add smartphone control, i thought blynk was the ideal app for that.
As all my stuff is already in python, i tried to control first by manual action like adding a button. But after hours reading tutos or example still did not manage to make that so simple stuff.
Does someone has any advices to help me going farther with blink … just to make a blynk button controling a raspberry gpio state in python code.

Thanks for any help…

@++

ok …
finally managed to do something working with the following code
if improvement needed please feel free to add ! i can go a little farther now !

import blynklib
import RPi.GPIO as GPIO

BLYNK_AUTH = 'my token'

# initialize Blynk
blynk = blynklib.Blynk(BLYNK_AUTH)

WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_PIN_EVENT] Pin: V{} Value: '{}'"


ledPin = 11 # define ledPin
GPIO.setmode(GPIO.BOARD) # use PHYSICAL GPIO Numbering
GPIO.setup(ledPin, GPIO.OUT) # set the ledPin to OUTPUT mode
GPIO.output(ledPin, GPIO.LOW) # make ledPin output LOW level 

# register handler for virtual pin V4 write event
@blynk.handle_event('write V4')
def write_virtual_pin_handler(pin, value):
        if value == ['1']:
                print(WRITE_EVENT_PRINT_MSG.format(pin, value))
                print('LED allumee')
                GPIO.output(ledPin, GPIO.HIGH)
        else:
                print(WRITE_EVENT_PRINT_MSG.format(pin, value))
                print('LED eteinte')
                GPIO.output(ledPin, GPIO.LOW) 

while True:
    blynk.run()