@@ -22,42 +22,47 @@ class EasyMqtt : public MqttEntry {
22
22
Handle connections to mqtt
23
23
*/
24
24
void connect () {
25
- if (WiFi.status () != WL_CONNECTED ) {
25
+ if (WiFi.status () == WL_DISCONNECTED ) {
26
26
debug (" Connecting to wifi: " + config ().getString (" wifi.ssid" , " " ));
27
27
WiFi.mode (WIFI_STA);
28
- WiFi.begin (config ().getCString (" wifi.ssid" , " " ), config ().getCString (" wifi.password" , " " ));
28
+ WiFi.begin (config ().getString (" wifi.ssid" , " " ).c_str (), config ().getString (" wifi.password" , " " ).c_str ());
29
+
30
+ #ifdef DEBUG
31
+ WiFi.printDiag (Serial);
32
+ #endif
29
33
30
34
int timer = 0 ;
31
- while ((WiFi.status () != WL_CONNECTED) && timer < 10 ) {
35
+ while ((WiFi.status () == WL_DISCONNECTED) && timer < 50 ) {
36
+ Serial.println (WiFi.status ());
32
37
delay (500 );
33
38
timer++;
34
39
// ToDo: handle timeout, and create AP
35
40
}
36
- if (timer < 10 ) {
41
+ if (timer < 50 && WiFi. status () == WL_CONNECTED ) {
37
42
debug (" WiFi connected" );
43
+ debug (" IP address" , WiFi.localIP ().toString ());
38
44
} else {
39
45
debug (" WiFi connection timeout - Setup AP" );
40
- // ToDo: create AP
41
46
WiFi.mode (WIFI_AP);
42
- WiFi.softAP (config ().getCString (" wifi.ap" , " EasyMqtt" ), " 123456" );
47
+ WiFi.softAP (config ().getString (" wifi.ap" , " EasyMqtt" ).c_str (), " 123456" );
48
+ debug (" IP address" , WiFi.softAPIP ().toString ());
43
49
}
44
- debug (" IP address" , WiFi.localIP ().toString ());
45
50
debug (" devideId" , deviceId);
46
51
webPortal.setup (*this );
47
52
}
48
53
if (mqttClient.state () == MQTT_DISCONNECTED) {
49
- // Setup MQTT
54
+ debug ( " Configure MQTT" );
50
55
mqttClient.setClient (wifiClient);
51
56
mqttClient.setCallback ([&](const char * topic, uint8_t * payload, unsigned int length) {
52
57
each ([=](MqttEntry* entry){
53
58
entry->callback (topic, payload, length);
54
59
});
55
60
});
56
- mqttClient.setServer (config ().getCString (" mqtt.host" , " " ), config ().getInt (" mqtt.port" , 1883 ));
61
+ mqttClient.setServer (config ().getString (" mqtt.host" , " " ). c_str ( ), config ().getInt (" mqtt.port" , 1883 ));
57
62
}
58
63
if (!mqttClient.connected ()) {
59
64
debug (" Connecting to MQTT" );
60
- if (mqttClient.connect (deviceId.c_str (), config ().getCString (" mqtt.username" , " " ), config ().getCString (" mqtt.password" , " " ))) {
65
+ if (mqttClient.connect (deviceId.c_str (), config ().getString (" mqtt.username" , " " ). c_str () , config ().getString (" mqtt.password" , " " ). c_str ( ))) {
61
66
debug (" Connected to MQTT" );
62
67
63
68
setPublishFunction ([&](MqttEntry* entry, String message){
0 commit comments