Skip to content

Commit

Permalink
Fixed problem with onMessage callback
Browse files Browse the repository at this point in the history
  • Loading branch information
danimtb committed Mar 28, 2017
1 parent 9f0e217 commit c3eda8b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 17 additions & 4 deletions MqttManager/MqttManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

#include "ArduinoJson.h"

void (*messageReceivedCallback)(std::string , std::string);

void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
{
std::string topicString(topic);
std::string payloadString(payload);

messageReceivedCallback(topic, payload);
}


MqttManager::MqttManager()
{
m_connected = false;
Expand All @@ -24,9 +35,11 @@ void MqttManager::setup(std::string mqttServer, std::string mqttPort, std::strin
IPAddress server;
server.fromString(m_mqttServer.c_str());

m_mqttClient.setServer(server, m_mqttPort);
m_mqttClient.setCredentials(mqttUsername.c_str(), mqttPassword.c_str());
m_mqttClient.onMessage(onMqttMessage);

m_mqttClient.setCleanSession(false);
m_mqttClient.setCredentials(mqttUsername.c_str(), mqttPassword.c_str());
m_mqttClient.setServer(server, m_mqttPort);

m_deviceStatusInfoTimer.start();
m_checkConnectivityTimer.start();
Expand Down Expand Up @@ -155,9 +168,9 @@ void MqttManager::publishMQTT(std::string topic, std::string payload)
}
}

void MqttManager::setCallback(void (*callback)(char*, char*, AsyncMqttClientMessageProperties, size_t, size_t, size_t ))
void MqttManager::setCallback(void (*callback)(std::string , std::string))
{
m_mqttClient.onMessage(callback);
messageReceivedCallback = callback;
}

void MqttManager::setLastWillMQTT(std::string topic, const char* payload)
Expand Down
3 changes: 2 additions & 1 deletion MqttManager/MqttManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string>
#include <vector>


class MqttManager
{
private:
Expand Down Expand Up @@ -57,7 +58,7 @@ class MqttManager
void setup(std::string mqttServer, std::string mqttPort, std::string mqttUsername, std::string mqttPassword);

void setDeviceData(std::string deviceName, std::string hardware, std::string deviceIP, std::string firmware, std::string firmwareVersion);
void setCallback(void (*callback)(char*, char*, AsyncMqttClientMessageProperties, size_t, size_t, size_t ));
void setCallback(void (*callback)(std::string , std::string));
void setLastWillMQTT(std::string topic, const char* payload);

void setDeviceStatusInfoTime(unsigned long deviceStatusInfoTime);
Expand Down

0 comments on commit c3eda8b

Please sign in to comment.