Does ESP8266 has softuart module

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Does ESP8266 has softuart module

Post by shaoziyang » Mon Dec 12, 2016 4:49 am

Does ESP8266's micropython has softuart module? Becasue I need more uart.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Does ESP8266 has softuart module

Post by Roberthh » Mon Dec 12, 2016 6:08 am

No, it does not have a software UART. Implementing that in Python would possible, if 2400 Baud (or less) is sufficient. Anything faster has to be implemented in the firmware.

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: Does ESP8266 has softuart module

Post by mcauser » Mon Dec 12, 2016 12:25 pm

The ESP8266 has a 2nd hardware UART, but it is TX only.

shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Re: Does ESP8266 has softuart module

Post by shaoziyang » Mon Dec 12, 2016 1:15 pm

Roberthh wrote:No, it does not have a software UART. Implementing that in Python would possible, if 2400 Baud (or less) is sufficient. Anything faster has to be implemented in the firmware.
I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Does ESP8266 has softuart module

Post by Roberthh » Tue Dec 13, 2016 8:08 pm

I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.
That sounds very interesting. Are you willing to share this code with the community?

User avatar
JonHylands
Posts: 69
Joined: Sun Dec 29, 2013 1:33 am

Re: Does ESP8266 has softuart module

Post by JonHylands » Sat Dec 31, 2016 2:37 pm

shaoziyang wrote: I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.
I would be interested in this also. I need to connect to a GPS, running at 9600 baud.

shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Re: Does ESP8266 has softuart module

Post by shaoziyang » Fri Jan 06, 2017 2:49 pm

Roberthh wrote:
I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.
That sounds very interesting. Are you willing to share this code with the community?
It is work fine with 1200bps, I have not test it for other baudrate, it maybe need to adjust self.bitdelay value.

And in receive mode, it will loss data if receive continuous data.

Code: Select all

import time
import machine
from machine import Pin

class SOFTUART(object):
    def __init__(self, tx, rx, baud = 1200):
        self.tx = tx
        self.rx = rx
        self.tx.init(Pin.OUT)
        self.rx.init(Pin.IN)
        self.baud(baud)
        self.timeout = False

    def baud(self, baud):
        self.bitdelay = 1000000 // baud - 200

    def dt(self, dt=''):
        if dt=='':
            return self.bitdelay
        else:
            self.bitdelay = dt

    def putc(self, dat):
        self.tx(0)
        time.sleep_us(self.bitdelay)
        for i in range(8):
            self.tx(dat%2)
            dat = dat >> 1
            time.sleep_us(self.bitdelay)
        self.tx(1)
        time.sleep_us(self.bitdelay)

    def puts(self, str, cr=1):
        for i in range(len(str)):
            time.sleep_ms(1)
            self.putc(ord(str[i]))
        if cr > 0:
            time.sleep_ms(1)
            self.putc(0x0D)

    def put(self, buf):
        for i in range(len(buf)):
            time.sleep_ms(1)
            self.putc(buf[i])

    def timeout(self):
        return self.timeout

    def getc(self, timeout=20):
        t = 0
        self.timeout = False
        while self.rx():
            time.sleep_us(20)
            t = t + 1
            if t < timeout*5:
                pass
            else:
                self.timeout = True
                return -1
        time.sleep_us(self.bitdelay + self.bitdelay//8)
        dat = 0
        for i in range(8):
            dat = dat >> 1
            if self.rx():
                dat = dat | 0x80
            time.sleep_us(self.bitdelay)
        time.sleep_us(self.bitdelay)
        return dat

    def get(self, num=0):
        dat=bytearray(0)
        while True:
            t = self.getc()
            if self.timeout:
                return dat
            else:
                dat.append(t)
            if num > 0:
                if num > 1:
                    num = num - 1
                else:
                    return dat
softuart.zip
(695 Bytes) Downloaded 388 times

shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Re: Does ESP8266 has softuart module

Post by shaoziyang » Fri Jan 06, 2017 2:49 pm

JonHylands wrote:
shaoziyang wrote: I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.
I would be interested in this also. I need to connect to a GPS, running at 9600 baud.
please see above, I have only test 1200bps.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Does ESP8266 has softuart module

Post by pythoncoder » Sat Jan 07, 2017 7:55 am

See also http://forum.micropython.org/viewtopic.php?f=16&t=2204. I haven't tested it but it looks very promising.
Peter Hinch
Index to my micropython libraries.

Post Reply