title: ESP32 Freezes After 12 Hours
body: <p>Good afternoon, I want to make a continuously operable gateway that measures temperature with esp32.
  The device sends data via MQTT every 1.5 seconds.
  Esp freezes after 12 - 24 hours.
  I read that some users operate the device every 6-7 months without resetting, how can I operate the device for a long time without resetting it?</p>
  <p><strong>Note1:</strong> As a workaround in the code I reset the device with MQTT, Ethernet and ESP.restart () every 12 hours before it can freeze.</p>
  <p><strong>Note2</strong>: LEDs indicate that the system is working.</p>
  <ul>
  <li>Red lights when ethernet or MQTT cannot be connected.</li>
  <li>Blue indicates trying to connect to MQTT or ethernet.</li>
  <li>Green indicates that the system is active. When the device was frozen, the green led was still on.</li>
  </ul>
  <p>Versions of libraries I use:</p>
  <ul>
  <li>Arduino IDE == 1.8.14</li>
  <li>ESP Library Version == 1.0.6</li>
  <li>MQTT Library Version == 2.5.0</li>
  <li>ESP Library Version == 2.0.9</li>
  </ul>
  <p>ESP Link: <a href="https://www.direnc.net/esp32-wroom-32u-wifi-bluetooth-gelistirme-modulu-en" rel="nofollow noreferrer">https://www.direnc.net/esp32-wroom-32u-wifi-bluetooth-gelistirme-modulu-en</a></p>
  <p>The code:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;UIPEthernet.h&gt;
  #include &lt;ArduinoJson.h&gt;
  #include &lt;MQTT.h&gt;
  #include &lt;DHT.h&gt;
  //////////////////////// ETHERNET CONFİG //////////////////////////
  byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  //////////////////////// ETHERNET CONFİG //////////////////////////
  
  
  //////////////////////// LED &amp; DHT22 CONFİG //////////////////////////
  byte red = 2;
  byte green = 0;
  byte blue = 4;
  byte temp = 15;
  #define DHTTYPE DHT22
  DHT dht = DHT(temp, DHTTYPE);
  //////////////////////// LED &amp; DHT22 CONFİG //////////////////////////
  
  
  //////////////////////// MQTT CONFİG //////////////////////////
  #define mqttServer IPAddress(xxx,xxx,xx,xxx)
  const char* mqttUser = &quot;test&quot;;
  const char* mqttPassword = &quot;test&quot;;
  const char* deviceId = &quot;Ai-Sens 1&quot;;
  //////////////////////// MQTT CONFİG //////////////////////////
  
  
  unsigned long lastMillis = 0;
  byte x;
  byte y;
  EthernetClient net;
  MQTTClient client;
  
  
  //Receives an external reset command every 12 hours
  void callback(String &amp;topic, String &amp;payload) { 
    if (payload == &quot;1&quot;)
    {
      ESP.restart(); // This is just a workaround to avoid the freezing issue
    }  
  }
  
  
  void setup_eth() {
    delay(10);
    digitalWrite(blue, HIGH); // turn the LED on
    //Serial.print(&quot;connecting ethernet...&quot;);
    if (Ethernet.begin(mac) == 0) {
      // Serial.println(&quot;Failed to configure Ethernet using DHCP&quot;);
      for (;;)
        ;
    }
    Ethernet.begin(mac, Ethernet.localIP());
    digitalWrite(blue, LOW);
  }
  
  
  void connect() {
    //Serial.print(&quot;connecting mqtt...&quot;);
    digitalWrite(blue, HIGH);
    while (!client.connected()) {
  
      if (client.connect(deviceId, mqttUser, mqttPassword)) {
        // Subscribe
  
        //client.subscribe(&quot;esp/data1&quot;);
      } else {
        delay(500);
      }
    }
    //Serial.println(&quot;\nconnected!&quot;);
    digitalWrite(blue, LOW);
    //client.subscribe(&quot;esp/data1&quot;);
  }
  
  
  
  void setup() {
    //Serial.begin(115200);
  
    pinMode(red, OUTPUT);
    pinMode(green, OUTPUT);
    pinMode(blue, OUTPUT);
    digitalWrite(red, LOW);
    digitalWrite(green, LOW);
    digitalWrite(blue, LOW);
  
    dht.begin();
    setup_eth();
  
    client.setHost(mqttServer, 1883);
    client.setKeepAlive(90);
    client.begin(mqttServer, net);
  
    connect();
  
  }
  
  void loop() {
    client.loop();
    delay(10);
    
    client.subscribe(&quot;esp/rst&quot;);
    client.onMessage(callback);
    
    StaticJsonBuffer&lt;300&gt; JSON;
    JsonObject&amp; JSONencoder = JSON.createObject();
  
    if (millis() - lastMillis &gt; 1500) {
      digitalWrite(green, LOW);
      digitalWrite(blue, LOW);
      digitalWrite(red, LOW);
      if (!client.connected()) {
        digitalWrite(red, HIGH);
        if (y == 5) {
  
          //Serial.println(&quot;Reboot System Now....&quot;);
          ESP.restart();
          y = 0;
        }
        y++;
        connect();
      }
  
      if (Ethernet.linkStatus() == LinkON) {
        // Serial.println(&quot;On&quot;);
        digitalWrite(green, HIGH);
      }
      else {
        digitalWrite(red, HIGH);
  
        if (x == 5) {
          //Serial.println(&quot;Reboot System Now....&quot;);
          ESP.restart();
          x = 0;
        }
        x++;
      }
      lastMillis = millis();
      //Serial.println(Ethernet.maintain ());
  
      JSONencoder[&quot;DEVICE = &quot;] = deviceId;
      JSONencoder[&quot;USER = &quot;]   = mqttUser;
      JSONencoder[&quot;TEMP = &quot;]   = dht.readTemperature();
      JSONencoder[&quot;HUMD = &quot;]   = dht.readHumidity();
      JSONencoder[&quot;STATE = &quot;]  = true;
  
      char JSONmessageBuffer[100];
      JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  
      client.publish(&quot;esp/data1&quot;, JSONmessageBuffer);
    }
  }
  </code></pre>