Series to think about "IoT front-end development" ④ IoT prototyping with MicroPython Part 1

Introduction

I am interested in so-called field IoT, which uses IoT devices outdoors such as farmland. We believe that the key to the widespread use of IoT devices in this field is the flexibility to customize functions in each field and the reduction of costs per device. Below, we will take up MicroPython, a python environment for embedded devices that may be useful for getting started in these fields.

MicroPython, attention required ♪

(Since it is a keyword that is easy to get rid of, if you are interested, please do it yourself.)

Why Focus on MicroPython for IoT

Works with devices with lower specifications than Raspberry Pi.

The Raspberry pi, which is famous as an educational single board computer, is said to have shipped about 2 million units for industrial use in 2016. Source Why "Raspberry Pi" is growing for industrial equipment

As for the details, it is interesting that the Raspberry Pi for education or "amateur" has become widely used as an industrial computer, referring to the explanation of embedded media.

The micropython that we are paying attention to this time is one that can perform I / O control based on python on a microcomputer board such as STM32 that can be purchased at a retail price of several hundred yen. On the official website, * "MicroPython employs many advanced coding techniques, and lots of tricks to maintain a compact size while still having a full set of features." * Is stated. There is no doubt that the creator of MicroPython is a geek, the created MicroPython environment is ready to use, and you can try it step by step using REPL, which is quite user-friendly (at least, it is related to embedded systems Even a non-IT engineer might be familiar with it).

The specifications of the reference board pyboard are as follows (see the official website for details)

You can see that python works with a RAM capacity of 200KB or less (although it is a rich environment in a "microcomputer").

I have always thought that the ability to operate a language higher than C (?) With specifications similar to the STM32 microcomputer board and enable network communication will be a key player for the spread of IoT devices. There is. MicroPython is considered one of the candidates. Of course, lua / mruby / lightweight javascript etc. will definitely be attractive candidates. However, I think that MicroPython, which has already appeared as a real user, is one step ahead.

What may happen in the next few years.

Let's write about 5 years after the microcomputer board equipped with MicroPython-like language like New Year.

① 2022, five years from now. IoT is demonstrating agricultural productivity gains in rural Asia / Africa (using a small grant from the World Bank or something). Introduced is a sensor device that is waterproof and dustproof on a microcomputer board that can be purchased in small lots for 10 yen per unit. A MicroPython-like scripting language works. Each microcomputer board was handed to n villagers in m units, and all of them were inserted into the farmland managed by each person and the soil in the surrounding area.

――N × m = 10,000 units, the procurement cost is over 1 million yen.

(2) A microcomputer board setting class will be held by NGO staff to explain the graphical development environment for introducing the script language on the microcomputer board. In this development environment, you can get a bird's-eye view of 100 million devices and customize each microcomputer board with a particle size such as "reporting plants that are about to die."

――Somehow, I'm assuming a development environment like an advanced version of Squeak. Dr. Alan Kay's Squeak (Smalltalk-80) is well known in the eToy field. The following "run a handwritten car" (example below). I hope that such abstraction power will be developed in an IoToy manner (?). In reality, the use of AI will be essential. https://youtu.be/DXMScjdzl_I?t=8m10s

③ (Since it is just an ideal theory until it is realized, it is a bullet point style on the premise of Africa.) Rural people installed 10,000 microcomputer boards in X square kilometer mail. People will be able to utilize the report from the whole as an intermediate technology to solve the problem of "water shortage", which is a common problem in many rural African villages. A few years later, the village production union will send a production forecast report to neighboring cities to help raise the income of the villagers.

――From the people in the “aid industry” who are working on international cooperation by making full use of the ODA budget, it may seem like a mere dream of technology first, and in fact, the technology itself is just a support. However, there are things that cannot be broken through without technology. If a sensor device of 30 yen per node appears, it will be possible to build a sensor network of tens of millions of international jet planes that can report the condition of farmland with the money that 10,000 people in the aid industry fly. (It will become a private business depending on the added value). ――Since it is just a dream, I will not write details, but the key communication technologies are satellite communication technology and Bluetooth technology after Bluetooth 5 in addition to the mobile phone network for IoT.

Reference Bluetooth SIG announces "Bluetooth 5" ——Function expansion for complex IoT environment

MicroPython overview

Official site and code example

Reference MicroPython official website https://micropython.org

The code example on the top page is quite catchy.

Indeed, from the example of "LED Chika"

led.py


import pyb

# turn on an LED
pyb.LED(1).on()

From reading files from a "normal python" SD card.

os.py


import os

# list root directory
print(os.listdir('/'))

# print current directory
print(os.getcwd())

