PubSubClient.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. PubSubClient.h - A simple client for MQTT.
  3. Nick O'Leary
  4. http://knolleary.net
  5. */
  6. #ifndef PubSubClient_h
  7. #define PubSubClient_h
  8. #include <Arduino.h>
  9. #include "IPAddress.h"
  10. #include "Client.h"
  11. #include "Stream.h"
  12. #define MQTT_VERSION_3_1 3
  13. #define MQTT_VERSION_3_1_1 4
  14. // MQTT_VERSION : Pick the version
  15. //#define MQTT_VERSION MQTT_VERSION_3_1
  16. #ifndef MQTT_VERSION
  17. #define MQTT_VERSION MQTT_VERSION_3_1_1
  18. #endif
  19. // MQTT_MAX_PACKET_SIZE : Maximum packet size
  20. #ifndef MQTT_MAX_PACKET_SIZE
  21. #define MQTT_MAX_PACKET_SIZE 512 // need to fix this here, because this define cannot be overruled within the Arduino sketch...
  22. #endif
  23. // MQTT_KEEPALIVE : keepAlive interval in Seconds
  24. #ifndef MQTT_KEEPALIVE
  25. #define MQTT_KEEPALIVE 15
  26. #endif
  27. // MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds
  28. #ifndef MQTT_SOCKET_TIMEOUT
  29. #define MQTT_SOCKET_TIMEOUT 15
  30. #endif
  31. // MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
  32. // in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
  33. // pass the entire MQTT packet in each write call.
  34. //#define MQTT_MAX_TRANSFER_SIZE 80
  35. // Possible values for client.state()
  36. #define MQTT_CONNECTION_TIMEOUT -4
  37. #define MQTT_CONNECTION_LOST -3
  38. #define MQTT_CONNECT_FAILED -2
  39. #define MQTT_DISCONNECTED -1
  40. #define MQTT_CONNECTED 0
  41. #define MQTT_CONNECT_BAD_PROTOCOL 1
  42. #define MQTT_CONNECT_BAD_CLIENT_ID 2
  43. #define MQTT_CONNECT_UNAVAILABLE 3
  44. #define MQTT_CONNECT_BAD_CREDENTIALS 4
  45. #define MQTT_CONNECT_UNAUTHORIZED 5
  46. #define MQTTCONNECT 1 << 4 // Client request to connect to Server
  47. #define MQTTCONNACK 2 << 4 // Connect Acknowledgment
  48. #define MQTTPUBLISH 3 << 4 // Publish message
  49. #define MQTTPUBACK 4 << 4 // Publish Acknowledgment
  50. #define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
  51. #define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
  52. #define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
  53. #define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
  54. #define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
  55. #define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
  56. #define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
  57. #define MQTTPINGREQ 12 << 4 // PING Request
  58. #define MQTTPINGRESP 13 << 4 // PING Response
  59. #define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
  60. #define MQTTReserved 15 << 4 // Reserved
  61. #define MQTTQOS0 (0 << 1)
  62. #define MQTTQOS1 (1 << 1)
  63. #define MQTTQOS2 (2 << 1)
  64. #ifdef ESP8266
  65. #include <functional>
  66. #define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
  67. #else
  68. #define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
  69. #endif
  70. class PubSubClient {
  71. private:
  72. Client* _client;
  73. uint8_t buffer[MQTT_MAX_PACKET_SIZE];
  74. uint16_t nextMsgId;
  75. unsigned long lastOutActivity;
  76. unsigned long lastInActivity;
  77. bool pingOutstanding;
  78. MQTT_CALLBACK_SIGNATURE;
  79. uint16_t readPacket(uint8_t*);
  80. boolean readByte(uint8_t * result);
  81. boolean readByte(uint8_t * result, uint16_t * index);
  82. boolean write(uint8_t header, uint8_t* buf, uint16_t length);
  83. uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
  84. IPAddress ip;
  85. const char* domain;
  86. uint16_t port;
  87. Stream* stream;
  88. int _state;
  89. public:
  90. PubSubClient();
  91. PubSubClient(Client& client);
  92. PubSubClient(IPAddress, uint16_t, Client& client);
  93. PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
  94. PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
  95. PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
  96. PubSubClient(uint8_t *, uint16_t, Client& client);
  97. PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
  98. PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
  99. PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
  100. PubSubClient(const char*, uint16_t, Client& client);
  101. PubSubClient(const char*, uint16_t, Client& client, Stream&);
  102. PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
  103. PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
  104. PubSubClient& setServer(IPAddress ip, uint16_t port);
  105. PubSubClient& setServer(uint8_t * ip, uint16_t port);
  106. PubSubClient& setServer(const char * domain, uint16_t port);
  107. PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
  108. PubSubClient& setClient(Client& client);
  109. PubSubClient& setStream(Stream& stream);
  110. boolean connect(const char* id);
  111. boolean connect(const char* id, const char* user, const char* pass);
  112. boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
  113. boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
  114. void disconnect();
  115. boolean publish(const char* topic, const char* payload);
  116. boolean publish(const char* topic, const char* payload, boolean retained);
  117. boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
  118. boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
  119. boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
  120. boolean subscribe(const char* topic);
  121. boolean subscribe(const char* topic, uint8_t qos);
  122. boolean unsubscribe(const char* topic);
  123. boolean loop();
  124. boolean connected();
  125. int state();
  126. };
  127. #endif