[209] | 1 | /*
|
---|
| 2 | PubSubClient.h - A simple client for MQTT.
|
---|
| 3 | Nick O'Leary
|
---|
| 4 | http://knolleary.net
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | #ifndef PubSubClient_h
|
---|
| 8 | #define PubSubClient_h
|
---|
| 9 |
|
---|
| 10 | #include <Arduino.h>
|
---|
| 11 | #include "IPAddress.h"
|
---|
| 12 | #include "Client.h"
|
---|
| 13 | #include "Stream.h"
|
---|
| 14 |
|
---|
| 15 | #define MQTT_VERSION_3_1 3
|
---|
| 16 | #define MQTT_VERSION_3_1_1 4
|
---|
| 17 |
|
---|
| 18 | // MQTT_VERSION : Pick the version
|
---|
| 19 | //#define MQTT_VERSION MQTT_VERSION_3_1
|
---|
| 20 | #ifndef MQTT_VERSION
|
---|
| 21 | #define MQTT_VERSION MQTT_VERSION_3_1_1
|
---|
| 22 | #endif
|
---|
| 23 |
|
---|
| 24 | // MQTT_MAX_PACKET_SIZE : Maximum packet size
|
---|
| 25 | #ifndef MQTT_MAX_PACKET_SIZE
|
---|
| 26 | #define MQTT_MAX_PACKET_SIZE 128
|
---|
| 27 | #endif
|
---|
| 28 |
|
---|
| 29 | // MQTT_KEEPALIVE : keepAlive interval in Seconds
|
---|
| 30 | #ifndef MQTT_KEEPALIVE
|
---|
| 31 | #define MQTT_KEEPALIVE 15
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
| 34 | // MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds
|
---|
| 35 | #ifndef MQTT_SOCKET_TIMEOUT
|
---|
| 36 | #define MQTT_SOCKET_TIMEOUT 15
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
| 39 | // MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
|
---|
| 40 | // in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
|
---|
| 41 | // pass the entire MQTT packet in each write call.
|
---|
| 42 | //#define MQTT_MAX_TRANSFER_SIZE 80
|
---|
| 43 |
|
---|
| 44 | // Possible values for client.state()
|
---|
| 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
|
---|
| 55 |
|
---|
| 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
|
---|
| 71 |
|
---|
| 72 | #define MQTTQOS0 (0 << 1)
|
---|
| 73 | #define MQTTQOS1 (1 << 1)
|
---|
| 74 | #define MQTTQOS2 (2 << 1)
|
---|
| 75 |
|
---|
| 76 | #ifdef ESP8266
|
---|
| 77 | #include <functional>
|
---|
| 78 | #define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
|
---|
| 79 | #else
|
---|
| 80 | #define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
|
---|
| 81 | #endif
|
---|
| 82 |
|
---|
| 83 | class PubSubClient {
|
---|
| 84 | private:
|
---|
| 85 | Client* _client;
|
---|
| 86 | uint8_t buffer[MQTT_MAX_PACKET_SIZE];
|
---|
| 87 | uint16_t nextMsgId;
|
---|
| 88 | unsigned long lastOutActivity;
|
---|
| 89 | unsigned long lastInActivity;
|
---|
| 90 | bool pingOutstanding;
|
---|
| 91 | MQTT_CALLBACK_SIGNATURE;
|
---|
| 92 | uint16_t readPacket(uint8_t*);
|
---|
| 93 | boolean readByte(uint8_t * result);
|
---|
| 94 | boolean readByte(uint8_t * result, uint16_t * index);
|
---|
| 95 | boolean write(uint8_t header, uint8_t* buf, uint16_t length);
|
---|
| 96 | uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
|
---|
| 97 | IPAddress ip;
|
---|
| 98 | const char* domain;
|
---|
| 99 | uint16_t port;
|
---|
| 100 | Stream* stream;
|
---|
| 101 | int _state;
|
---|
| 102 | public:
|
---|
| 103 | PubSubClient();
|
---|
| 104 | PubSubClient(Client& client);
|
---|
| 105 | PubSubClient(IPAddress, uint16_t, Client& client);
|
---|
| 106 | PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
|
---|
| 107 | PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
---|
| 108 | PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
---|
| 109 | PubSubClient(uint8_t *, uint16_t, Client& client);
|
---|
| 110 | PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
|
---|
| 111 | PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
---|
| 112 | PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
---|
| 113 | PubSubClient(const char*, uint16_t, Client& client);
|
---|
| 114 | PubSubClient(const char*, uint16_t, Client& client, Stream&);
|
---|
| 115 | PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
---|
| 116 | PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
---|
| 117 |
|
---|
| 118 | PubSubClient& setServer(IPAddress ip, uint16_t port);
|
---|
| 119 | PubSubClient& setServer(uint8_t * ip, uint16_t port);
|
---|
| 120 | PubSubClient& setServer(const char * domain, uint16_t port);
|
---|
| 121 | PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
|
---|
| 122 | PubSubClient& setClient(Client& client);
|
---|
| 123 | PubSubClient& setStream(Stream& stream);
|
---|
| 124 |
|
---|
| 125 | boolean connect(const char* id);
|
---|
| 126 | boolean connect(const char* id, const char* user, const char* pass);
|
---|
| 127 | boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
|
---|
| 128 | boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
|
---|
| 129 | void disconnect();
|
---|
| 130 | boolean publish(const char* topic, const char* payload);
|
---|
| 131 | boolean publish(const char* topic, const char* payload, boolean retained);
|
---|
| 132 | boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
|
---|
| 133 | boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
---|
| 134 | boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
---|
| 135 | boolean subscribe(const char* topic);
|
---|
| 136 | boolean subscribe(const char* topic, uint8_t qos);
|
---|
| 137 | boolean unsubscribe(const char* topic);
|
---|
| 138 | boolean loop();
|
---|
| 139 | boolean connected();
|
---|
| 140 | int state();
|
---|
| 141 | };
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | #endif
|
---|