Connecting Thread Devices to the Cloud: A Step-by-Step Guide

Introduction

Now that we have secure CoAP communication between Thread devices, it’s time to take the next leap. We will connect your Thread network to the cloud. This episode will walk you through bridging Thread sensor data to an MQTT broker via a Thread Border Router.

What Is a Thread Border Router?

A Thread Border Router (BR) is a special node. It bridges the isolated Thread network with external IP-based networks like Ethernet or Wi-Fi. It enables communication between cloud services and low-power Thread devices.

Architecture Overview

Here’s what we’re building:

  • Device A: A Thread node with a temperature sensor and CoAP server.
  • Border Router: A device running OpenThread Border Router (OTBR) connected to Wi-Fi/Ethernet.
  • Cloud Broker: An MQTT server (e.g., Mosquitto or AWS IoT).

The Border Router fetches data from Device A via CoAP and publishes it to the MQTT broker.

Setting Up the Border Router

You can use a Raspberry Pi or NXP i.MX-based board as your Thread Border Router. It runs the OpenThread Border Router stack. It connects to both Thread via an STM32WB dongle or similar. It also connects to the internet.


sudo ot-ctl
> state
leader
> router table
> ipaddr

Ensure your Thread network is active and the BR can ping Thread nodes.

Installing MQTT Client

Install an MQTT client like mosquitto-clients on the Border Router:


sudo apt install mosquitto-clients

Script to Bridge CoAP to MQTT

On the BR, create a Python or C script that:

  • Periodically sends a CoAP GET to /temp on Device A.
  • Parses the response.
  • Publishes the result to an MQTT topic like thread/sensor/temp.

import paho.mqtt.publish as publish
import subprocess

def get_temp_from_thread():
    output = subprocess.check_output(["coap-client", "-m", "get", "coap://[fdde:...]:5683/temp"])
    return output.decode()

while True:
    temp = get_temp_from_thread()
    publish.single("thread/sensor/temp", temp, hostname="mqtt.yourserver.com")

Data Format and Topic Structure

Organize your MQTT topics in a clean hierarchy:

  • thread/sensor/temp
  • thread/device/led (for control)

This structure enables dashboards and analytics platforms to subscribe easily.

Security Considerations

  • Use TLS for MQTT if your broker supports it.
  • Protect the CoAP and MQTT endpoints using ACLs and tokens.

Conclusion

You’ve now connected your local Thread network to the cloud using MQTT and a Thread Border Router. This setup opens the door to building real-time dashboards, cloud automation, and remote monitoring systems. In the next episode, we’ll build a dashboard to visualize the Thread data.

Leave a comment