Friday 20 April 2018

Getting the odd 74880 baudrate for ESP8266 in Ubuntu

ESP8266's official demos use a very odd baudrate 74880 for the UART (except for the AT demo). It is rarely supported by the operating systems. For Ubuntu, the serial port only supports those 'standard' baudrates, 38400, 57600, 115200 etc.

In Linux, it's very easy to see what's going on at the serial port, you don't even need a software to do that, use the terminal command 'cat':

$cat /dev/ttyUSB3

where ttyUSB3 is the device number in Ubuntu of the on-board USB-Serial converter of ESP Launcher.

But if you are not at 74880, you'll only see junks on the serial port when you run the IoT Demo.

 After a lot of searching on the internet, I finally find this python program, it can set the baud rate to any value you like, thanks for the excellent work of the creator of it.

#!/usr/bin/python
# set nonstandard baudrate. http://unix.stackexchange.com/a/327366/119298
import sys,array,fcntl

# from /usr/lib/python2.7/site-packages/serial/serialposix.py
# /usr/include/asm-generic/termbits.h for struct termios2
#  [2]c_cflag [9]c_ispeed [10]c_ospeed
def set_special_baudrate(fd, baudrate):
    TCGETS2 = 0x802C542A
    TCSETS2 = 0x402C542B
    BOTHER = 0o010000
    CBAUD = 0o010017
    buf = array.array('i', [0] * 64) # is 44 really
    fcntl.ioctl(fd, TCGETS2, buf)
    buf[2] &= ~CBAUD
    buf[2] |= BOTHER
    buf[9] = buf[10] = baudrate
    assert(fcntl.ioctl(fd, TCSETS2, buf)==0)
    fcntl.ioctl(fd, TCGETS2, buf)
    if buf[9]!=baudrate or buf[10]!=baudrate:
       print("failed. speed is %d %d" % (buf[9],buf[10]))
       sys.exit(1)

set_special_baudrate(0, int(sys.argv[1]))

You need to have Python installed on your computer to run it. The usage is pretty simple, if the program is named set_baud_rate.py, change to the folder that contains the file, and run:

$./set_baud_rate.py <>/dev/ttyUSB3 74880 

Now you can see the ESP8266's bootup messages.

3 comments:

  1. Why not just do:
    $(stty 74880< /dev/ttyUS3; sleep 300)&

    This puts the device in the speed you need for 5 minutes

    ReplyDelete
  2. Oops I meant:
    $(stty 74880; sleep 300)< /dev/ttyUSB3 &

    ReplyDelete
    Replies
    1. Thanks Narayan, I'm not a Linux expert, but from somewhere I read that stty can only work with 'standard' baud rate like 115200, 38400 etc. and my experience is the same. But I would like to try as you said.

      Delete