@@ -10,8 +10,8 @@ class MqttMap {
10
10
String (*inFunction)() = NULL ;
11
11
12
12
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
15
15
16
16
MqttMap* parent = NULL ;
17
17
MqttMap* next = NULL ;
@@ -21,7 +21,7 @@ class MqttMap {
21
21
MqttMap (const char * _name, MqttMap& _parent) {
22
22
name = _name;
23
23
parent = &_parent;
24
- interval = 0 ;
24
+ interval = - 1 ;
25
25
}
26
26
27
27
virtual String getTopic () {
@@ -31,15 +31,15 @@ class MqttMap {
31
31
return String (name);
32
32
}
33
33
}
34
-
34
+
35
35
public:
36
36
MqttMap (const char * _name) {
37
37
name = _name;
38
38
interval = 5 ;
39
39
}
40
40
41
41
int getInterval () {
42
- if (interval == 0 ) {
42
+ if (interval < 0 ) {
43
43
return parent->getInterval ();
44
44
}
45
45
return interval;
@@ -49,6 +49,12 @@ class MqttMap {
49
49
interval = _interval;
50
50
}
51
51
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
+
52
58
void callback (const char * _topic, uint8_t * payload, unsigned int length) {
53
59
if (strcmp (getTopic ().c_str (), _topic) == 0 ) {
54
60
if (outFunction != NULL ) {
@@ -68,6 +74,9 @@ class MqttMap {
68
74
}
69
75
}
70
76
77
+ /* *
78
+ *
79
+ */
71
80
void subscribe (PubSubClient& client) {
72
81
if (outFunction != NULL ) {
73
82
String topic = getTopic ();
@@ -81,15 +90,18 @@ class MqttMap {
81
90
}
82
91
}
83
92
93
+ /* *
94
+ *
95
+ */
84
96
void loop (PubSubClient& client) {
85
97
if (inFunction != NULL ) {
86
98
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
+ }
93
105
}
94
106
}
95
107
MqttMap* child = children;
@@ -99,6 +111,9 @@ class MqttMap {
99
111
}
100
112
}
101
113
114
+ /* *
115
+ *
116
+ */
102
117
MqttMap & operator [](const char * name) {
103
118
MqttMap * child = children;
104
119
while (child != NULL ) {
@@ -113,7 +128,7 @@ class MqttMap {
113
128
return *children;
114
129
}
115
130
116
- /* *
131
+ /* *
117
132
* Read data from function and send it to mqtt
118
133
*/
119
134
void operator <<(String (*inFunction)()) {
0 commit comments