Errors while testing new Python Library for Blynk20 [Solved]

Hi,
I’m testing the code shown here using this updated library for python.
I do connect to the cloud but I get disconnected with errors related to the library.
Has someone already tested said library with an ESP32?
TIA

MicroPython v1.16 on 2021-06-23; ESP32 module with ESP32
Type "help()" for more information.
>>> 
...
>>> %Run -c $EDITOR_CONTENT

    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ for Python v1.0.0 (esp32)

IP: 192.168.1.23
Connecting to blynk.cloud:443...
Connecting to fra1.blynk.cloud:443...
Traceback (most recent call last):
  File "<stdin>", line 36, in <module>
  File "<stdin>", line 32, in runLoop
  File "BlynkLib.py", line 261, in run
  File "BlynkLib.py", line 202, in process
  File "BlynkLib.py", line 68, in emit
  File "BlynkLib.py", line 221, in redirect
  File "BlynkLib.py", line 240, in connect
OSError: [Errno 12] ENOMEM
>>>

See solution here

Can you describe the solution because i don’t get it on the other post. I have the pi zero and it keeps disconnecting after a while. Running on lib 1.0.0…

You probably have this line wrong:

blynk = BlynkLib.Blynk(BLYNK_AUTH, server="blynk.cloud")

Try to use your assigned local server instead of “blynk.cloud”. You’ll find it on the right bottom corner of your dashboard page.

Do you mean like this? Region: fra1. But its not working.

blynk = BlynkLib.Blynk(BLYNK_AUTH, server=Region:fra1) ^ SyntaxError: invalid syntax

The sintax error is on the :

No.

You need to add the region subdomain, so this:

blynk = BlynkLib.Blynk(BLYNK_AUTH, server="blynk.cloud")

becomes this:

blynk = BlynkLib.Blynk(BLYNK_AUTH, server="fra1.blynk.cloud")

More info (although for a different purpose) here:

Pete.

Thank you. Now it connects. I will test it and see if its stay connected for longer periods.

1 Like

If you’re still having timing issues, try to play with this code’s variables:

    # Initialize Blynk
    blynk = BlynkLib.Blynk(BLYNK_AUTH,
                           insecure=True,          # disable SSL/TLS
                           server='fra1.blynk.cloud',   # set server address
                           port=80,                # set server port
                           heartbeat=30,           # set heartbeat to 30 secs
                           log=print               # use print function for debug logging
                           )

Do you mean like this?

blynk = BlynkLib.Blynk(BLYNK_AUTH,server="fra1.blynk.cloud",insecure=True,port=80,heartbeat=30,log=print)

Yes. Have you tried it?

For me its not working.

   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ for Python v1.0.0 (linux)