# open and read a file from the SD card
with open('/sd/readme.txt') as f:
    print(f.read())

Features of micropython that I'm paying attention to

Around the following.

--micropython is a lightweight python3 implemented from scratch in C language (C99) (MIT license). --A machine with lower specifications than Raspberry Pi can perform network communication with python code (currently, wifi communication is possible with a board with a retail price of about 500 yen). ――It seems that the function expansion on the C language side can be done smoothly via FFI. (For example, expansion of local functions such as addition of voice reading function, expansion of server linkage function such as JSON-based REST communication module)

Many ancestors are trying in Japan, so it is good to refer to that.

Build micropython development environment

micropython can be built in various environments using the C99 compiler. It can be built in various * nix environments as well as environments without an OS. For IoT amateurs (I), it's easy to build on my own Linux machine for the time being. Since it's a big deal, I thought that it would be about 1000 yen in 5 years, I replaced a low-spec device (used windows vista machine (RAM 1GB) with SSD, and installed lightweight Ubuntu 16.04 (32bit) as the OS. We will work on things).

Obtaining and building the source code

Get the source code from the original github. https://github.com/micropython/micropython The source code for * nix is located under the / unix folder.

On Ubuntu, which already has gcc or clang, I could build it just by installing:

sudo apt install libffi-dev pkg-config

Since the C source code size is not large, the build is completed in about 2 minutes even on a low spec machine.

Generated micropython binary

After building, you will have a small python3 compatible environment that can run independently (current size is 378KB). You should try how far it is compatible with python3. You can start REPL while checking the version.

micropyREPL.png

To read and customize the source code.

-(Additional note) In a low-spec linux environment, it seems comfortable to use sublimetext3 to browse the source code. It's instant to open hundreds of thousands of lines of source code, and it opens the binaries as they are.

Look at the source code and explore the extension points. The makefile prepared for each environment is the starting point because it is designed so that it can be cross-compiled. The / py folder is the main body of micropython and is referenced from makefiles for various environments.

-include mpconfigport.mk
include ../py/mkenv.mk

FROZEN_DIR = scripts

# define main target
PROG = micropython

# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h

# OS name, for simple autoconfig
UNAME_S := $(shell uname -s)

# include py core make definitions
include ../py/py.mk

(Omitted below)

The following can be considered as the direction of customization.

--Add the python interpreter command itself. --Add a C language library called from python.

My image is an approach that "solidifies" useful features into the C library that I've tried with the python REPL.

From now on.

At the moment, the spread of Internet 4.0-like field IoT is in the middle of the road (just started). Also, MircoPython has nothing to do with my work. And there are few remarkable cases of IoT utilization in the field of so-called international cooperation.

I think it's time to prepare. The field IoT field where equipment will rust if you leave it on your cheeks. Perhaps because it costs a lot, looking back over the last 10 years, the path of progress is surprisingly slow. This requires different technical approaches and may utilize different solutions.

You may be a top runner if you achieve some results before the Olympic year. Therefore, since I wrote Series of thinking about "IoT front-end development" (1) Implementation language Nim in the IoT era, I have been thinking about the following.

--I would like to utilize nim to efficiently customize the behavior of small ultra-power-saving IoT devices based on C language * Reference. ――I want to implement a server-side implementation to store and process the data of tens of millions of field IoT devices as reliably as possible (I'm doing something similar in my daytime work, of course. Easy solutions are also appearing [^ 1]). ――We would like to cooperate with equipment for field users who have a power source, such as automobiles and agricultural machinery (if logistics is integrated, it will become a target in the mobility IoT field). --I want to prepare a mechanism that can pursue the authenticity of the data stored on the server side in the medium to long term (thinking about using blockchain) * Reference --Interface between IoT devices and users involved in the field (I feel that interactive ones are desirable.) The historical educational environment of the Smalltalk environment may be useful. * Reference

Underlying everything is technical rationality and cost-effectiveness.

The next step for me is to realize an execution environment called ** Python <-> C <-> Nim **, so next time around that.

[^ 1]: For example, MilkCocoa https://mlkcca.com/sample.html

Recommended Posts

Series to think about "IoT front-end development" ④ IoT prototyping with MicroPython Part 1
How to build a python2.7 series development environment with Vagrant
Think about dropouts with MNIST
Test Driven Development with Django Part 3
Test Driven Development with Django Part 4
Test Driven Development with Django Part 2
Test Driven Development with Django Part 1
Test Driven Development with Django Part 5
How to authenticate with Django Part 2
How to authenticate with Django Part 3
I tried to create a plug-in with HULFT IoT Edge Streaming [Development] (2/3)
A guidebook for doing IoT with MicroPython easily to the last minute