---
layout: default
title: API Documentation
---
Creates an uninitialised client instance. Before it can be used, it must be configured with the property setters: Creates a partially initialised client instance. Before it can be used, the server details must be configured: Creates a fully configured client instance. Connects the client. Connects the client with a Will message specified. Connects the client with a username and password specified. Connects the client with a Will message, username and password specified. Connects the client with a Will message, username, password and clean-session flag specified. Note : even if the Disconnects the client. Publishes a string message to the specified topic. Publishes a string message to the specified topic. Publishes a message to the specified topic. Publishes a message to the specified topic, with the retained flag as specified. Publishes a message stored in Begins sending a publish message. The payload of the message is provided by one or more calls to Writes a byte as a component of a publish started with a call to Writes an array of bytes as a component of a publish started with a call to Finishing sending a message that was started with a call to Subscribes to messages published to the specified topic. Unsubscribes from the specified topic. This should be called regularly to allow the client to process incoming messages and maintain its connection to the server. Checks whether the client is connected to the server. Returns the current state of the client. If a connection attempt fails,
this can be used to get more information about the failure. Sets the server details. Sets the message callback function. Sets the client. Sets the stream. The following configuration options can be used to configure the library.
They are contained in Constructors
Functions
Other
PubSubClient ()
EthernetClient ethClient;
PubSubClient client;
void setup() {
client.setClient(ethClient);
client.setServer("broker.example.com",1883);
// client is now configured for use
}
PubSubClient (client)
EthernetClient ethClient;
PubSubClient client(ethClient);
void setup() {
client.setServer("broker.example.com",1883);
// client is now ready for use
}
Parameters
Client
, typically EthernetClient
.PubSubClient (server, port, [callback], client, [stream])
Parameters
Client
, typically EthernetClient
.Stream
, used to store received messages. See the mqtt_stream
example for more information.boolean connect (clientID)
Parameters
Returns
boolean connect (clientID, willTopic, willQoS, willRetain, willMessage)
Parameters
Returns
boolean connect (clientID, username, password)
Parameters
Returns
boolean connect (clientID, username, password, willTopic, willQoS, willRetain, willMessage)
Parameters
Returns
boolean connect (clientID, username, password, willTopic, willQoS, willRetain, willMessage, cleanSession)
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 ()
int publish (topic, payload)
Parameters
Returns
int publish (topic, payload, retained)
Parameters
Returns
int publish (topic, payload, length)
Parameters
Returns
int publish (topic, payload, length, retained)
Parameters
Returns
int publish_P (topic, payload, length, retained)
PROGMEN
to the specified topic, with the retained flag as specified.Parameters
Returns
boolean beginPublish (topic, payloadLength, retained)
write
followed by a call to endPublish
.Parameters
Returns
size_t write (uint8_t)
beginPublish
.Parameters
Returns
size_t write (payload, length)
beginPublish
.Parameters
Returns
boolean beginPublish ()
beginPublish
.Returns
boolean subscribe (topic, [qos])
Parameters
Returns
boolean unsubscribe (topic)
Parameters
Returns
boolean loop ()
Returns
int connected ()
Returns
int state ()
Returns
PubSubClient.h
):
MQTT_CONNECTION_TIMEOUT
- the server didn't respond within the keepalive timeMQTT_CONNECTION_LOST
- the network connection was brokenMQTT_CONNECT_FAILED
- the network connection failedMQTT_DISCONNECTED
- the client is disconnected cleanlyMQTT_CONNECTED
- the client is connectedMQTT_CONNECT_BAD_PROTOCOL
- the server doesn't support the requested version of MQTTMQTT_CONNECT_BAD_CLIENT_ID
- the server rejected the client identifierMQTT_CONNECT_UNAVAILABLE
- the server was unable to accept the connectionMQTT_CONNECT_BAD_CREDENTIALS
- the username/password were rejectedMQTT_CONNECT_UNAUTHORIZED
- the client was not authorized to connectPubSubClient setServer (server, port)
Parameters
Returns
PubSubClient setCallback (callback)
Parameters
Returns
PubSubClient setClient (client)
Parameters
Client
, typically EthernetClient
.Returns
PubSubClient setStream (stream)
Parameters
Stream
, used to store received messages. See the mqtt_stream
example for more information.Returns
Configuration Options
PubSubClient.h
.
MQTT_MAX_PACKET_SIZE
Default: 128 bytes
MQTT_KEEPALIVE
Default: 15 seconds
MQTT_VERSION
Default: MQTT 3.1.1
MQTT_MAX_TRANSFER_SIZE
Default: undefined (complete packet passed in each write call)
MQTT_SOCKET_TIMEOUT
connect
.
Default: 15 seconds
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)
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.