Creative DIY Microcontroller Projects with TinyGo and WebAssembly

Book description

Explore embedded programming, and get hands-on with real-world embedded projects relating to IoT, low-powered devices, and other complex systems using TinyGo and WebAssembly

Key Features

  • Build creative embedded apps with TinyGo using low-powered devices and microcontrollers
  • Understand the practicality involved in integrating hardware and sensors while programming them using TinyGo
  • Use TinyGo in modern browsers to display embedded applications' statistics on WebAssembly dashboards

Book Description

While often considered a fast and compact programming language, Go usually creates large executables that are difficult to run on low-memory or low-powered devices such as microcontrollers or IoT. TinyGo is a new compiler that allows developers to compile their programs for such low-powered devices. As TinyGo supports all the standard features of the Go programming language, you won't have to tweak the code to fit on the microcontroller.

This book is a hands-on guide packed full of interesting DIY projects that will show you how to build embedded applications. You will learn how to program sensors and work with microcontrollers such as Arduino UNO and Arduino Nano IoT 33. The chapters that follow will show you how to develop multiple real-world embedded projects using a variety of popular devices such as LEDs, 7-segment displays, and timers. Next, you will progress to build interactive prototypes such as a traffic lights system, touchless hand wash timer, and more. As you advance, you'll create an IoT prototype of a weather alert system and display those alerts on the TinyGo WASM dashboard. Finally, you will build a home automation project that displays stats on the TinyGo WASM dashboard.

By the end of this microcontroller book, you will be equipped with the skills you need to build real-world embedded projects using the power of TinyGo.

What you will learn

  • Discover a variety of TinyGo features and capabilities while programming your embedded devices
  • Explore how to use display devices to present your data
  • Focus on how to make TinyGo interact with multiple sensors for sensing temperature, humidity, and pressure
  • Program hardware devices such as Arduino Uno and Arduino Nano IoT 33 using TinyGo
  • Understand how TinyGo works with GPIO, ADC, I2C, SPI, and MQTT network protocols
  • Build your first TinyGo IoT and home automation prototypes
  • Integrate TinyGo in modern browsers using WebAssembly

Who this book is for

If you are a Go developer who wants to program low-powered devices and hardware such as Arduino UNO and Arduino Nano IoT 33, or if you are a Go developer who wants to extend your knowledge of using Go with WebAssembly while programming Go in the browser, then this book is for you. Go hobbyist programmers who are interested in learning more about TinyGo by working through the DIY projects covered in the book will also find this hands-on guide useful.

