Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PubSubClient.h 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 128
  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. // Maximum size of fixed header and variable length size header
  65. #define MQTT_MAX_HEADER_SIZE 5
  66. #if defined(ESP8266) || defined(ESP32)
  67. #include <functional>
  68. #define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
  69. #else
  70. #define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
  71. #endif
  72. #define CHECK_STRING_LENGTH(l,s) if (l+2+strlen(s) > MQTT_MAX_PACKET_SIZE) {_client->stop();return false;}
  73. /** PubSubClient class */
  74. class PubSubClient : public Print
  75. {
  76. private:
  77. Client* _client;
  78. uint8_t buffer[MQTT_MAX_PACKET_SIZE];
  79. uint16_t nextMsgId;
  80. unsigned long lastOutActivity;
  81. unsigned long lastInActivity;
  82. bool pingOutstanding;
  83. MQTT_CALLBACK_SIGNATURE;
  84. uint16_t readPacket(uint8_t*);
  85. bool readByte(uint8_t * result);
  86. bool readByte(uint8_t * result, uint16_t * index);
  87. bool write(uint8_t header, uint8_t* buf, uint16_t length);
  88. uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
  89. // Build up the header ready to send
  90. // Returns the size of the header
  91. // Note: the header is built at the end of the first MQTT_MAX_HEADER_SIZE bytes, so will start
  92. // (MQTT_MAX_HEADER_SIZE - <returned size>) bytes into the buffer
  93. size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
  94. IPAddress ip;
  95. const char* domain;
  96. uint16_t port;
  97. Stream* stream;
  98. int _state;
  99. public:
  100. PubSubClient(); //!< PubSubClient
  101. explicit PubSubClient(Client& client); //!< PubSubClient
  102. PubSubClient(IPAddress, uint16_t, Client& client); //!< PubSubClient
  103. PubSubClient(IPAddress, uint16_t, Client& client, Stream&); //!< PubSubClient
  104. PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); //!< PubSubClient
  105. PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client,
  106. Stream&); //!< PubSubClient
  107. PubSubClient(uint8_t *, uint16_t, Client& client); //!< PubSubClient
  108. PubSubClient(uint8_t *, uint16_t, Client& client, Stream&); //!< PubSubClient
  109. PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); //!< PubSubClient
  110. PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client,
  111. Stream&); //!< PubSubClient
  112. PubSubClient(const char*, uint16_t, Client& client); //!< PubSubClient
  113. PubSubClient(const char*, uint16_t, Client& client, Stream&); //!< PubSubClient
  114. PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client); //!< PubSubClient
  115. PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client,
  116. Stream&); //!< PubSubClient
  117. PubSubClient& setServer(IPAddress ip, uint16_t port); //!< setServer
  118. PubSubClient& setServer(uint8_t * ip, uint16_t port); //!< setServer
  119. PubSubClient& setServer(const char * domain, uint16_t port); //!< setServer
  120. PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE); //!< setCallback
  121. PubSubClient& setClient(Client& client); //!< setClient
  122. PubSubClient& setStream(Stream& stream); //!< setStream
  123. bool connect(const char* id); //!< connect
  124. bool connect(const char* id, const char* user, const char* pass); //!< connect
  125. bool connect(const char* id, const char* willTopic, uint8_t willQos, bool willRetain,
  126. const char* willMessage); //!< connect
  127. bool connect(const char* id, const char* user, const char* pass, const char* willTopic,
  128. uint8_t willQos, bool willRetain, const char* willMessage); //!< connect
  129. bool connect(const char* id, const char* user, const char* pass, const char* willTopic,
  130. uint8_t willQos, bool willRetain, const char* willMessage, bool cleanSession); //!< connect
  131. void disconnect(); //!< disconnect
  132. bool publish(const char* topic, const char* payload); //!< publish
  133. bool publish(const char* topic, const char* payload, bool retained); //!< publish
  134. bool publish(const char* topic, const uint8_t * payload, unsigned int plength); //!< publish
  135. bool publish(const char* topic, const uint8_t * payload, unsigned int plength,
  136. bool retained); //!< publish
  137. bool publish_P(const char* topic, const char* payload, bool retained); //!< publish
  138. bool publish_P(const char* topic, const uint8_t * payload, unsigned int plength,
  139. bool retained); //!< publish
  140. // Start to publish a message.
  141. // This API:
  142. // beginPublish(...)
  143. // one or more calls to write(...)
  144. // endPublish()
  145. // Allows for arbitrarily large payloads to be sent without them having to be copied into
  146. // a new buffer and held in memory at one time
  147. // Returns 1 if the message was started successfully, 0 if there was an error
  148. bool beginPublish(const char* topic, unsigned int plength, bool retained); //!< beginPublish
  149. // Finish off this publish message (started with beginPublish)
  150. // Returns 1 if the packet was sent successfully, 0 if there was an error
  151. int endPublish(); //!< endPublish
  152. // Write a single byte of payload (only to be used with beginPublish/endPublish)
  153. virtual size_t write(uint8_t); //!< write
  154. // Write size bytes from buffer into the payload (only to be used with beginPublish/endPublish)
  155. // Returns the number of bytes written
  156. virtual size_t write(const uint8_t *buffer, size_t size); //!< write
  157. bool subscribe(const char* topic); //!< subscribe
  158. bool subscribe(const char* topic, uint8_t qos); //!< subscribe
  159. bool unsubscribe(const char* topic); //!< unsubscribe
  160. bool loop(); //!< loop
  161. bool connected(); //!< connected
  162. int state(); //!< state
  163. };
  164. #endif