Hi All,
I'm new to micropython and the esp8266, but I've been a long time Python and Arduino user. On my first IoT data logger project I was finding micropython a joy to develop in and the software machine.I2C library quite easy to adapt for an AM2315 (
http://www.adafruit.com/products/1293) humidity and temperature sensor. However, I hit a wall when I needed to integrate a UART communicating carbon dioxide sensor and found that the hardware UARTs were tied up and there was no built-in software serial module.
I actually found deshipu 's original suggestion to be quite helpful. After learning the basics of writing C extension modules (and a lot of trial-and-error hacking) I was able to successfully adapt a `softuart` C code library (
http://github.com/plieningerweb/esp8266-software-uart) and wrap it in a micropython extension class `machine.SoftUART` that is based very closely on `machine.UART`.
I wanted to share this code to help others who may find this thread, so I put together a github micropython fork that makes the needed changes (
https://github.com/p-v-o-s/micropython/ ... 8a89287a8f).
This firmware can be built according to Adafruit's helpful Feather HUZZAH guide (
https://learn.adafruit.com/building-and ... d-firmware) if you substitute this repo instead of the one included on the vagrant VM.
In order initialize the SoftUART object, two Pin objects, tx & rx, are the only required arguments (similar to the I2C object's sda & scl pins). For example:
>>> import machine
>>> ser = machine.SoftUART(machine.Pin(12),machine.Pin(14), baudrate=9600, timeout=1000, timeout_char = 10)
>>> print(ser)
SoftUART(tx=12, rx=14, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, timeout_char=10)
>>> ser.
init flush read readline
readinto write
>>>
The baudrate and timeouts are configurable; however, the data bits, stop bits, and parity are fixed. The softuart.c library uses a statically allocated receive buffer with a default size of 64 bytes (
https://github.com/p-v-o-s/micropython/ ... tuart.h#L4). The new `flush` method resets the receive buffer to empty state. One major shortcoming of the `machine.SoftUART` implementation is that only one instance of a SoftUART class can function in overlapping code since it uses a global Softuart struct instance `softuartDevice` - I was unable to get multiple instances working properly using dynamic allocation.
Please let me know if you find this useful or need help to get it working.