Connecting to fra1.blynk.cloud:80...
< 29 1 | xxx
Traceback (most recent call last):
  File "/home/pi/temp1_blynk.py", line 14, in <module>
    blynk = BlynkLib.Blynk(BLYNK_AUTH,server="fra1.blynk.cloud",insecure=True,port=80,heartbeat=30,log=print)  File "/usr/local/lib/python3.9/dist-packages/BlynkLib.py", line 214, in __init__
    BlynkProtocol.__init__(self, auth, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/BlynkLib.py", line 81, in __init__
    self.connect()
  File "/usr/local/lib/python3.9/dist-packages/BlynkLib.py", line 245, in connect
    BlynkProtocol.connect(self)
  File "/usr/local/lib/python3.9/dist-packages/BlynkLib.py", line 125, in connect
    self._send(MSG_HW_LOGIN, self.auth)
  File "/usr/local/lib/python3.9/dist-packages/BlynkLib.py", line 117, in _send
    self._write(msg)
  File "/usr/local/lib/python3.9/dist-packages/BlynkLib.py", line 249, in _write
    self.conn.write(data)
AttributeError: 'socket' object has no attribute 'write'

Could you post your basic connection code?
Also, BlynkLib.Blynk(BLYNK_AUTH,server=“fra1.blynk.cloud”,insecure=True,port=80,heartbeat=30,log=print) aren’t you missing the BLYNK_AUTH string?

No its all in. With just the AUTH and Server the connection works but its not stable. I get at least one disconnect from morning till 12. If i add the parameters you suggested i get the error that i posted.

import BlynkLib


BLYNK_TEMPLATE_ID = 'x'
BLYNK_DEVICE_NAME = 'xi'
BLYNK_AUTH = 'xxx'

blynk = BlynkLib.Blynk(BLYNK_AUTH,server="fra1.blynk.cloud",insecure=True,port=80,heartbeat=30,log=print)

@mat_pav can you please put triple backticks at the beginning and end of your code, and your serial output.

You used a single backtick last time, and it doesn’t format your code correctly.

Triple backticks look like this:
```

Thanks,

Pete.

I don’t know what’s wrong bur now I’m getting disconnected after 2 minutes. The script is still running no error just the connection with the server is done. Last week I was getting around 2h without disconnect with the same script.

import BlynkLib
import RPi.GPIO as GPIO

BLYNK_TEMPLATE_ID = 'xxx'
BLYNK_DEVICE_NAME = 'Namakalni'
BLYNK_AUTH = 'xxx'

blynk = BlynkLib.Blynk(BLYNK_AUTH, server="fra1.blynk.cloud")

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

# Definiranje pinov za upravljanje posameznih vej
veje = [
    {'ime': 'releVEJA_1', 'rele_pin': 13, 'rele_mode': GPIO.OUT, 'rele_init': GPIO.LOW, 'blynk_pin': 4},
    {'ime': 'releVEJA_2', 'rele_pin': 16, 'rele_mode': GPIO.OUT, 'rele_init': GPIO.LOW, 'blynk_pin': 5},
    {'ime': 'releVEJA_3', 'rele_pin': 19, 'rele_mode': GPIO.OUT, 'rele_init': GPIO.LOW, 'blynk_pin': 6},
    {'ime': 'releVEJA_4', 'rele_pin': 20, 'rele_mode': GPIO.OUT, 'rele_init': GPIO.LOW, 'blynk_pin': 7},
    {'ime': 'releVEJA_5', 'rele_pin': 26, 'rele_mode': GPIO.OUT, 'rele_init': GPIO.LOW, 'blynk_pin': 8},
    {'ime': 'releVEJA_6', 'rele_pin': 21, 'rele_mode': GPIO.OUT, 'rele_init': GPIO.LOW, 'blynk_pin': 9}
]


# Nastavitve: RASPBERRY PIN MODE in začetne vrednosti
GPIO.setmode(GPIO.BCM) # use PHYSICAL GPIO Numbering
for veja in veje:
    # vsem vejam nastavimo output in začetno stanje
    rele_pin = veja['rele_pin']
    rele_mode = veja['rele_mode']
    rele_init = veja['rele_init']
    GPIO.setup(rele_pin, rele_mode)
    GPIO.output(rele_pin, rele_init)


@blynk.on("V*")
def blynk_handle_vpins(pin, value):

    print('PIN: ', pin)

    # gre skozi vse veje in sproži ukaze za posamezen pin
    for veja in veje:
        veja_ime = veja['ime']
        veja_blynk_pin = veja['blynk_pin']
        veja_rele_pin = veja['rele_pin']
        
        if pin == str(veja_blynk_pin):

            if value == ['1']:
                print("V{} value: {} Status: Vklopljeno!".format(veja_ime, value))
                GPIO.output(veja_rele_pin, GPIO.HIGH)
            
            else:
                print("V{} value: {} Status: Izklopljeno!".format(veja_ime, value))
                GPIO.output(veja_rele_pin, GPIO.LOW)
        
        else:
            # preskoči veje, ki jih pin ne upravlja
            pass

while True:
    blynk.run()

Even if i have the code like this is the same.

blynk = BlynkLib.Blynk(BLYNK_AUTH)

Can you help me. I don’t know what else i can try. Thank you.

why this function is used

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