Micropython on the WeMos D1 Mini 0 comments

I've been playing with MicroPython lately, and more specifically on a WeMos D1 Mini . It's a really nice, cheap ($4 at the time I write this) ESP8266 based board, that can run MicroPython.

I have an original pyboard from the MicroPython Kickstarter, but never got around to doing much with it. Lately I've been fiddling around trying to make a Wifi to Infrared bridge, so I can control my TV from my computers/phones. Unfortunately the pyboard does not come with Wifi, and addons seem to be at least $25 and often far more. A D1 mini on the other hand is $4 and competely replaces the pyboard for an application like this.

Compared to the pyboard the D1 mini is slower (default clock speed is about half a pyboard, but it can be overclocked to come close), and if Wifi is running, you only have about 36Kb of memory available for your own application. It supports 2.4 GHz Wi-Fi (802.11 b/g/n, supporting WPA/WPA2) and has a built in antenna. It also has 4 Mbytes of Flash storage built in, compared to the pyboards 1 Mbyte.
If you are shopping for a D1 mini you will also come accross the D1 mini Pro. It comes with 16 MBytes of flash, is smaller and lighter than a D1 mini and has an external antenna connector as well as a built in antenna. The only catch  right now is that Micropython only detects 1MB  of storage. If you can live with that until Micropython supports it fully, its probably a better buy and only costs $1 more. (Check this issue  to see if the full 16 MBytes has support yet).

If you want something with slightly better support and documentation, take a lok at the Adafruit Feather HUZZAH. At $15 its much more expensive than a D1 mini. But still a lot cheaper than a pyboard and an adaptor. Adafruit has extensive documentation and tutorials on their site. These are worth a read even if you do go with a D1 mini.

The D1 mini usually comes with Arduino or NodeMCU preinstalled. So to use it you have to either find prebuilt firmware, or build it yourself. I found several tutorials, but all either assumed a slightly different operating system, or skipped steps that caused me a lot of frustration. What follows are build and installation instructions assuming you are running Ubuntu 16.04 (Xenial).

Before we start

In order to connect to your D1 mini, you will need your user to be a member of the dialout group. Run the following command:

sudo addgroup $USER dialout

and then log out and log in again

Getting prebuilt firmware

If you are happy to use prebuilt firmware, you can find it here. You are looking for the ESP8266 section. Then skip to the Installation Instructions below.

Building the firmware on Ubuntu 16.04

First we need to make sure we have all the packages and libraries we will need installed:

sudo apt-get install gperf bison flex help2man libncurses5-dev make autoconf texinfo libtool libtool-bin g++ python unzip python-serial git screen make

Now clone the ESP SDK (All the instructions from here on in assume you start in your home directory, if you want to do it elsewhere, modify the commands to match)

cd ~
git clone --recursive https://github.com/pfalcon/esp-open-sdk.git

Now lets build the SDK, this took about 20 minutes on my laptop

cd esp-open-sdk/
make STANDALONE=y

Now the SDK is ready to use, lets put in on our path.

export PATH=$HOME/esp-open-sdk/xtensa-lx106-elf/bin:$PATH

You will need to run that command any time you open a new terminal and want to use the SDK.

Now we are ready to build Micropython, lets check it out from github.

cd ~
git clone https://github.com/micropython/micropython.git
cd micropython
git submodule update --init


And finally, build our firmware

make -C mpy-cross
cd esp8266/
make axtls
make

And now we should our firmware in a file called firmware-combined.bin in the build directory.

Installing Micropython on the D1 Mini

Find the port your board shows up on, if you dont know, the easiest way is to run

ls /dev/tty*

then plug in your MicroPython board and run

ls /dev/tty*

again. Compare the two lists and find the entry that appears after the board is plugged in.

Mine shows up as /dev/ttyUSB0

Before we install MicroPython, its best to erase the current contents of the flash drive.

esptool.py --port /dev/ttyUSB0 erase_flash

If this refuses to write, check your user is a member of the dialout group and if not add them as shown above.

Now we write our firmware file to the board.

cd $HOME/micropython/esp8266

esptool.py -p /dev/ttyUSB0 write_flash 0x0000000 ./build/firmware-combined.bin

Press the little reset button on the side of the board. The board will reboot, the blue LED on the side will blink briefly and MicroPython should be running.

Connecting to our micropython board.

We can use screen to connect to the board

screen /dev/ttyUSB0 115200

You should now see a python REPL!

Lets try something simple

>>> 1 + 1
2
>>>

Hurray, we have Python running on this tiny computer!

The D1 Mini has a blue LED built in, lets blink it!

>>> from machine import Pin
>>> p2 = Pin(2, Pin.OUT)
>>> p2.high()
>>> p2.low()
>>> p2.high()
>>> p2.low()

As you switch from high to low, you should see the LED turn on and off. But the real point of this board is the Wifi support, lets connect to a Wifi network.

>>> import network
>>> wifi = network.WLAN(network.STA_IF)
>>> wifi.active(True)
>>> wifi.connect('your-ssid', 'your-password')

Nothing too exciting here, but lets make a HTTP request

>>> import usocket as socket
>>> s = socket.socket()
>>> address = socket.getaddrinfo("google.com", 80)
>>> print("Address:", address)
>>> connect_address = address[0][-1]
>>> print("Connect address:", addr)
>>> s.connect(connect_address)
>>> s.send(b"GET / HTTP/1.0\n\n")
>>> while True:
>>>     data = s.recv(4096)
>>>     if data:
>>>         print(str(data, 'utf8'), end='')
>>>     else:
>>>         break

You should see the HTML for the google homepage come back. Congratulations! Your tiny computer is connected to the internet.

I'll try and follow this up with more details on the Wifi -> Infrared work I'm doing.

Comments

There are currently no comments

New Comment

required
required (not published)
optional