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 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:
Now that this library is available, you can set up a pin…
So now you have an object called “pin” attached to GPIO0, you can do something with.
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)
Sure enough a dim light.
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.
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.