11 #include "IPAddress.h" 
   15 #define MQTT_VERSION_3_1      3 
   16 #define MQTT_VERSION_3_1_1    4 
   21 #define MQTT_VERSION MQTT_VERSION_3_1_1 
   25 #ifndef MQTT_MAX_PACKET_SIZE 
   26 #define MQTT_MAX_PACKET_SIZE 128 
   30 #ifndef MQTT_KEEPALIVE 
   31 #define MQTT_KEEPALIVE 15 
   35 #ifndef MQTT_SOCKET_TIMEOUT 
   36 #define MQTT_SOCKET_TIMEOUT 15 
   45 #define MQTT_CONNECTION_TIMEOUT     -4 
   46 #define MQTT_CONNECTION_LOST        -3 
   47 #define MQTT_CONNECT_FAILED         -2 
   48 #define MQTT_DISCONNECTED           -1 
   49 #define MQTT_CONNECTED               0 
   50 #define MQTT_CONNECT_BAD_PROTOCOL    1 
   51 #define MQTT_CONNECT_BAD_CLIENT_ID   2 
   52 #define MQTT_CONNECT_UNAVAILABLE     3 
   53 #define MQTT_CONNECT_BAD_CREDENTIALS 4 
   54 #define MQTT_CONNECT_UNAUTHORIZED    5 
   56 #define MQTTCONNECT     1 << 4  // Client request to connect to Server 
   57 #define MQTTCONNACK     2 << 4  // Connect Acknowledgment 
   58 #define MQTTPUBLISH     3 << 4  // Publish message 
   59 #define MQTTPUBACK      4 << 4  // Publish Acknowledgment 
   60 #define MQTTPUBREC      5 << 4  // Publish Received (assured delivery part 1) 
   61 #define MQTTPUBREL      6 << 4  // Publish Release (assured delivery part 2) 
   62 #define MQTTPUBCOMP     7 << 4  // Publish Complete (assured delivery part 3) 
   63 #define MQTTSUBSCRIBE   8 << 4  // Client Subscribe request 
   64 #define MQTTSUBACK      9 << 4  // Subscribe Acknowledgment 
   65 #define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request 
   66 #define MQTTUNSUBACK    11 << 4 // Unsubscribe Acknowledgment 
   67 #define MQTTPINGREQ     12 << 4 // PING Request 
   68 #define MQTTPINGRESP    13 << 4 // PING Response 
   69 #define MQTTDISCONNECT  14 << 4 // Client is Disconnecting 
   70 #define MQTTReserved    15 << 4 // Reserved 
   72 #define MQTTQOS0        (0 << 1) 
   73 #define MQTTQOS1        (1 << 1) 
   74 #define MQTTQOS2        (2 << 1) 
   77 #define MQTT_MAX_HEADER_SIZE 5 
   79 #if defined(ESP8266) || defined(ESP32) 
   81 #define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback 
   83 #define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int) 
   86 #define CHECK_STRING_LENGTH(l,s) if (l+2+strlen(s) > MQTT_MAX_PACKET_SIZE) {_client->stop();return false;} 
   93     uint8_t buffer[MQTT_MAX_PACKET_SIZE];
 
   95     unsigned long lastOutActivity;
 
   96     unsigned long lastInActivity;
 
   98     MQTT_CALLBACK_SIGNATURE;
 
   99     uint16_t readPacket(uint8_t*);
 
  100     bool readByte(uint8_t * result);
 
  101     bool readByte(uint8_t * result, uint16_t * index);
 
  102     bool write(uint8_t header, uint8_t* buf, uint16_t length);
 
  103     uint16_t writeString(
const char* 
string, uint8_t* buf, uint16_t pos);
 
  108     size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
 
  123     PubSubClient(uint8_t *, uint16_t, Client& client, Stream&); 
 
  124     PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); 
 
  125     PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client,
 
  128     PubSubClient(
const char*, uint16_t, Client& client, Stream&); 
 
  129     PubSubClient(
const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); 
 
  130     PubSubClient(
const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client,
 
  141     bool connect(
const char* 
id, 
const char* user, 
const char* pass); 
 
  142     bool connect(
const char* 
id, 
const char* willTopic, uint8_t willQos, 
bool willRetain,
 
  143                  const char* willMessage); 
 
  144     bool connect(
const char* 
id, 
const char* user, 
const char* pass, 
const char* willTopic,
 
  145                  uint8_t willQos, 
bool willRetain, 
const char* willMessage); 
 
  146     bool connect(
const char* 
id, 
const char* user, 
const char* pass, 
const char* willTopic,
 
  147                  uint8_t willQos, 
bool willRetain, 
const char* willMessage, 
bool cleanSession); 
 
  149     bool publish(
const char* topic, 
const char* payload); 
 
  150     bool publish(
const char* topic, 
const char* payload, 
bool retained); 
 
  151     bool publish(
const char* topic, 
const uint8_t * payload, 
unsigned int plength); 
 
  152     bool publish(
const char* topic, 
const uint8_t * payload, 
unsigned int plength,
 
  154     bool publish_P(
const char* topic, 
const char* payload, 
bool retained); 
 
  155     bool publish_P(
const char* topic, 
const uint8_t * payload, 
unsigned int plength,
 
  165     bool beginPublish(
const char* topic, 
unsigned int plength, 
bool retained); 
 
  170     virtual size_t write(uint8_t); 
 
  173     virtual size_t write(
const uint8_t *buffer, 
size_t size); 
 
  175     bool subscribe(
const char* topic, uint8_t qos);