You will subscribe to the neighbouring visitor counter and print its full message to Serial Monitor. Do not change your climate publishing code. A device can publish its own measurements and read another team’s topic at the same time.
This page adds one function and one subscribe call. The header handles MQTT’s byte arrays, reconnects, and restores the subscription. Your function receives readable text.
The registered cross-team topic is:
garden/entrance/counter/gk-01/count
Add a message callback
Name what happens on arrival. Add a function that prints the topic and message it receives.
done when: the sketch verifies with
showVisitorMessage().Subscribe in
setup(). Give the helper the topic and the function to run. Either side ofspine.begin()works; the helper repeats the subscription after every reconnect.done when: Serial Monitor prints the subscription topic.
Keep the client moving. Call
spine.loop()continuously. Do not hide it behind the one-minute sensor timer.done when: a visitor-counter message appears without pressing reset.
Verify the declared relationship. Open your device permalink and read the connection log.
done when: it shows your device subscribed to the visitor-counter topic.
Add this function above setup():
// This is the address we want to listen to.
const char* VISITOR_TOPIC =
"garden/entrance/counter/gk-01/count";
// GardenSpine runs this when the visitor message arrives.
void showVisitorMessage(
const char* topic,
const char* message
) {
Serial.print("received ");
Serial.println(topic);
Serial.print("payload ");
Serial.println(message);
}
Add this to setup(), beside spine.begin():
bool listening = spine.subscribe(
VISITOR_TOPIC,
showVisitorMessage
);
Serial.print("subscribe ");
Serial.print(VISITOR_TOPIC);
Serial.print(" · ");
Serial.println(listening ? "ok" : "failed");
Keep this at the top of loop():
spine.loop();
subscribe garden/entrance/counter/gk-01/count · ok
received garden/entrance/counter/gk-01/count
payload {"device_id":"gk-01","ts":null,"value":184,"unit":"events","fw":"0.4.0"}The number in the example is a dummy starting value. The real value depends on the next message published by gk-01.
Your device remains alive and continues publishing climate readings. Its connection log states that it subscribed to garden/entrance/counter/gk-01/count.
If it did not work
- Subscribe says
failed: the topic or the function name is missing, or you already subscribed to eight topics. Eight is the limit. - Subscribe says
ok, but nothing prints: keepspine.loop()outside the publish timer and wait for the next counter update. - Messages stop after Wi-Fi drops: keep
spine.loop()running. The helper reconnects and subscribes again. - The topic looks almost right: copy it from this page.
entrance,counter,gk-01, andcountare all required. - Climate publishing stops: remove long
delay()calls. Timers let publishing and subscriptions share one loop.