Adafruit_MQTT_Library/Adafruit_MQTT.h

118 lines
2.7 KiB
C
Raw Normal View History

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
#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 SERVERNAME_SIZE 25
2015-06-02 04:51:59 +02:00
#define PASSWORD_SIZE 25
#define USERNAME_SIZE 41
2015-06-01 03:07:17 +02:00
#define CLIENTID_SIZE 23
2015-06-01 00:38:33 +02:00
#define FEEDNAME_SIZE 40
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
#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
#define MQTT_CONN_KEEPALIVE 15 // in seconds
2015-06-03 00:33:03 +02:00
#define MAXBUFFERSIZE (85)
#define MAXSUBSCRIPTIONS 5
#define SUBSCRIPTIONDATALEN 20
//#define DEBUG_MQTT_CONNECT
//#define DEBUG_MQTT_SUBSCRIBE
//#define DEBUG_MQTT_READSUB
//#define DEBUG_MQTT_PUBLISH
//#define DEBUG_MQTT_PACKETREAD
class Adafruit_MQTT_Subscribe; // forward decl
2015-06-01 00:38:33 +02:00
class Adafruit_MQTT {
public:
Adafruit_MQTT(const char *server, uint16_t port, const PROGMEM char *cid, const PROGMEM char *user, const PROGMEM char *pass);
2015-06-01 00:38:33 +02:00
uint8_t connectPacket(uint8_t *packet);
2015-06-01 05:19:13 +02:00
virtual boolean publish(const char *topic, char *payload, uint8_t qos) {}
uint8_t publishPacket(uint8_t *packet, const char *topic, char *payload, uint8_t qos);
2015-06-01 05:19:13 +02:00
virtual boolean ping(uint8_t t) {}
2015-06-02 04:51:59 +02:00
uint8_t pingPacket(uint8_t *packet);
2015-06-01 05:56:27 +02:00
2015-06-03 00:33:03 +02:00
virtual boolean subscribe(Adafruit_MQTT_Subscribe *sub) {}
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
virtual Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout = 0) {};
2015-06-01 00:38:33 +02:00
protected:
int8_t errno;
const char *servername;
2015-06-01 00:38:33 +02:00
uint32_t serverip;
int16_t portnum;
const char *clientid;
const char *username;
const char *password;
2015-06-01 00:38:33 +02:00
2015-06-03 00:33:03 +02:00
Adafruit_MQTT_Subscribe *subscriptions[MAXSUBSCRIPTIONS];
2015-06-01 00:38:33 +02:00
uint8_t buffer[MAXBUFFERSIZE];
};
class Adafruit_MQTT_Publish {
public:
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0);
2015-06-01 00:38:33 +02:00
bool publish(char *s);
2015-06-01 05:19:13 +02:00
//bool publish(double f);
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-06-01 00:38:33 +02:00
private:
Adafruit_MQTT *mqtt;
const char *topic;
2015-06-01 05:19:13 +02:00
uint8_t qos;
2015-06-01 00:38:33 +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-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
};
#endif /* header guard */