ESP32 Virtual Pin Read with Python

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
aimanazli17
Posts: 2
Joined: Mon Jan 11, 2021 6:33 am

ESP32 Virtual Pin Read with Python

Post by aimanazli17 » Mon Jan 11, 2021 8:20 am

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 micropython. 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()

Lennyz1988
Posts: 6
Joined: Thu Aug 02, 2018 6:22 am

Re: ESP32 Virtual Pin Read with Python

Post by Lennyz1988 » Sun Jan 17, 2021 5:15 pm

Do you have an enter before the while True?

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

while True:
blynk.run()

aimanazli17
Posts: 2
Joined: Mon Jan 11, 2021 6:33 am

Re: ESP32 Virtual Pin Read with Python

Post by aimanazli17 » Sat Jan 23, 2021 4:19 am

#...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)

I only can enter this code, which means that I can see the printed message at the terminal, other than that cannot see any printed message. So, what is the problem?

Post Reply