diff --git a/platformio.ini b/platformio.ini index 06ff046..cf6efa9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,4 +14,8 @@ board = esp32doit-devkit-v1 framework = arduino monitor_port = COM[13] monitor_speed = 115200 -lib_deps = knolleary/PubSubClient@^2.8 +lib_deps = + knolleary/PubSubClient@^2.8 + prampec/IotWebConf@^2.3.1 + nickgammon/Regexp@^0.1.0 + lennarthennigs/Button2@^1.2.0 diff --git a/src/main.cpp b/src/main.cpp index 340cffa..17f8c4a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,6 +34,11 @@ WiFiUDP udp; WiFiClient espClient; PubSubClient client(espClient); +boolean needMqttConnect = false; +boolean needReset = false; +unsigned long lastMqttConnectionAttempt = 0; + +void wifiConnected(); #define MSG_BUFFER_SIZE (50) char msg[MSG_BUFFER_SIZE]; @@ -111,7 +116,7 @@ void callback(char* topic, byte* payload, unsigned int length) { void change(Button2& btn) { if (btn.isPressed()) { - Serial.print("released"); + Serial.println("released"); if (millis() - last_sended < MINIMAL_GAP) { Serial.println("Minimal gap not meeted"); @@ -122,6 +127,11 @@ void change(Button2& btn) { } } +void wifiConnected() +{ + needMqttConnect = true; +} + void setup() { Serial.begin(115200); @@ -137,11 +147,16 @@ void setup() iotWebConf.addParameter(&separator2); iotWebConf.addParameter(&bellParam); iotWebConf.setConfigSavedCallback(&configSaved); + iotWebConf.setWifiConnectionCallback(&wifiConnected); iotWebConf.setFormValidator(&formValidator); iotWebConf.getApTimeoutParameter()->visible = true; // -- Initializing the configuration. - iotWebConf.init(); + boolean validConfig = iotWebConf.init(); + if (!validConfig) + { + mqttServerValue[0] = '\0'; + } // -- Set up required URL handlers on the web server. server.on("/", handleRoot); @@ -157,10 +172,14 @@ void setup() Serial.println("Ready."); } -void reconnect() { - // Loop until we're reconnected - while (!client.connected()) { - Serial.print("Attempting MQTT connection..."); +boolean reconnect() { + unsigned long now = millis(); + if (1000 > now - lastMqttConnectionAttempt) + { + // Do not repeat within 1 sec. + return false; + } + Serial.println("Connecting to MQTT server..."); // Create a random client ID String clientId = "ESP32Client-BELL"; clientId += String(bellParamValue); @@ -169,13 +188,14 @@ void reconnect() { Serial.println("connected"); // Once connected, publish an announcement... client.subscribe("home/bell/sendring"); + return true; } else { Serial.print("failed, rc="); Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); + Serial.println(" try again in 1 seconds"); // Wait 5 seconds before retrying - delay(5000); - } + lastMqttConnectionAttempt = now; + return false; } } @@ -187,14 +207,22 @@ void loop() client.loop(); buttonBELL.loop(); - if ((iotWebConf.getState() == IOTWEBCONF_STATE_ONLINE) && (!client.connected())) + if (needMqttConnect) { + client.setServer(mqttServerValue, 1883); + client.setCallback(callback); + if (reconnect()) + { + needMqttConnect = false; + } + } + else if ((iotWebConf.getState() == IOTWEBCONF_STATE_ONLINE) && (!client.connected())) + { + Serial.println("MQTT reconnect"); client.setServer(mqttServerValue, 1883); client.setCallback(callback); reconnect(); - } - - + } } /**