Strugling with Python

Hw: Raspberry Pi

Hi,

The following test code is used to turn on/off several widget leds.

The code connects to blynk-clouds (have not tried a local server yet) and works fine for a while till it crushes with a Broken Pipe error 32. To recover automatically from it (since I cannot find the reason of the crush), I’m using the sync_all() function but, I get a warning shown below. How are the v pins registered in Python?

TIA

#!/usr/bin/env python

#import libraries
import BlynkLib
import sys
from gpio import ledsOn #from gpio.py script

BLYNK_AUTH = '****'

#widgets virtual leds
wleds = [11,12,13,14,15,16]

# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

#get leds status to turn leds ON/OFF on app
def widgetsleds():
  ledslist = []
  for i in range(6): #total 6 contacts
     ledslist.insert(i,ledsOn(i)) #get swicth status
     if ledslist[i] == 1:
       blynk.virtual_write(wleds[i], 0)
     else:
       blynk.virtual_write(wleds[i], 255)
  ledslist.clear()
  
def blynk_connected():
  print("Updating all values from the server...")
  blynk.sync_all()
  
def blynk_loop():
  # Start Blynk (this call should never return)
  blynk.run()
  
#activate timers must be multiple of 50ms
blynk.set_user_task(widgetsleds(), 1000)

if __name__ == "__main__":
    while True:
      try:
        blynk_loop()
      except BrokenPipeError as e:
        print('Socket error {}'.format(e))
        blynk.on_connect(blynk_connected)
      except IOError as e:
        if e.Errno == Errno.EPIPE:
          print('EPIPE error {}'.format(e))
          blynk.on_connect(blynk_connected)
        else:
          print("Unexpected error:", sys.exc_info()[0])
          raise

TCP: Connecting to blynk-cloud.com:80
Blynk connection successful, authenticating…
Access granted, happy Blynking!
Socket error [Errno 32] Broken pipe
TCP: Connecting to blynk-cloud.com:80
Blynk connection successful, authenticating…
Access granted, happy Blynking!

Updating all values from the server…

Warning: Virtual write to unregistered pin 14
Warning: Virtual write to unregistered pin 15
Warning: Virtual write to unregistered pin 11
Warning: Virtual write to unregistered pin 12
Warning: Virtual write to unregistered pin 13
Warning: Virtual write to unregistered pin 16

Sample pin:

# V23 is button to restart the script
@blynk.VIRTUAL_WRITE(23)
def v23_write_handler(value):
	if int(value == '0'): # button returned to OFF
		sys.exit()

Thanks @Costas, I use that format for the buttons but, what about turning on a virtual led as shown in the code?

I flash my LED in a timer loop like this:

	# flash LED	
	if ledstate == 0:
		ledstate = 1
	else:
		ledstate = 0
	blynk.virtual_write(20, ledstate * 255)	

My Python code also auto generates these:

1 Like

and you’re not getting “Virtual write to unregistered pin 20” warnings? :open_mouth:
EDIT: Are you using blynk.sync_all() func? Maybe not?

No warnings and I only ever sync the pins I need. Sync all is overkill.

I understand. In my case, blynk.sync_virtual(1,2,…) also spits out warnings:roll_eyes:

You have not defined the pins for the LEDs.

Actually I haven’t either.

I do it here, no?

No. It looks like if you want to sync an LED then you define it like all the other pins.

Ok, will rewrite the code and stay away from looping arrays by registring the pins individually like:

# Register virtual pin handler
@blynk.VIRTUAL_READ(myVpin)
def myVpin_read_handler():
   #read relay status, if 1 then       
   blynk.virtual_write(myVpin, 255)

and hopefully will solve my issue

I believe this means you have a widget set to V20 in your project that has no registered (matching) code in your script. Normally it would not do anything until used in the App, or called by a sync command, and then you get the error.

@Gunner that was a question for @Costas as I’m not using pin 20.

and yes, it makes sense. If the App is off and the code is requesting a pin sync where there is nothing to sync with, a warning is well justified :neutral_face:

Well it was an answer from me :stuck_out_tongue_winking_eye: as I have seen that error many times until I realised the issue and removed the rogue widget I was unaware that I wasn’t using. BTW, it will do the same thing if presented with an API call for same missing vPin, so no widget required for that.