microPython and the ESP

Flush with success at writing trivial code on NEO platform to control lights and displays, in Python, I thought I’d have a go at microPython on the ESP8266 and the ESP32.

Getting this working on the ESP8266 was trivial as the binary files start at zero – so really you just have to use your favourite tool to flash the chip (this is not the place to teach anyone how to flash chips incidentally).

I grabbed the binary file for the ESP8266 from here and set my  usual programming software to start at location 0, rebooted the board, hooked up a serial interface at 115KBaud and pressed reset.

I was greeted with >>> which apparently is quite normal and tried the obvious…

print “Hello world”

Well, it didn’t work  and after a little searching it was obvious why. While this example would work in Python 2.7, microPython is working to the standard of Python 3.4 which insists for whatever reason on using parentheses.

print (“Hello world”)

That worked  and produced the desired output from the serial port – as did some simpl (including floating point) maths.  If you don’t know Python, well, it is interesting – without braces it uses indentation to define what is and is not inside a function etc… so basically if your code does not LOOK right – it won’t work! It is not THAT different to other popular languages however.

When in the development “environment” – i.e. the serial connection – the command line system will take care of that indenting for you – but if you want to actually insert indented code you can hit CTRL-A for pasting in code – and control-B to get out of it.

Up to now, all of this is applicable to both the ESP8266 and the ESP32.

So the first thing was to enable something called WEBREPL which lets you do your programming over a sockets connection. https://learn.adafruit.com/micropython-basics-esp8266-webrepl/access-webrepl.  If you are one for jumping the gun as I am and if you’re planning to test this on an ESP32 – skip this for now as it is “in development” for the 32.. but for the ESP8266 it works a treat and frees you from having to use the serial port.

Well, having said “hello world”, the next thing you might want to try is turning a light on and off (I should say that microPython handles I2c and SPI but I’m just keeping this simple for now).

In Python you import libraries then use stuff in those libraries. So to handle simple GPIO you need to:

import machine

Now that this library is available, you can set up a pin…

pin=machine.Pin(0)

So now you have an object called “pin” attached to GPIO0, you can do something with.

pin.value(1)

pin.high() seems to do the same.

If you plan on testing this – the numbering usually applies to Arduino as for some reason they’ve followed that standard – so you might want to pop up an image on Google comparing normal ESP numbers with Arduino numbers.

Having said that, on the ESP32, I set up pin 4 in code and put a LED on P4 – worked a treat. Just for the sake of it I tried the PWM - it is 10 bit which is good enough for most stuff.

pwm = machine.PWM(machine.Pin(4)
pwm.freq(60)
pwm.duty(12)

Sure enough a dim light.

pwm test

I also tried PWM on the ESP8266 – works a treat.

So right now the current version for the ESP8266 is 1.9 and for the ESP32, slightly behind at 1.87.

Here is a reference for doing more complicated stuff like setting up the WIFI access point and general timers and more including PWM (tested, worked) analog input (tested, worked and a lot more – if you do have a play, let me know if the other things like I2c work) - http://docs.micropython.org/en/v1.9/esp8266/esp8266/quickref.html

The one limit you may come across with the ESP is RAM – right now there is only 24K left to play with and that could stop you doing any big projects. That is apparently set to change in the future as there is an active development effort on that chip -  the ESP32 board has more RAM by some way. My ESP32 reported that it had 87K left – that’s a lot more healthy – but then the board is more expensive.

import gc
gc.mem_free()

There is a file system on the boards so if you upload a library (simple enough) it goes into FLASH.  Two files are important – boot.py and main.py – if uploaded they will survive power down and run on power up… so boot.py can be used to set up the WIFI (the ESP8266 remembers the last access point, the ESP32 does not) and main.py can be used for your general program.

Now my use of this is going to be limited at the moment because for most of my projects I like to use MQTT. The current MQTT (umqtt.simple) works a treat but is not that rock-solid when it comes to loss of signals etc..  there is an add on called umqtt.robust but if you look in the comments – the last one by craftyguy says it is not that… robust.  So this needs work as I’m not about to start off with a base of unreliable connectivity.  No doubt if the interest keeps up, in time this will change so if you’re not reading this on June 1st 2027 you might want to do a little background searching.

So in essence a nice simple way to control the ESP8266 and ESP32 with knowledge you may have already gained messing around with Raspberry Pi etc… but maybe it is a little early to jump in at the deep end? Anyone else had experience of using MQTT on microPython? Have you made it reliable in the face of lost connectivity or other issues? If so – comments below.

I hope the links provided prove useful to you – they helped me immensely.

Facebooktwittergoogle_pluspinterestlinkedin

7 thoughts on “microPython and the ESP

    1. There is an awful lot of breath expelled in there about whether to use high/low, on, off, 1, 0 - I'd have thought getting the communications reliably was a much higher priority issue. Could be I've popped in a little early on this one.

    1. Hah, yes, I could do a whole Monty Python version of home control.. Honestly, at this point... I'm playing - I think it is always good to look out of the box... but from the perspective of, say the ESP8266 as controllers, until someone can demonstrate completely reliable WIFI+MQTT, that simply is a non-starter for me. I've looked at the WIFI connection code and it would be simple to add multiple access points and other goodies - and the nice thing is that people could make changes without having to understand compiler environments.

      I am for example also looking at the Mongoose project, using Javascript as a working language - but while all this playing about is going on, good old compiled C code is what is actually driving much of my stuff right now. Right now my C code can run multiple things at once and do it well.

  1. I'm in the last stages of developing a reed switch door monitor using micropython on an ESP8266-01 to publish an MQTT topic to a Node-RED flow running on my raspberry pi. The end result will send me a text when the switch opens.

    I have not lifecycle tested this yet, so by the tone of your article I guess I may have to replace the upython code with arduino if I run into issues... Bummer.

    1. Well maybe a better way is to leave comment on the Mqtt developer's Github page pleading for robust code. For many developers, interest is enough to motivate improvement.

      1. Thank you for the feedback. I really appreciate all that you do and publish. It's inspiring to this 45 YO Mechanical Engineer and hardware hacker wannabe.
        I'm going to set things up as is (if i ever get the time to finish) and test it's reliability. I'll report back on the results. My system is fairly primitive and if the libraries have issues with this then the authors should know about it with proper data supporting it.

Leave a Reply

Your email address will not be published. Required fields are marked *