Skip to content

Commit dcb835c

Browse files
committed
Fix bug in callback, and some other improvements
1 parent 8854244 commit dcb835c

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/EasyMqtt.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class EasyMqtt : public MqttMap {
3131
if (mqttClient.connect(deviceId.c_str(), mqtt_username, mqtt_password)) {
3232
Serial.println("MQTT connected");
3333
subscribe(mqttClient);
34+
getDiag()["log"].publish(mqttClient, "connected to MQTT");
3435
} else {
3536
Serial.print("failed, rc=");
3637
Serial.print(mqttClient.state());
@@ -44,8 +45,9 @@ class EasyMqtt : public MqttMap {
4445
EasyMqtt() : MqttMap("easyMqtt") {
4546
deviceId = String(ESP.getChipId());
4647

47-
getDiag().setInterval(30l);
48+
getDiag().setInterval(30);
4849
getDiag()["mem"]["heap"] << [](){ return String(ESP.getFreeHeap()); };
50+
getDiag()["uptime"] << [](){ return String(millis()); };
4951
}
5052

5153
/**
@@ -59,7 +61,7 @@ class EasyMqtt : public MqttMap {
5961
delay(500);
6062
Serial.print(".");
6163
}
62-
Serial.println("WiFi connected");
64+
Serial.println("WiFi connected");
6365
Serial.println("IP address: ");
6466
Serial.println(WiFi.localIP());
6567

src/MqttMap.h

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class MqttMap {
1010
String (*inFunction)() = NULL;
1111

1212
const char* name = "N/A";
13-
int interval = 0;
14-
unsigned long nextUpdate = 0; // Last read of data
13+
int interval = -1;
14+
unsigned long lastUpdate = 0; // Last read of data
1515

1616
MqttMap* parent = NULL;
1717
MqttMap* next = NULL;
@@ -21,7 +21,7 @@ class MqttMap {
2121
MqttMap(const char* _name, MqttMap& _parent) {
2222
name = _name;
2323
parent = &_parent;
24-
interval = 0;
24+
interval = -1;
2525
}
2626

2727
virtual String getTopic() {
@@ -31,15 +31,15 @@ class MqttMap {
3131
return String(name);
3232
}
3333
}
34-
34+
3535
public:
3636
MqttMap(const char* _name) {
3737
name = _name;
3838
interval = 5;
3939
}
4040

4141
int getInterval() {
42-
if(interval == 0) {
42+
if(interval < 0) {
4343
return parent->getInterval();
4444
}
4545
return interval;
@@ -49,6 +49,12 @@ class MqttMap {
4949
interval = _interval;
5050
}
5151

52+
void publish(PubSubClient& client, String message) {
53+
String topic = getTopic();
54+
printf("publish[%s]: %s\n", topic.c_str(), message.c_str());
55+
client.publish(topic.c_str(), message.c_str());
56+
}
57+
5258
void callback(const char* _topic, uint8_t* payload, unsigned int length) {
5359
if(strcmp(getTopic().c_str(), _topic) == 0) {
5460
if(outFunction != NULL) {
@@ -68,6 +74,9 @@ class MqttMap {
6874
}
6975
}
7076

77+
/**
78+
*
79+
*/
7180
void subscribe(PubSubClient& client) {
7281
if(outFunction != NULL) {
7382
String topic = getTopic();
@@ -81,15 +90,18 @@ class MqttMap {
8190
}
8291
}
8392

93+
/**
94+
*
95+
*/
8496
void loop(PubSubClient& client) {
8597
if(inFunction != NULL) {
8698
unsigned long time = millis();
87-
if(time >= nextUpdate) {
88-
nextUpdate = time + (getInterval() * 1000);
89-
String topic = getTopic();
90-
String value = inFunction();
91-
printf("Value(%s): %s\n", topic.c_str(), value.c_str());
92-
client.publish(topic.c_str(), value.c_str());
99+
if(time >= (lastUpdate + (getInterval() * 1000))) {
100+
lastUpdate = time;
101+
String value = inFunction();
102+
if(value != "") {
103+
publish(client, value);
104+
}
93105
}
94106
}
95107
MqttMap* child = children;
@@ -99,6 +111,9 @@ class MqttMap {
99111
}
100112
}
101113

114+
/**
115+
*
116+
*/
102117
MqttMap & operator[](const char* name) {
103118
MqttMap * child = children;
104119
while(child != NULL) {
@@ -113,7 +128,7 @@ class MqttMap {
113128
return *children;
114129
}
115130

116-
/**
131+
/**
117132
* Read data from function and send it to mqtt
118133
*/
119134
void operator<<(String (*inFunction)()) {

0 commit comments

Comments
 (0)