I am using MicroPython on a ESP32 using the Blynk IoT platform to control a stepper motor. However, I want the stepper motor to stop running when my program detects that the state of the virtual pin that I am using to control the motor changes from 1 to 0, but I can't seem to implement this using the BlynkLib library for MicroPython and Blynk. I can write to a virtual pin to change its value using blynk.virtual_write(pin that I want to change, value that I want to change it to), but I don't see a function like blynk.virtual_read(pin that you want to read value from). Is there any other way that I could implement this that I am missing? You can find the BlynkLib library for MicroPython at this link: https://github.com/vshymanskyy/blynk-library-python.
This is my code so far, I know that it doesn't work, but I hope that you get the idea of what I am trying to do:
import time
import network
import BlynkLib
import steppers
WIFI_SSID = "ssid"
WIFI_PASS = "pass"
BLYNK_AUTH = "auth token"
# Initialize network connection
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(WIFI_SSID, WIFI_PASS)
# Wait for connection
while not station.isconnected():
pass
# Initialize stepper motor pins and mode
motor = steppers.HBRIDGE(14, 27, 26, 25, mode=3)
# Initialize Blynk connection
blynk = BlynkLib.Blynk(BLYNK_AUTH)
# Define button handler
def button_handler(pin, value):
print("Button on V{} pressed: {}".format(pin, value))
while int(pin) == 1:
motor.step(800, 400)
if blynk.virtual_read(value[0]) == 0: # This method doesn't work. But I want to implement something like this.
break
if int(value[0]) == 0:
print("State changed. Turning motor off.")
print("Button V{} pressed. Turning off after 5 seconds...".format(pin))
time.sleep(5)
blynk.virtual_write(pin, 0)
# Assign button handler to virtual pins
blynk.on("V*", button_handler)
# Run the Blynk main loop
while True:
blynk.run()