MQTT Client for ENC28J60 Ethernet modules. Depends on the UIPEthernet library.

Dependents:   mBuino_ENC28_MQTT MQTT_DHT11_ENC28J60 MQTT_DHT11_ENC28J60_tushar MQTT_Hello_ENC28J60

MQTT library for ENC28J60 Ethernet modules.

/media/uploads/hudakz/enc28j60_module01.jpg

Depends on the UIPEthernet library.

Example program:

Import programMQTT_Hello_ENC28J60

MQTT Client example program. Ethernet connection is via an ENC28J60 module.

Revision:
3:5e31b4687aad
Parent:
2:ca17c5d846e7
Child:
4:8620de6d1696
--- a/MQTTClient.h	Sun Nov 29 18:12:22 2015 +0000
+++ b/MQTTClient.h	Tue Sep 03 13:41:16 2019 +0000
@@ -3,15 +3,13 @@
   Nicholas O'Leary
   http://knolleary.net
 
-  Ported to mbed by Zoltan Hudak <hudakz@inbox.com>
+  Ported to mbed by Zoltan Hudak <hudakz@outlook.com>
 */
 #ifndef MQTTClient_h
 #define MQTTClient_h
 
-#include <mbed.h>
-#include "Client.h"
-//#include "Stream.h"
-
+#include "mbed.h"
+#include "TcpClient.h"
 
 #define MQTT_MAX_PACKET_SIZE   256  // MQTT_MAX_PACKET_SIZE : Maximum packet size
 #define MQTT_KEEPALIVE          15  // MQTT_KEEPALIVE : keepAlive interval in Seconds
@@ -25,51 +23,51 @@
 #define MQTTPUBCOMP         7 << 4  // Publish Complete (assured delivery part 3)
 #define MQTTSUBSCRIBE       8 << 4  // Client Subscribe request
 #define MQTTSUBACK          9 << 4  // Subscribe Acknowledgment
-#define MQTTUNSUBSCRIBE     10 << 4 // Client Unsubscribe request
-#define MQTTUNSUBACK        11 << 4 // Unsubscribe Acknowledgment
-#define MQTTPINGREQ         12 << 4 // PING Request
-#define MQTTPINGRESP        13 << 4 // PING Response
-#define MQTTDISCONNECT      14 << 4 // Client is Disconnecting
-#define MQTTReserved        15 << 4 // Reserved
-#define MQTTQOS0            (0 << 1)
-#define MQTTQOS1            (1 << 1)
-#define MQTTQOS2            (2 << 1)
+#define MQTTUNSUBSCRIBE     10<< 4  // Client Unsubscribe request
+#define MQTTUNSUBACK        11<< 4  // Unsubscribe Acknowledgment
+#define MQTTPINGREQ         12<< 4  // PING Request
+#define MQTTPINGRESP        13<< 4  // PING Response
+#define MQTTDISCONNECT      14<< 4  // Client is Disconnecting
+#define MQTTReserved        15<< 4  // Reserved
+#define MQTTQOS0            (0<< 1)
+#define MQTTQOS1            (1<< 1)
+#define MQTTQOS2            (2<< 1)
 
 class   MQTTClient
 {
-private:
-    Client*         _client;
-    uint8_t         buffer[MQTT_MAX_PACKET_SIZE];
-    uint16_t        nextMsgId;
-    unsigned long   lastOutActivity;
-    unsigned long   lastInActivity;
-    bool            pingOutstanding;
-    void (*onMessage) (char*, uint8_t*, unsigned int);
-    uint16_t    readPacket(uint8_t* );
-    uint8_t     readByte(void);
-    bool        write(uint8_t header, uint8_t* buf, uint16_t length);
-    uint16_t    writeString(char* string, uint8_t* buf, uint16_t pos);
-    IPAddress   ip;
-    char*       domain;
-    uint16_t    port;
-    Stream*     stream;
+    TcpClient*      _client;
+    uint8_t         _buffer[MQTT_MAX_PACKET_SIZE];
+    uint16_t        _nextMsgId;
+    unsigned long   _lastOutActivity;
+    unsigned long   _lastInActivity;
+    bool            _pingOutstanding;
+    IpAddress       _ip;
+    char*           _domain;
+    uint16_t        _port;
+    Stream*         _stream;
+
+    void            (*_onMessage) (char*, uint8_t*, unsigned int);
+    uint16_t        _readPacket(uint8_t* );
+    uint8_t         _readByte(void);
+    bool            _write(uint8_t header, uint8_t* buf, uint16_t length);
+    uint16_t        _writeString(const char* string, uint8_t* buf, uint16_t pos);
 public:
     MQTTClient(void);
-    MQTTClient(IPAddress& , uint16_t, void (* ) (char*, uint8_t*, unsigned int), Client& client);
-    MQTTClient(IPAddress& , uint16_t, void (* ) (char*, uint8_t*, unsigned int), Client& client, Stream& );
-    MQTTClient(char* , uint16_t, void (* ) (char*, uint8_t*, unsigned int), Client& client);
-    MQTTClient(char* , uint16_t, void (* ) (char*, uint8_t*, unsigned int), Client& client, Stream& );
-    bool    connect(char* );
-    bool    connect(char* , char* , char* );
-    bool    connect(char* , char* , uint8_t, uint8_t, char* );
-    bool    connect(char* , char* , char* , char* , uint8_t, uint8_t, char* );
+    MQTTClient(IpAddress& , uint16_t, void (* ) (char*, uint8_t*, unsigned int), TcpClient& client);
+    MQTTClient(IpAddress& , uint16_t, void (* ) (char*, uint8_t*, unsigned int), TcpClient& client, Stream& );
+    MQTTClient(const char* , uint16_t, void (* ) (char*, uint8_t*, unsigned int), TcpClient& client);
+    MQTTClient(const char* , uint16_t, void (* ) (char*, uint8_t*, unsigned int), TcpClient& client, Stream& );
+    bool    connect(const char* );
+    bool    connect(const char* , const char* , const char* );
+    bool    connect(const char* , const char* , uint8_t, uint8_t, const char* );
+    bool    connect(const char* , const char* , const char* , const char* , uint8_t, uint8_t, const char* );
     void    disconnect(void);
-    bool    publish(char* , char* );
-    bool    publish(char* , uint8_t* , unsigned int);
-    bool    publish(char* , uint8_t* , unsigned int, bool);
-    bool    subscribe(char* );
-    bool    subscribe(char* , uint8_t qos);
-    bool    unsubscribe(char* );
+    bool    publish(const char* , const char* );
+    bool    publish(const char* , uint8_t* , unsigned int);
+    bool    publish(const char* , uint8_t* , unsigned int, bool);
+    bool    subscribe(const char* );
+    bool    subscribe(const char* , uint8_t qos);
+    bool    unsubscribe(const char* );
     bool    loop(void);
     bool    connected(void);
 };