ESP8266 WIFI single relay ESP-12F Dev board

I am beginner. I am using ESP8266 WIFI single relay ESP-12F Dev board to send some data over MQTT with PubSub client, it used to work well but after sometime(1-2 days) it does not receive message through MQTT. When I reset the board, it get starts working. I do not where the issue is and where is it freezing.

I am storing MQTT credentials in EEPROM also.

Here's my code

#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <PubSubClient.h>
#include <WiFiManager.h>   
#include <EEPROM.h>

WiFiClient espClient;
PubSubClient client(espClient);

const char MAIN_page[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<body>

<h3> HTML Form ESP8266</h3>

<form action="/action_page">
  Topic name:<br>
  <input type="text" name="PublishTopic" value="">
  <br>
  Broker name:<br>
  <input type="text" name="Mqtt_Broker" value="">
  <br><br>
  <input type="submit" value="Submit">
</form> 

</body>
</html>
)=====";

//SSID and Password of your WiFi router
const char* ssid = "Cherry";  //Daalchini@2.4Ghz
const char* password = "ravan@123"; //G_2425G_A

ESP8266WebServer server(80); //Server on port 80

//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() {
 String s = MAIN_page; //Read HTML contents
 server.send(200, "text/html", s); //Send web page
}
//===============================================================
// This routine is executed when you press submit
//===============================================================
void handleForm() {
 String PublishTopic = server.arg("PublishTopic"); 
 String Mqtt_Broker = server.arg("Mqtt_Broker"); 
 const char* topic =  PublishTopic.c_str(); //strstr( PublishTopic.c_str(), "]" );
 const char* mqtt_server = Mqtt_Broker.c_str();

 Serial.print("topic:");
 Serial.println(topic);
 Serial.print("PublishTopic:");
 Serial.println(PublishTopic);

 Serial.print("mqtt_server:");
 Serial.println(mqtt_server);
 Serial.print("MQTT Broker:");
 Serial.println(Mqtt_Broker);
 
 if(mqtt_server)
 {
 client.setServer(mqtt_server, 1883);
 client.setCallback(callback);
 reconnect(topic);

 if(!client.connected())
{
reconnect(topic);
Serial.print("disconnected");
}
 }
 
 String s = "<a href='/'> Go Back </a>";
 server.send(200, "text/html", s); //Send web page
 // Writing
      int str1AddrOffset = writeStringToEEPROM(0,topic);
      int str2AddrOffset = writeStringToEEPROM(str1AddrOffset,mqtt_server);

}
//==============================================================
//                  SETUP
//==============================================================

//===========================================

void callback(char* topic, byte* payload, unsigned int length)
{

Serial.print("Message arrived : ");
 Serial.print(topic);
Serial.print(" : ");
 for (int i = 0; i < length; i++)
 {
 Serial.print((char)payload[i]);
 }

}


//===========================================

void reconnect(const char* topic)
{

while(!client.connected()){
Serial.println("Attempting MQTT connection");
if(client.connect(""))
{
Serial.println("Connected");
//client.publish("justinmqtt/house/TMP","Connected!");
client.subscribe(topic);
Serial.print("subscribed!");
}
else
{
Serial.print("Failed, rc = ");
Serial.print(client.state());
Serial.println("Waiting for 5 seconds to try again");
delay(5000);
 }
 }
}

void setup(void){
  Serial.begin(9600);
  EEPROM.begin(512);
  WiFi.begin(ssid, password);     //Connect to your WiFi router
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println("WiFi");
    String newStr1;
    String newStr2;
    int readstr = readingData(0, &newStr1);
    int readstr1 = readingData(readstr, &newStr2);
    const char* top =  newStr1.c_str(); //strstr( PublishTopic.c_str(), "]" );
    const char* servermqtt = newStr2.c_str();
   
    Serial.println("top");
    Serial.println(top);
    Serial.println("servermqtt");
    Serial.println(servermqtt);
    if(servermqtt)
    {
       Serial.print("entering in a loop: ");
        client.setServer(servermqtt, 1883);
        client.setCallback(callback);
        reconnect(top);

    if(!client.connected())
    {
        reconnect(top);
        Serial.print("disconnected");
    }
    }
    else{
   Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
 
  server.on("/", handleRoot);      //Which routine to handle at root location
  server.on("/action_page", handleForm); //form action is handled here

  server.begin();                  //Start server
  Serial.println("HTTP server started");
    }
    
//  Serial.print("IP address: ");
//  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
// 
//  server.on("/", handleRoot);      //Which routine to handle at root location
//  server.on("/action_page", handleForm); //form action is handled here
//
//  server.begin();                  //Start server
//  Serial.println("HTTP server started");
}

//==============================================================
//                    WRITE EEPROM
//==============================================================
int writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  Serial.println("Writing Data ");
  byte len = strToWrite.length();
  Serial.println(len);
  EEPROM.write(addrOffset, len);
  for (int i = 0; i < len; i++)
  {
    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
    
  }
  EEPROM.commit();
  return addrOffset + 1 + len;
}
//==============================================================
//                    READ EEPROM
//==============================================================
int readingData(int addrOffset, String *strToRead)
{
  Serial.println("Reading Data");
  int newStrLen = EEPROM.read(addrOffset);
  char data[newStrLen + 1];
  for (int i = 0; i< newStrLen; i++) {
   data[i] = EEPROM.read(addrOffset + 1 + i);
 }
  data[newStrLen] = '\0';
  Serial.println(data);

  *strToRead = String(data);
  return addrOffset + 1 + newStrLen;
  

}

//==============================================================
//                     LOOP
//==============================================================
void loop(void){
  server.handleClient(); //Handle client requests
  yield();
  delay(50);
  client.loop();
}

@sugandha123, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.