My project is not running as i want it to. it always implements "else" part in code and not "if" part. also blynk keeps going offline on its own. also i cannot find a way to make it function like an automatic switch with delay of 5-10 minutes

from machine import Pin, PWM
from time import sleep
import time
import network
import blynklib
from servo import Servo



# Create our Servo object, assigning the
# GPIO pin connected the PWM wire of the servo
my_servo = Servo(pin_id=15)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('***********','**************************')
 


BLYNK_TEMPLATE_ID ="******************"

BLYNK_TEMPLATE_NAME ="Pico W"
BLYNK_AUTH_TOKEN ="***********************************"

# connect the network       
wait = 10
while wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    wait -= 1
    print('waiting for connection...')
    time.sleep(1)
 
# Handle connection error
if wlan.status() != 3:
    raise RuntimeError('network connection failed')
else:
    print('connected')
    ip=wlan.ifconfig()[0]
    print('IP: ', ip)
    
angle_for_switch_contact = 110
delay_ms = 25  # Amount of milliseconds to wait between servo movements

    
"Connection to Blynk"


# initialize Blynk
blynk = blynklib.Blynk(BLYNK_AUTH_TOKEN)

# register handler for virtual pin V4 write event
@blynk.on("V15")
def write_V15_handler(value):
    
   if int(value[0]==1):

        for position in range(0,180):
            print(position)
            my_servo.write(position)
            
            time.sleep_ms(1000)
        
        
        
   else: 
        
        for position in reversed(range(0,180)):
            print(position)
            my_servo.write(position)
            
            time.sleep_ms(1000)
            
    

while True:
    blynk.run()

@Jacklord Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard (the characters you’ve used are not the correct ones).

I’d also suggest that you edit the title of your post to make it much shorter, and add some text in your post that describes your hardware, Blynk library version etc etc

Pete.

i just did that, please help me i need your guidance. where can i learn to use blynk in thonny with micropython base? i cant find any course or book to help me with that?

i want to learn it starting from basics. for instance in my code i dont know the purpose of “value” in blynk code snippet. and why it gives me error whenever i put any other number besides 0 in value[0].

You did the triple backticks, but nothing else.
I doubt you’ll get assistance until you at least state your hardware and library version.

Pete.

I am using pico w, board and thonny to code.

What is library version? Blynklib version or blynk version?

You will have installed a library locally to enable this to work.
The library version will appear in the Blynk splash screen of your serial monitor.

Pete.

please revert back.

What?

Pete.

you asked for the library version and i shared an image containing information pertaining to the later. please check that and respond back. :slight_smile:

I don’t have any advice to offer regarding your issue, as I don’t use Python.

As a moderator to the forum I was attempting to try to get you to provide all of the information that should be provided with any new “Need help…” topic. When you create a new topic in this category you are told to provide this information, and how to correctly format your code.
Instead of providing the requested information in the format requested, it’s taken 8 posts before we finally have that info.

Your issue is that you’re unlikely to attract the attention of the small number of Python programmers in the Blynk community because you didn’t edit your topic title to make it clear that this is a Python issue.

Pete.

  1. Make sure your code works without Blynk and consider using gc.collect().
  2. When connecting to your router, use wlan.config(pm = 0xa11140) # it turns off power savings.
  3. Try to stay not too far from your router.
  4. When connecting to Blynk try this:
# Initialize Blynk
try:
#     blynk = BlynkLib.Blynk(BLYNK_AUTH) # faster
    
#      or....

    blynk = BlynkLib.Blynk(BLYNK_AUTH,
        insecure=True,          	# disable SSL/TLS
        server='lon1.blynk.cloud',	# lon1 fra1.blynk.cloud or blynk.cloud
        port=80,                	# set server port
        heartbeat=30, 				# set heartbeat to 30 secs      
        log=print              	# use print function for debug logging
        )
except OSError as err:
    print(f'OS ERROR {err}')
    reset()
  1. Make sure your lib is from here…https://github.com/vshymanskyy/blynk-library-python/tree/master
  2. For more ideas see https://community.blynk.cc/t/connect-pi-pico-w-in-micropython-with-blynk/63693/33