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

Testing Python library


#1

Hi,

The following code interacts with a second file which controls the pinout of a Raspberry board.

For some reason, the relay I’m activating gets turned on twice when selected from the app.

I’ve a similar code running on a Arduino Yun for over a year without a problem.

I’m probably doing some wrong since this’ the first time I’m playing with the Blynk-Python lib .

Any help is appreciated.

#!/usr/bin/python3

#import libraries
import BlynkLib
from gpio import relayOn #from gpio.py script

BLYNK_AUTH = '***'

# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

@blynk.VIRTUAL_WRITE(8)
def my_write_handler(value):
  if value: # is the button pressed?
    print('test.........')
    relayOn(8)

#start Blynk
blynk.run()

Printout from the console…

pi@pi:~/sketches $ python3 test.py

___  __          __

/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/

Give Blynk a Github star! => https://github.com/vshymanskyy/blynk-library-python

TCP: Connecting to blynk-cloud.com:80
Blynk connection successful, authenticating…
Access granted, happy Blynking!
test…
test…


#2

Every button press consists of two state changes… Press (ON) and Release (OFF)… thus two actions unless your code specifically looks for one or the other state.

@blynk.VIRTUAL_WRITE(8)
def my_write_handler(value):
  if int(value) == 1: # The button is pressed
    print('test.........')
    relayOn(8)
  else: # The button is released

#3

@Gunner Thanks, that was the issue. I thought that if value: is equivalent to if value == True: or if value == 1.

if int(value) == 1:

worked as well if value == ‘1’: but I understand it should be an integer.