Blynk 2.0 Notifications on Raspberry pi using Python3 not working

Raspberry pi 3b, running python3. Blynk Lib Python v1.0.0 (linux)
I need help with my python code.
Have looked through the forums and internet for this problem and can’t find the solution.

I am trying to incorporate Blynk Notifications into my python script.
This is only part of my code, but basically does the same thing.
I can use this code to turn lights on/off (timed) daily.
Also have my doorbell hooked up so when someone rings the bell a light goes on.

I have incorporated Blynk 2.0 into the script so I can turn the lights on and off with the Blynk app on my Android phone and Web Template, which works excellently.

My only problem is coding the Blynk.logEvent in the script

I have an event to notify me when a light is turned on using "Blynk.logEvent(downstairs_light_ on). I get an Error either Blynk not defined or AttributeError: ‘function’ object has no attribute ‘logEvent’ depending on where I place the Blynk.logEvent()

I do get the Events notification using the HTTPS API and my Auth Code and “downstairs_light_on” or
“some_one_at_the_door”

Thanks in advance
RichardM

#!/usr/bin/python

import time
import datetime
import os
import RPi.GPIO as GPIO
import BlynkLib
from BlynkTimer import BlynkTimer
from time import sleep
GPIO.setwarnings(False)

BLYNK_AUTH = ""
BLYNK_TEMPLATE_ID = ""
BLYNK_DEVICE_NAME = "Quickstart Template"
GPIO.setmode(GPIO.BOARD) #Use Board pin out numbers

# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

@blynk.on("connected")
def blynk_connected(ping):
    print('Blynk ready. Ping:', ping, 'ms')
    
@blynk.on("disconnected")
def blynk_disconnected():
    print('Blynk disconnected')

@blynk.on("V*")
def blynk_handle_vpins(pin, value):
    print("V{} value: {}".format(pin, value))

@blynk.on("V0") #Garage
def V0_write_handler(value):
    if int(value[0]) != 0:
        GPIO.output(R17, GPIO.LOW)      #Turn On Relay 7 if all above conditions are true
        time.sleep(SleepTimeSR);
        GPIO.output(R17, GPIO.HIGH)
        print('Garage ON')
    else:
        GPIO.output(R18, GPIO.LOW)      #Turn On Relay 7 if all above conditions are true
        time.sleep(SleepTimeSR);
        GPIO.output(R18, GPIO.HIGH)
        print('Garage OFF')

@blynk.on("V1") #Back Door Plug
def V1_write_handler(value):
    if int(value[0]) != 0:
        GPIO.output(R5, GPIO.LOW)      #Turn On Relay 7 if all above conditions are true
        time.sleep(SleepTimeSR);
        GPIO.output(R5, GPIO.HIGH)
        print('Xmas ligths ON')
    else:
        GPIO.output(R6, GPIO.LOW)      #Turn On Relay 7 if all above conditions are true
        time.sleep(SleepTimeSR);
        GPIO.output(R6, GPIO.HIGH)
        print('Xmas lights OFF')

@blynk.on("V2") #Kitchen Sink
def V2_write_handler(value):
    if int(value[0]) != 0:
        GPIO.output(R5, GPIO.LOW)      #Turn On Relay 7 if all above conditions are true
        time.sleep(SleepTimeSR);
        GPIO.output(R5, GPIO.HIGH)
        print('Kitchsn Sink ON')
    else:
        GPIO.output(R6, GPIO.LOW)      #Turn On Relay 7 if all above conditions are true
        time.sleep(SleepTimeSR);
        GPIO.output(R6, GPIO.HIGH)
        print('Kitchen Sink OFF')
        
@blynk.on("V3") #Family Room #4
def V3_write_handler(value):
    if int(value[0]) != 0:
        GPIO.output(R1, GPIO.LOW)      #Turn On Relay 7 if all above conditions are true
        time.sleep(SleepTimeSR);
        GPIO.output(R1, GPIO.HIGH)
        print('Family Room 4 ON')
    else:
        GPIO.output(R2, GPIO.LOW)      #Turn On Relay 7 if all above conditions are true
        time.sleep(SleepTimeSR);
        GPIO.output(R2, GPIO.HIGH)
        print('Family Room 4 OFF')

