Micro Python

This is all about how to program a ESP8266 using the Micro Python programming language.

It will be broken into 4 Sections:

  1. Hello World - getting started and simple warm-ups
  2. Let's talk!
  3. Obligatory Blinky
  4. Can we do things with the Servo?

Some useful resources:

Helpful Notes and Random Whitterings

Taking a screenshot

If you're using Windows, simply press the PrtScr key and Windows will take a screen capture at that point and store it in the Clipboard. Open Paint, paste (either Edit -> Paste, or hold the Ctrl key and press the V key). Then save the file somewhere you will remember (you can always create a folder in your Pictures directory called 'MicroPython' and in there save the file).

Connecting to the ESP 8266

Lets look at what a ESP8266 is and what it can do.

A bit of background. The ESP8266 is a CPU. Sounds big and complicated, but it's really not. It takes a load of commands and runs them. Thats all a processor is.

Programming small device like this is called Embedded Application Development.

Application is also a big and complicated idea, but it also isn't.. honestly it's just a collection of commands.

So what we're doing here, is writing up a bunch of commands and throwing them at the ESP8266 to see what it does (hopefully what we planned it to do), and recording the result.

Now, there still isn't a global standard way to program these sorts of things (in fact there's no global way to program anything!) but everything all uses similar languages and similar ideas, so when someone say's 'I can program in Python language', it's not that it's a completely different language like English vs Japanese, it's more like UK vs American English. They are quite similar but there can be suprising differences in the small stuff.

So, that said, how do we program an ESP8266 in Python?

What tools do we need?

First off, how do we communicate with it? Well there's two main ways, either through a serial terminal or through a WebRepl webpage that issues serial commands to a WiFi connected ESP8266 on the same WiFi network. Now an ESP8266 does NOT have WebRepl enabled and usable when you first start using it, so first lets spend some time talking about:

Communicating over a Serial Terminal

Now you may or not be able to skip this part, if you know about connecting to a device through a serial terminal, then plug the ESP8266 into your computer, start up your serial terminal and connect to it and if you get >>> you've successfully connected.

If you just read the above and are completely lost, rather than explain in my words what someone else has made a much better version of, go here and read this: micropython.org - 2.1 repl over the serial port. Again when you've got >>> you are ready to move onto the next bit.

Enabling WebRepl

Now, this is a choice for you, do you only ever want to program the board using a Serial Terminal? If you do then frankly you're done! :) Go onto the main sections and start having fun!

If however you do want to use WebRepl to control the ESP8266 from a webpage (it also gives you a really simple file uploader to get your scripts onto the ESP8266) then follow these steps: micropythoin.org - webrepl prompt over wifi.

You can then use this link to connect to the ESP8266. It will start up it's own WiFi network, so you'll need to disconnect from your normal WiFi network and connect to the ESP one that will appear: http://micropython.org/webrepl/#192.168.4.1:8266/

It will show a web page like this:

Web Repl

Press the Connect button at the top and it should ask you for a password:

Web Repl - password prompt

Put in the password you set when you enabled WebRepl and you should then see the Python prompt (>>>):

Web Repl

Thats it!

You are now connected to the ESP8266 Python REPL engine (we'll talk about what a REPL is in a later section, it's not that it's complicated but it will make much more sense when we do some other things later).

When we get further in you'll need some way to write a python file, but you can just use notepad if you really want. I mean, don't but you can....

The reason I say that is that code is just that, it needs decoding by your human brain to be understood.

Code editors like notepad++, sublime text editor, Visual Studio Code (which is what I reccomend) and it's big daddy Visual Studio will take that code and make bits of it different colours.

They do this depending on whether they are doing things like functions or methods, or memory things like string variables or arrays, or just plain simple text strings or flat boring numbers.

The easiest way to think about that is imagine a page from a Pratchett book, but then the text in a sentence is coloured based on whether it's a persons name, a magical term, a city name, right down to nouns and adjectives. Imagine how quickly you'd read it! Your brain would skip through really quickly!

Here's some python script in that coloured format: ```python friends = ['john', 'pat', 'gary', 'michael'] for i, name in enumerate(friends): print ("iteration {iteration} is {name}".format(iteration=i, name=name))

```

So finally, think about this device and how we are connecting to it. It having it's own WiFi network is cool and all, but surely it would be better if it was connected to the one you normally use for the internet? Then all the devices on that network would be able to talk to it. You could (and you can do this) set up a webpage that the ESP hands out to people that visit it on the local network that when they press a button it turns on a LED, for a simple example.

So how do we set the ESP to not be a WiFi Network and be a client to a WiFi Network Access Point thats already there?

Well, we can put a file called boot.py that is a python script that is run once everytime the ESP boots. So if we put the code in there that did this for us, each time the ESP boots it would run that code and connect to the WiFi network we specified.

So yeah, I created a REALLY simple script for exactly that: boot.py.md - simple connect to wifi network on boot python script Copy/paste the contents of that file to a file called boot.py and use WebRepl to upload it to the ESP. If it doesn't appear in your network, don't worry or fret, just connect the ESP to a Serial Terminal using a USB cable and then you can debug what it think's it's connected as :) I'll leave you to find your way through that though, a google of esp8266 micropython wont connect to wifi will get you started. If it does though, use the link up there for WebRepl, but change the default IP from 192.168.4.1:8266 to whatever the IP is of your ESP8266.
Hello World