## IoT Adventures in TinyGo Thushan Fernando ![TinyGo logo](assets/tinygo-logo.png) [@thushanfernando](https://twitter.com/thushanfernando) [github.com/thushan](https://github.com/thushan)

TinyGo is Go for Tiny devices.

 

...also for WebAssembly (WASM) and CLI Tools.

Thanks! Tiny Presentation! Questions?

Just Joking!

Don't know Go?

Don't go! We'll cover it lightly.

Go was created by Google in 2007.


By Robert Griesemer, Rob Pike & Ken Thompson...

...who disliked the chaos of C++ at Google.

v1.0 was released in 2012, latest is v1.15.2.

Features of Golang

  • statically typed, easy to follow language
  • extremely fast compilation
  • efficient execution on modern multicore systems
  • built-in concurrency & garbage collection
  • reliable dependency management - go modules
  • built in testing, profiling & race condition checking
  • opinionated - GoDoc, gofmt

Simplest Go Program


							package main
							import "fmt"
							func main() {
								fmt.Println("Hello Voyager!")
							}
						
					$ go run hello-voyager.go
					> Hello Voyager!
					

Take a...

Tour of Go...

What's Tiny then?

...It's small, but we want Tiny.

Devices like...

Arduino Uno / Nano

Read the History of Arduino

Arduino Nano 33 - IoT | BLE | Sense

Newest, most powerful Arduino for Modern IoT makers.

Teensy

ESP8266 / ESP32

...Boring? But wait...

Gameboy Advance

Nintendo Switch

Tiny Devices

Arduino Uno 33 IoT 33 BLE 33 BLE Sense Teensy v4 ESP8266 ESP32
Controller ATmega32P SAMD21 nRF52840 nRF52840 CortexM4 L106 XtensaLX6
Type 8-bit 32-bit 32-bit 32-bit 32-bit 32-bit 32-bit
Instructions AVR ARM ARM ARM ARM AT ULP
Cores 1 1 1 1 1 1 2
Clock 16Mhz 48Mhz 64Mhz 64Mhz 600Mhz 80Mhz 160Mhz
EEPROM / SRAM (kb) 2 32 256 256 1024 64 520
Flash (kb) 32 256 1024 1024 2048 4096 4096

Every tiny bit counts!

So Go on Tiny Devices?

Go was designed for speed, not size.

We need something optimised for size.

So Back to TinyGo

Created by Ayke van Laëthem!

It's an LLVM based Compiler

Supports a subset of Go language & data types

And a smaller set of standard packages

To make lean binaries to fit into tiny devices

And binaries for Linux, WASM & one-day Windows!

How Tiny Are We Talking?

Let's find out...

Go vs TinyGo Binary Size


						package main

						func main() {
							println("Hello Voyager!")
						}
					
$ make build-tinygo-comparison
					
1.1M build/hello-voyager_golang
					21K  build/hello-voyager_tinygo
					

What is this voodoo magic?

TinyGo relies on the existing Go toolchain

After the Go SSA phase, it has a go at trimming

Prunes & optimises for target architecture

Let's Get Started

Windows
						$ scoop install tinygo
					
Mac
					$ brew tap  tinygo-org/tools
					$ brew install tinygo
					
Linux
						$ wget https://github.com/tinygo-org/tinygo/releases/download/v0.15.0/tinygo_0.15.0_amd64.deb
						$ sudo dpkg -i tinygo_0.15.0_amd64.deb
					

Let's also install avr-dude

Programs our Arduino Atmel AVR MCU.

Windows
						$ scoop install avr-dude
					
Mac
						$ brew install avrdude
					
Linux
						$ sudo apt-get install gcc-avr avr-libc avrdude
					

One more thing on Windows...

Windows
						$ choco install make
					

There's a joke in there somewhere...

Let's see some examples

First how the examples are structured:

├── /_build
├── /blink-bling
│   └── main.go
├── /blink-ello
│   └── main.go
├── /distance-sensor
│   └── main.go
├── /go-compare
│   └── main.go
├── /temp-sensor
|   └── main.go
├── Makefile
├── go.mod
├── go.sum
└── platformio.ini
						

Let's make them work.

Make makes it easy

All examples have a make target.

							$ make build-arduino-blink-ello
							$ make flash-arduino-blink-ello
						

Another target to clean our build artefacts.

							$ make clean
						

Easy to present, easy to try later.

Arduino Target Examples

Our Target board is Arduino based.

Widely used, based on the AVR Instruction set

That's why we installed avr-dude earlier.

Let's look at Arduino Uno support.

Token Hello World

Let's make something blink - blink-ello.

							$ make build-arduino-blink-ello
							$ make flash-arduino-blink-ello
						

Machine Package

This is the Hardware Abstraction Layer in TinyGo

Defines all our board hardware - pins, buses, sensors

Let's look at Arduino Machine specifications.

Blink with some Bling

Let's make something blink - blink-bling.

							$ make flash-arduino-blink-bling
						

Go routines in TinyGo

...are a big thing!

We specified a scheduler when flashing

							$ tinygo flash -target arduino -scheduler coroutines ...
						

Which does the hard work of concurrency

Well supported on ARCM, still improving on AVR.

Opens up a whole host of new ideas.

Using Drivers in TinyGo

Let's use the BME280 sensor - temp-sensor.

							$ make flash-arduino-temp-sensor
						

Gives us Temperature, Humidity & Altitude.

Drivers Package

Drivers are a go module.

							$ go get tinygo.org/x/drivers
						

There's a host of common devices supported already.
Sensors, LEDs/Smart Displays, RTCs, GPS.

Take a look at Supported Hardware Devices(53).

TinyGo is still maturing

There's still a few things to implement in TinyGo.

It's a good way to learn Go

Slides, Resources etc

github.com/thushan/talk-tinygo-adventures

Thanks! Questions?