lillesvin.net

Getting Started with NodeMCU ESP32

I just got my shipment from Shenzhen, China: 2 NodeMCUs built around the ESP32. Until now I’ve only played a bit around with a couple of Arduinos (Uno Rev3 and Nano), the micro:bit and a ESP8266, and I was looking forward to seeing how doing Lua on an MCU would work. I mean, the Arduino IDE and C is pretty neat and powerful but it’s just not my cup of tea—and neither is JavaScript and MicroPython (as the micro:bit uses).

It turns out, however, that the dev tools for the ESP32 aren’t entirely mature yet so it’s been a lot of trial and error plus reading docs so far. I had to set up a build environment for the firmware because all the “all-in-one” packages don’t support the ESP32 yet, so I spun up a virtual machine running Debian Stretch, and after failing my way through the make process I finally settled on a list of packages that needed to be installed for it to work: git build-essential libncurses5-dev flex bison gperf python-serial. (You can skip libncurses5-dev if you forgo make menuconfig and write the sdkconfig by hand but the interactive UI is really just a lot easier to deal with even though it’s not searchable.) And then I discovered that I for some reason couldn’t access my serial ports through VirtualBox so I resorted to booting my work laptop (Ubuntu 16.04) and finally I could both compile and flash my firmware. (Note: I could probably have transfered the binary from the virtual machine to the host OS and flashed it from there but my work laptop was right there anyway.)

So the complete process for building and flashing the firmware looks something like this:

sudo apt install git build-essential libncurses5-dev flex bison gperf python-serial
git clone --branch dev-esp32 --recurse-submodules https://github.com/nodemcu/nodemcu-firmware.git nodemcu-firmware-esp32
cd nodemcu-firmware-esp32
make menuconfig
make flash

(Remember to select the correct serial port under Serial flasher config ---> Default Serial Port.)

Finally I was ready to upload some test code, the Hello World of MCUs: blink, so I grabbed the ESP8266 Lua Loader (which appears to work fine—albeit with slighly limited functionality—for the ESP32) and uploaded the following init.lua:

LED_PIN = 4
LED_STATE = 0

gpio.config({ gpio=LED_PIN, dir=gpio.OUT })

tmr.create():alarm(1000, tmr.ALARM_AUTO, function()
    if LED_STATE < 1 then
        gpio.write(LED_PIN, 1)
        LED_STATE = 1
    else
        gpio.write(LED_PIN, 0)
        LED_STATE = 0
    end
end)

And—lo and behold—it worked! Ish… I’m reasonably sure the frequency of the blinking was much faster than 1000 ms. but it was blinking and I was happy. Now that the hardest part is over I just need to get a cool idea, code it and get all the dollars!