Initial port of the Arduino MQTT Client: http://knolleary.net/arduino-client-for-mqtt/ Updated with larger timeout and buffer sizes

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Fork of MQTT by Joerg Wende

Committer:
ansond
Date:
Mon Mar 31 18:47:05 2014 +0000
Revision:
5:989d4fba3bf6
Parent:
4:3e73a2546953
Child:
6:f8294e112608
updates

Who changed what in which revision?

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