Skip to content

Commit c3eda8b

Browse files
committed
Fixed problem with onMessage callback
1 parent 9f0e217 commit c3eda8b

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

MqttManager/MqttManager.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
#include "ArduinoJson.h"
44

5+
void (*messageReceivedCallback)(std::string , std::string);
6+
7+
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
8+
{
9+
std::string topicString(topic);
10+
std::string payloadString(payload);
11+
12+
messageReceivedCallback(topic, payload);
13+
}
14+
15+
516
MqttManager::MqttManager()
617
{
718
m_connected = false;
@@ -24,9 +35,11 @@ void MqttManager::setup(std::string mqttServer, std::string mqttPort, std::strin
2435
IPAddress server;
2536
server.fromString(m_mqttServer.c_str());
2637

27-
m_mqttClient.setServer(server, m_mqttPort);
28-
m_mqttClient.setCredentials(mqttUsername.c_str(), mqttPassword.c_str());
38+
m_mqttClient.onMessage(onMqttMessage);
39+
2940
m_mqttClient.setCleanSession(false);
41+
m_mqttClient.setCredentials(mqttUsername.c_str(), mqttPassword.c_str());
42+
m_mqttClient.setServer(server, m_mqttPort);
3043

3144
m_deviceStatusInfoTimer.start();
3245
m_checkConnectivityTimer.start();
@@ -155,9 +168,9 @@ void MqttManager::publishMQTT(std::string topic, std::string payload)
155168
}
156169
}
157170

158-
void MqttManager::setCallback(void (*callback)(char*, char*, AsyncMqttClientMessageProperties, size_t, size_t, size_t ))
171+
void MqttManager::setCallback(void (*callback)(std::string , std::string))
159172
{
160-
m_mqttClient.onMessage(callback);
173+
messageReceivedCallback = callback;
161174
}
162175

163176
void MqttManager::setLastWillMQTT(std::string topic, const char* payload)

MqttManager/MqttManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <string>
1616
#include <vector>
1717

18+
1819
class MqttManager
1920
{
2021
private:
@@ -57,7 +58,7 @@ class MqttManager
5758
void setup(std::string mqttServer, std::string mqttPort, std::string mqttUsername, std::string mqttPassword);
5859

5960
void setDeviceData(std::string deviceName, std::string hardware, std::string deviceIP, std::string firmware, std::string firmwareVersion);
60-
void setCallback(void (*callback)(char*, char*, AsyncMqttClientMessageProperties, size_t, size_t, size_t ));
61+
void setCallback(void (*callback)(std::string , std::string));
6162
void setLastWillMQTT(std::string topic, const char* payload);
6263

6364
void setDeviceStatusInfoTime(unsigned long deviceStatusInfoTime);

0 commit comments

Comments
 (0)