IO7: Device-to-cloud messaging: MQTT and CoAP

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Designing topics and payloads

🎯You'll be able to

Design a topic structure and a message format that stay clear as a fleet grows and changes.

🏠The everyday picture

A postal address goes from big to small: state, district, village, house. Anyone can find a house, and a whole village's mail can be sorted together.

📘What it really is

📝A worked example

Input: a first draft topic jalsetu/T07 with payload {"level": 940, "temp": 26}.

Steps:

  1. Add the site and the kind: jalsetu/S01/T07/reading.
  2. Name units and add what the server needs: {"seq": 312, "ts": 1793635200, "level_mm": 940, "level_pct": 47, ...}.
  3. Put status on jalsetu/S01/T07/status, separate from readings.

Expected output: Project 2's agreed format, as in docs/FORMATS.md.

🌍Where it's used

📝Case scenario

A company used topics like sensor1, sensor2, ..., with no site. When a second city joined, nobody could subscribe to one city's devices. Renaming topics to company/city/device/kind across the fleet took months.

🖱️Try it

Design topics for a building with 20 rooms, each with a temperature sensor and a light switch. Include readings, status and commands. Success: a pattern where one wildcard subscription gets all temperatures.

⚠️Common mistakes

🛠️For your project

Project 2's test 6 checks the topic and exactly the agreed keys, using a standard MQTT client.

✅Quick check

Why put the site in the topic?

Show the answer

So one subscription can take all of a site's devices, and permissions can be set by site.

Why add a version to a message format?

Show the answer

So old and new firmware can coexist while a fleet updates.

← PreviousMQTT: topics, QoS, retained messages, wills and keepalive
ProLearners Assist