Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MQTTClient.h
00001 /* 00002 MQTTClient.h - A simple client for MQTT. 00003 Nicholas O'Leary 00004 http://knolleary.net 00005 00006 Ported to mbed by Zoltan Hudak <hudakz@outlook.com> 00007 */ 00008 #ifndef MQTTClient_h 00009 #define MQTTClient_h 00010 00011 #include "mbed.h" 00012 #include "TcpClient.h" 00013 00014 #define MQTT_MAX_PACKET_SIZE 256 // MQTT_MAX_PACKET_SIZE : Maximum packet size 00015 #define MQTT_KEEPALIVE 15 // MQTT_KEEPALIVE : keepAlive interval in Seconds 00016 #define MQTTPROTOCOLVERSION 3 00017 #define MQTTCONNECT 1 << 4 // Client request to connect to Server 00018 #define MQTTCONNACK 2 << 4 // Connect Acknowledgment 00019 #define MQTTPUBLISH 3 << 4 // Publish message 00020 #define MQTTPUBACK 4 << 4 // Publish Acknowledgment 00021 #define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1) 00022 #define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2) 00023 #define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3) 00024 #define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request 00025 #define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment 00026 #define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request 00027 #define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment 00028 #define MQTTPINGREQ 12 << 4 // PING Request 00029 #define MQTTPINGRESP 13 << 4 // PING Response 00030 #define MQTTDISCONNECT 14 << 4 // Client is Disconnecting 00031 #define MQTTReserved 15 << 4 // Reserved 00032 #define MQTTQOS0 0 << 1 00033 #define MQTTQOS1 1 << 1 00034 #define MQTTQOS2 2 << 1 00035 00036 class MQTTClient 00037 { 00038 TcpClient _client; 00039 uint8_t _buffer[MQTT_MAX_PACKET_SIZE]; 00040 uint16_t _nextMsgId; 00041 time_t _lastOutActivity; 00042 time_t _lastInActivity; 00043 bool _pingOutstanding; 00044 IpAddress _ip; 00045 char* _domain; 00046 uint16_t _port; 00047 Stream* _stream; 00048 00049 uint16_t _readPacket(uint8_t*); 00050 uint8_t _readByte(); 00051 bool _write(uint8_t header, uint8_t* buf, uint16_t length); 00052 uint16_t _writeString(const char* string, uint8_t* buf, uint16_t length); 00053 00054 Callback<void (char*, uint8_t*, uint16_t)> _onMessage; 00055 public: 00056 MQTTClient(); 00057 MQTTClient(IpAddress& ip, uint16_t port); 00058 MQTTClient(IpAddress& ip, uint16_t port , Stream& stream); 00059 MQTTClient(const char* domain, uint16_t port); 00060 MQTTClient(const char* domain, uint16_t port, Stream& stream); 00061 bool connect(const char*); 00062 bool connect(const char* , const char* , const char*); 00063 bool connect(const char* , const char* , uint8_t, uint8_t, const char*); 00064 bool connect(const char* , const char* , const char* , const char* , uint8_t, uint8_t, const char*); 00065 void disconnect(); 00066 bool publish(const char* , const char*); 00067 bool publish(const char* , uint8_t* , uint16_t); 00068 bool publish(const char* , uint8_t* , uint16_t, bool); 00069 bool subscribe(const char*); 00070 bool subscribe(const char* , uint8_t qos); 00071 bool unsubscribe(const char*); 00072 bool poll(); 00073 bool connected(); 00074 void attach(Callback<void (char*, uint8_t*, uint16_t)>); 00075 template<typename T, typename M> 00076 void attach(T *obj, M method ) { attach(callback(obj, method)); } 00077 }; 00078 #endif
Generated on Sat Jul 16 2022 08:45:21 by
1.7.2