123
MQTTClient.h@3:5e31b4687aad, 2019-09-03 (annotated)
- Committer:
- hudakz
- Date:
- Tue Sep 03 13:41:16 2019 +0000
- Revision:
- 3:5e31b4687aad
- Parent:
- 2:ca17c5d846e7
- Child:
- 4:8620de6d1696
Mbed OS 5 enabled.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:83c732b10e95 | 1 | /* |
hudakz | 0:83c732b10e95 | 2 | MQTTClient.h - A simple client for MQTT. |
hudakz | 0:83c732b10e95 | 3 | Nicholas O'Leary |
hudakz | 0:83c732b10e95 | 4 | http://knolleary.net |
hudakz | 2:ca17c5d846e7 | 5 | |
hudakz | 3:5e31b4687aad | 6 | Ported to mbed by Zoltan Hudak <hudakz@outlook.com> |
hudakz | 0:83c732b10e95 | 7 | */ |
hudakz | 0:83c732b10e95 | 8 | #ifndef MQTTClient_h |
hudakz | 0:83c732b10e95 | 9 | #define MQTTClient_h |
hudakz | 0:83c732b10e95 | 10 | |
hudakz | 3:5e31b4687aad | 11 | #include "mbed.h" |
hudakz | 3:5e31b4687aad | 12 | #include "TcpClient.h" |
hudakz | 0:83c732b10e95 | 13 | |
hudakz | 2:ca17c5d846e7 | 14 | #define MQTT_MAX_PACKET_SIZE 256 // MQTT_MAX_PACKET_SIZE : Maximum packet size |
hudakz | 2:ca17c5d846e7 | 15 | #define MQTT_KEEPALIVE 15 // MQTT_KEEPALIVE : keepAlive interval in Seconds |
hudakz | 2:ca17c5d846e7 | 16 | #define MQTTPROTOCOLVERSION 3 |
hudakz | 2:ca17c5d846e7 | 17 | #define MQTTCONNECT 1 << 4 // Client request to connect to Server |
hudakz | 2:ca17c5d846e7 | 18 | #define MQTTCONNACK 2 << 4 // Connect Acknowledgment |
hudakz | 2:ca17c5d846e7 | 19 | #define MQTTPUBLISH 3 << 4 // Publish message |
hudakz | 2:ca17c5d846e7 | 20 | #define MQTTPUBACK 4 << 4 // Publish Acknowledgment |
hudakz | 2:ca17c5d846e7 | 21 | #define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1) |
hudakz | 2:ca17c5d846e7 | 22 | #define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2) |
hudakz | 2:ca17c5d846e7 | 23 | #define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3) |
hudakz | 2:ca17c5d846e7 | 24 | #define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request |
hudakz | 2:ca17c5d846e7 | 25 | #define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment |
hudakz | 3:5e31b4687aad | 26 | #define MQTTUNSUBSCRIBE 10<< 4 // Client Unsubscribe request |
hudakz | 3:5e31b4687aad | 27 | #define MQTTUNSUBACK 11<< 4 // Unsubscribe Acknowledgment |
hudakz | 3:5e31b4687aad | 28 | #define MQTTPINGREQ 12<< 4 // PING Request |
hudakz | 3:5e31b4687aad | 29 | #define MQTTPINGRESP 13<< 4 // PING Response |
hudakz | 3:5e31b4687aad | 30 | #define MQTTDISCONNECT 14<< 4 // Client is Disconnecting |
hudakz | 3:5e31b4687aad | 31 | #define MQTTReserved 15<< 4 // Reserved |
hudakz | 3:5e31b4687aad | 32 | #define MQTTQOS0 (0<< 1) |
hudakz | 3:5e31b4687aad | 33 | #define MQTTQOS1 (1<< 1) |
hudakz | 3:5e31b4687aad | 34 | #define MQTTQOS2 (2<< 1) |
hudakz | 0:83c732b10e95 | 35 | |
hudakz | 2:ca17c5d846e7 | 36 | class MQTTClient |
hudakz | 0:83c732b10e95 | 37 | { |
hudakz | 3:5e31b4687aad | 38 | TcpClient* _client; |
hudakz | 3:5e31b4687aad | 39 | uint8_t _buffer[MQTT_MAX_PACKET_SIZE]; |
hudakz | 3:5e31b4687aad | 40 | uint16_t _nextMsgId; |
hudakz | 3:5e31b4687aad | 41 | unsigned long _lastOutActivity; |
hudakz | 3:5e31b4687aad | 42 | unsigned long _lastInActivity; |
hudakz | 3:5e31b4687aad | 43 | bool _pingOutstanding; |
hudakz | 3:5e31b4687aad | 44 | IpAddress _ip; |
hudakz | 3:5e31b4687aad | 45 | char* _domain; |
hudakz | 3:5e31b4687aad | 46 | uint16_t _port; |
hudakz | 3:5e31b4687aad | 47 | Stream* _stream; |
hudakz | 3:5e31b4687aad | 48 | |
hudakz | 3:5e31b4687aad | 49 | void (*_onMessage) (char*, uint8_t*, unsigned int); |
hudakz | 3:5e31b4687aad | 50 | uint16_t _readPacket(uint8_t* ); |
hudakz | 3:5e31b4687aad | 51 | uint8_t _readByte(void); |
hudakz | 3:5e31b4687aad | 52 | bool _write(uint8_t header, uint8_t* buf, uint16_t length); |
hudakz | 3:5e31b4687aad | 53 | uint16_t _writeString(const char* string, uint8_t* buf, uint16_t pos); |
hudakz | 0:83c732b10e95 | 54 | public: |
hudakz | 2:ca17c5d846e7 | 55 | MQTTClient(void); |
hudakz | 3:5e31b4687aad | 56 | MQTTClient(IpAddress& , uint16_t, void (* ) (char*, uint8_t*, unsigned int), TcpClient& client); |
hudakz | 3:5e31b4687aad | 57 | MQTTClient(IpAddress& , uint16_t, void (* ) (char*, uint8_t*, unsigned int), TcpClient& client, Stream& ); |
hudakz | 3:5e31b4687aad | 58 | MQTTClient(const char* , uint16_t, void (* ) (char*, uint8_t*, unsigned int), TcpClient& client); |
hudakz | 3:5e31b4687aad | 59 | MQTTClient(const char* , uint16_t, void (* ) (char*, uint8_t*, unsigned int), TcpClient& client, Stream& ); |
hudakz | 3:5e31b4687aad | 60 | bool connect(const char* ); |
hudakz | 3:5e31b4687aad | 61 | bool connect(const char* , const char* , const char* ); |
hudakz | 3:5e31b4687aad | 62 | bool connect(const char* , const char* , uint8_t, uint8_t, const char* ); |
hudakz | 3:5e31b4687aad | 63 | bool connect(const char* , const char* , const char* , const char* , uint8_t, uint8_t, const char* ); |
hudakz | 2:ca17c5d846e7 | 64 | void disconnect(void); |
hudakz | 3:5e31b4687aad | 65 | bool publish(const char* , const char* ); |
hudakz | 3:5e31b4687aad | 66 | bool publish(const char* , uint8_t* , unsigned int); |
hudakz | 3:5e31b4687aad | 67 | bool publish(const char* , uint8_t* , unsigned int, bool); |
hudakz | 3:5e31b4687aad | 68 | bool subscribe(const char* ); |
hudakz | 3:5e31b4687aad | 69 | bool subscribe(const char* , uint8_t qos); |
hudakz | 3:5e31b4687aad | 70 | bool unsubscribe(const char* ); |
hudakz | 2:ca17c5d846e7 | 71 | bool loop(void); |
hudakz | 2:ca17c5d846e7 | 72 | bool connected(void); |
hudakz | 0:83c732b10e95 | 73 | }; |
hudakz | 0:83c732b10e95 | 74 | #endif |