@@ -26,19 +26,28 @@ class EasyMqtt : public MqttEntry {
26
26
if (WiFi.status () == WL_DISCONNECTED) {
27
27
debug (" Connecting to wifi: " + config ().getString (" wifi.ssid" , " " ));
28
28
WiFi.mode (WIFI_STA);
29
- WiFi.begin (config ().getString (" wifi.ssid" , " " ).c_str (), config ().getString (" wifi.password" , " " ).c_str ());
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
39
31
40
#ifdef DEBUG
32
41
WiFi.printDiag (Serial);
33
42
#endif
34
43
35
44
int timer = 0 ;
36
- while ((WiFi.status () == WL_DISCONNECTED) && timer < 50 ) {
45
+ while ((WiFi.status () == WL_DISCONNECTED) && timer < 60 ) {
37
46
debug (" Wifi Status" , String (WiFi.status ()));
38
47
delay (500 );
39
48
timer++;
40
49
}
41
- if (timer < 50 && WiFi.status () == WL_CONNECTED) {
50
+ if (timer < 60 && WiFi.status () == WL_CONNECTED) {
42
51
debug (" WiFi connected" );
43
52
debug (" IP address" , WiFi.localIP ().toString ());
44
53
} else {
@@ -54,17 +63,31 @@ class EasyMqtt : public MqttEntry {
54
63
55
64
void connectMqtt () {
56
65
if (!mqttClient.connected () && mqttDelay < millis ()) {
57
- debug (" Configure MQTT" );
66
+ debug (" Connecting to MQTT" );
58
67
mqttClient.setClient (wifiClient);
59
68
mqttClient.setCallback ([&](const char * topic, uint8_t * payload, unsigned int length) {
60
69
each ([=](MqttEntry* entry){
61
70
entry->callback (topic, payload, length);
62
71
});
63
72
});
64
- mqttClient.setServer (config ().getString (" mqtt.host" , " " ).c_str (), config ().getInt (" mqtt.port" , 1883 ));
73
+
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);
65
89
66
- debug (" Connecting to MQTT" );
67
- if (mqttClient.connect (deviceId.c_str (), config ().getString (" mqtt.username" , " " ).c_str (), config ().getString (" mqtt.password" , " " ).c_str ())) {
90
+ if (mqttClient.connect (deviceId.c_str (), user, pass)) {
68
91
debug (" Connected to MQTT" );
69
92
70
93
setPublishFunction ([&](MqttEntry* entry, String message){
@@ -82,7 +105,7 @@ class EasyMqtt : public MqttEntry {
82
105
} else {
83
106
debug (" Connection to MQTT failed, rc" , String (mqttClient.state ()));
84
107
85
- mqttDelay = millis () + 1000 ;
108
+ mqttDelay = millis () + 5000 ;
86
109
}
87
110
}
88
111
}
@@ -102,6 +125,9 @@ class EasyMqtt : public MqttEntry {
102
125
103
126
public:
104
127
EasyMqtt () : MqttEntry(" easyMqtt" , mqttClient) {
128
+ #ifdef DEBUG
129
+ Serial.begin (115200 );
130
+ #endif
105
131
deviceId = String (ESP.getChipId ());
106
132
107
133
debug (" test" );
0 commit comments