PubSubClient
MQTT client library for Arduino
Public Types | Public Member Functions | List of all members
PubSubClient Class Reference

Main do-everything class that sketches will use. More...

#include <PubSubClient.h>

Public Types

typedef void(* callback_t) (const MQTT::Publish &)
 

Public Member Functions

 PubSubClient (Client &c)
 Simple constructor. More...
 
 PubSubClient (Client &c, IPAddress &ip, uint16_t port=1883)
 Constructor with the server ip address.
 
 PubSubClient (Client &c, String hostname, uint16_t port=1883)
 Constructors with the host name.
 
PubSubClientset_server (IPAddress &ip, uint16_t port=1883)
 Set the server ip address.
 
PubSubClientset_server (String hostname, uint16_t port=1883)
 Set the server host name.
 
callback_t callback (void) const
 Get the callback function.
 
PubSubClientset_callback (callback_t cb)
 Set the callback function.
 
PubSubClientunset_callback (void)
 Unset the callback function.
 
PubSubClientset_max_retries (uint8_t mr)
 Set the maximum number of retries when waiting for response packets.
 
bool connect (String id)
 Connect to the server with a client id. More...
 
bool connect (String id, String willTopic, uint8_t willQos, bool willRetain, String willMessage)
 Connect to the server with a client id and "will" parameters. More...
 
void disconnect (void)
 Disconnect from the server.
 
bool publish (String topic, String payload)
 Publish a string payload. More...
 
bool publish (String topic, const uint8_t *payload, uint32_t plength, bool retained=false)
 Publish an arbitrary data payload. More...
 
bool publish (String topic, MQTT::payload_callback_t pcb, uint32_t length, bool retained=false)
 Publish an arbitrary data payload from a callback. More...
 
bool publish_P (String topic, PGM_P payload, uint32_t plength, bool retained=false)
 Publish an arbitrary data payload stored in "program" memory. More...
 
bool subscribe (String topic, uint8_t qos=0)
 Subscribe to a topic. More...
 
bool unsubscribe (String topic)
 Unsubscribe from a topic.
 
bool loop ()
 Wait for packets to come in, processing them. More...
 
bool connected ()
 Are we connected?
 
bool connect (MQTT::Connect &conn)
 Connect with a pre-constructed MQTT message object.
 
bool publish (MQTT::Publish &pub)
 Publish with a pre-constructed MQTT message object.
 
bool subscribe (MQTT::Subscribe &sub)
 Subscribe with a pre-constructed MQTT message object.
 
bool unsubscribe (MQTT::Unsubscribe &unsub)
 Unsubscribe with a pre-constructed MQTT message object.
 

Detailed Description

Main do-everything class that sketches will use.

Constructor & Destructor Documentation

PubSubClient::PubSubClient ( Client &  c)

Simple constructor.

Use set_server() before connect()

Member Function Documentation

bool PubSubClient::connect ( String  id)

Connect to the server with a client id.

Parameters
idClient id for this device
bool PubSubClient::connect ( String  id,
String  willTopic,
uint8_t  willQos,
bool  willRetain,
String  willMessage 
)

Connect to the server with a client id and "will" parameters.

The "will" is a message that is published when this client unexpectantly disconnects from the broker i.e without sending a DISCONNECT message.

Parameters
idClient id for this device
willTopicTopic of the "will" message
willQosQoS level of the "will" message
willRetainRetain setting of the "will" message
willMessageContent (payload) of the "will" message
bool PubSubClient::loop ( )

Wait for packets to come in, processing them.

Also periodically pings the server

bool PubSubClient::publish ( String  topic,
String  payload 
)

Publish a string payload.

Parameters
topicTopic of the message
payloadString text of the message
bool PubSubClient::publish ( String  topic,
const uint8_t *  payload,
uint32_t  plength,
bool  retained = false 
)

Publish an arbitrary data payload.

Parameters
topicTopic of the message
payloadPointer to contents of the message
plengthLength of the message (pointed to by payload) in bytes
retainedIf true, this message will be stored on the server and supplied to any future subscribers whose subscriptions match its topic name.
bool PubSubClient::publish ( String  topic,
MQTT::payload_callback_t  pcb,
uint32_t  length,
bool  retained = false 
)

Publish an arbitrary data payload from a callback.

Parameters
topicTopic of this message
pcbA callback function that writes the payload directly to the network Client object
lengthThe length of the data that 'pcb' will send
bool PubSubClient::publish_P ( String  topic,
PGM_P  payload,
uint32_t  plength,
bool  retained = false 
)

Publish an arbitrary data payload stored in "program" memory.

Parameters
topicTopic of the message
payloadPointer to contents of the message, stored in "program" memory
plengthLength of the message (pointed to by payload) in bytes
retainedIf true, this message will be stored on the server and supplied to any future subscribers whose subscriptions match its topic name.
bool PubSubClient::subscribe ( String  topic,
uint8_t  qos = 0 
)

Subscribe to a topic.

Parameters
topicTopic filter
qosQoS value. 0 => no handshake, 1 => single handshake, 2 => two-way handshake

The documentation for this class was generated from the following files: