Skip to content

refactor(examples): shared MQTT demo crate on publish / subscribe (#391, stage 2) - #401

Merged
lxsaah merged 1 commit into
feature/388-input-output-connectionsfrom
feature/391-demos-common
Nov 2, 2026
Merged

lxsaah merged 1 commit into
feature/388-input-output-connectionsfrom
feature/391-demos-common

Conversation

@lxsaah

@lxsaah lxsaah commented Nov 2, 2026

Copy link
Copy Markdown
Member

Description

Stage 2 of #391, into the #392 feature branch. mqtt-connector-demo-common becomes the runtime-agnostic home of everything both MQTT demos share, and both demos move to connect + publish / subscribe.

mqtt-connector-demo-common

  • Contracts: Temperature and TemperatureCommand are always Serialize / Deserialize / JSON #[derive(Linkable)], on no_std + alloc too. The data-contracts, serde, alloc and derive features are gone, along with the hand-rolled no_std JSON (to_json_vec, from_json, parse_json_no_std).
  • sim.rs (new):
    • SimThermometers { indoor, outdoor, server_room } hands out owned SimChannels that are neither Clone nor Copy.
    • Thermometer::on(channel) is an Input<T> for every T: From<Reading>, with .every_secs(n) and .readings(n).
    • impl From<Reading> for Temperature lives next to the contract.
    • It only uses RuntimeContext, so the same input runs on Tokio and Embassy and replaces the three producer functions in each demo.
  • Keys and monitors are unchanged. The README is updated.

tokio-mqtt-connector-demo

  • let mqtt = builder.connect(..), .input(Thermometer::on(indoor).readings(5)), publish / subscribe with the keys' #[link_address] topics.
  • main.rs went from 215 to 154 lines; most of the rest is the module docs.
  • MQTT behaviour is unchanged:
    • same topics and QoS / retain
    • five readings per sensor, two seconds apart
    • the same payload bytes, since the derive uses serde_json::to_vec, as to_json_vec did on std

embassy-mqtt-connector-demo

  • The records use the shared thermometers, which keep cycling endlessly as before, plus publish / subscribe.

  • The serial remote-access server stays on with_connector, since it takes no links.

  • Payloads now come from serde_json instead of the hand-rolled one-decimal formatter, so they're identical to the Tokio demo's.

  • Release size for thumbv8m.main-none-eabihf (arm-none-eabi-size -A):

    section before after delta
    .text 557,548 539,948 −17,600
    .rodata 65,568 65,608 +40
    .data 532 532 0
    .bss 124,292 124,308 +16

The board module and the B1 PushButton follow in stage 3.

Checks

CI doesn't run on PRs into the feature branch, so I ran these locally:

  • mqtt-connector-demo-common tests with std, and the no_std build
  • cargo clippy -D warnings on the common crate (no_std and std), the tokio demo, and the embassy demo for thumbv8m.main-none-eabihf
  • the embassy demo in debug and release for thumbv8m.main-none-eabihf
  • a tokio demo run without a broker:
    • build() succeeds
    • MQTT subscribes 2 topics
    • all three thermometers produce every 2 s
    • only the connection is refused, as expected

Not done: a run against a real broker, because Docker's daemon wasn't running. The embassy demo was not flashed.

Related Issue

🤖 Generated with Claude Code

mqtt-connector-demo-common is now the runtime-agnostic home of what both
MQTT demos share:
- contracts that are always Linkable (JSON derive, no_std + alloc); the
  data-contracts / serde / alloc / derive features and the hand-rolled
  no_std JSON helpers are gone
- the keys with their #[link_address] topics
- SimThermometers / Thermometer: owned, non-Clone sensor channels and
  one Input for every T: From<Reading>, replacing three producer
  functions in each demo
- the monitors

Both demos register MQTT with connect and link with publish /
subscribe; the serial remote-access server stays on with_connector.
Tokio payloads are unchanged (the derive uses serde_json::to_vec, as
to_json_vec did). Embassy release .text shrinks by 17,600 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lxsaah
lxsaah merged commit 942b77f into feature/388-input-output-connections Nov 2, 2026
4 checks passed
@lxsaah
lxsaah deleted the feature/391-demos-common branch November 2, 2026 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant