You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have this MicroPython class running on my esp32. I did try to simplify it a bit and not include code not relevant to my question.
from machine import Pin
import sys
import time
import uselect
led = Pin(2, Pin.OUT)
serialPoll = uselect.poll()
serialPoll.register(sys.stdin, uselect.POLLIN)
serialBuffer = ""
def handle_command(message):
"""
Handle a message received over serial.
The message may contain multiple commands within it.
Each command within the message will be split into separate submessages which will be handled one at a time.
:param message: (string) The message to handle
"""
if not message: # if the string is empty
return 1
if ("\n" in message):
if submessage == "2on":
led.on() # the led turns on
time.sleep(1) # the led stays on for this time period
sys.stdout.buffer.write("led on\n") # the led is turning off here for some reason when using the Python app but not with the Arduino IDE serial monitor
else: # unrecognized message
sys.stdout.buffer.write("error\n")
global serialBuffer
serialBuffer = ""
def read_serial():
"""
Reads and returns all available data over serial.
:return: Returns the character which were read, otherwise returns None
"""
if serialPoll.poll(): # check the serialPoll every 10 milliseconds
return sys.stdin.readline()
else:
return None
while True:
# continuously read messages over serial and handle them
serialData = read_serial()
if serialData != None:
serialBuffer += str(serialData)
handle_command(serialBuffer)
When I test this code using the Arduino IDE's built in serial monitor the ESP32 behaves as intended. I am sending the message "2on" with the new line character selected from the drop down menu, and when I do that the onboard LED turns on and the ESP32 responds with "led on". I have attached an image below, there is not much to see, but basically I hit the restart button on the ESP32, so the ESP32 dumped a bunch of stuff to the monitor when restarting, then I typed "2on" into the message box (I did not actually type the quotation marks into the box, I just added those to try to make it clear what I typed), and hit enter. The LED turned on and the device responded with "led on".
When I try to recreate this using a Python app I wrote the ESP32 is not behaving as intended. When I run the python class which is shown below the LED on the ESP32 will turn on and stay on for 1 second (the time.sleep(1) is only there so I could test if the LED would stay on) and then after the time.sleep() has passed the LED turns off because of an unknown reason.
import serial
import time
# create a serial object
ser = serial.Serial(port = 'COM4',
baudrate = 115200,
bytesize = serial.EIGHTBITS,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
timeout = 5)
time.sleep(2) # wait for the connection to establish
cmd = ("2on")
ser.write(cmd.encode()) # send "2on"
cmd = '\n'
ser.write(cmd.encode()) # send the newline character
response = ser.readline().decode().strip()
print("received response: " + response)
ser.close() # close the serial connection
I have also tried using the following command to send the data to the ESP32:
ser.write(b'2on\n') # send a command to the ESP32 and wait for the response
Another potentially helpful piece of information is that I have tried using the onboard LED (Pin 2 on the ESP32) as well as other external LEDs with the MicroPython code/ESP32 and the behavior is the same either way.
What I would like to know is why is the LED on the ESP32 turning off when I use the Python app, as well as how can I resolve that problem.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a follow up question to this post: https://github.com/orgs/micropython/discussions/11096#discussion-4989105
I have this MicroPython class running on my esp32. I did try to simplify it a bit and not include code not relevant to my question.
When I test this code using the Arduino IDE's built in serial monitor the ESP32 behaves as intended. I am sending the message "2on" with the new line character selected from the drop down menu, and when I do that the onboard LED turns on and the ESP32 responds with "led on". I have attached an image below, there is not much to see, but basically I hit the restart button on the ESP32, so the ESP32 dumped a bunch of stuff to the monitor when restarting, then I typed "2on" into the message box (I did not actually type the quotation marks into the box, I just added those to try to make it clear what I typed), and hit enter. The LED turned on and the device responded with "led on".
When I try to recreate this using a Python app I wrote the ESP32 is not behaving as intended. When I run the python class which is shown below the LED on the ESP32 will turn on and stay on for 1 second (the time.sleep(1) is only there so I could test if the LED would stay on) and then after the time.sleep() has passed the LED turns off because of an unknown reason.
I have also tried using the following command to send the data to the ESP32:
Another potentially helpful piece of information is that I have tried using the onboard LED (Pin 2 on the ESP32) as well as other external LEDs with the MicroPython code/ESP32 and the behavior is the same either way.
What I would like to know is why is the LED on the ESP32 turning off when I use the Python app, as well as how can I resolve that problem.
Beta Was this translation helpful? Give feedback.
All reactions