O'Reilly logo
live online training icon Live Online training

Practical MQTT for the Internet of Things

Hands-on MQTT examples for real-time communications with Raspberry Pi and Arduino

Leon Anavi

This live course will teach you how to connect the Internet of Things with the industrial machine-to-machine protocol, MQTT. MQTT is a publish/subscribe protocol with a message broker for real-time communication. In this course, you will explore the best free and open source MQTT brokers and client libraries that you can use "out of the box" to improve your IoT solutions for home automation and industrial use. We'll complete hands-on laboratory exercises that provide exact steps on how to install and use MQTT on Raspberry Pi and Arduino-compatible devices, including integration in the popular open-source platform, Home Assistant.

It’s important for any developer to possess the ability to interconnect and control the Internet of Things (IoT) in real-time. This course will help you gain the knowledge to use low-cost hardware such as Raspberry Pi and Arduino with cutting-edge industrial grade open source technologies.

What you'll learn-and how you can apply it

  • Understand how (near) real-time machine-to-machine communication with MQTT works
  • Learn how to use industry-grade open source MQTT brokers
  • Learn how to use the open source MQTT client libraries through the most popular programming languages (C, C++, Java, JavaScript, Python, PHP, Go, Rust, etc.)
  • Install and set up the open source MQTT broker Mosquitto on Raspberry Pi
  • Using HTU21D I2C sensor module for temperature and humidity on Raspberry Pi and Arduino
  • Create Node.js command-line application for publishing and receiving MQTT messages
  • Create HTML5 web page with the Paho library for publishing and subscribing to MQTT topics through web sockets
  • Learn the benefits of using open source hardware for rapid and low-cost IoT prototyping
  • Install and use Arduino IDE
  • Create Arduino sketch for connecting to WiFi and MQTT broker and controlling RGB LED strip remotely
  • Install and configure Home Assistant and integrate Arduino compatible devices using MQTT

This training course is for you because...

This course targets professional web, mobile, and embedded developers as well as students and IoT enthusiasts who would like to build or improve their IoT-based applications that require reliable and fast communication between multiple devices.


  • Basic knowledge about programming
  • Basic familiarity with GNU/Linux distributions and Raspberry Pi

Required Hardware:

  • Raspberry Pi 3 or 3 B+ with USB power supply (5V/2.5A), keyboard, mouse, and monitor
  • microSD card class 10 (8GB or more)
  • HTU21D I2C sensor module for temperature and humidity
  • ANAVI Light Controller, USB to UART serial debugger, 12V RGB LED strip and 12V power supply

About your instructor

  • Leon Anavi is an open source enthusiast and a senior software engineer. He is an active contributor to various open source software and hardware projects. His professional experience includes embedded Linux as well as web and mobile application development.

    Leon holds a masters in Information Technology from the Technical University Sofia. He is the author of the Tizen Cookbook printed by Packt Publishing. His previous speaking experience includes talks about open source software and hardware during events in San Francisco, Portland (OR), Hong Kong, Shanghai, Shenzhen, Brussels, Berlin, Bratislava, Prague, Sofia and his hometown Plovdiv.


The timeframes are only estimates and may vary according to how the class is progressing


Section 1: Introduction to MQTT (40 min)

  • Overview of popular protocols of real-time communication between the Internet of Things
  • Understanding how MQTT works
  • Reviewing MQTT key features

Lab 1: Installing Raspbian and Mosquitto on Raspberry Pi 3 (30 min)

  • Download, install to the mircoSD card, boot and configure Raspbian GNU/Linux distribution on Raspberry Pi.
  • Enable serial console output and I2C through raspi-config. Install Mosquitto.

Break (10 mins)

Section 2: Selecting the most appropriate MQTT broker for your project (40 min)

  • Overview of MQTT brokers for industrial projects and home automation applications
  • Comparison of most popular free and open source MQTT brokers: Mosquitto, Mosca, EMQ, VerneMQ, ActiveMQ, RabbitMQ, etc.
  • Conclusions and recommendations for selecting the most appropriate MQTT broker for your project

Lab 2: Configuring Mosquitto on Raspberry Pi (30 min)

  • Configure and enable web socket support in Mosquitto, hands-on experience with its system services in Raspbian, installing mosquitto_pub and mosquitto_sub command line MQTT clients and verifying that Mosquitto and MQTT are fully functional.

Break (10 mins)

Section 3: Integrating MQTT client library in your own project (40 min)

  • Introducing the benefits of using an open source MQTT client library for IoT projects
  • Learning the most popular open source MQTT client libraries for popular programming languages and frameworks such as C, C++, Java, JavaScript, Node.js, Python, Go, Rust and C#
  • Understanding how to use Node.js and MQTT for real-time communication of the Internet of Things (IoT)

Lab 3: Implementing MQTT client using Node.js (40 min)

  • Install Node.js on Raspberry Pi. Attach HTU21D temperature and humidity sensor to Raspberry Pi.
  • Write a Node.js application to publish data from the sensor through MQTT.


Section 4: Web Sockets (40 min)

  • Introducing the Paho Project for JavaScript MQTT clients
  • Understanding how web sockets work in HTML5 and JavaScript
  • Publishing and receiving MQTT messages over web sockets

Lab 4: Creating HTML5 web page with the Paho Project (30 min)

  • Creating HTML5 web page for showing temperature received through MQTT via web socket.

Break (10 mins)

Section 5: Arduino & MQTT (40 min)

  • Introducing the open source hardware movement
  • Exploring Arduino
  • Getting started with Arduino IDE
  • Writing Arduino sketches
  • Exploring pubsubclient, the Arduino library that provides support for MQTT

Lab 5: Creating Arduino sketch for publishing temperature and humidity through MQTT (30 min)

  • Install Arduino IDE.
  • Write Arduino sketch for ANAVI Light Controller that connects to a WiFi network and MQTT broker, reports temperature and humidity from HTU21D I2C sensor modules and remotely controls 12V RGB LED strip.

Break (10 mins)

Section 6: Home Assistant (40 min)

  • Overview of popular open source platforms for home automation
  • Introducing Home Assistant
  • Understanding how Home Assistant works
  • Integrating custom devices in Home Assistant using various components

Lab 6: Installing Home Assistant on Raspberry Pi and integrating Arduino compatible devices via MQTT (40 min)

  • Install and configure Home Assistant on Raspberry Pi. Integrated ANAVI Light Controller to Home Assistant with appropriate MQTT components.
  • Turn on and off the lights of the 12V RGB LED strip as well as monitor temperature and humidity from Home Assistant on your smartphone, tablet or computer.