![internet server – Learn how to keep away from MQTT reconnect Loop internet server – Learn how to keep away from MQTT reconnect Loop](https://techtoguide.com/wp-content/uploads/2023/12/esp32-Arduino-GFX-Library-breaks-after-reinstalling.png)
[ad_1]
I’ve the next code designed to permit me to manage my system from my web site utilizing mqtt dealer whereas web is obtainable. I additionally inbuilt a native webserver to manage the system in case of web outage given I am nonetheless related to the identical router my system is related to.
#embrace <ESP8266WiFi.h>
#embrace <PubSubClient.h>
int ledPin = 2;
const char* ssid = "SSID";
const char* password = "PASS";
const char* mqtt_server = "MQTTSERVER";
const char* mqtt_user = "MQTTUSER";
const char* mqtt_pass = "MQTTPASS";
const char* mqtt_topic = "subject";
String mqttpayload;
WiFiServer server(80);
WiFiClient espClient;
PubSubClient shopper(espClient);
void setup() {
Serial.start(115200); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW);
WiFi.start(ssid, password);
whereas (WiFi.standing() != WL_CONNECTED) { delay(100); Serial.print("."); } Serial.println("");
Serial.print("Related to WiFi");
shopper.setServer(mqtt_server, 1883); server.start();
}
void loop() {
shopper.setCallback(callback);
reconnect();
if ( mqttpayload == "OFF" ) { digitalWrite(ledPin, HIGH); Serial.println("Switced OFF"); }
if ( mqttpayload == "ON" ) { digitalWrite(ledPin, LOW); Serial.println("Switced ON"); }
mqttpayload = "";
WiFiClient cclient = server.obtainable();
if (!cclient) { return; } whereas(!cclient.obtainable()){ }
String request = cclient.readStringUntil('r'); Serial.println(request); cclient.flush();
int worth = HIGH;
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, LOW); worth = LOW; Serial.println("Publish message: ON"); reconnect(); shopper.publish(mqtt_topic, "ON", true);
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, HIGH); worth = HIGH; Serial.println("Publish message: OFF"); reconnect(); shopper.publish(mqtt_topic, "OFF", true);
}
cclient.println("HTTP/1.1 200 OK");
cclient.println("Content material-Sort: textual content/html");
cclient.println("");
cclient.println("<!DOCTYPE HTML>");
cclient.println("<html>");
cclient.print("LED standing: ");
if(worth == HIGH) { cclient.print("OFF"); } else { cclient.print("ON"); }
cclient.println("<br><br>");
cclient.println("Flip <a href="/LED=ON">ON</a><br>");
cclient.println("Flip <a href="/LED=OFF">OFF</a><br>");
cclient.println("</html>");
Serial.println("");
}
void callback(char* subject, byte* payload, unsigned int size) {
for (int i = 0; i < size; i++) { mqttpayload += (char)payload[i]; }
}
void reconnect() {
if (!shopper.related()) {
whereas (!shopper.related()) {
Serial.print("MQTT..");
if (shopper.join("2021", mqtt_user, mqtt_pass)) {
Serial.println("MQTT related"); shopper.subscribe(mqtt_topic);
} else { error(); }
}
}
shopper.loop();
}
void error() { Serial.print("failed, rc="); Serial.print(shopper.state()); Serial.println(" attempt once more in 5 seconds"); delay(5000); }
The issue lies once I check it whereas related to my router with no web. The code loops attempting to connect with MQTT server and get caught which does not enable me to ship native HTTP requests.
I attempted modifying the MQTT reconnect loop to the one beneath, which solves the issue however introduces one other problem which is delaying the HTTP requests as much as 6-9 seconds.
void reconnect() {
if (shopper.join("2021", mqtt_user, mqtt_pass)) {
Serial.println("MQTT related");
shopper.subscribe(mqtt_topic);
} else { error(); }
shopper.loop();
}
So the query right here, what ought to I do to get round this problem?
[ad_2]