5
5
#include < PubSubClient.h>
6
6
#include " MqttEntry.h"
7
7
#include " WebPortal.h"
8
- #include " ConfigEntry.h"
9
8
10
9
class EasyMqtt : public MqttEntry {
11
10
private:
12
11
WiFiClient wifiClient;
13
12
PubSubClient mqttClient;
14
13
WebPortal webPortal;
15
14
16
- ConfigEntry* configEntry;
17
-
18
15
String deviceId = " deviceId" ;
19
16
long mqttDelay = 0 ;
20
17
18
+ const char * wifi_ssid = " N/A" ;
19
+ const char * wifi_password = " N/A" ;
20
+
21
+ const char * mqtt_host = " N/A" ;
22
+ int mqtt_port = 1883 ;
23
+ const char * mqtt_username = " N/A" ;
24
+ const char * mqtt_password = " N/A" ;
25
+
21
26
protected:
22
27
/* *
23
28
Handle connections to mqtt
24
29
*/
25
30
void connectWiFi () {
26
31
if (WiFi.status () == WL_DISCONNECTED) {
27
- debug (" Connecting to wifi: " + config (). getString ( " wifi.ssid " , " " ) );
32
+ debug (" Connecting to wifi" );
28
33
WiFi.mode (WIFI_STA);
29
34
30
- WiFi.begin (config (). getCString ( " wifi.ssid " , " " ), config (). getCString ( " wifi.password " , " " ) );
35
+ WiFi.begin (wifi_ssid, wifi_password );
31
36
32
37
#ifdef DEBUG
33
38
WiFi.printDiag (Serial);
@@ -45,7 +50,7 @@ class EasyMqtt : public MqttEntry {
45
50
} else {
46
51
debug (" WiFi connection timeout - Setup AP" );
47
52
WiFi.mode (WIFI_AP);
48
- WiFi.softAP (config (). getCString ( " wifi.ap " , " EasyMqtt" ) , " 123456" );
53
+ WiFi.softAP (" EasyMqtt" , " 123456" );
49
54
debug (" IP address" , WiFi.softAPIP ().toString ());
50
55
}
51
56
debug (" devideId" , deviceId);
@@ -63,9 +68,9 @@ class EasyMqtt : public MqttEntry {
63
68
});
64
69
});
65
70
66
- mqttClient.setServer (config (). getCString ( " mqtt.host " , " " ), config (). getInt ( " mqtt.port " , 1883 ) );
71
+ mqttClient.setServer (mqtt_host, mqtt_port );
67
72
68
- if (mqttClient.connect (deviceId.c_str (), config (). getCString ( " mqtt.username " , " " ), config (). getCString ( " mqtt.password " , " " ) )) {
73
+ if (mqttClient.connect (deviceId.c_str (), mqtt_username, mqtt_password )) {
69
74
debug (" Connected to MQTT" );
70
75
71
76
setPublishFunction ([&](MqttEntry* entry, String message){
@@ -95,16 +100,12 @@ class EasyMqtt : public MqttEntry {
95
100
#endif
96
101
deviceId = String (ESP.getChipId ());
97
102
98
- configEntry = new ConfigEntry (mqttClient, *this );
99
- addChild (configEntry);
103
+ setInterval (10 );
100
104
101
105
get (" system" ).setInterval (30 );
102
106
get (" system" )[" deviceId" ] << [this ]() {
103
107
return deviceId;
104
108
};
105
- get (" system" )[" name" ] << [this ]() {
106
- return config ().getString (" device.name" , deviceId);
107
- };
108
109
get (" system" )[" mem" ][" heap" ] << []() {
109
110
return String (ESP.getFreeHeap ());
110
111
};
@@ -140,11 +141,6 @@ class EasyMqtt : public MqttEntry {
140
141
ESP.restart ();
141
142
}
142
143
};
143
- get (" system" )[" config" ][" reset" ] >> [this ](String value) {
144
- if (value == " reset" ) {
145
- config ().reset ();
146
- }
147
- };
148
144
}
149
145
150
146
void debug (String msg) {
@@ -160,10 +156,6 @@ class EasyMqtt : public MqttEntry {
160
156
debug (key + " = " + value);
161
157
}
162
158
163
- ConfigEntry & config () {
164
- return *configEntry;
165
- }
166
-
167
159
void debug (String msg) {
168
160
#ifdef DEBUG
169
161
Serial.println (msg);
@@ -190,19 +182,19 @@ class EasyMqtt : public MqttEntry {
190
182
Deprecated
191
183
*/
192
184
void wifi (const char * ssid, const char * password) {
193
- config (). set ( " wifi. ssid" , ssid) ;
194
- config (). set ( " wifi. password" , password) ;
185
+ wifi_ssid = ssid;
186
+ wifi_password = password;
195
187
}
196
188
197
189
/* *
198
190
Configure mqtt
199
191
Deprecated
200
192
*/
201
193
void mqtt (const char * host, int port, const char * username, const char * password) {
202
- config (). set ( " mqtt. host" , host) ;
203
- config (). set ( " mqtt. port" , String (port)) ;
204
- config (). set ( " mqtt. username" , username) ;
205
- config (). set ( " mqtt. password" , password) ;
194
+ mqtt_host = host;
195
+ mqtt_port = port;
196
+ mqtt_username = username;
197
+ mqtt_password = password;
206
198
}
207
199
208
200
/* *
0 commit comments