Skip to content

Commit

Permalink
MQTT Resubscribe on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
zipou committed Dec 3, 2018
1 parent d7ae122 commit 2d4fdc4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/MqttLib/src/MqttLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ MQTTClient _client;

MqttLibCallback MqttLib::_callback;
MqttLibErrorCallback MqttLib::_errorCallback;
MqttLibConnectCallback MqttLib::_connectCallback;

char* _username;
char* _password;
char* _clientId;
char* _topicList;

void MqttLib::setErrorCallback(MqttLibErrorCallback callback) {
MqttLib::_errorCallback = callback;
}

void MqttLib::setConnectCallback(MqttLibConnectCallback callback) {
MqttLib::_connectCallback = callback;
}

void MqttLib::setCallback(MqttLibCallback callback) {
MqttLib::_callback = callback;
Expand Down Expand Up @@ -85,6 +90,8 @@ void MqttLib::publish(const char* topic, const char* message) {
_client.publish(topic, message);
}



bool MqttLib::connect() {
int i = 0;
Serial.print("Trying to connect to MQTT");
Expand All @@ -101,6 +108,9 @@ bool MqttLib::connect() {
++i;
}
Serial.println("MQTT connected");
if (MqttLib::_connectCallback != NULL) {
(*MqttLib::_connectCallback)();
}
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/MqttLib/src/MqttLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

typedef void (*MqttLibCallback)(const char* topic, const char* payload);
typedef void (*MqttLibErrorCallback)();
typedef void (*MqttLibConnectCallback)();

class MqttLib {

Expand All @@ -16,8 +17,10 @@ class MqttLib {
void static acknowledge(String payload);
void subscribe(const char* topic);
void setCallback(MqttLibCallback _callback);
void setConnectCallback(MqttLibConnectCallback _connectCallback);
void setErrorCallback(MqttLibErrorCallback _errorCallback);
static MqttLibCallback _callback;
static MqttLibErrorCallback _errorCallback;
static MqttLibConnectCallback _connectCallback;
protected:
};
10 changes: 8 additions & 2 deletions src/EspGw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ void mqttErrorCallback() {
restartEsp();
}

void mqttConnectCallback() {
mqttlib.subscribe(MQTT_TOPIC_IN);
mqttlib.subscribe(MQTT_TOPIC_IN_RAW);
}

void mqttCallback(const char* topic, const char* message) {
if (strcmp(topic, MQTT_TOPIC_IN) == 0) {
StaticJsonBuffer<200> jsonBuffer;
Expand Down Expand Up @@ -137,10 +142,11 @@ void setup() {
Serial.println("ClientId");
Serial.println(clientId);
mqttlib.setErrorCallback(mqttErrorCallback);
mqttlib.setConnectCallback(mqttConnectCallback);
mqttlib.init(MQTT_HOST, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD, clientId);
mqttlib.setCallback(mqttCb);
mqttlib.subscribe(MQTT_TOPIC_IN);
mqttlib.subscribe(MQTT_TOPIC_IN_RAW);
// mqttlib.subscribe(MQTT_TOPIC_IN);
// mqttlib.subscribe(MQTT_TOPIC_IN_RAW);

rf.init(RF_TRANSMITTER_PIN, RF_RECEIVER_PIN);
RFLibCallback afunc = &rfCallback;
Expand Down

0 comments on commit 2d4fdc4

Please sign in to comment.