@@ -23,13 +23,14 @@ class EasyMqtt : public MqttEntry {
23
23
*/
24
24
void connect () {
25
25
if (WiFi.status () != WL_CONNECTED) {
26
- const char * ssid = config ().getCString (" wifi.ssid" , " " );
27
- const char * password = config ().getCString (" wifi.password" , " " );
26
+ String ssid = config ().getString (" wifi.ssid" , " " );
27
+ String password = config ().getString (" wifi.password" , " " );
28
+ debug (" Connecting to wifi: " + ssid);
28
29
WiFi.mode (WIFI_STA);
29
- WiFi.begin (ssid, password);
30
+ WiFi.begin (ssid. c_str () , password. c_str () );
30
31
31
32
int timer = 0 ;
32
- while (WiFi.status () != WL_CONNECTED && timer < 10 ) {
33
+ while (( WiFi.status () != WL_CONNECTED) && timer < 10 ) {
33
34
delay (500 );
34
35
timer++;
35
36
// ToDo: handle timeout, and create AP
@@ -44,22 +45,23 @@ class EasyMqtt : public MqttEntry {
44
45
debug (" devideId" , deviceId);
45
46
webPortal.setup (*this );
46
47
}
48
+ String host = config ().getString (" mqtt.host" , " " );
49
+ int port = config ().getInt (" mqtt.port" , 1883 );
47
50
if (mqttClient.state () == MQTT_DISCONNECTED) {
48
51
// Setup MQTT
49
- const char * host = config ().getCString (" mqtt.host" , " " );
50
- int port = config ().getInt (" mqtt.port" , 1883 );
51
52
mqttClient.setClient (wifiClient);
52
53
mqttClient.setCallback ([&](const char * topic, uint8_t * payload, unsigned int length) {
53
54
each ([=](MqttEntry* entry){
54
55
entry->callback (topic, payload, length);
55
56
});
56
57
});
57
- mqttClient.setServer (host, port);
58
+ mqttClient.setServer (host. c_str () , port);
58
59
}
59
60
if (!mqttClient.connected ()) {
60
- const char * username = config ().getCString (" mqtt.username" , " " );
61
- const char * password = config ().getCString (" mqtt.password" , " " );
62
- if (mqttClient.connect (deviceId.c_str (), username, password)) {
61
+ String username = config ().getString (" mqtt.username" , " " );
62
+ String password = config ().getString (" mqtt.password" , " " );
63
+ debug (" Connecting to MQTT: " + host + " on port " + String (port));
64
+ if (mqttClient.connect (deviceId.c_str (), username.c_str (), password.c_str ())) {
63
65
debug (" Connected to MQTT" );
64
66
debug (" Topic" , getTopic ());
65
67
each ([&](MqttEntry* entry){
@@ -68,8 +70,8 @@ class EasyMqtt : public MqttEntry {
68
70
}
69
71
});
70
72
} else {
71
- debug (" failed, rc" , String (mqttClient.state ()));
72
- delay (5000 );
73
+ debug (" Connection to MQTT failed, rc" , String (mqttClient.state ()));
74
+ delay (500 );
73
75
}
74
76
}
75
77
}
0 commit comments