2 years ago

#28434

test-img

Ali Shaikh

Sending data between ESP32 and computer through USB with micropython

I have an ESP32 connected to to my computer through the usb port and have tried sending and receiving serial data. It works, but I can't make it so the ESP32 recognizes what is being sent using this code

from machine import Pin, PWM, UART #type: ignore
import time

uart = UART(115200)

while True:
    if uart.any():
        msg = uart.read()
        uart.write(msg)
        if msg == 'hello':
            uart.write("hello received")

The ESP32 should send "hello received", but it returns "hello" instead. Furthermore, this code only works when sending serial data from the arduino serial monitor. When I try running a separate python script to send "hello" and print what the ESP32 returns,

import serial, time

ser = serial.Serial('COM5', 115200) 

ser.write(b"hello")
time.sleep(1)
i = ser.readline()

print(i)

Nothing gets printed to the console. Is there a problem with my code? Thanks

Edit: Alright I fixed my code up to include the \n and it still didn’t work. I found out using this script that I’m not sending any serial data ESP32:

from machine import Pin, PWM, UART #type: ignore 
import time 
uart = UART(1,115200) 

while True: 
     print("starting") 
     msg = uart.readline() 
     print(msg) 
     time.sleep(0.01) 

Computer:

import serial, time 
ser = serial.Serial('COM5', 115200) 
while True: 
     ser.write(b'hello\r\n') 
     print(ser.readline()) 
     time.sleep(1) 

The esp returns b'starting\r\n' b'None\r\n

python

esp32

uart

micropython

0 Answers

Your Answer

Accepted video resources