Thursday, May 16, 2019

Configuring the M5Stack for Full Geek Mode

I stumbled on the M5Stack microcontroller platform a few weeks ago, and it was love at first sight. This bad boy is a low cost, tiny computer with WiFi, Bluetooth, a microphone, speaker, LED bar and display built in. The case is compatible with Legos, has a nifty magnetic backing and generally has excellent build quality. This platform makes my micro:bit look like a Model-T.

Fortunately, I discovered the M5Stack around my birthday, so all it took was a not so subtle hint to Mom for one to show up at my door. She was kind enough to splurge and get me the M5Stack Fire; the top of the line model!

The M5Stack continues the tradition of the micro:bit by being accessible to program. I followed the instructions printed on the tiny (but useful) pamphlet that came with the device. In a couple of minutes I found myself at flow.m5stack.com where I could drag and drop puzzle pieces to write code for my device. Very cool.

While I marvel at ability to program my device using little more than a web browser, I know this isn't ideal. I want emacs friendly files, an interactive repl and command-line friendly deployment. A bit of research suggested the device was compatible with a vanilla version of MicroPython, a package which promises expert level features (repl! telnet! curl!) without the newbie UI. Thankfully, those before me had tuned MicroPython so it was easy to get setup on the M5Stack. These two packages do the heavy lifting:

The first resource provides instructions and a BUILD command to create and flash a MicroPython firmware to the device. The second package simplifies programming a MicroPython based M5Stack. It provides helper libraries and a series of make targets to handle tasks like pushing files to the device and starting a repl.

All told, I ran into one issue while setting my M5 up.

After building MicroPython and flashing it to my device, I found the M5Stack unresponsive. I could hear a bit of static when I hit the reboot button, after that the device was dead. I tried re-building and re-flashing the firmware multiple times, but to no avail.

On a whim, I typed make repl and shockingly, found myself connected to the M5. While the device had no signs of life, it was in fact running. Upon closer inspection, I realized I'd gone through the ./BUILD menuconfig process and neglected to select the Display module:

After rebuilding the firmware, this time with display support, and using make sync to push the default M5 code to the device, I found that it booted up just fine.

Editing the default code that comes with the M5Stack 'Kitchen Sink' allowed me to code a trivial app in a few minutes. I also confirmed that my device can connect up to my network.

#pylint: disable=import-error
from machine import I2C, Pin, SPI, Timer
from input import DigitalInput
from ip5306 import IP5306
from micropython import const
import display
import m5stack
#pylint: enable=import-error

INFO_Y = const(181)

tft = m5stack.Display()

tft.text(tft.CENTER, 45,        "Hello World\n")
tft.text(tft.CENTER, tft.LASTY, "-Ben Simon\n")

i2c = I2C(scl=Pin(22), sda=Pin(21))

# Scan for I2C devices:
# 0x56 = Wheel
# 0x68 = MPU6500
# 0x75 = IP5306
devices = map(hex, i2c.scan())
print("I2C devices found:", end=" ")
print(", ".join(devices))

battery = IP5306(i2c)
tft.text(tft.RIGHT, 2, str(battery.level) + "%")

def battery_level(timer):
    tft.text(tft.RIGHT, 2, str(battery.level) + "%")

timer_0 = Timer(0)
timer_0.init(period=5000, mode=Timer.PERIODIC, callback=battery_level)

def do_connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        tft.text(tft.CENTER, INFO_Y, 'connecting to network...')
        sta_if.active(True)
        sta_if.connect('XXXXX', 'YYYYY')
        while not sta_if.isconnected():
            pass
    tft.text(tft.CENTER, INFO_Y, 'network config: ' + ','.join(sta_if.ifconfig()))


do_connect();

Hurray! It lives and it's super geeky to develop with! With this groundwork laid, it's time to turn my attention to my first real project. Stay tuned...

No comments:

Post a Comment