Archive

Posts Tagged ‘esp32’

Getting Started with MicroPython on ESP32 – Hello World, GPIO, and WiFi

October 16th, 2017 12 comments

I’ve been playing with several ESP32 boards over the months, and tried several firmware images. I started with a tutorial for Arduino Core on ESP32, a few month later I tested ESP32 JavaScript programming with Espruino on ESPino32 board, and recently Espressif Systems sent me ESP32 PICO core development board powered by their ESP32-PICO-D4 SiP, and while I took some pretty photos, I had not used it so far.

So I decided to go with yet another firmware, and this time, I played with MicroPython on ESP32, and will report my experience with basic commands, controlling GPIOs, and WiFi in this getting started post.

Flashing Micropython Firmware to ESP32 Board

Source code is available on Github, as a fork of MicroPython repo as ESP32 support has not been upstreamed yet. We could built the firmware from source, but there’s also a pre-built binary which you can download on MicroPython website.

I’ll be using Ubuntu 16.04 for the instructions, which should be pretty similar for other Linux distributions, especially the ones based on Debian, and if you’re using Windows 10, you should be able to follow the same instructions after installing Windows Subsystem for Linux with Ubuntu on your computer.

Let’s open a terminal, to download the firmware (October 14):

If you have not done so already, install the latest version of esptool:

Now connect the board via a micro USB to USB cable to your computer. The log should like like:

In my case, the device is ttyUSB0, but it may vary depending on the board used. We can now erase the flash, and copy the firmware to the board:

If the last step is successfull,  the output should be similar to the one below:

As a side note, version 2.1 of esptool does not know about ESP32-PICO-D4, but it can still detect an ESP32 device, and the update went through normally.

Hello World Sample / Boot Log with MicroPython

We can test the firmware, by connecting to the board using minicom, screen, putty, or whatever software you feel most comfortable with. I went with minicom, setup a connection to /dev/ttyUSB0 device with 115200 bps baudrate. I immediately tested the print function, and made an hard reset to check out the boot log:

The reset command will first generate some errors message, before rebooting the board:

We can type help function to get some more help:

I also often refered to MicroPython 1.9.2 documentation to write this quick start guide.

LED Blink Sample with MicroPython

The easiest way to test GPIOs is to connect an LED, since the board does not have user LED, only the power lED. I connected a 5V LED to pin 21 via a transistor to make the 3.3V to 5V conversion.

Controlling the LED in the command line interface is easy. Import the machine library, set the pin to output, and change the pin level as needed:

Success! But what about doing a proper blink sample? MicroPython developers’ official PyBoard would show as a USB mass storage drive in you computer, where can copy Python files like boot.py and main.py files, but in the case of ESP32 PICO core, it appears the only option is to use the serial console for programming, as we can’t simply copy files to the board from the host computer.

I  found a solution on Techtutorialsx – which also has plenty of articles about MicroPython on ESP32/ESP8266. We need ampy script that can be install from our Linux terminal:

However, the first time I tried it I got an error:

I installed files module, but the error remained. So instead I installed it for Python 3:

I then created blink.py on my computer to blink the LED every 500 ms:

Before uploading the file to the board, you can try to run it as follow:

If you have plenty of errors here, that’s probably because your code is incorrect. Since I’m not very familiar with Python, it happened to me a couple of times, until I got the code right, and the LED was blinking as expected.

Now that we’ve made sure the code works, we can now copy our sample to the board…

… reconnect to the serial console, and verify the file is there:

To run the program type the following:

The LED should blink again. You can interrupt the program with Ctrl+C, and if you want to soft reset the board, press Ctrl+D.

In order to automatically start the blink program at each boot, rename blink.py to main.py, delete blink.py, and copy main.py instead:

Power cycle the board, and the LED should start blinking almost immediately.

ESP32 WiFi with MicroPython (Station and AP modes)

We’ve got GPIOs working, but one of the most important feature of ESP32 is obvisouly WiFi. I’ll start by configuring the board in station mode. First import the network library, set the board to station mode, and scan access points:

The latter should return a list of access points with ssid, bssid, channel, RSSI, authmode, and hidden status as explained here.

I can then connect the board to one of the access points with:

The log above with IP address should give  a clue, but you can check connection status with the following function:

