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