Tutorial 03 of 05

Read someone else.

Subscribe to another team's topic and print their data to Serial Monitor.

Success looks likeThe visitor counter value appears on your screen.

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

  1. Name what happens on arrival. Add a function that prints the topic and message it receives.

    done when: the sketch verifies with showVisitorMessage().

  2. Subscribe in setup(). Give the helper the topic and the function to run. Either side of spine.begin() works; the helper repeats the subscription after every reconnect.

    done when: Serial Monitor prints the subscription topic.

  3. 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.

  4. 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();
Expected Serial Monitor output
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.

Expected browser result

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

Next: turn the incoming message into movement.