DEV Community

Ken W Alger
Ken W Alger

Posted on

An Overview of MicroPython

There are a lot of options available when one starts exploring the exciting field of the Internet of Things, or IoT. Not only are there many choices of micro-controllers, there are also a variety of languages in which to program these devices. Many times the micro-controllers are sold as being dependent on C or C++ as a language, which is great, but I don't know either of those. There is some great and amazing working being led by Andrew Chalkley around using the JavaScript language for micro-controllers over at thingsSDK and if JavaScript is your language, I'd highly recommend checking out their work. There are also lots of resources for using Java for IoT, Nathan Tippy, for example is an excellent resource.

All those are great languages, but what about if you are a beginning developer just interested in playing around in IoT? Or what if you are a Pythonista and don't want to pick up another programming language? Well, for many micro-controllers there is a Python option which is, arguably easier to learn in many ways than Java, JavaScript, C or C++. That option being MicroPython.

MicroPython is a tiny, open source Python programming language interpreter than runs on embedded boards. It was originally developed in 2013 and released in 2014 by Dr. Damian P. George of Cambridge University. It implements Python 3 and is designed specifically to run on devices with limited resources.

As with many things in the IoT world, the use of a particular language is greatly influenced by the tools, libraries, and packages one has available. MicroPython comes with an interactive read-evaluate-print-loop (REPL) for quick prototyping and also fully supports loading MicroPython scripts onto a device to run as well. It supports an extensive Python based library and for lower level operations can be extended with C/C++ functions as needed.

Many of the tasks that beginning IoT folks start off with, such as blinking lights, reading switches, driving servos, etc. are all easy to do in MicroPython. Further, it is often incredibly easy to read as well as it is all Python based. For example, if one has MicroPython installed on a NodeMCU ESP8266 board, a popular and inexpensive board to start working in IoT, with a basic LED light connected to GPIO Pin 5Â it is pretty simple to get the light to turn on (high state) and off (low state).

import machine

led = machine.Pin(5, machine.Pin.OUT)
# Turn on LED
led.high()
# Turn off LED
led.low()

The machine module is included with MicroPython and handles many of the hardware connections from software.  It allows access to things like GPIO pins, CPU frequency, I2C busses, etc. If it is low-level on the hardware, the machine module is the way to go!

As one can imagine, cramming everything from Python 3 into a micro-controller isn't possible, but core data types and modules which make sense for embedded systems are included. Another limitation is that since Python is a high level programming language it isn't as fast or lean as C or C++. While many operations aren't impacted by this, if your application requires tight timing or performance is of utmost importance, using MicroPython can be an issue. However, as stated earlier, one can extend MicroPython to use C/C++ in these circumstances.

Since MicroPython is open source, check it out on GitHub, one can further extend or adapt it specific use case scenarios. For example, if you need something specific to run at boot time you can include it in the package that is flashed to the device. Pretty handy and convenient.

Hopefully I have at least sparked your interest enough in using MicroPython for IoT to take a further look at it. The development site actually has a live, interactive device where you can try out some code and see it run on a real world device. Give it a try and leave a comment below to share with others what you've done and built!

Follow me on Twitter @kenwalger to get the latest updates on my postings, or see the original post on my blog.

Top comments (9)

Collapse
 
georgeoffley profile image
George Offley

Vague question: Do you have insights about building security into your IoT devices through MicroPython? Would it all be handled in the Python code? Is this a consideration given that most people (myself included) reading this would only use it in a hobbyist capacity?

Collapse
 
kenwalger profile image
Ken W Alger

George, that is a great question. I think I would need additional information about what type of security you are thinking about specifically. Network security? Trying to secure the application itself? Data security?

This is a great topic, I'd enjoy continuing the conversation...

Collapse
 
georgeoffley profile image
George Offley

As would I, especially considering the issues surrounding IoT attacks these days. I suppose from a networking standpoint it's about how you setup your network, firewalls and closing ports and what have you. I'm curious if your IoT device that you build using MicroPython would be able to incorporate networking security. If it's all through Python, I suppose you would do it like you normally would. What do you think of some of the hardware security issues surrounding the homebrew IoT devices?

Thread Thread
 
kenwalger profile image
Ken W Alger

Here's a talk by Adam Englander on IoT Lock Down

youtube.com/watch?v=NajRdsNqtMw&in...

It is from the end of 2015 which is a life time ago in the IoT world, but still has some good points. I will do some additional research as well as to where the current state of IoT security sits.

Thread Thread
 
georgeoffley profile image
George Offley

I will as well since I'm pretty curious. Only way IoT has any staying power is if security standards and practices are advanced. At least in my view. I can't see customers wanting something that they're positive can be hijacked and used against them. Unless the government is doing it, then no one cares for some reason.

Thread Thread
 
kenwalger profile image
Ken W Alger

That is certainly a big part of it, yes. I was talking with some folks at a university engineering department and they have the same concerns about standardization in IoT before they start producing courses of any depth on the subject.

Thread Thread
 
georgeoffley profile image
George Offley

At least some people are talking about it. The way these devices are being churned out with little to no security; No wonder we keep seeing stories about IoT botnets, huge hacks and camera wielding sex toys that anyone can get on. The last thing we need is another IoT botnet some vindictive teenager with $50 can buy and cause havoc with.

Collapse
 
anaptfox profile image
Taron Foxworth

This is a great introduction to MicroPython Ken!

Collapse
 
kenwalger profile image
Ken W Alger

Thanks! Great post on micro-controllers on your part as well!