@@ -27,18 +27,7 @@ class EasyMqtt : public MqttEntry {
27
27
debug (" Connecting to wifi: " + config ().getString (" wifi.ssid" , " " ));
28
28
WiFi.mode (WIFI_STA);
29
29
30
- String sSsid = config ().getString (" wifi.ssid" , " " );
31
- char ssid[sSsid .length () + 1 ];
32
- strcpy (ssid, sSsid .c_str ());
33
-
34
- String name = config ().getString (" device.name" , deviceId);
35
- // WiFi.hostname("WebPool"); // Set the name of the device
36
-
37
- String sPass = config ().getString (" wifi.password" , " " );
38
- char pass[sPass .length () + 1 ];
39
- strcpy (pass, sPass .c_str ());
40
-
41
- WiFi.begin (ssid, pass);
30
+ WiFi.begin (config ().getCString (" wifi.ssid" , " " ), config ().getCString (" wifi.password" , " " ));
42
31
43
32
#ifdef DEBUG
44
33
WiFi.printDiag (Serial);
@@ -56,7 +45,7 @@ class EasyMqtt : public MqttEntry {
56
45
} else {
57
46
debug (" WiFi connection timeout - Setup AP" );
58
47
WiFi.mode (WIFI_AP);
59
- WiFi.softAP (config ().getString (" wifi.ap" , " EasyMqtt" ). c_str ( ), " 123456" );
48
+ WiFi.softAP (config ().getCString (" wifi.ap" , " EasyMqtt" ), " 123456" );
60
49
debug (" IP address" , WiFi.softAPIP ().toString ());
61
50
}
62
51
debug (" devideId" , deviceId);
@@ -74,23 +63,9 @@ class EasyMqtt : public MqttEntry {
74
63
});
75
64
});
76
65
77
- String sHost = config ().getString (" mqtt.host" , " " );
78
- char host[sHost .length () + 1 ];
79
- strcpy (host, sHost .c_str ());
80
-
81
- int port = config ().getInt (" mqtt.port" , 1883 );
82
-
83
- String sUser = config ().getString (" mqtt.username" , " " );
84
- char user[sUser .length () + 1 ];
85
- strcpy (user, sUser .c_str ());
86
-
87
- String sPass = config ().getString (" mqtt.password" , " " );
88
- char pass[sPass .length () + 1 ];
89
- strcpy (pass, sPass .c_str ());
90
-
91
- mqttClient.setServer (host, port);
66
+ mqttClient.setServer (config ().getCString (" mqtt.host" , " " ), config ().getInt (" mqtt.port" , 1883 ));
92
67
93
- if (mqttClient.connect (deviceId.c_str (), user, pass )) {
68
+ if (mqttClient.connect (deviceId.c_str (), config (). getCString ( " mqtt.username " , " " ), config (). getCString ( " mqtt.password " , " " ) )) {
94
69
debug (" Connected to MQTT" );
95
70
96
71
setPublishFunction ([&](MqttEntry* entry, String message){
@@ -178,6 +153,19 @@ class EasyMqtt : public MqttEntry {
178
153
return *configEntry;
179
154
}
180
155
156
+ void debug (String msg) {
157
+ #ifdef DEBUG
158
+ Serial.println (msg);
159
+ #endif
160
+ if (mqttClient.connected ()) {
161
+ get (" system/debug" ).publish (msg);
162
+ }
163
+ }
164
+
165
+ void debug (String key, String value) {
166
+ debug (key + " = " + value);
167
+ }
168
+
181
169
String getDeviceId () {
182
170
return deviceId;
183
171
}
0 commit comments