đŸ”„ Blynk library for Python / MicroPython v0.2.0 is released!

Everything im testing works great. I found once i got into it, it was hard to stop playing :slight_smile:

notes: the timer lib wasnt included with the main lib when i ran the pip install. no big deal, just copied them over.
table.widget support and email would be my only requests from this library.

Thanks @vshymanskyy , nice work!
Larry

I agree
 something about Python, it’s syntax, and the speed/simplicity of “edit, save, run/test, edit, save, run/test
” (well, at least on a RPi) is a refreshing change from the Arduino IDE method.

I am finally even able to use Python 3.x

I looks like it is on its way
 at least this small part of it is referenced in the library :wink:

image

Like with BlynkTimer, clearly a work in progress, so keep updating as and when it looks like updates are released.

Just a quick note:

In the tweet _notify.py library example (or when checking a response from a button),
if value[0]: # is the the button is pressed?
checks for a string ‘0’ or ‘1’ for being a “list” so, the correct syntax should be
if (value[0] == '1'): # the button is pressed?

For non Python library, the value is an int.

1 Like

Thanks, you’re right

Opps
 so looks like I will soon have to change all my previously changed code back :stuck_out_tongue:

After trial and error I was able to get this to work


if value[0] == str('1'):

@vshymanskyy what do you mean with the note on line 70 of the BlynkLib These are mainly for backward-compatibility you can use “blynk.ON()” instead because blynk.ON doesn’t substitute “VIRTUAL_WRITE/READ”, right?

Also, virtual pins (blynk, pin) are always treated as strings, right?

So, the following code is not correct

v_led_pin = 1
@blynk.VIRTUAL_WRITE(v_led_pin)

while this one is?

v_led_pin = '1'
v_arry = ['1', '2', '3']

@blynk.VIRTUAL_WRITE(v_led_pin)
@blynk.VIRTUAL_WRITE(v_arry[1])

Hi @vshymanskyy, With the following test code I converted the old “@blynk.VIRTUAL_WRITE” function to the new one “@blynk.ON” as per the library’s note

These are mainly for backward-compatibility you can use “blynk.ON()” instead

However, I noticed an inconsistency with the type of variable it can be used. At times an array is admitted and at time it’s not.

#!/usr/bin/python3

#import libraries
from __future__ import print_function
import BlynkLib
from BlynkTimer import BlynkTimer
from subprocess import check_output
import logging, time, gc
from datetime import datetime as dt

logging.basicConfig(filename='/home/pi/blynk.log',level=logging.DEBUG)

BLYNK_AUTH = '****' 

# Initialize Blynkxy
blynk = BlynkLib.Blynk(BLYNK_AUTH)

#virtual pins array
v_arry = ['0','1','V2','V3'] #<--- 'V2' works but not 'V3'

#*** old format ***
@blynk.VIRTUAL_WRITE(v_arry[0])
def btn0_handler(value):
  btn0status = value[0]
  if btn0status == '1':
    print('btn v0 pressed')
    time.sleep(.1)
  else:
    print('btn v0 released')

@blynk.VIRTUAL_WRITE(v_arry[1])
def btn1_handler(value):
  #security check
  #btn1 is activated only when btn0 is being pressed
  if value[0] == '1':
          print('btn v1 pressed')


#*** new format ***
@blynk.ON("connected")
def blynk_connected(ping):
  print('Blynk ready. Ping:', ping, 'ms')

@blynk.ON(v_arry[2])
def btn2_handler(value):
  if(value[0] == '1'):
    print('btn v2 pressed')

@blynk.ON('readV*')    #is this correct?
def uptime_handle(V3): #won't accept an array
  gc.collect()
  #get board total uptime
  cmd = 'uptime -p'
  t = check_output(cmd, shell = True).decode("utf-8")
  t = t.rstrip('\n\r')
  t = t.replace('up','')
  blynk.virtual_write(V3, t)

if __name__ == "__main__":
  while True:
    try:
      blynk.run()
    except IOError as e:
      if(e.errno == errno.EPIPE):
        logging.exception("msg from blynkmain @ " + str(dt.now()))

Hmm
 i’m confused
 you say “official library”. Whats the difference to the following one? Especially on the attribute “official”?

I guess some people are helping out by adding extra codes to the lib making it “unofficial” ?

I think they are bothe “official” in the sense that they are mostly written by Blynk’s own @vshymanskyy
 but the newest copy/version (0.2.4) is now under the Blynkkk GitHub banner as of a couple of months ago, probably making it the new, new official version :stuck_out_tongue:

Yes it looks like this is the new, new official library.
But I neither use or work on this one :wink: This is a fork for one of our potential customers.

I see
 well, I don’t want to mess up my installation with any potentially conflicting code, so I will stick with just your official 0.2.0 :stuck_out_tongue: Hope you do keep it updated as needed.

1 Like

On the subject of Python is anyone using SSL? If so could we see the code snippet please.

@Costas Hey Paul, just an off subject question: are you running your Python projects on a local or blynk-cloud server?

@Emilio during the test phase we run local Python to cloud based Blynk server but we also have remote Python server to a separate cloud based Blynker server. The second scenario is basically our production model after testing.

Problem: blynk.set_user_task(my_user_task, 3000)
AttributeError: ‘Blynk’ object has no attribute ‘set_user_task’
Question: what to do so with the attribute error,can someone answer me??


from gpiozero import LED, Button, Buzzer
import BlynkLib
import Adafruit_DHT

led1 = LED(17)
led2 = LED(18)
led3 = LED(27)
led4 = LED(22)
led5 = LED(25)
led6 = LED(12)
led7 = LED(13)
led8 = LED(19)

sw1 = Button(21)
buzzer = Buzzer(26)
sensor_type = Adafruit_DHT.DHT22
sensor_pin = 17

BLYNK_AUTH = 'YourAuthToken'

# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

# Register Virtual Pins
@blynk.VIRTUAL_WRITE(1)
def my_write_handler(value):
    print('Current V1 value: {}'.format(value[0]))
    if int(format(value[0])) == 1:
        led8.on()
    else:
        led8.off()

def sw1Pressed():
    buzzer.beep(0.1, 0.1, 2)
    print('SW1 is pressed')
    blynk.notify('SW1 is pressed')

def my_user_task():
    humidity, temperature = Adafruit_DHT.read_retry(sensor_type, sensor_pin)
    print('Humidity = {:.2f}%\tTemperature = {:.2f}C'.format(humidity, temperature))
    blynk.virtual_write(2, '{:.2f}'.format(temperature))
    blynk.virtual_write(3, '{:.2f}'.format(humidity))

blynk.set_user_task(my_user_task, 3000)

sw1.when_pressed = sw1Pressed

try:
    while True:
        blynk.run()

except KeyboardInterrupt:
    sys.exit(0)

this was removed completely. please check out new examples

where is the new example?

Maybe the timer.py example here?:

Pete.

I see

Really Thanks for helping
Finally can finish my project