User Manual
Callback Handler Parameters
topic: The topic that caused the subscribe callback to fire (UTF8-encoded)
message: The UTF8 encoded message associated with topic_data
bool unsubscribe( const char* topicFilter );
Unsubscribes from a specific topic or topic filter.
Parameters: The topic or topic filter to unsubscribe from
Returns: 'True' (1) is the unsubscribe was successful, otherwise 'false' (0).
Last Will
MQTT has a concept called a 'Last Will' message. The optional 'Last Will' message can be set using a user-define topic,
/**************************************************************************/
/*!
@brief MQTT subscribe event callback handler
@param topic The topic causing this callback to fire
@param message The new value associated with 'topic'
@note 'topic' and 'message' are UTF8Strings (byte array), which means
they are not null-terminated like C-style strings. You can
access its data and len using .data & .len, although there is
also a Serial.print override to handle UTF8String data types.
*/
/**************************************************************************/
void subscribed_callback(UTF8String topic, UTF8String message)
{
// Print out topic name and message
Serial.print("[Subscribed] ");
Serial.print(topic);
Serial.print(" : ") ;
Serial.println(message);
// Echo back
Serial.print("Echo back to " TOPIC_ECHO " ... ");
mqtt.publish(TOPIC_ECHO, message); // Will halt if an error occurs
Serial.println("OK");
// Unsubscribe from SUBSCRIBED_TOPIC2 if we received an "stop" message
// Won't be able to echo anymore
if ( message == "stop" )
{
Serial.print("Unsubscribing from " TOPIC_SUBSCRIBE " ... ");
mqtt.unsubscribe(TOPIC_SUBSCRIBE); // Will halt if fails
Serial.println("OK");
}
}
Note the use of UTF8String for 'topic' and 'message' since the strings that are returned are UTF8 encoded
and NOT NULL terminated, so we need to use this helper to convert them to something we can safely print.
© Adafruit Industries https://learn.adafruit.com/introducing-the-adafruit-wiced-feather-wifi Page 115 of 202










