TTGO ESP32 (OLED + 18650) running MicroPython

> Coding, hacking, computer graphics, game dev, and such...
User avatar
fips
Site Admin
Posts: 170
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague
Contact:

TTGO ESP32 (OLED + 18650) running MicroPython

Post by fips »

Although I've been more than happy with a bunch of ESP8266 development boards scattered around my house all running Lua-based NodeMCU, I couldn't resist to try out the new even more powerful and feature-packed ESP32. I've decided to order this super convenient module on eBay marketed as 'TTGO ESP32 Development Board', which not only hosts the ESP32 itself but also has a 0.96 OLED display and 18650 battery compartment onboard, it also implements all the power management jazz around USB charging.

Image

NodeMCU seemed like a natural choice for my new ESP32, but I quickly learned that the support was not ready yet and decided to look elsewhere. MicroPython was on my list for a long time so I gave it a try, here's how I went about it:

First, the latest MicroPython firmware for the ESP32 board needs to be downloaded from here (esp32-20180416-v1.9.3-548-gd12483d9.bin at the time of writing)

Second, we will need to install the esptool.py tool, the easies way to get it is via pip, like so:

Code: Select all

$ pip install esptool
which outputs:

Code: Select all

Collecting esptool
  Downloading https://files.pythonhosted.org/packages/cd/68/c28961d88cf50ca6d5de5e4b354dc47f77b9e74d4cd4d5bee4feaa7963b3/esptool-2.3.1.tar.gz (72kB)
    100% |################################| 81kB 744kB/s
Requirement already satisfied: pyserial>=2.5 in c:\dev_tools\python27\lib\site-packages (from esptool)
Collecting pyaes (from esptool)
  Downloading https://files.pythonhosted.org/packages/44/66/2c17bae31c906613795711fc78045c285048168919ace2220daa372c7d72/pyaes-1.6.1.tar.gz
Collecting ecdsa (from esptool)
  Downloading https://files.pythonhosted.org/packages/63/f4/73669d51825516ce8c43b816c0a6b64cd6eb71d08b99820c00792cb42222/ecdsa-0.13-py2.py3-none-any.whl (86kB)
    100% |################################| 92kB 1.4MB/s
Installing collected packages: pyaes, ecdsa, esptool
  Running setup.py install for pyaes ... done
  Running setup.py install for esptool ... done
Successfully installed ecdsa-0.13 esptool-2.3.1 pyaes-1.6.1
Third, the esptool.py can now be used to flash the MicroPython firmware into the ESP32 board, like so:

Code: Select all

$ esptool.py --chip esp32 --port COM3 write_flash -z 0x1000 esp32-20180414-v1.9.3-548-gd12483d9.bin
which outputs:

Code: Select all

esptool.py v2.3.1
Connecting........___
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 973584 bytes to 604211...
Wrote 973584 bytes (604211 compressed) at 0x00001000 in 54.0 seconds (effective 144.2 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
And that's basically it, now we can connect to the board using a serial terminal (like PuTTY). You'll most likely be welcomed by the Python prompt (REPL). A list of all available modules can be obtained easily by typing:

Code: Select all

>>> help('modules')
which gives:

Code: Select all

__main__          flashbdev         random            uos
_boot             framebuf          re                upip
_onewire          gc                select            upip_utarfile
_thread           hashlib           socket            upysh
apa106            heapq             ssl               urandom
array             inisetup          struct            ure
binascii          io                sys               urequests
btree             json              time              uselect
builtins          machine           ubinascii         usocket
cmath             math              ucollections      ussl
collections       micropython       uctypes           ustruct
dht               neopixel          uerrno            utime
ds18x20           network           uhashlib          utimeq
errno             ntptime           uheapq            uzlib
esp               onewire           uio               websocket
esp32             os                ujson             zlib
Plus any modules on the filesystem
We can even go ahead and initialize the I2C bus and scan for any attached devices:

Code: Select all

>>> import machine
>>> i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5))
>>> i2c.scan()
[60]
As you can see above the onboard OLED display was found, its address is 60 (0x3c). So far so good!

There's another useful tool worth installing which will help us to upload files to the board. The tool in question is Adafruit MicroPython Tool (ampy), you can get it via pip as well, like so:

Code: Select all

$ pip install adafruit-ampy
which outputs:

Code: Select all

Collecting adafruit-ampy
  Using cached adafruit-ampy-1.0.3.tar.gz
Requirement already up-to-date: click in c:\dev_tools\python27\lib\site-packages (from adafruit-ampy)
Collecting pyserial (from adafruit-ampy)
  Using cached pyserial-3.4-py2.py3-none-any.whl
Installing collected packages: pyserial, adafruit-ampy
  Found existing installation: pyserial 3.2.1
    Uninstalling pyserial-3.2.1:
      Successfully uninstalled pyserial-3.2.1
  Running setup.py install for adafruit-ampy ... done
Successfully installed adafruit-ampy-1.0.3 pyserial-3.4
Since, the latest (nightly) MicroPython firmware we've just installed does not contain the module for SSD1306 which is needed to drive the OLED display, we will have to download ssd1306.py manually from here, and use ampy to upload it to the board, like so:

Code: Select all

$ ampy --port COM3 put ssd1306.py
After that we can verify that the file has been successfully uploaded by typing:

Code: Select all

>>> import os
>>> os.listdir()
['boot.py', 'ssd1306.py']
Finally, we've got all the necessary pieces together, now it's quite easy to print out 'Hello World!' on the display, this can be achieved like this:

Code: Select all

>>> import machine, ssd1306
>>> i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5))
>>> oled = ssd1306.SSD1306_I2C(128, 64, i2c, 60)
>>> oled.fill(0)
>>> oled.text("Hello World!", 0, 0)
>>> oled.show()
I was quite impressed by the simplicity of the whole setup. MicroPython is a step up from Lua-based NodeMCU in my opinion, this opens up a lot of great possibilities and makes the ESP ecosystem even more pleasant to work with!