Table of contents

  1. Creative DIY Microcontroller Projects with TinyGo and WebAssembly
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Code in Action
    6. Download the color images
    7. Conventions used
    8. Get in touch
    9. Reviews
  6. Chapter 1: Getting Started with TinyGo
    1. Technical requirements
    2. Understanding what TinyGo is
      1. How TinyGo works
      2. Comparing TinyGo to Go
    3. Setting up TinyGo
      1. Installing on Linux
      2. Installing on Windows
      3. Installing on macOS
      4. Installing on Docker
    4. Understanding IDE integration
      1. VS Code integration
      2. General IDE integration
      3. Setting up Goland
      4. Integrating any editor
    5. The Arduino UNO
      1. Getting to know the technical specifications
      2. Exploring pinout
    6. Checking out the Hello World of Things
      1. Getting the requirements ready
      2. Preparing the project
      3. Programming the microcontroller
      4. Flashing the program
      5. Using TinyGo Playground
    7. Summary
    8. Questions
  7. Chapter 2: Building a Traffic Lights Control System
    1. Technical requirements
    2. Lighting an external LED
    3. Lighting an LED when a button is pressed
      1. Building the circuit
      2. Programming the logic
    4. Building traffic lights
      1. Building the circuit
      2. Creating a folder structure
      3. Writing the logic
    5. Building traffic lights with pedestrian lights
      1. Assembling the circuit
      2. Setting up the project structure
      3. Writing the logic
    6. Summary
    7. Questions
    8. Further reading
  8. Chapter 3: Building a Safety Lock Using a Keypad
    1. Technical requirements
    2. Writing to the serial port
    3. Monitoring the serial port
    4. Monitoring input from a keypad
      1. Building the circuit
      2. Understanding the workings of a 4x4 keypad
    5. Writing the driver
      1. Driver variables
      2. Configure
      3. GetIndices
      4. GetKey
      5. main
    6. Finding drivers for TinyGo
      1. Contributing drivers to TinyGo
    7. Controlling a servomotor
      1. Understanding SG90 servomotors
      2. Building the circuit
      3. Writing the servo control logic
    8. Building a safety lock using a keypad
      1. Building the circuit
      2. Writing the logic
    9. Summary
    10. Questions
  9. Chapter 4: Building a Plant Watering System
    1. Technical requirements
    2. Reading soil moisture sensor data
      1. Assembling the circuit
      2. Finding thresholds
      3. Understanding ADC in TinyGo
      4. Writing a library for the sensor
      5. Testing the library
    3. Reading water level sensor data
      1. Writing a water level sensor library
    4. Controlling a buzzer
      1. Writing a buzzer library
    5. Controlling a pump
      1. Working with relays
      2. Writing a pump library
    6. Watering your plants
    7. Summary
    8. Questions
    9. References
  10. Chapter 5: Building a Touchless Handwash Timer
    1. Technical requirements
    2. Introducing the Arduino Nano 33 IoT
      1. Installing Bossa
    3. Learning to measure distances
      1. Understanding the HC-SR04 sensor
      2. Assembling the circuit
      3. Writing a library
      4. Unit testing in TinyGo
      5. Writing an example program for the library
    4. Using 4-digit 7-segment displays
      1. Using a MAX7219
      2. Writing a library to control the MAX7219
      3. Writing a library to control the hs42561k display
    5. Putting it all together
    6. Summary
    7. Questions
  11. Chapter 6: Building Displays for Communication using I2C and SPI Interfaces
    1. Technical requirements
    2. Exploring the TinyGo drivers
    3. Displaying text on an HD44780 16x2 LCD display
      1. Building the circuit
      2. Understanding I2C
      3. Writing the code
    4. Displaying user input on the display
    5. Building a CLI
    6. Understanding SPI
    7. Displaying a simple game
      1. Building the circuit
      2. Using an ST7735 display
      3. Developing a game
    8. Summary
    9. Questions
  12. Chapter 7: Displaying Weather Alerts on the TinyGo Wasm Dashboard
    1. Technical requirements
    2. Building a weather station
      1. Assembling the circuit
      2. Programming the weather station
    3. Sending MQTT messages to a broker
      1. Implementing the Wi-Fi package
      2. Implementing an MQTT client abstraction layer
      3. Implementing the weather station
    4. Introducing Wasm
    5. Displaying sensor data and weather alerts on a Wasm page
      1. Serving the application
      2. Implementing the weather app
    6. Summary
    7. Questions
  13. Chapter 8: Automating and Monitoring Your Home through the TinyGo Wasm Dashboard
    1. Technical requirements
    2. Building a home automation dashboard
      1. Creating a reusable MQTT component
      2. Setting up the Wasm instantiation code
      3. Creating the HTML template
      4. Implementing the login view logic
      5. Implementing the dashboard component
      6. Implementing the main logic
      7. Serving the app
    3. Building the home automation client
      1. Setting up the circuit
      2. Implementing the logic
    4. Requesting data from the microcontroller
      1. Checking other implementation ideas
    5. Summary
    6. Questions
  14. Appendix – "Go"ing Ahead
    1. Blocking a goroutine
      1. Reading from a channel
      2. A select statement
      3. Sleeping is a blocking call
    2. Finding heap allocations
  15. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
  16. Afterword
    1. Why subscribe?
  17. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: Creative DIY Microcontroller Projects with TinyGo and WebAssembly
  • Author(s): Tobias Theel
  • Release date: May 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800560208