-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt.cpp
More file actions
99 lines (82 loc) · 2.71 KB
/
Copy pathmqtt.cpp
File metadata and controls
99 lines (82 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// **********************************************************************************
// ESP8266 / ESP32 Mqtt
// **********************************************************************************
//
// History : V1.00 2025-03-06 - First release
// V1.01 2025-03-07
// Add mqttClient.setKeepAlive(MQTT_KeepAlive_Timeout);
// Add mqttClient.subscribe((String(topic) + "/set/#").c_str());
// to receive command like topic/set/#
// ex : Message arrived on topic CLIMATE : [WIFI-RELAY/set/setmode], 2
// **********************************************************************************
#include "WifiRelay.h"
#include "mqtt.h"
/*
* Constructor
*/
MQTT::MQTT() {
}
/*
* Function: bool MQTT::Connect(_fn_mqttCallBack mqttCallback, char *host, uint16_t port, char *topic, char *user, char *pswd, bool subscribe)
* Purpose : Connect to mqtt broker
* Input : _fn_mqttCallBack
* : host
* : port
* : topic
* : user
* : pswd
* : subscribe true to subscribe to receive message like topic/set/#
*
* return true if connection ok else false
*/
bool MQTT::Connect(_fn_mqttCallBack mqttCallback, char *host, uint16_t port, char *topic, char *user, char *pswd, bool subscribe) {
bool ret = false;
DebugF("Connexion au serveur MQTT... ");
if ( WiFi.status() == WL_CONNECTED){
//DebugF ("execution tache MQTT / wifi connecté Init mqtt=");
//Debugln (mtt_Init);
if (!mtt_Init){
// Suite au remplacement de
// PubSubClient mqttClient(mqttWifiClient);
// par
// PubSubClient mqttClient;
// il faut rajouter
mqttClient.setClient(mqttWifiClient);
mqttClient.setServer(host, port); //Configuration de la connexion au serveur MQTT
mqttClient.setCallback(mqttCallback); //La fonction de callback qui est executée à chaque réception de message
mtt_Init = true;
}
ret = mqttClient.connected();
if (!ret) {
Debugln (">>>> Connect again MQTT ...");
mqttClient.setKeepAlive(MQTT_KeepAlive_Timeout);
ret = mqttClient.connect(topic, user, pswd);
if (ret && subscribe)
mqttClient.subscribe((String(topic) + "/set/#").c_str());
}
}
if (ret) {
DebuglnF("OK");
} else {
DebuglnF("KO");
}
return(ret);
}
/*
* bool MQTT::Publish (char *topic, char *payload , bool retained)
*/
bool MQTT::Publish (const char *topic, const char *payload , bool retained)
{
bool ret = false;
ret = mqttClient.publish(topic, payload , retained);
return(ret);
}
/*
* void MQTT::Loop()
*
* comment : must be called in sketch loop() every second to receive subscribe messages
*/
void MQTT::Loop()
{
mqttClient.loop();
}