2015-06-11 00:38:34 +02:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Adafruit Industries
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
2015-06-01 00:38:33 +02:00
|
|
|
#ifndef _ADAFRUIT_MQTT_H_
|
|
|
|
#define _ADAFRUIT_MQTT_H_
|
|
|
|
|
|
|
|
#if ARDUINO >= 100
|
|
|
|
#include "Arduino.h"
|
|
|
|
#else
|
|
|
|
#include "WProgram.h"
|
|
|
|
#endif
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
// Uncomment/comment to turn on/off debug output messages.
|
|
|
|
//#define MQTT_DEBUG
|
|
|
|
|
|
|
|
// Set where debug messages will be printed.
|
|
|
|
#define DEBUG_PRINTER Serial
|
|
|
|
|
|
|
|
// Define actual debug output functions when necessary.
|
|
|
|
#ifdef MQTT_DEBUG
|
|
|
|
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
|
|
|
|
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
|
|
|
|
#define DEBUG_PRINTBUFFER(buffer, len) { printBuffer(buffer, len); }
|
|
|
|
#else
|
|
|
|
#define DEBUG_PRINT(...) {}
|
|
|
|
#define DEBUG_PRINTLN(...) {}
|
|
|
|
#define DEBUG_PRINTBUFFER(buffer, len) {}
|
|
|
|
#endif
|
|
|
|
|
2015-06-01 00:38:33 +02:00
|
|
|
#define MQTT_PROTOCOL_LEVEL 3
|
|
|
|
|
2015-06-02 04:51:59 +02:00
|
|
|
#define MQTT_CTRL_CONNECT 0x01
|
|
|
|
#define MQTT_CTRL_CONNECTACK 0x02
|
|
|
|
#define MQTT_CTRL_PUBLISH 0x03
|
2015-06-03 00:33:03 +02:00
|
|
|
#define MQTT_CTRL_SUBSCRIBE 0x08
|
|
|
|
#define MQTT_CTRL_SUBACK 0x09
|
2015-06-02 04:51:59 +02:00
|
|
|
#define MQTT_CTRL_PINGREQ 0x0C
|
|
|
|
#define MQTT_CTRL_PINGRESP 0x0D
|
2015-06-01 05:19:13 +02:00
|
|
|
|
|
|
|
#define MQTT_QOS_1 0x1
|
|
|
|
#define MQTT_QOS_0 0x0
|
2015-06-01 00:38:33 +02:00
|
|
|
|
|
|
|
#define CONNECT_TIMEOUT_MS 3000
|
2015-06-01 05:19:13 +02:00
|
|
|
#define PUBLISH_TIMEOUT_MS 500
|
2015-06-02 04:51:59 +02:00
|
|
|
#define PING_TIMEOUT_MS 500
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2015-06-12 21:59:14 +02:00
|
|
|
// Adjust as necessary, in seconds. Default to 5 minutes.
|
|
|
|
#define MQTT_CONN_KEEPALIVE 300
|
2015-06-05 22:10:22 +02:00
|
|
|
|
|
|
|
// Largest full packet we're able to send.
|
2015-07-05 23:29:13 +02:00
|
|
|
// Need to be able to store at least ~90 chars for a connect packet with full
|
2015-06-05 22:10:22 +02:00
|
|
|
// 23 char client ID.
|
2015-07-06 22:43:52 +02:00
|
|
|
#define MAXBUFFERSIZE (125)
|
2015-06-05 18:09:58 +02:00
|
|
|
|
2015-06-01 00:38:33 +02:00
|
|
|
#define MQTT_CONN_USERNAMEFLAG 0x80
|
|
|
|
#define MQTT_CONN_PASSWORDFLAG 0x40
|
|
|
|
#define MQTT_CONN_WILLRETAIN 0x20
|
|
|
|
#define MQTT_CONN_WILLQOS 0x08
|
|
|
|
#define MQTT_CONN_WILLFLAG 0x04
|
|
|
|
#define MQTT_CONN_CLEANSESSION 0x02
|
|
|
|
|
2015-06-05 18:09:58 +02:00
|
|
|
// how many subscriptions we want to be able to
|
|
|
|
// track
|
2015-06-03 00:33:03 +02:00
|
|
|
#define MAXSUBSCRIPTIONS 5
|
2015-06-05 18:09:58 +02:00
|
|
|
|
|
|
|
// how much data we save in a subscription object
|
|
|
|
// eg max-subscription-payload-size
|
2015-06-03 00:33:03 +02:00
|
|
|
#define SUBSCRIPTIONDATALEN 20
|
|
|
|
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
extern void printBuffer(uint8_t *buffer, uint8_t len);
|
2015-06-03 00:33:03 +02:00
|
|
|
|
|
|
|
class Adafruit_MQTT_Subscribe; // forward decl
|
2015-06-01 00:38:33 +02:00
|
|
|
|
|
|
|
class Adafruit_MQTT {
|
|
|
|
public:
|
2015-07-06 22:43:52 +02:00
|
|
|
Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
|
2015-06-08 10:48:25 +02:00
|
|
|
const char *user, const char *pass);
|
2015-06-05 06:16:08 +02:00
|
|
|
virtual ~Adafruit_MQTT() {}
|
|
|
|
|
|
|
|
// Connect to the MQTT server. Returns 0 on success, otherwise an error code
|
|
|
|
// that indicates something went wrong:
|
|
|
|
// -1 = Error connecting to server
|
|
|
|
// 1 = Wrong protocol
|
|
|
|
// 2 = ID rejected
|
|
|
|
// 3 = Server unavailable
|
|
|
|
// 4 = Bad username or password
|
|
|
|
// 5 = Not authenticated
|
|
|
|
// 6 = Failed to subscribe
|
|
|
|
int8_t connect();
|
|
|
|
|
|
|
|
// Disconnect from the MQTT server. Returns true if disconnected, false
|
|
|
|
// otherwise.
|
|
|
|
virtual bool disconnect() = 0; // Subclasses need to fill this in!
|
|
|
|
|
2015-07-05 23:29:13 +02:00
|
|
|
// Return true if connected to the MQTT server, otherwise false.
|
|
|
|
virtual bool connected() = 0; // Subclasses need to fill this in!
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
// Publish a message to a topic using the specified QoS level. Returns true
|
|
|
|
// if the message was published, false otherwise.
|
|
|
|
bool publish(const char *topic, char *payload, uint8_t qos);
|
2015-07-06 22:43:52 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
// Add a subscription to receive messages for a topic. Returns true if the
|
|
|
|
// subscription could be added, false otherwise.
|
|
|
|
bool subscribe(Adafruit_MQTT_Subscribe *sub);
|
|
|
|
|
|
|
|
// Check if any subscriptions have new messages. Will return a reference to
|
|
|
|
// an Adafruit_MQTT_Subscribe object which has a new message. Should be called
|
|
|
|
// in the sketch's loop function to ensure new messages are recevied. Note
|
|
|
|
// that subscribe should be called first for each topic that receives messages!
|
|
|
|
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout=0);
|
|
|
|
|
|
|
|
// Ping the server to ensure the connection is still alive. Returns true if
|
|
|
|
// successful, otherwise false.
|
|
|
|
bool ping(uint8_t t);
|
2015-06-02 21:23:42 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
protected:
|
|
|
|
// Interface that subclasses need to implement:
|
2015-06-01 05:19:13 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
// Connect to the server and return true if successful, false otherwise.
|
|
|
|
virtual bool connectServer() = 0;
|
2015-06-01 05:56:27 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
// Send data to the server specified by the buffer and length of data.
|
|
|
|
virtual bool sendPacket(uint8_t *buffer, uint8_t len) = 0;
|
2015-06-03 00:33:03 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
// Read MQTT packet from the server. Will read up to maxlen bytes and store
|
|
|
|
// the data in the provided buffer. Waits up to the specified timeout (in
|
|
|
|
// milliseconds) for data to be available. If checkForValidPubPacket is true
|
|
|
|
// then the received data is verified to make sure it's a complete packet.
|
|
|
|
virtual uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
|
|
|
|
bool checkForValidPubPacket = false) = 0;
|
2015-06-03 00:33:03 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
// Shared state that subclasses can use:
|
2015-06-02 21:23:42 +02:00
|
|
|
const char *servername;
|
2015-06-01 00:38:33 +02:00
|
|
|
int16_t portnum;
|
2015-06-02 21:23:42 +02:00
|
|
|
const char *clientid;
|
|
|
|
const char *username;
|
|
|
|
const char *password;
|
2015-06-05 22:10:22 +02:00
|
|
|
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
private:
|
2015-06-03 00:33:03 +02:00
|
|
|
Adafruit_MQTT_Subscribe *subscriptions[MAXSUBSCRIPTIONS];
|
2015-06-05 06:16:08 +02:00
|
|
|
|
|
|
|
// Functions to generate MQTT packets.
|
|
|
|
uint8_t connectPacket(uint8_t *packet);
|
|
|
|
uint8_t publishPacket(uint8_t *packet, const char *topic, char *payload, uint8_t qos);
|
|
|
|
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
|
|
|
|
uint8_t pingPacket(uint8_t *packet);
|
2015-06-01 00:38:33 +02:00
|
|
|
};
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
|
2015-06-01 00:38:33 +02:00
|
|
|
class Adafruit_MQTT_Publish {
|
|
|
|
public:
|
2015-06-02 21:23:42 +02:00
|
|
|
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0);
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2015-06-02 21:23:42 +02:00
|
|
|
bool publish(char *s);
|
2015-06-11 00:38:34 +02:00
|
|
|
bool publish(double f, uint8_t precision=2); // Precision controls the minimum number of digits after decimal.
|
|
|
|
// This might be ignored and a higher precision value sent.
|
2015-06-01 00:38:33 +02:00
|
|
|
bool publish(int32_t i);
|
2015-06-01 05:19:13 +02:00
|
|
|
bool publish(uint32_t i);
|
2015-07-06 22:43:52 +02:00
|
|
|
|
2015-06-01 00:38:33 +02:00
|
|
|
private:
|
|
|
|
Adafruit_MQTT *mqtt;
|
2015-06-02 21:23:42 +02:00
|
|
|
const char *topic;
|
2015-06-01 05:19:13 +02:00
|
|
|
uint8_t qos;
|
2015-06-01 00:38:33 +02:00
|
|
|
};
|
|
|
|
|
2015-06-02 21:23:42 +02:00
|
|
|
class Adafruit_MQTT_Subscribe {
|
2015-06-01 00:38:33 +02:00
|
|
|
public:
|
2015-06-03 00:33:03 +02:00
|
|
|
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname, uint8_t q=0);
|
2015-06-02 21:23:42 +02:00
|
|
|
|
2015-06-01 00:38:33 +02:00
|
|
|
bool setCallback(void (*callback)(char *));
|
|
|
|
|
2015-06-03 00:33:03 +02:00
|
|
|
const char *topic;
|
|
|
|
uint8_t qos;
|
|
|
|
|
|
|
|
uint8_t * lastread[SUBSCRIPTIONDATALEN];
|
|
|
|
private:
|
|
|
|
Adafruit_MQTT *mqtt;
|
2015-06-01 00:38:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
#endif
|