Skip to content

Commit d07ec6f

Browse files
committed
Better support for publishing on a topic, include a simple debug method
1 parent 6594747 commit d07ec6f

File tree

4 files changed

+89
-60
lines changed

4 files changed

+89
-60
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# EasyMqtt
2-
Easy handling of Mqtt on esp6288
2+
Easy handling of Mqtt on esp8266
33

44
This library is currentlly under heavy development.
55
And shoud not be used at the moment.
@@ -19,7 +19,7 @@ void setup() {
1919
mqtt.mqtt("host", 1883, "user", "pass");
2020

2121

22-
mqtt["wifi"]["rssi"] << [](){ return String(WiFi.RSSI()); };
22+
mqtt["foo"] << [](){ return "bar"; };
2323
}
2424

2525
void loop() {

ToDo.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,34 @@
55
Change MqttMap to include reference to mqttclient
66
* Change loop, publish, ... to not need mqttclient
77

8-
Add connected + will to indicate if device is online
8+
Add easyMqtt/$id/system/connected + will to indicate if device is online
99

10-
Add better debugging (remote ?)
10+
Add better debugging
11+
* Add new EasyDebug class, extends Print (Print.h), and prints to easyMqtt/$id/system/debug if enabled
12+
* Add getDebugger on EasyMqtt that returns the EasyDebug instance
1113

1214
## Future
1315

16+
Add system info
17+
* easyMqtt/$id/system/uptime
18+
* easyMqtt/$id/system/mem
19+
* easyMqtt/$id/system/reset
20+
* easyMqtt/$id/system/debug (see above)
21+
* easyMqtt/$id/system/config
22+
1423
Add support for filters
1524
* Don't send if value is the same as last.
1625
* Don't send if value is Nan
1726

1827
Add support for float type (extend MqttMap)
1928

2029
Add publish configured endpoints, to support openhab2 auto configure
21-
22-
Add web interface
30+
something like
31+
easyMqtt/$id/system/config
32+
{
33+
"id"="1dfasfa",
34+
"ip"="192.168.1.79",
35+
"endpoints"=["/temparatur", "/humidity"]
36+
}
37+
38+
Read / Write / String / Number / ...

src/EasyMqtt.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class EasyMqtt : public MqttMap {
1010
WiFiClient wifiClient;
1111
PubSubClient mqttClient;
1212

13-
String deviceId = "EasyMqtt";
13+
String deviceId = "deviceId";
1414
const char* mqtt_username = "N/A";
1515
const char* mqtt_password = "N/A";
1616

@@ -19,19 +19,14 @@ class EasyMqtt : public MqttMap {
1919
return String("easyMqtt/") + deviceId;
2020
}
2121

22-
MqttMap & getDiag() {
23-
return operator[]("diag");
24-
}
25-
2622
/**
2723
Handle connections to mqtt
2824
*/
2925
void mqttReconnect() {
3026
while (!mqttClient.connected()) {
3127
if (mqttClient.connect(deviceId.c_str(), mqtt_username, mqtt_password)) {
32-
Serial.println("MQTT connected");
33-
subscribe(mqttClient);
34-
getDiag()["log"].publish(mqttClient, "connected to MQTT");
28+
debug("Connected to MQTT");
29+
subscribe();
3530
} else {
3631
Serial.print("failed, rc=");
3732
Serial.print(mqttClient.state());
@@ -42,16 +37,24 @@ class EasyMqtt : public MqttMap {
4237
}
4338

4439
public:
45-
EasyMqtt() : MqttMap("easyMqtt") {
40+
EasyMqtt() : MqttMap("easyMqtt", mqttClient) {
4641
deviceId = String(ESP.getChipId());
4742

48-
getDiag().setInterval(30);
49-
getDiag()["mem"]["heap"] << []() {
43+
get("system").setInterval(30);
44+
get("system")["mem"]["heap"] << []() {
5045
return String(ESP.getFreeHeap());
5146
};
52-
getDiag()["uptime"] << []() {
47+
get("system")["uptime"] << []() {
5348
return String(millis());
5449
};
50+
get("system")["reset"] >> [](String value) {
51+
ESP.reset();
52+
};
53+
}
54+
55+
void debug(String msg) {
56+
Serial.println(msg);
57+
get("system")["debug"].publish(msg);
5558
}
5659

5760
/**
@@ -73,12 +76,15 @@ class EasyMqtt : public MqttMap {
7376
Serial.println(ESP.getChipId());
7477

7578
// Setup wifi diag
76-
getDiag()["wifi"]["rssi"] << []() {
79+
get("system")["wifi"]["rssi"] << []() {
7780
return String(WiFi.RSSI());
7881
};
79-
getDiag()["wifi"]["ssid"] << []() {
82+
get("system")["wifi"]["ssid"] << []() {
8083
return WiFi.SSID();
8184
};
85+
get("system")["wifi"]["ip"] << []() {
86+
return WiFi.localIP().toString();
87+
};
8288
}
8389

8490
/**
@@ -100,7 +106,7 @@ class EasyMqtt : public MqttMap {
100106
void loop() {
101107
mqttReconnect();
102108
mqttClient.loop();
103-
MqttMap::loop(mqttClient);
109+
MqttMap::loop();
104110
}
105111
};
106112

src/MqttMap.h

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,26 @@ class MqttMap {
1313
int interval = -1;
1414
unsigned long lastUpdate = 0; // Last read of data
1515

16+
PubSubClient* client = NULL;
17+
1618
MqttMap* parent = NULL;
1719
MqttMap* next = NULL;
1820
MqttMap* children = NULL;
1921

2022
protected:
21-
MqttMap(const char* _name, MqttMap& _parent) {
23+
MqttMap(const char* _name, PubSubClient& _mqttClient, MqttMap& _parent) {
2224
name = _name;
2325
parent = &_parent;
26+
client = &_mqttClient;
2427
interval = -1;
2528
}
2629

30+
MqttMap(const char* _name, PubSubClient& _mqttClient) {
31+
name = _name;
32+
client = &_mqttClient;
33+
interval = 5;
34+
}
35+
2736
virtual String getTopic() {
2837
if (parent) {
2938
return parent->getTopic() + "/" + name;
@@ -32,37 +41,13 @@ class MqttMap {
3241
}
3342
}
3443

35-
public:
36-
MqttMap(const char* _name) {
37-
name = _name;
38-
interval = 5;
39-
}
40-
41-
int getInterval() {
42-
if (interval < 0) {
43-
return parent->getInterval();
44-
}
45-
return interval;
46-
}
47-
48-
void setInterval(int _interval) {
49-
interval = _interval;
50-
}
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-
5844
void callback(const char* _topic, uint8_t* payload, unsigned int length) {
5945
if (strcmp(getTopic().c_str(), _topic) == 0) {
6046
if (outFunction != NULL) {
6147
String _payload = "";
6248
for (int i = 0; i < length; i++) {
6349
_payload += (char)payload[i];
6450
}
65-
// printf("Found callback, %s: %s\n", _topic, _payload.c_str());
6651
outFunction(_payload);
6752
}
6853
} else {
@@ -73,47 +58,69 @@ class MqttMap {
7358
}
7459
}
7560
}
76-
61+
7762
/**
7863
7964
*/
80-
void subscribe(PubSubClient& client) {
65+
void subscribe() {
8166
if (outFunction != NULL) {
8267
String topic = getTopic();
83-
printf("Subscribe: %s\n", topic.c_str());
84-
client.subscribe(topic.c_str());
68+
client->subscribe(topic.c_str());
8569
}
8670
MqttMap* child = children;
8771
while (child != NULL) {
88-
child->subscribe(client);
72+
child->subscribe();
8973
child = child->next;
9074
}
9175
}
9276

9377
/**
9478
9579
*/
96-
void loop(PubSubClient& client) {
80+
void loop() {
9781
if (inFunction != NULL) {
9882
unsigned long time = millis();
9983
if (time >= (lastUpdate + (getInterval() * 1000))) {
10084
lastUpdate = time;
10185
String value = inFunction();
10286
if (value != "") {
103-
publish(client, value);
87+
publish(value);
10488
}
10589
}
10690
}
10791
MqttMap* child = children;
10892
while (child != NULL) {
109-
child->loop(client);;
93+
child->loop();
11094
child = child->next;
11195
}
11296
}
11397

98+
public:
99+
int getInterval() {
100+
if (interval < 0) {
101+
return parent->getInterval();
102+
}
103+
return interval;
104+
}
105+
106+
void setInterval(int _interval) {
107+
interval = _interval;
108+
}
109+
110+
void publish(String message) {
111+
client->publish(getTopic().c_str(), message.c_str());
112+
}
113+
114114
/**
115+
* Create or get the sub topic with the name {name}
116+
*/
117+
MqttMap & get(const char* name) {
118+
return operator[](name);
119+
}
115120

116-
*/
121+
/**
122+
* Create or get the sub topic with the name {name}
123+
*/
117124
MqttMap & operator[](const char* name) {
118125
MqttMap * child = children;
119126
while (child != NULL) {
@@ -123,21 +130,21 @@ class MqttMap {
123130
child = child->next;
124131
}
125132
MqttMap * oldChild = children;
126-
children = new MqttMap(name, *this);
133+
children = new MqttMap(name, *client, *this);
127134
children->next = oldChild;
128135
return *children;
129136
}
130137

131138
/**
132-
Read data from function and send it to mqtt
133-
*/
139+
* Read data from function and send it to mqtt
140+
*/
134141
void operator<<(String (*inFunction)()) {
135142
MqttMap::inFunction = inFunction;
136143
}
137144

138145
/**
139-
Handle data comming from mqtt
140-
*/
146+
* Handle data comming from mqtt
147+
*/
141148
void operator>>(void (*outFunction)(String payload)) {
142149
MqttMap::outFunction = outFunction;
143150
}

0 commit comments

Comments
 (0)