--- layout: default title: API Documentation ---

Constructors

Functions

Other

PubSubClient ()

Creates an uninitialised client instance.

Before it can be used, it must be configured with the property setters:

EthernetClient ethClient;
PubSubClient client;

void setup() {
    client.setClient(ethClient);
    client.setServer("broker.example.com",1883);
    // client is now configured for use
}

PubSubClient (client)

Creates a partially initialised client instance.

Before it can be used, the server details must be configured:

EthernetClient ethClient;
PubSubClient client(ethClient);

void setup() {
    client.setServer("broker.example.com",1883);
    // client is now ready for use
}
Parameters

PubSubClient (server, port, [callback], client, [stream])

Creates a fully configured client instance.

Parameters

boolean connect (clientID)

Connects the client.

Parameters
Returns

boolean connect (clientID, willTopic, willQoS, willRetain, willMessage)

Connects the client with a Will message specified.

Parameters
Returns

boolean connect (clientID, username, password)

Connects the client with a username and password specified.

Parameters
Returns

boolean connect (clientID, username, password, willTopic, willQoS, willRetain, willMessage)

Connects the client with a Will message, username and password specified.

Parameters
Returns

boolean connect (clientID, username, password, willTopic, willQoS, willRetain, willMessage, cleanSession)

Connects the client with a Will message, username, password and clean-session flag specified.

Note : even if the cleanSession is set to false/0 the client will not retry failed qos 1 publishes. This flag is only of use to maintain subscriptions on the broker.

Parameters
Returns

void disconnect ()

Disconnects the client.

int publish (topic, payload)

Publishes a string message to the specified topic.

Parameters
Returns

int publish (topic, payload, retained)

Publishes a string message to the specified topic.

Parameters
Returns

int publish (topic, payload, length)

Publishes a message to the specified topic.

Parameters
Returns

int publish (topic, payload, length, retained)

Publishes a message to the specified topic, with the retained flag as specified.

Parameters
Returns

int publish_P (topic, payload, length, retained)

Publishes a message stored in PROGMEN to the specified topic, with the retained flag as specified.

Parameters
Returns

boolean beginPublish (topic, payloadLength, retained)

Begins sending a publish message. The payload of the message is provided by one or more calls to write followed by a call to endPublish.

Parameters
Returns

size_t write (uint8_t)

Writes a byte as a component of a publish started with a call to beginPublish.

Parameters
Returns

size_t write (payload, length)

Writes an array of bytes as a component of a publish started with a call to beginPublish.

Parameters
Returns

boolean beginPublish ()

Finishing sending a message that was started with a call to beginPublish.

Returns

boolean subscribe (topic, [qos])

Subscribes to messages published to the specified topic.

Parameters
Returns

boolean unsubscribe (topic)

Unsubscribes from the specified topic.

Parameters
Returns

boolean loop ()

This should be called regularly to allow the client to process incoming messages and maintain its connection to the server.

Returns

int connected ()

Checks whether the client is connected to the server.

Returns

int state ()

Returns the current state of the client. If a connection attempt fails, this can be used to get more information about the failure.

Returns

PubSubClient setServer (server, port)

Sets the server details.

Parameters
Returns

PubSubClient setCallback (callback)

Sets the message callback function.

Parameters
Returns

PubSubClient setClient (client)

Sets the client.

Parameters
Returns

PubSubClient setStream (stream)

Sets the stream.

Parameters
Returns

Configuration Options

The following configuration options can be used to configure the library. They are contained in PubSubClient.h.

MQTT_MAX_PACKET_SIZE
Sets the largest packet size, in bytes, the client will handle. Any packet received that exceeds this size will be ignored.

Default: 128 bytes

MQTT_KEEPALIVE
Sets the keepalive interval, in seconds, the client will use. This is used to maintain the connection when no other packets are being sent or received.

Default: 15 seconds

MQTT_VERSION
Sets the version of the MQTT protocol to use.

Default: MQTT 3.1.1

MQTT_MAX_TRANSFER_SIZE
Sets the maximum number of bytes passed to the network client in each write call. Some hardware has a limit to how much data can be passed to them in one go, such as the Arduino Wifi Shield.

Default: undefined (complete packet passed in each write call)

MQTT_SOCKET_TIMEOUT
Sets the timeout when reading from the network. This also applies as the timeout for calls to connect.

Default: 15 seconds

Subscription Callback

If the client is used to subscribe to topics, a callback function must be provided in the constructor. This function is called when new messages arrive at the client.

The callback function has the following signature:

void callback(const char[] topic, byte* payload, unsigned int length)
Parameters

Internally, the client uses the same buffer for both inbound and outbound messages. After the callback function returns, or if a call to either publish or subscribe is made from within the callback function, the topic and payload values passed to the function will be overwritten. The application should create its own copy of the values if they are required beyond this.