Today’s microcontrollers (like the STM32 Blue Pill) pack so many features in a tiny package… yet few embedded programmers are capable of exploiting the full potential of modern microcontrollers. Many of us (my IoT students included) seem to be stuck in the 1980s — painstakingly writing C programs for small computers.

It’s time to drop our legacy programming practices and adopt smarter, safer ways to exploit these microcontrollers… starting with Apache Mynewt and Rust.

Mynewt is a modern realtime operating system that runs on many microcontroller platforms, even on devices with little RAM and ROM like Blue Pill. It has an excellent Sensor Framework for creating IoT devices.

Sensor Network with Blue Pill, ESP8266, nRF24L01 and Mynewt

Stretching Mynewt on Blue Pill to the limit, I have created sophisticated sensor networks with CoAP encoding (JSON and CBOR), transmitting and receiving simultaneously on both ESP8266 and nRF24L01 modules.

Sadly, Mynewt only supports applications developed in C, consistent with its frugal origins. But C programming is a rare skill today (and C frustrates my IoT students too). What about other languages? MicroPython and JavaScript seem too heavy for a lightweight Mynewt device… perhaps Rust?

Mynewt and Rust: The Perfect Match

In my previous experiments with Embedded Rust (including this one), I know that Rust flies light and fast just like C. Rust has great support in coding tools like Visual Studio Code, which makes it easier to code for newbies.

Rust advocates “Safe Coding”. Rust disallows bad code that may cause our device to crash. Rust could be the perfect match for Mynewt!

In this article we’ll mix Mynewt with Rust and learn…

1️⃣ Why is Rust better than C for embedded programming

2️⃣ How to call C functions from Rust and vice versa

3️⃣ What’s Unsafe Coding and how to make coding safer

4️⃣ How Rust compiles programs

5️⃣ How to inject Rust code into the Mynewt build

6️⃣ How to install and configure the Rust and Mynewt code

7️⃣ And finally we’ll see an actual Rust application running on Mynewt with Blue Pill

Here’s the Rust application code that we’ll be running: A sensor application that polls Blue Pill’s internal temperature sensor every 10 seconds and displays the result…