pirks
pirks

Reputation: 53

esp8266 and cp2102 don't work with python serial

esp8266 and cp2102 don't work! Why?

import serial

sp="/dev/ttyUSB0"

port = serial.Serial(sp)

while True:
    port.write("AT+RST")
    rcv = port.read(10)
    print rcv

I pressed "AT+RST"[Enter] and don't have "READY" after it.

Upvotes: 0

Views: 1666

Answers (2)

Ben
Ben

Reputation: 63

Make sure you include CRLF (\r\n) characters at the end of your command. I lost a day messing with this before I figured that out. I got the local echo back of the command but since I never sent a \r\n I would not get any more data. Here is what works for me as a basic terminal in Python using pyserial:

import serial
import time

ser = serial.Serial('/dev/tty.usbserial-A8004xaO', 115200, timeout=2.5)
while True:
    cmd = raw_input("> ");
    ser.write(cmd + "\r\n")
    ret = ser.read(len(cmd)) # eat echo
    time.sleep( 0.2 )
    while ( ser.inWaiting() ):
        ret = ser.readline().rstrip()
        print ret

Upvotes: 1

user149341
user149341

Reputation:

You aren't setting a baud rate when opening the serial port. The default is probably not appropriate for the ESP8266.

Upvotes: 0

Related Questions