Cannot connect to Blynk using raspberry pi 4 model B

Hello, I am now using python to connect raspberry pi 4 model B with Blynk. However, the connection shown on Blynk always offline even they are all in the same internet connection. Is there any code’s problem as the output came out with no error.

The code for main1.py:


BLYNK_TEMPLATE_ID = "TMPL6818TwA8M"
BLYNK_TEMPLATE_NAME = "IoT Project 1"
BLYNK_AUTH = "L2GSGH1gM0_FR2H-eAQqPmvTPmdKNzW_"

import RPi.GPIO as GPIO
from ldr_pr import LDR
import blynklib 
from blynkapi import Blynk
import sys

LED_1 = Blynk(BLYNK_AUTH, pin = "V5")
LED_2 = Blynk(BLYNK_AUTH, pin = "V7")
LED_3 = Blynk(BLYNK_AUTH, pin = "V6")
All_LED = Blynk(BLYNK_AUTH, pin = "V3")
DEC = Blynk(BLYNK_AUTH, pin = "V8")

blynk = blynklib.Blynk(BLYNK_AUTH)

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

# register handler for virtual pin V5 write event
@blynk.handle_event('write V5')
def write_virtual_pin_handler(pin, value):
    print(WRITE_EVENT_PRINT_MSG.format(pin, value))

def my_write():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    p=0
    for i in range(0,5):
        p=p+LDR(11,13)
    p_avg = p/5
    blynk.virtual_write(2, '{:.2f}'.format(p_avg))
        #print(p_avg)
    if p_avg > 500:
        GPIO.output(18, GPIO.HIGH)
        GPIO.output(15,GPIO.HIGH)
        GPIO.output(16,GPIO.HIGH)
        value=1
        blynk.virtual_write(3, value)

    else:
        GPIO.output(18, GPIO.LOW)
        GPIO.output(15,GPIO.LOW)
        GPIO.output(16,GPIO.LOW)
        value=0
        blynk.virtual_write(3, value)

READ_PRINT_MSG = "[READ_VIRTUAL_PIN_EVENT] Pin: V{}"


# register handler for virtual pin V3 reading
@blynk.handle_event('read V3')
def read_virtual_pin_handler(pin):
    print(READ_PRINT_MSG.format(pin))
    blynk.virtual_write(pin, random.randint(0, 255))

def my_read():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    first_led=LED_1.get_val()
    second_led=LED_2.get_val()
    third_led=LED_3.get_val()
    ld_1=int(float(first_led[0]))
    ld_2=int(float(second_led[0]))
    ld_3=int(float(third_led[0]))
    if ld_1==1:
        GPIO.output(18, GPIO.HIGH)
    else:
        GPIO.output(18,GPIO.LOW)

    if ld_2==1:
        GPIO.output(15, GPIO.HIGH)
    else:
        GPIO.output(15,GPIO.LOW)

    if ld_3==1:
        GPIO.output(16, GPIO.HIGH)
    else:
        GPIO.output(16,GPIO.LOW)

try:
    while True:
        dc = DEC.get_val()
        if dc and isinstance(dc[0], (int, float)):
            dec = int(float(dc[0]))
            if dec == 0:
                my_write()
            else:
                my_read()
        blynk.run()

except KeyboardInterrupt:
    sys.exit(0)

The code for ldr_pr.py:

import RPi.GPIO as GPIO
import time

def LDR(cpin,lpin):
        GPIO.setmode(GPIO.BOARD)
        cap=0.0000001
        adj=2.130620985
        GPIO.setup(cpin, GPIO.OUT)
        GPIO.setup(lpin, GPIO.OUT)
        GPIO.output(cpin, False)
        GPIO.output(lpin, False)
        time.sleep(0.2)
        GPIO.setup(cpin, GPIO.IN)
        time.sleep(0.2)
        GPIO.output(lpin, True)
        starttime=time.time()
        endtime=time.time()
        while (GPIO.input(cpin) == GPIO.LOW):
            endtime=time.time()
        measureresistance=endtime-starttime
        res=(measureresistance/cap)*adj
        return(res/10)

What version of the Blynk Python library are you using?

What does your serial monitor show?

Pete.

I’m using Blynk python 3 library and I’m using thinkSpeak

But what version of the Blynk library are you using?
What version does it say in BlynkLib.py ?

Pete.

I’m using version 0.2.6

You should be using v1.0.0 from here:

There isn’t a bundled release, so you need to manually copy the files.

Pete.

I have changed to this version already but it still not functioning.
Now, the output came out this error:
AttributeError: ‘Blynk’ object has no attribute ‘VIRTUAL_WRITE’.

after the code:

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

# register handler for virtual pin V5 write event
@blynk.handle_event('write V5')
def write_virtual_pin_handler(pin, value):
    print(WRITE_EVENT_PRINT_MSG.format(pin, value))

has changed to @Blynk.VIRTUAL_WRITE(1)

Have you looked at the examples?

Pete.

Yeah. This is my code in main1.py looks like:

BLYNK_AUTH = "Ua4TS-IEH_s-oV5Jll1I0s7gJdFYEkfE"

import RPi.GPIO as GPIO
from ldr_pr import LDR
import BlynkLib 
from blynkapi import Blynk
import sys

# BLYNK_AUTH = 'Ua4TS-IEH_s-oV5Jll1I0s7gJdFYEkfE'

LED_1 = Blynk(BLYNK_AUTH, pin = "V5")
LED_2 = Blynk(BLYNK_AUTH, pin = "V7")
LED_3 = Blynk(BLYNK_AUTH, pin = "V6")
All_LED = Blynk(BLYNK_AUTH, pin = "V3")
DEC = Blynk(BLYNK_AUTH, pin = "V8")

