BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

ESP32 virtual pin read with python

Hello, I need some help with my coding. I am using NodeMCU ESP32 to send data to blynk apps using virtual pins. I can already connect to wifi and blynk but the data cannot be displayed in the app. I am trying to connect to blynk server. I programme using python. Any thoughts on improving the code?

  import usocket as socket
except:
  import socket
  
import blynklib
import network
import machine
from machine import Pin, ADC
from time import sleep
import utime as time
import esp
esp.osdebug(None)

import gc
gc.collect()
WIFI_SSID = ''
WIFI_PASS = ''

BLYNK_AUTH = ''
#internet connection 
print("Connecting to WiFi...")
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(WIFI_SSID, WIFI_PASS)
print (station.isconnected()) # False = not connected , by right it should get TRUE
while not station.isconnected(): #if the station is not connected
    time.sleep(1)
    print('WiFi connect retry ...')
 
print('IP: ', station.ifconfig())

# connect to blynk apps
print("Connecting to Blynk...")
blynk = blynklib.Blynk(BLYNK_AUTH)

# define input pin 
port = ADC(Pin(34))             # pin for steps
port.atten(ADC.ATTN_11DB) 

# register handler for virtual pin V10 reading
@blynk.handle_event('read V10')
def stepcounter_handler (pin):
  stepcount = 0
  print (stepcount)
  while True:
    print('ready to read the value')
    port_value = port.read()
    sleep(1)
    if port_value >= 2050:
      print (port_value)
      stepcount += 1 
      print ('Number of steps:',stepcount)
      print('Read event on pin {}'.format(pin))
      blynk.virtual_write(pin, stepcount)
     
    else:
      print (port_value)
      print ('no steps')


###########################################################
# infinite loop that waits for event
###########################################################

#...connect to blynk cloud...###
@blynk.handle_event("connect")
def connect_handler():
    print("[CONNECT_EVENT]")
    print('Sleeping 2 sec in connect handler...')
    time.sleep(2)
    blynk.disconnect()

@blynk.handle_event("disconnect")
def disconnect_handler():
    print("[DISCONNECT_EVENT]")
    print('Sleeping 4 sec in disconnect handler...')
    time.sleep(4)

#...connect to blynk apps...###
@blynk.handle_event('internal_acon')
def app_connect_handler(*args):
    print("[APP_CONNECT_EVENT]")

@blynk.handle_event('internal_adis')
def app_disconnect_handler(*args):
    print("[APP_DISCONNECT_EVENT]")
while True:
    blynk.run()