I'm trying to send single-character commands to a micropython1.22.2 program running on a Pi Pico.
This article seems to describe how to accomplish this use case.
Unfortunately it does not work.
The article suggests that the USB serial port can be shared with the REPL used by Thonny by using the select module and creating an instance of a polling object poll_obj = select.poll()
Here is the code, with some debugging added.
The text "Starting" is printed, and the LED blinks once, but never again when characters are typed into the REPL.
The characters are echoed, but the responses generated by the code do not occur.
Assuming the original code worked at some point, is there any fix to make it work now with 1.22.2?
This article seems to describe how to accomplish this use case.
Unfortunately it does not work.
The article suggests that the USB serial port can be shared with the REPL used by Thonny by using the select module and creating an instance of a polling object poll_obj = select.poll()
Here is the code, with some debugging added.
The text "Starting" is printed, and the LED blinks once, but never again when characters are typed into the REPL.
The characters are echoed, but the responses generated by the code do not occur.
Assuming the original code worked at some point, is there any fix to make it work now with 1.22.2?
Code:
import selectfrom picozero import pico_temp_sensor, pico_led, DigitalOutputDevice, DigitalInputDeviceimport sysimport timeimport machinerelay_pin = 20# set relay PIN objectrelay_drv = DigitalOutputDevice(relay_pin,active_high=True,initial_value=False)# Create an instance of a polling object poll_obj = select.poll()# Register sys.stdin (standard input) for monitoring read events with priority 1poll_obj.register(sys.stdin,1)# Pin object for controlling onboard LEDled=machine.Pin("LED",machine.Pin.OUT)print('Starting')led.on()time.sleep(1)led.off()while True: # Check if there is any data available on sys.stdin without blocking if poll_obj.poll(0): led.on() # blink LED if any character is polled time.sleep(1) led.off() # Read one character from sys.stdin ch = sys.stdin.read(1) # check presence response if data== b'?': print('@') # presence response if data== b'P': #pulse relay relay_drv.on() time.sleep(10) relay_drv.off() # Check if the character read is 't' if ch=='t': # Toggle the state of the LED led.value(not led.value()) # Print a message indicating that the LED has been toggled print ("LED toggled" ) # Small delay to avoid high CPU usage in the loop time.sleep(0.1)
Statistics: Posted by timg11 — Sat Jun 01, 2024 9:05 pm