blynk = BlynkLib.Blynk(BLYNK_AUTH)

@blynk.VIRTUAL_WRITE(1)
# WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_PIN_EVENT] Pin: V{} Value: '{}'"
# 
# # register handler for virtual pin V5 write event
# @blynk.handle_event('write V5')
# def write_virtual_pin_handler(pin, value):
#     print(WRITE_EVENT_PRINT_MSG.format(pin, value))

def my_write():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    p=0
    for i in range(0,5):
        p=p+LDR(11,13)
    p_avg = p/5
    blynk.virtual_write(2, '{:.2f}'.format(p_avg))
        #print(p_avg)
    if p_avg > 500:
        GPIO.output(18, GPIO.HIGH)
        GPIO.output(15,GPIO.HIGH)
        GPIO.output(16,GPIO.HIGH)
        value=1
        blynk.virtual_write(3, value)

    else:
        GPIO.output(18, GPIO.LOW)
        GPIO.output(15,GPIO.LOW)
        GPIO.output(16,GPIO.LOW)
        value=0
        blynk.virtual_write(3, value)


@blynk.VIRTUAL_READ(4)

# READ_PRINT_MSG = "[READ_VIRTUAL_PIN_EVENT] Pin: V{}"
# 
# 
# # register handler for virtual pin V3 reading
# @blynk.handle_event('read V3')
# def read_virtual_pin_handler(pin):
#     print(READ_PRINT_MSG.format(pin))
#     blynk.virtual_write(pin, random.randint(0, 255))

def my_read():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    first_led=LED_1.get_val()
    second_led=LED_2.get_val()
    third_led=LED_3.get_val()
    ld_1=int(float(first_led[0]))
    ld_2=int(float(second_led[0]))
    ld_3=int(float(third_led[0]))
    if ld_1==1:
        GPIO.output(18, GPIO.HIGH)
    else:
        GPIO.output(18,GPIO.LOW)

    if ld_2==1:
        GPIO.output(15, GPIO.HIGH)
    else:
        GPIO.output(15,GPIO.LOW)

    if ld_3==1:
        GPIO.output(16, GPIO.HIGH)
    else:
        GPIO.output(16,GPIO.LOW)

try:
    while True:
        dc = DEC.get_val()
        if dc and isinstance(dc[0], (int, float)):
            dec = int(float(dc[0]))
            if dec == 0:
                my_write()
            else:
                my_read()
        blynk.run()

except KeyboardInterrupt:
    sys.exit(0)

I have copied the files and the error was in this line: @blynk.VIRTUAL_WRITE(1)
with this error: AttributeError: ‘Blynk’ object has no attribute ‘VIRTUAL_WRITE’.

I’d suggest you look again.
The examples show the virtual write commands in lower case…

blynk.virtual_write(0, "time:" + str(t))

Pete.

Now Blynk is online already. However, the hardware component part the LED does not functioning. How can I change the code to make LED function?

this is the code:

BLYNK_AUTH = "Ua4TS-IEH_s-oV5Jll1I0s7gJdFYEkfE"

import RPi.GPIO as GPIO
from ldr_pr import LDR
import BlynkLib 
from blynkapi import Blynk
import sys
import time

LED_1 = Blynk(BLYNK_AUTH, pin = "V5")
LED_2 = Blynk(BLYNK_AUTH, pin = "V7")
LED_3 = Blynk(BLYNK_AUTH, pin = "V6")
All_LED = Blynk(BLYNK_AUTH, pin = "V3")
DEC = Blynk(BLYNK_AUTH, pin = "V8")

blynk = BlynkLib.Blynk(BLYNK_AUTH)

@blynk.VIRTUAL_WRITE(1)

def my_write():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    p=0
    for i in range(0,5):
        p=p+LDR(11,13)
    p_avg = p/5
    blynk.virtual_write(2, '{:.2f}'.format(p_avg))
        #print(p_avg)
    if p_avg > 500:
        GPIO.output(18, GPIO.HIGH)
        GPIO.output(15,GPIO.HIGH)
        GPIO.output(16,GPIO.HIGH)
        value=1
        blynk.virtual_write(3, value)

    else:
        GPIO.output(18, GPIO.LOW)
        GPIO.output(15,GPIO.LOW)
        GPIO.output(16,GPIO.LOW)
        value=0
        blynk.virtual_write(3, value)

@blynk.VIRTUAL_READ(4)

def my_read():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    first_led=LED_1.get_val()
    second_led=LED_2.get_val()
    third_led=LED_3.get_val()
    ld_1=int(float(first_led[0]))
    ld_2=int(float(second_led[0]))
    ld_3=int(float(third_led[0]))
    if ld_1==1:
        GPIO.output(18, GPIO.HIGH)
    else:
        GPIO.output(18,GPIO.LOW)

    if ld_2==1:
        GPIO.output(15, GPIO.HIGH)
    else:
        GPIO.output(15,GPIO.LOW)

    if ld_3==1:
        GPIO.output(16, GPIO.HIGH)
    else:
        GPIO.output(16,GPIO.LOW)

try:
    while True:
        dc = DEC.get_val()
        if dc and isinstance(dc[0], (int, float)):
            dec = int(float(dc[0]))
            if dec == 0:
                my_write()
            else:
                my_read()
        blynk.run()

except KeyboardInterrupt:
    sys.exit(0)

The lines: @blynk.VIRTUAL_WRITE(1) & @blynk.VIRTUAL_READ(4) came out with attribute error

And I followed with this link: GitHub - vshymanskyy/blynk-library-python: Blynk library for Python. Works with Python 2, Python 3, MicroPython.