and use ifconfig to get the IP info:

Switching to AP mode is easy with the three commands below configuring the board with ESP32-PICO-CNX SSID:

At this stage I can see ESP32-PICO-CNX on my phone, but it’s an open connection. We can change that with authmode option that can take 5 values:

  • 0 – open
  • 1 – WEP
  • 2 – WPA-PSK
  • 3 – WPA2-PSK
  • 4 – WPA/WPA2-PSK

I’ll use WPA2-PSK and define the password with the config function.

Working as planned…

ESP32 Web Server with Micropython

Many ESP32 project will require a web interface for monitoring or configuration. Let’s first setup the board as an access point using the command we’ve used above:

Now create webserver.py file based on Python code found here that’s supposed to return the status of some GPIO pins in an HTML table:

Copy the file to the board:

Start the serial console again, import/run the python sample we’ve copied, and connect to the board (in my case http://192.168.4.1):

 

It works as expected, but we wrote the HTML code inside the Python file, and you need to handle socket programming by yourself. To further simply the task, some MicroPython web servers such as MicroWebSrv, and Picoweb are available.

MicroWebSrv (Not working yet for me)

I tried to install MicroWebSrv first, but never managed to make it work. I still reproduce the step I followed in case somebody finds out what I did wrong. I got the code, and copied files from the Linux terminal:

We can check the files are where they are supposed to be:

Go into the terminal (aka REPL console) to start a basic example, after setting up a connection:

I could connect to the server, but I would always get 404 error.

PicoWeb

So instead I switched to picoweb, adapting the instructions here and there. It’s very easy to install.  First make sure you have a working Internet connection in your board (i.e. set station mode), and install the web server with upip:

That’s the output if everything goes according to plans:

Now let’s go back to the host computer to create an html document, for example index.html:

as well as picowebtest.py sample file that will request the HTML page from the board, and return it to the client.

You’ll need to change “192.168.0.108” by the IP address of your board.

Let’s copy both files to the board…

… go back to the serial console, connect in station mode, and run the sample:

Type or copy/paste the URL in the last line into a web browser, and you should get the output below.

ESP32 Bluetooth with MicroPython

There’s no Bluetooth support in the official MicroPython documentation, because it’s work in progress, and for the most adventurous MrSulry released an alpha version  a few days ago. The Bluetooth API is also in flux, but the basic code to enable Bluetooth should look like:

I’ll update that section once Bluetooth makes it to the stable release, and/or when I’m sure the API is frozen.

Other ESP32 (Micro)Python Resources

I’ve just covered a few things that can be done with MicroPyhon on ESP32, and beside the official documentation, you can also check the various MicroPython ESP32 tutoral on techtutorialsx blog. Loboris also made another MicroPython ESP32 firmware that supports pSRAM as MicroPython may use a lot of RAM. If you’re interested in Python for ESP32, but Zerynth is another option for Python on ESP32 that works with an IDE/GUI available for Windows, Linux and MAC OS X. [Update: Yet other options are Pumbaa a port of MicroPython running on top of Simba, and Pycom version of MicroPython]

This TTGO Board Combines ESP32, LoRa Radio, and OLED Display for just $10

October 13th, 2017 12 comments

Just one year ago, it would cost around $15 to $20+ to get an ESP32 board, that is if you were lucky/fast enough to order one one before it went out of stock. Since then, availability is no longer an issue, and you now can get an ESP32 development board for as low as about $7, or even around $4 during promotions.

Today, I was made aware of another board sold under the “TTGO” brand, that includes not only ESP32 WiFi and Bluetooth SoC, but also a (433 MHz) LoRa radio, and an OLED display. Price? Just $10 plus shipping ($1.75 here).

Battery not Included – Click to Enlarge

TTGO ESP32/LoRa board specifications:

  • WiSoC – Espressif ESP32
  • Storage – 32MB on-board flash (or maybe just 16MB?)
  • LoRa
    • Semtech SX1278 with u.FL connector + 433MHz antenna (N.B.: Antenna must be connected during use or the Semtech chip could be damaged)
    • Sensitivity” ~ -148dBm; output power: +20dBm
  • Display – 0.96″ blue OLED display
  • USB – 1x micro USB port for debugging (CP2102) and power
  • Expansion – 2x 18-pin headers with GPIOs, UART, ADC, Touch, SPI, power signals… (See pinout diagram)
  • Misc – Charging Status LED
  • Power Supply – 5V via micro USB port, 2-pin battery header, 5V Pin. (Operating voltage: 3.3V to 7V)

The board can be programmed with the Arduino IDE after downloading and installing the TTGO folder in arduino/hardware. After selecting “WiFi_LoRa_32” board, you should be able to load various samples to play with the board.

Click to Enlarge

The board is sold with a 433MHz antenna, and two male headers. You’ll save a little bit on shipping if you purchase two kits instead.  The board can also be found on eBay and Banggood.

Thanks to Mpampis for the tip.

HeartyPatch is an Open Source Wireless ECG Patch Powered by ESP32 WiSoC (Crowdfunding)

October 2nd, 2017 No comments

Smart health gadgets will soon have a bigger part to play in our lives, especially for health monitoring. It mainly started with fitness trackers, but now we are starting to see connected devices such as blood pressure monitors, including the upcoming watch like Omron HeartVue, thermometer, scales, vital sign monitoring systems, certified medical SBC‘s to allow engineers to developer their own medical applications, and even open source surgical robots.

HeartPatch is one of those medical board that specifically aims at measuring ECG data, and sent it over Bluetooth or WiFi thanks to Espressif ESP32 WiSoC.

HeartPatch specifications:

  • SoC – Espressif Systems ESP32 dual core Tensilica LX6 processor with Wi-Fi/Bluetooth
  • ECG Chip – Maxim MAX30003 analog front-end
  • USB – 1x micro USB connector for programming, data, power, and battery charging
  • Debugging – USB-UART bridge based on CP2104
  • Misc – Onboard Snap-on Buttons for disposable electrode pads, RGB LED,
  • Battery – 450 mAH LiPo battery
  • Dimensions – 65 mm x 42 mm x 4 mm without battery; Dimensions with Case: ~70 mm x 46 mm x 12.7 mm

Basic Kit with Battery and Electrodes

The developers explain that HeartyPatch has several advantages over other low-price heart monitors:

  • ECG-based R-R Interval Measurement is more accurate than optical heart-rate measurement
  • Wide Dynamic Range for robust functioning during movement (not available in traditional ECG monitors)
  • Mathematical and Machine Learning Algorithms for automatic detection of arrhythmia, stress, and several other physiological conditions (not available with regular heart-rate patches)
  • Small, Wearable Form-factor with snap connectors for disposable, pre-gelled ECG electrodes.
  • Open Source and Non-proprietary – can be used with any software or algorithm

HealthyPatch is fully open source hardware with all files available on Github. The current GUI can support three modes:  beat-to-beat, Arrhythmia detection, and Heart-rate variability. If you have the required skills, you’d be able to add other modes to the user interface, or even roll your own. Note that ESP32 currently supports all BLE profiles, but the baseband works only in Bluetooth Classic mode. It will not affect the function, but battery life will be shorter than normal. Espressif Systems claims this will be fixed in the next release (SDK or Silicon?). If you want to follow the project’s progress over time, you may want to visit the Hackaday.io page.

HeartyPatch has just been launched on CrowdSupply, where you can get the basic kit with the board, a 450 mAh Li-Ion battery (soldered to the board), and a set of 10 disposable electrode pads with a $87 or more pledge. You can also add a case for $15, and shipping is free to the US, $15 to the rest of the world. Delivery is scheduled for December 14, 2017.

Google Cloud IoT Core Enters Public Beta, Various Devkits Available

September 29th, 2017 No comments

Back in May, I wrote about Allwinner R18 based Banana Pi BPI-M64 Board with Google Cloud IoT Core support, as Google unveils the new cloud service during Google I/O. However, at the time it was only available to selected partners, and Google has recently launched the public beta making their IoT device management platform available to all.

Click to Enlarge

I first learned about this through an ARM community blog post announcing availability of the ARM-based IoT Kit for Cloud IoT Core on Adafruit using Raspberry Pi 3 board,  a breadboard, and various modules that can be managed through Google services.

But that are plenty of other IoT kits or boards for Google Cloud IoT Core including:

You’ll find purchase links and documentation for each board on Google Cloud IoT Core’s IoT Kit page. Sample code specific to the RPI3 kit can also be found on Github.

Google Cloud IoT Core Architecture / Features Overview

Google IoT Core is free to use for up to 250 MB/month with no limit on the number of devices, and if you exceed this limit pricing per MB depends on data usage:

  • 250MB to 250 GB – $0.0045 per MB
  • 250GB to 5 TB – $0.0020 per MB
  • Over 5 TB – $0.00045 per MB

TECHBASE Moduino X Series Industrial IoT Modules / Endpoints are Based on ESP32 WiSoC

September 27th, 2017 3 comments

We’ve previously covered TECHBASE ModBerry industrial IoT gateways leveraging Raspberry Pi 3, FriendlyELEC NanoPi M1 Plus, or AAEON’s UP Linux boards. The company has now launched Moduino X series modules powered by Espressif ESP32 WiFi + Bluetooth SoC to be used as end points together with their ModBerry gateways.

Moduino X1

Two models have been developed so far, namely Moduino X1 and X2, with the following specifications:

  • Wireless Module – ESP32-WROVER with ESP32 dual-core Tensilica LX6 processor @ 240 MHz, 4MB pSRAM (512KB as option), 4MB SPI flash;
  • External Storage – X2 only: micro SD card slot
  • Connectivity
    • 802.11 b/g/n WiFi up to 16 Mbps + Bluetooth 4.2 LE with u.FL antenna connector
    • X2 only: 10/100M Ethernet
    • Options: LoRa (Semtech SX1272); Sigfox (TI CC1125); LTE Cat M1/NB1; Zigbee
  • Serial – 2x RS-232/485
  • Display – Optional 0.96″ OLED display with 128×64 resolution
  • Expansion I/Os
    • 4x Digital I/O (0 ~ 3V)
    • 2x Analog Input:
    • A2 Only: 2x analog output (optional)
    • A2 only: support for Techbase ExCard add-on modules for extr RS-232/485 ports, Ethernet ports, PCIe slots, analog input and output, digital I/Os, relays, M-Bus interface, etc…
  • Battery – Optional battery power support (A1 only); optional UPS function with LiPo battery or Supercapacitor
  • Power Supply -5V DC
  • Dimensions
    • A1 – ABS: 90 x 36 x 32 mm (LxWxH); Aluminum: 95 x 35 x 41 mm (LxWxH)
    • A2 – ABS: 90 x 71 x 32 mm (LxWxH); Aluminum: 95 x 71 x 41 mm (LxWxH)

Moduino A1 consumes less than A2, and can be powered by batteries only, but both models can use battery as UPS. The modules support Espressif ESP-IDF SDK, Zephyr Project, Arduino programming, MicroPython, Mongoose OS, and more, and would typically be used as meters & sensor nodes capable of reporting temperature, humidity, pressure, acceleration, & light with attached sensors. More sensors are being developed by the company.

Moduino X2 (right)

Moduino X1 & X2 appear to be available now, but you’d need to contact the company to get price information. Visit Moduino X series product page for more details.

Need to Program Many ESP-WROOM-32 / ESP-32S Modules? This Board Should Help

September 18th, 2017 2 comments

We’ve just published an article about a 3D printed jig to program some ESP8266 light bulbs, but as I watched Andreas Spiess’s latest video about ESP32 boards, he showed a board specifically designed to flash firmware to ESP-WROOM-32 or/and ESP-32S modules, which could be useful if you have many to program.

Click to Enlarge

The acrylic base does not appear to be offered by all vendors, as some use some standoffs instead to lift the board up. You just need to insert your compatible ESP32 module in the board, flash the firmware it, take it out, and more to the next module. It can also be used as a development board since it exposes I/Os via three 14-pin headers, comes with a on/off button, reset and program buttons, as well as a micro USB port for power, programming and debugging

I first found it on Banggood, where it is sold for $14.99 shipped, the best price at the time of writing, but you can also purchase it on Amazon, eBay, Aliexpress, and I’m sure other websites. Just search for “ESP32 Test Board Small Batch Burn Fixture”.

Wemos LOLIN32 Lite Board Powered by ESP32 Rev 1 Chip Sells for $4.90

September 14th, 2017 3 comments

Wemos introduced the first low cost ESP32 board with LOLIN32 board going for $6.90 plus shipping in April, but the company is now back with a new Lite version of the board switching ESP-WROOM-32 module with their own design around ESP32 Rev 1 chip (with various silicon bug fixes), and a lower $4.90 price tag to which you need to add ~$2 for shipping.

Click to Enlarge

Wemos LOLIN32 Lite is also smaller, so we’ll lose some of the pins (mostly extra power pins), but the I/Os look the same:

  • SoC – Espressif ESP32-DOWD6Q Rev 1.0 dual core Tensilica Xtensa LX6 processor with WiFi and BLE
  • Connectivity – 802.11 b/g/n WiFi + Bluetooth LE
  • I/Os via 2x 13-pin headers with digital I/Os, analog inputs, UART, I2C, SPI, VP/VN, DAC…
    • 3.3V I/O voltage
    • Breadboard compatible
  • USB – 1x micro USB port for power and programming/debugging
  • Misc – Reset button
  • Power – 5V via micro USB + battery header for Lithium battery (charging current: 500mA max)
  • Dimensions & Weight – TBD

The board sold on Aliexpress is pre-loaded with micropython firmware, but you could also change that to Arduino, or other supported firmware. The Wiki has limited information for now.

Getting Started with Espruino & JavaScript on ESP32 with ESPino32 Board

September 11th, 2017 No comments

Venus Supply Co., Ltd, better known as ThaiEasyElec, is a company based in Thailand, selling embedded systems and development board, as well as providing development services based in Thailand. The company sent me their latest board called ESPino32 powered by Espressif ESP-WROOM-32 WiFi and Bluetooth module for evaluation. While the board is supported in Arduino-esp32, I’ve already tested Arduino with ESP32-Bit module & ESP32-T board, so after checking out the hardware, I’ll load it with something different: Espruino, a firmware allowing for JavaScript programming over the serial console, or a Web based IDE.

ESPino32 Unboxing and Soldering

The board shipped with four female headers, and I/O stickers.

Click to Enlarge

The board includes ESP-WROOM-32, exposes I/Os through four 10-pin headers, features CP2104 chip for serial to USB debugging via micro USB port, two buttons (reset and program), a user LED connected to IO16, and a jumper to select between regulated power supply (micro USB or Vin), or battery power (Vbat).

Click to Enlarge

If you’re going to integrate your board in a project, you may want to use it asif without header to save on space, but for prototyping and use with a breadboard, we should start by soldering the four female headers. It’s even a little easier than with other headers, since you can simply place the board on top of the headers to do the soldering.

Click to Enlarge

Once we’re done, we can apply the stickers on all four headers, which will make it easier to play with while connecting the jumper cables.

Now we can insert the board into a breadboard, connect an external 5V LED through pin 16, and connect a micro USB cable to a computer to get power and access the board.

Click to Enlarge

A board with female headers has the advantage of providing two usable rows on each side of the board. With male-only you’d lose that extra row, unless you use a narrower board such as  ESP32 Pico Core board.

That’s the output I get when connecting the board to my Linux computer:

Quick Start Guide for Espruino on ESP32

Espruino has a page about ESP32 support that explains what is working:

  • onewire
  • hardware SPI
  • hardware I2C
  • DAC
  • ADC
  • Serial
  • WIFI – as a client and access point

and what is not (yet):

  • Over-The-Air (OTA) firmware updates.
  • Bluetooth and BLE

So we can’t play with Bluetooth, but WiFi and GPIO should work. There are also some instructions in that page which I will follow and adapt (since some are not working/out of date) below.

First we need to download the latest version of Espruino, in my case Espruino 1.94.

Espruino Firmware for various board – Click to Enlarge

The zip file includes firmware for all supported platforms include the company’s own Espruino boards & Puck.js, Micro::bit, OlimeXino, Raspberry Pi, STM32 discovery boards, and more..

For our use, we need to get into espruino_1v94_espruino, where we’ll find 3 binary files (bootloader.bin, espruino_esp32.bin, and partitions_espruino.bin), as well as README_flash.txt that explains how to do the update in Windows with flash_download_tools_v3.4.4.zip, or in Linux with esptool.py from the ESP-IDF SDK. I’m running Ubuntu 16.04, so I’ll go with the later, but since most people won’t need to install the ESP-IDF SDK, you can instead get esptool from pip for Python 2.7 or 3.4 or newer:

if you’ve used esptool previously for other esp32/esp8266 board(s) before, you can upgrade esptool with:

In my case, I had installed an older version of esptool (v0.4.6) with apt when I played with NodeMCU board, so I removed it:

Now that we have the latest esptool utility installed, we can flash the image we’ve  just extracted:

It worked the first time. Log of successful installation:

At this point, in theory, you can install Espruino Web IDE chrome extension,  click on the connect icon on the left top corner, select /dev/ttyUSB0 port, and program away.

Click to Enlarge

Espruino Web IDE will also show in Ubuntu 16.04 dash. But in practise, there’s a known issue that the first time you won’t be able to connect through the Web IDE, and indeed I could not.. The work around is to first connect using screen or minicom in a terminal window:

We can then run an hello world sample:

That “=undefined” is a little confusing, but Espruino developers explain that is expected :

This is normal and it indicates the result of the last operation, which in this case is the return value of console.log, which is always undefined.

The next step is to configure a WiFi connection to your access point:

Replace “YOUR_SSID” and “YOUR_SSID_PASSWORD” with the value for your WiFi router. If this is successful, you should  see a message like shortly after:

The line wifi.save() will make sure the WiFi connection is permanent, so the board will reconnect to the router automatically after each reboot.

We can now go back to Espruino Web IDE, click on the Setup icon on the top right corner, go to Communications tab, and input the IP address (192.168.0.112 in my case) in the field “Connect over TCP Address“.

Click to Enlarge

You don’t even need to connect the board to your computer at this stage, if you be powered by a battery, or a USB power adapter. If we click on the Connect icon again, we’ll have the option to select TCP/IP: 192.168.0.112.

Click to Enlarge

I wrote a simply LED blink demo in the right part of the window, and clicked on he Upload button (third button in the middle) to upload and start the program:

Click to Enlarge

D16 is connected both the board’s IO16 LED and the LED on the board. IO16 will be turned on when D16 is low, and my LED when D16 is high every half second. I’ve shot a quick demo below.

However, if you reboot or power cycle the board, your program will not automatically start. So if you want the program to be “permanent”, add save command at the end of your code:

The output from the console should look like when you click on Upload button.

I could turn off and on the board, the LED demo resumed automatically without having to upload the code from the IDE.

Note that at this stage, I started to have some strange issues, like failure to connect to the board, and sometimes it would should “module http not found” or “module wiki not found”, as I wrote code for a web server.

It could be you need include the code for a permanent WiFi connection, before running the save command. It was still working sometimes, but I decided to connect through micro USB cable via Espruino Web IDE (it worked at this stage) to carry on with my tests more reliably.

In case you want to use ESPino32 board as small webserver, you can do so by creating a access point, and returning a simple “hello world” with the following code:

I click on Upload button, and I could access web server from Firefox.

The main advantage of Espruino over the Arduino IDE is that it’s much faster to try your code on the target, since there’s no need to compile a binary, upload to the board, and flash to storage during development. Another advantage depends on your skill set, as if you’re a seasoned web developer with a good knowledge of JavaScript, you won’t need to learn C programming used in Arduino IDE.

For other interface (SPI, I2C, ADC…) and more advanced tasks, you may check Espruino ESP32 page, but be forewarned, as the blink and web server samples did not work for me (unknown variable and missing semi-colon) by default. So you may want to read through the API reference in case the samples do not work. The company behind Espruino, Pur3 Ltd, is likely focusing most of the development efforts of their own hardware platforms like Puck.js and Espruino boards, and there’s more activity for those in the forums. So if you are interested in JavaScript on micro-controllers, but don’t want too many issues, ESP32 may not be best platform to learn, but if you like challenges, go ahead! 🙂

I’d like to thank ThaiEasyElec for sending an ESPino32 board sample. The company sells the board locally and globally for respectively 590 THB / ~$16 plus shipping on their website, where you’ll also find some documentation in English and Thai language. If you are based in Thailand, you can get further discount and free shipping if you purchase through LINE app.