@blynk.on("V4") #Front Door
def V4_write_handler(value):
    if int(value[0]) != 0:
        GPIO.output(R3, GPIO.LOW)       
        time.sleep(SleepTimeSR);
        GPIO.output(R3, GPIO.HIGH)
        print('Front Door ON')
    else:
        GPIO.output(R4, GPIO.LOW)     
        time.sleep(SleepTimeSR);
        GPIO.output(R4, GPIO.HIGH)
        print('Front Door OFF')

@blynk.on("V5") #Down Stairs Light on
def V5_write_handler(value):
    if int(value[0]) != 0:
        GPIO.output(R13, GPIO.LOW)       
        time.sleep(SleepTimeSR);
        GPIO.output(R13, GPIO.HIGH)
        print('downstairs_on')
        Blynk.logEvent('downstairs_on')   #*Get an error here when light is turned on*
    else:
        GPIO.output(R14, GPIO.LOW)     
        time.sleep(SleepTimeSR);
        GPIO.output(R14, GPIO.HIGH)
        print('Downstairs OFF')
    
@blynk.on("V6") #Living Room
def V6_write_handler(value):
    if int(value[0]) != 0:
        GPIO.output(R15, GPIO.LOW)       
        time.sleep(SleepTimeSR);
        GPIO.output(R15, GPIO.HIGH)
        print('Living Room ON')
    else:
        GPIO.output(R16, GPIO.LOW)     
        time.sleep(SleepTimeSR);
        GPIO.output(R16, GPIO.HIGH)
        print('Living Room OFF')

@blynk.on("V8") #Relay Power
def V8_write_handler(value):
    if int(value[0]) != 0:
        GPIO.output(m, GPIO.HIGH)       
        print('Relay Power ON')
    else:
        GPIO.output(m, GPIO.LOW)     
        print('Relay Power OFF')
  

pinList = [11, 8, 10, 13, 15, 16, 18, 19, 22, 24, 26, 32, 36, 29, 31, 33, 21, 23]
pinList1 = [12]
pinList2 = [35]
pinList3 = [40]
pinList4 = [38] #Relay Power
pinList5 = [37] #Door bell Power

# loop through pins and set mode and state to 'low'

for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)
for l in pinList3: #Door Bell
    GPIO.setup(l, GPIO.IN)
for n in pinList5:     
    GPIO.setup(n, GPIO.OUT)
    GPIO.output(n, GPIO.LOW)
    
#Virtual inputs Door Bell
def detect(ev=None):
    state=GPIO.input(l)
    blynk.virtual_write(12, state)
    Blynk.logEvent('some_one_at_the_door') #**Get an Error when door bell rings**

GPIO.add_event_detect(l, GPIO.BOTH, callback=detect, bouncetime=200)


#Define Relay outputs
R1 = 11
R2 = 13
R3 = 15
R4 = 16
R5 = 18
R6 = 22
R7 = 32
R8 = 36
R9 = 29
R10 = 31
R11 = 33
R12 = 23
R13 = 19
R14 = 21
R15 = 8
R16 = 10
R17 = 24
R18 = 26
PIR_PIN = 12
PIR_IN = 35
PIR_DOOR = 40


SleepTimeS = 0.5
SleepTimeL = 1
SleepTimeSR = 0.4 #Time between Relay on off
#SleepTimePIR = 15 #Time for PIR light to be on
# Setup GPIO pins
GPIO.setup(R1, GPIO.OUT)
GPIO.setup(R2, GPIO.OUT)
GPIO.setup(R3, GPIO.OUT)

GPIO.setup(R14, GPIO.OUT)
GPIO.setup(R15, GPIO.OUT)
GPIO.setup(R16, GPIO.OUT)
GPIO.setup(R17, GPIO.OUT)
GPIO.setup(R18, GPIO.OUT)
time.sleep(SleepTimeL)

##Turn on House Relay Power "m" & Door Bell Power "n"
GPIO.output(n, GPIO.HIGH)

Current_State  = 0
Previous_State = 0
Variable = 0
###today = datetime.date.today()
 
try:
 
  # Loop until users quits with CTRL-C
    while True :
        blynk.run()
            
except KeyboardInterrupt:
  print ("  Quit")
  # Reset GPIO settings
  GPIO.cleanup()        
#GPIO.cleanup()

The “Blynk.logEvent(event_code)” command belongs to the C++ library. The Python library command is “blynk.log_event(event_code)”.

Hey John

Thank you very much, works perfectly.
I knew it was something simple but just couldn’t find the right code.
I greatly appreciate that.
Richard

1 Like