MQTT client for mbed Port of the Arduino MQTT Client: http://knolleary.net/arduino-client-for-mqtt/

Committer:
dwijaybane
Date:
Thu Feb 04 06:46:15 2016 +0000
Revision:
1:05fe2e37b93d
Parent:
0:c492c0206878
keep alive timeout increased

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dwijaybane 0:c492c0206878 1 /*
dwijaybane 0:c492c0206878 2 PubSubClient.h - A simple client for MQTT.
dwijaybane 0:c492c0206878 3 Nicholas O'Leary Port
dwijaybane 0:c492c0206878 4 http://knolleary.net
dwijaybane 0:c492c0206878 5
dwijaybane 0:c492c0206878 6 Edutech IoT Team
dwijaybane 0:c492c0206878 7 */
dwijaybane 0:c492c0206878 8 #include "mbed.h"
dwijaybane 0:c492c0206878 9 #include "EthernetInterface.h"
dwijaybane 0:c492c0206878 10 #include "TCPSocketConnection.h"
dwijaybane 0:c492c0206878 11
dwijaybane 0:c492c0206878 12 #ifndef PubSubClient_h
dwijaybane 0:c492c0206878 13 #define PubSubClient_h
dwijaybane 0:c492c0206878 14
dwijaybane 0:c492c0206878 15
dwijaybane 0:c492c0206878 16
dwijaybane 0:c492c0206878 17 // MQTT_MAX_PACKET_SIZE : Maximum packet size
dwijaybane 0:c492c0206878 18 #define MQTT_MAX_PACKET_SIZE 128
dwijaybane 0:c492c0206878 19
dwijaybane 0:c492c0206878 20 // MQTT_KEEPALIVE : keepAlive interval in Seconds
dwijaybane 1:05fe2e37b93d 21 #define MQTT_KEEPALIVE 15000
dwijaybane 0:c492c0206878 22
dwijaybane 0:c492c0206878 23 #define MQTTPROTOCOLVERSION 3
dwijaybane 0:c492c0206878 24 #define MQTTCONNECT 1 << 4 // Client request to connect to Server
dwijaybane 0:c492c0206878 25 #define MQTTCONNACK 2 << 4 // Connect Acknowledgment
dwijaybane 0:c492c0206878 26 #define MQTTPUBLISH 3 << 4 // Publish message
dwijaybane 0:c492c0206878 27 #define MQTTPUBACK 4 << 4 // Publish Acknowledgment
dwijaybane 0:c492c0206878 28 #define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
dwijaybane 0:c492c0206878 29 #define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
dwijaybane 0:c492c0206878 30 #define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
dwijaybane 0:c492c0206878 31 #define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
dwijaybane 0:c492c0206878 32 #define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
dwijaybane 0:c492c0206878 33 #define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
dwijaybane 0:c492c0206878 34 #define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
dwijaybane 0:c492c0206878 35 #define MQTTPINGREQ 12 << 4 // PING Request
dwijaybane 0:c492c0206878 36 #define MQTTPINGRESP 13 << 4 // PING Response
dwijaybane 0:c492c0206878 37 #define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
dwijaybane 0:c492c0206878 38 #define MQTTReserved 15 << 4 // Reserved
dwijaybane 0:c492c0206878 39
dwijaybane 0:c492c0206878 40 #define MQTTQOS0 (0 << 1)
dwijaybane 0:c492c0206878 41 #define MQTTQOS1 (1 << 1)
dwijaybane 0:c492c0206878 42 #define MQTTQOS2 (2 << 1)
dwijaybane 0:c492c0206878 43
dwijaybane 0:c492c0206878 44 class PubSubClient {
dwijaybane 0:c492c0206878 45 private:
dwijaybane 0:c492c0206878 46 Timer t;
dwijaybane 0:c492c0206878 47 TCPSocketConnection _client;
dwijaybane 0:c492c0206878 48 char buffer[MQTT_MAX_PACKET_SIZE];
dwijaybane 0:c492c0206878 49 int nextMsgId;
dwijaybane 0:c492c0206878 50 unsigned long lastOutActivity;
dwijaybane 0:c492c0206878 51 unsigned long lastInActivity;
dwijaybane 0:c492c0206878 52 bool pingOutstanding;
dwijaybane 0:c492c0206878 53 void (*callback)(char*,char*,unsigned int);
dwijaybane 0:c492c0206878 54 int millis();
dwijaybane 0:c492c0206878 55 int readPacket(int);
dwijaybane 0:c492c0206878 56 char readByte();
dwijaybane 0:c492c0206878 57 bool write(short header, char* buf, int length);
dwijaybane 0:c492c0206878 58 int writeString(char* string, char* buf, int pos);
dwijaybane 0:c492c0206878 59 char* ip;
dwijaybane 0:c492c0206878 60 int port;
dwijaybane 0:c492c0206878 61 public:
dwijaybane 0:c492c0206878 62 PubSubClient();
dwijaybane 0:c492c0206878 63 PubSubClient(char*, int, void(*)(char*,char*,unsigned int));
dwijaybane 0:c492c0206878 64 bool connect(char *);
dwijaybane 0:c492c0206878 65 bool connect(char *, char *, char *);
dwijaybane 0:c492c0206878 66 bool connect(char *, char *, short, short, char *);
dwijaybane 0:c492c0206878 67 bool connect(char *, char *, char *, char *, short, short, char*);
dwijaybane 0:c492c0206878 68 void disconnect();
dwijaybane 0:c492c0206878 69 bool publish(char *, char *);
dwijaybane 0:c492c0206878 70 bool publish(char *, char *, unsigned int);
dwijaybane 0:c492c0206878 71 bool publish(char *, char *, unsigned int, bool);
dwijaybane 0:c492c0206878 72 // bool publish_P(char *, short PROGMEM *, unsigned int, bool);
dwijaybane 0:c492c0206878 73 bool subscribe(char *);
dwijaybane 0:c492c0206878 74 bool unsubscribe(char *);
dwijaybane 0:c492c0206878 75 bool loop();
dwijaybane 0:c492c0206878 76 bool connected();
dwijaybane 0:c492c0206878 77 };
dwijaybane 0:c492c0206878 78
dwijaybane 0:c492c0206878 79
dwijaybane 0:c492c0206878 80 #endif