Hello everyone, today we'll talk about Micropython, Python for miccontrollers, and the
meta-linics with the focus on sensor oriental applications, because that's what I specialize
in.
So I'm from a company called SoundSensing, we monitor machinery, such as ventilation systems,
et cetera, and for a building like this, for example, and we do that in automatic detection
of problems and notify the maintenance team.
We use micropython to prototype devices, and then they usually go into a dedicated device
using Bluetooth low energy.
So the goal of this presentation is that you as a embedded software and better, or software
developer, either your professional or hub-based, learn enough of micropython to consider
it, at least for a future project, or maybe you will rule it out based on some information
that you get.
So who here has a used, like, play doing micropython so before?
Yeah, that's quite a lot, okay, hopefully we'll learn something, and note that there's a micropython
stand as well in the AWS building, so afterwards I will go there and have it to chat with
others about micropython, but if you used it before or not, who here is used Python, so
just playing Python on the things?
That's basically everyone, that's great.
So you will especially learn something, you'll be able to use your skills in a new domain.
So I focus on sensor node systems, just very broadly, you can consider this as some sort
of device that will lead to red sensor data, process sensor data, and transcripts, or act
on it.
So this could be an iterative things type of device, a kind of logging device, it could
be a very wearable device, a phone or a smartwatch, or something like this, or it could
be used in better, specialized, like we monitor machinery, it could be use in robotics,
industrial automation, et cetera, so there's like a large amount of different things, but
they have similar characteristics, into red, process, and transmitter act.
In this case, we'll also consider, we'll focus mostly on this processing part, well cover
the others as well, where we can also do some digital sync processing in machine learning,
well, they really push that little Python on these little devices, so there's just some
examples that you could have, and we have actually code examples for my project for several
of these.
So we can go everything from really simple temperature logging, just regular logging, very simple
data, and small amounts, to image classification, quite complex or audio classification.
And the outline, we'll do a quick introduction to the project, a micro Python, we'll do a tour
of some of the features, focusing on the sensor stuff, sensor communication, connectivity,
so more usually online, or BLE, as mentioned in the prehistoric, and then especially native
scene modules.
So you don't have to just just Python with micro Python, you can combine it with C, which
is a really powerful feature, and allows you to do much more.
And then we'll cover a bit of sensors that use digital sync processing, or machine learning,
and a tiny bit about themented Linux in micro.
So micro Python was created in 2014, over 10 years ago, so it's now a really mature project,
I would say.
It has its sports devices with 64 kilowatts of RAM and more, although I would really recommend
going for some like 256 or 12 kilobytes to really get the benefits, it's a bit cramped
below that.
It supports a lot of different micro control families, and tries to be compatible with
C, Python as you can.
If you're just starting out, you haven't used micro Python before, I would really recommend
starting ESP32 device, it's one of the best supported ports, they're powered, you can get
chips very cheaply, but also complete devices, you know, you flash them, and they were
micro Python.
So even if you don't like, or if you're not that familiar with electronics yet, then
you might want to start up there instead of buying a chip.
Installing micro Python for ESP32, or similar, it's very simple, ESP32, you have the
program tools or in tip, or your Python package manager, you just tip and still add two
commands to flash your micro Python, and you have a little Python running on your little
device.
So basically from there on, you know how it goes, at least for the general programming
stuff.
You might want to install this NP remote, which is just convenient tool for communicating
with your device from your computer.
Typically, you're plugged in over your speed, but you can also do like a wrap-all over Bluetooth
or Wi-Fi, which can be convenient, for example, if it's deployed somewhere in your home,
you don't want to have to go to plugging all the time.
There's several IDs that support micro Python from like standard like ESCode, or dedicated
one's like Viper ID, online, or Tommy.
Simple things are simple in micro Python, which is fantastic.
So this here is a complete code in order to create a temperature sensor.
We are using one external library.
In this case, this device just has that temperature sensor in the accelerometer, so we're
actually talking to that to get the temperature, but it would be very similar if you would
get it from a digital, 10% or analog one.
So you do, this is using ACing IO, which makes this kind of network type things quite
convenient and concise, and we're communicating over like MQTT.
So you have this old kit and like one slide, which is super nice, and that's very easy
way to get started.
If you use Viper ID, you can, after you flash your device and create a step, you can open
Chromium, and that URL, and that's all you need to program your device with that.
You have the recollects and many other features.
So it's really convenient.
In educational setting, this is fantastic, because you can just give people these devices,
we might create installed, they plug it in, they're device with Chromium, and you have
it also works on Android.
If that's too much, I mean, if you don't have the hardware, maybe we should might
not, you are waiting for getting it from other Expresss, or a digital key or something.
Then you can, for example, go to this walkway.com, they have a browser-based simulator,
which doesn't look like this, where you can start programming right away, so if we
find me talking very boring, you can go into there and start programming.
There's also a PiScript, it's a project for Python in browser.
They also have a Python repel, just a plain repel on the front page, just go to the front
page, you have my Python.
It takes like, like, 100 milliseconds to look.
Quick tour.
So, my Python is Python, it is Python's implementation, but from scratch, right, and for
these constraint devices.
So which means there is not 100% CPython compatibility, like the Python that we are used
to running.
There's a very high degree, and the goal is to get as close as possible, but there are
some differences that are in parent, like when you're running under one megabyte of RAM,
or 100 kilobytes of RAM, there are some, for example, some,
modules in the standard CPython library, which doesn't really make sense there.
So, it's quite minimal, but you get when you flash your base micro Python, but then you
can install libraries from micro Python lib, for example, to get more featured libraries
from the standard CPython standard library, or common things like their quest module,
which is used for HTTP communication, where you can type them.
There's a documentation about known incompatibilities, and they most of them have a good
reason.
Like, there's like a reason where it's really tricky, or it could just cost too much
to implement it in the perfectly compatible way.
There's also a bunch of things that are not implemented, so like CPython, they add
the feature, of course, all the time, and for a project to follow, that has quite
challenging.
So, there's, you will find on the issues, you will find for each CPython major release,
you will find documentation about like what's implemented, how are people considering
to implement it, and if you have a pet feature that's not implemented, well, then you
can go there, and you can help out.
So, there's no CM module, CFFI compatibility, so it means like most of your Python
stuff will work if you massage it a bit, maybe use a little bit older syntax or not
the latest stuff, but CM modules like Python, and I'm sorry, pandas, or numpy, or such,
it doesn't work at all, because the CPython is different, but there is a CPython where
we get that.
It's also garbage collected, and this is very important, on a tiny device where it's not
the fastest thing in the world, that cycle will take like 1 to 10 milliseconds, and it's
very variable, so when you're doing low latency stuff, that can be tricky.
So if you have a risk case that is that, check and do some prototyping and see if that's
realistic.
We have some control over the garbage selector, like a kind of enabled disable force
collection, so on, but you can't do like incremental GCE, or an arena in GCE, or some
of that.
So, yeah, you will find it very familiar, small script will work at large programs,
we'll need restructuring the refactoring, and some of this is due to the nature of going
on to the tiny device.
So hardware access, that's of course additional from CPython, CPython doesn't have
this, but Python has this machine module with a set of classes to do your typical hardware
peripherals.
So this is what GPIO, analog digital buses, like SPI, ITC, communication, etc.
This is mostly combined in there, so that's something to familiarize yourself with, you
actually want to talk to something around your device.
It's common for all ports, it's a high degree of standardization by now, a few exceptions,
but means that your code will be portable across different devices.
Quite a component, there's a file system, so my programming with my Python is more like
programming a little computer rather than like a classical microcontroller device is much
more familiar, you get the Python experience.
Usually, it's only file system is only until a flash, or you can put it on a SD card
or both.
A little effect is recommended, like a proper embedded file system, a fat 32 is not great,
but it's supported a lot of places, so if you want to expose it over USB, you kind of have
to do that.
That means you can save and load from standard files using open and like this completely
stand, that's just Python by that time.
An MP remote is useful because then that has some tools to allow you to copy things
to that file system on your microcontroller or to a couple things back, which is really
nice, especially when we work with data, we want to be able to store data when we
will store data in a standard format, so I can save audio files as a wild file, and I can
just copy it over and read it as a wild file, instead of managing with custom data formats
and it usually gets very messy.
This makes it super easy.
There's a package manager called mid introduced right from two years ago, so you can then
from your computer with your USB attached device, you can do MP remote install in an
library that will be pulled from micro Python lib, or you can specify a via GitHub, where
it comes, or you can use a specific URL to basically get it from anywhere, which is accessible
on HTTP.
That can use that with your computer when your device is connected, or if you have configured
Wi-Fi or internet somehow on your device, like a USB 32, you can use mid as a package manager
as a module, so you don't need to go through your computer, which is super, can be quite
nice, but what I find the most interesting is that you can install native C modules at
a run time as well, so you don't just type in code, you can add some native code,
which is then accessible as a Python module, for that you need to specify a full URL,
and you need to specify the architecture of my Python version, so it's very quite a bit
of care to pick the right thing, but you can just drop in new native code, which is
super cool and quite unique for a better platform, and that's what I use a lot for
machine learning in the SPSL.
Connectivity, Bluetooth Low Energy is a very real supported, there's a high-level interface called
IoBilly, do use that if you can, and only go to the Bluetooth low-level if you have
to, it's much more convenient, it's one of actually the most pedagogical ways of dealing
with Bluetooth Low Energy in my experience in a better world.
Wi-Fi, there's the network.long, VLAN module, and internet on network.long modules.
Sport on at least many of the most common ports, so yeah, that means you can, 10 things
online, you can fetch things online, you can build things like this, yes, and then like
this Tesla example shown before, it would be feasible or just post something to social media,
like you detect an image of your cat, and you send it directly to the interwebs, that's
what it's for, huh?
So, see module, this is my favorite feature of micro-python, because it allows us to combine
efficient C code with convenient Python scripting, as is basically how we program Python
on computers for data science, type things like Undas, NumPy, TensorFlow, Keras, PyTorch,
all of that is implemented in C or C++ or Rust or only, and then access to Python.
You can do the same with micro-python, which is super powerful, and you can mix and match
as much as you want, find a balance.
So you can implement that as a user, or as a library, I have one library I will show you.
Of course, in the first micro-python modules that come with a core that you use, they are
a lot of them, not only, it uses Python as much as a C, but somewhere as in C.
Then you can also use, actually, as long as you're some code, you can either compile
to C or expose it and see API, you can use that as well.
So there's a project that goes through a wasom ahead of time compiler, and then you can
do C++, Rust, C, time and go, etc.
So there's a lot of powerful.
That is quite new.
So if you're doing that, beware that that's probably not like super smooth, but super
cool, and then we can help us improve that support.
The module looks something like this.
You, at the bottom, is the declaration of the module, like what kind of entry points to
you have.
In this case, there's a, it's called Global, but that's actually attached on the module
object, a factorial name, and that exposes its factorial function.
And then often you put, as long as it's done nothing, you know, with the binding kind
of layer, usually has plain C code from the outside.
So you can include a library, thread a couple of wrappers like that, expose it, and you're,
you can use your C code in my proprietary.
And thanks to the package manager, you can drop it in at runtime.
There are two ways of packaging the C modules, native modules, those are ones that can be
dropped in at runtime.
There are some limitations there around the linker.
There's a pull request, which has not been approved, it's not yet merged by Volodymyr
Schuminsky, that improves that a lot, so this is coming in the next release.
And the mixture is a little bit low, and then the external C modules, that's the modules
that are built into a firmware, like a firmware built as kind of traditional build process.
And that's really excellent, that's like how it's been doing in the 10 last 10 years.
So yeah, you need a little bit of picking shoes, but we're really improving this native
modules.
So Audiumput, you can as well do, with the machine I2S, it supports digital microphone,
external ADC, et cetera.
Unfortunately, right now, PDM microphones, or the protocol is not supported, so that's
something where it's ongoing.
We would love help, someone is working with the area.
There's a lot of good examples for dealing with I2C in audio, by I2F and it can look
something like this.
Camera input, there's no standard API for this, like a generic port that's available on
ports.
A good project for ESP32 called MicroPit and Camera API, which is a really good
to provide pre-built binaries that you can put on your ESP32, which has a camera on it,
and it really works.
There's also open and game, which is complete MicroPit and distribution with a lot of
computer vision and machine modules already built in.
So that's really powerful for that specific niche, if that's what you're interested in.
So a couple of examples, sensor nodes, with MicroPit on an EM learn, EM learn is a library
that I maintain and has EM MicroPit on bindings for digital scene processing and machine
learning.
Here's an example of a noise sensor, machine is sound level, for that you need to do this
A-weight incur, and you need to do infinite impulse response filter in or do that, there's
a standard way.
If you try to do that in MicroPit on, like even with a few performance tricks that is
available for MicroPit on, it still takes like almost 10 times as much time as you have.
For instance of time, so it doesn't work, but if you do this, you add the same module in,
and it runs in a fourth of the CPU that works really well.
There's example, these are all available as examples online.
And you can do them in a timid detection, this is like what you're doing, and that's
using around the forest, training a second learn, and that runs connects faster.
As you can do with Python, but it's much faster with C.
Image classification, with convolutional neural network, you can do like one to 10 FPS of
small images with a few layers CNN, so you can look quite a lot of simple tasks with
this.
That's also available as a C module that you can just drop in and for embedded Linux, motivation
there is memory efficiency.
So if you have a device which has under 512 megawatts RAM running Linux, regular Python,
is a bit, can be a bit painful.
For example, psychic learning will take 128 megabytes for just loading these modules.
So you haven't started doing anything useful yet.
So that's, can be painful.
The same you can do in a few megabytes with micro Python, going to be fast, so we have
time for some questions.
And the unique micro Python port though, it's really good for testing, like I do almost
all the development of the business logic on my machine, and then I run it on the
device for the hardware.
But the hardware parts in the Unix module is very sparse.
So we would love some help if anyone's interested in actually deploying that and using hardware
to implement that.
For example, Raspberry Pi, we should be able to use GPIO, etc., right to see and so on.
So to summarize, and take ways, I say that micro Python is a very productive environment,
mature, or 10 years now, especially for sensor devices, that's what I'm familiar with.
It's very, it gives the familiar Python environment an ease of use, excellent connectivity,
like Wi-Fi, et cetera, really convenient fast to get that up and running.
This mid-package manager is nice, especially the native modules, and we just drop in new
modules to test the Python experience and promote it very useful.
And then you can implement a lot of quite advanced processing of sensor data, especially
when you drop in some C modules here and there for efficiency reasons.
And then we provide this E-mode micro Python library, which is set of modules for digital
processing and machine learning.
So to just make that super easy, you don't have to write all those modules yourself.
So more, to where I'm going to be going to talk about low-sized machine learning, so
like really going more into the C level and really low-power machine learning on micro
controllers, if you just need to not come check it out.
And then there's a stand, what micro Python in the AWS building, and I will go there
after the first set of your talk, and hope to see people there.
There are stickers, boards, devices, other people interested in the same stuff, so happy
to chat there later.
And here what you are interested in.
So thank you very much.
So we have a time for one question.
When I went to a ride some code for a micro Python, you can choose between native or
C, it's up to you after, but what's the size difference in your experience?
That depends a lot on the type of task.
It can be quite small actually, but you mean C quite smaller than Python or whatever.
So your micro Python has a lot of tools to make it the byte code part efficient, and
there are actually some, I didn't show it, but there are some, there's like in head of time
compiler that you can add as a decorator to your function, and they will actually, there's
a compiler that will compile that into native code on your device, and that will basically
bring you to C level, so yeah, so it can be really close, but then there are some specific
cases where it can be quite different, but run time like the execution speed, that's like
10x, can be 100x, because micro Python is quite bad with floats, because they're all proper
Python objects, so each number is at least one allocation, so if you're adding two numbers
that's like four allocations, painful, so yeah, so but I always start the Python, add these
magical decorators, see if it's fast enough, and only if it's not fast enough or small enough,
then I use a C module.
So in that works like 90% of the time, you can just stay in Python land, cool, thank you, thank you