@@ -27,15 +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 sPass = config ().getString (" wifi.password" , " " );
35
- char pass[sPass .length () + 1 ];
36
- strcpy (pass, sPass .c_str ());
37
-
38
- WiFi.begin (ssid, pass);
30
+ WiFi.begin (config ().getCString (" wifi.ssid" , " " ), config ().getCString (" wifi.password" , " " ));
39
31
40
32
#ifdef DEBUG
41
33
WiFi.printDiag (Serial);
@@ -53,7 +45,7 @@ class EasyMqtt : public MqttEntry {
53
45
} else {
54
46
debug (" WiFi connection timeout - Setup AP" );
55
47
WiFi.mode (WIFI_AP);
56
- WiFi.softAP (config ().getString (" wifi.ap" , " EasyMqtt" ). c_str ( ), " 123456" );
48
+ WiFi.softAP (config ().getCString (" wifi.ap" , " EasyMqtt" ), " 123456" );
57
49
debug (" IP address" , WiFi.softAPIP ().toString ());
58
50
}
59
51
debug (" devideId" , deviceId);
@@ -71,23 +63,9 @@ class EasyMqtt : public MqttEntry {
71
63
});
72
64
});
73
65
74
- String sHost = config ().getString (" mqtt.host" , " " );
75
- char host[sHost .length () + 1 ];
76
- strcpy (host, sHost .c_str ());
77
-
78
- int port = config ().getInt (" mqtt.port" , 1883 );
79
-
80
- String sUser = config ().getString (" mqtt.username" , " " );
81
- char user[sUser .length () + 1 ];
82
- strcpy (user, sUser .c_str ());
83
-
84
- String sPass = config ().getString (" mqtt.password" , " " );
85
- char pass[sPass .length () + 1 ];
86
- strcpy (pass, sPass .c_str ());
87
-
88
- mqttClient.setServer (host, port);
66
+ mqttClient.setServer (config ().getCString (" mqtt.host" , " " ), config ().getInt (" mqtt.port" , 1883 ));
89
67
90
- if (mqttClient.connect (deviceId.c_str (), user, pass )) {
68
+ if (mqttClient.connect (deviceId.c_str (), config (). getCString ( " mqtt.username " , " " ), config (). getCString ( " mqtt.password " , " " ) )) {
91
69
debug (" Connected to MQTT" );
92
70
93
71
setPublishFunction ([&](MqttEntry* entry, String message){
@@ -110,19 +88,6 @@ class EasyMqtt : public MqttEntry {
110
88
}
111
89
}
112
90
113
- void debug (String msg) {
114
- #ifdef DEBUG
115
- Serial.println (msg);
116
- #endif
117
- if (mqttClient.connected ()) {
118
- get (" system/debug" ).publish (msg);
119
- }
120
- }
121
-
122
- void debug (String key, String value) {
123
- debug (key + " = " + value);
124
- }
125
-
126
91
public:
127
92
EasyMqtt () : MqttEntry(" easyMqtt" , mqttClient) {
128
93
#ifdef DEBUG
@@ -173,6 +138,19 @@ class EasyMqtt : public MqttEntry {
173
138
return *configEntry;
174
139
}
175
140
141
+ void debug (String msg) {
142
+ #ifdef DEBUG
143
+ Serial.println (msg);
144
+ #endif
145
+ if (mqttClient.connected ()) {
146
+ get (" system/debug" ).publish (msg);
147
+ }
148
+ }
149
+
150
+ void debug (String key, String value) {
151
+ debug (key + " = " + value);
152
+ }
153
+
176
154
String getDeviceId () {
177
155
return deviceId;
178
156
}
0 commit comments