Keeping Lolin32 lite up for more than a month

In this article, I will explain how I managed to get the WEMOS Lolin32 lite board running for more than a month on a single 1200 mAh LIPO battery. After showing my solution, I will get back on the biggest mistake I made during my tests (SPOILER: error handling is important).

My goal is to send data over MQTT every 10 minutes. The code is developed using the Arduino framework for ESP32, and is run on a Lolin32 lite board which is neat because it includes the LIPO circuitry. To send data over MQTT, I used the PubSubClient library. To save the battery, I used the deep sleep functionality of the ESP32.

#include 
#include 

#define uS_TO_S_FACTOR 1000000 /* Conversion factor for u seconds to seconds */
#define TIME_TO_SLEEP 600        /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int successCount = 0;
RTC_DATA_ATTR int errorCount = 0;

#define DEBUG 0

#if DEBUG
#define LOG(S) Serial.print(S);
#define ERROR(S) Serial.print(S);
#else
#define LOG(S)
#define ERROR(S) Serial.print(S);
#endif

#define WIFI_SSID "ssid"
#define WIFI_PASS "password"

const char MQTT_SERVER[] = "192.168.1.1";
const int MQTT_PORT = 1883;

void setup()
{
  Serial.begin(115200);

  //-- Wifi -------------------------------------------------------------------
  LOG("Connecting to wifi\n");
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  //-- Attente active du wifi --
  int wifiCount = 100;
  while (WiFi.status() != WL_CONNECTED && wifiCount--)
  {
    LOG(".");
    delay(100);
  }
  LOG("\n");
  if (WiFi.status() != WL_CONNECTED)
  {
    // Battery safety, go to sleep if failed
    ERROR("Failed to connect wifi: ");
    ERROR(WiFi.status());
    ERROR("\n");
    errorCount++;
    return;
  }
  LOG("Connected to wifi, with ip: ");
  LOG(WiFi.localIP());
  LOG("\n");

  //-- MQTT -------------------------------------------------------------------
  LOG("Connecting to MQTT\n");
  String boardId("lolin32lite");

  WiFiClient wifiClient;
  PubSubClient client(wifiClient);
  client.setServer(MQTT_SERVER, MQTT_PORT);
  if (!client.connect(boardId.c_str(), NULL, NULL))
  {
    // Battery safety, go to sleep if failed
    ERROR("Unable to connect MQTT\n");
    errorCount++;
    return;
  }
  LOG("Connected to MQTT\n");

  //-- Préparation du message -------------------------------------------------
  String payload = "{";
  payload += "\"success\":";
  payload += successCount;
  payload += ",\"error\":";
  payload += errorCount;
  payload += "}";

  String topic = "n/";
  topic += boardId;

  // Send payload
  if (client.publish(topic.c_str(), payload.c_str(), true)) {
    LOG("Published\n");
  } else {
    ERROR("MQTT publish failed\n");
    errorCount++;
    return;
  }
  //client.loop();
  
  client.disconnect();
  wifiClient.flush();
  
  //-- Increment boot number and print it every reboot ------------------------
  successCount++;
  delay(100); // I don't know why, but I needed to wait for data to be
              // transmitted. Even with the flush. I did not managed however
              // to figure out where it came from.

  //-- Success ----------------------------------------------------------------
}

void loop()
{
  //-- Deep sleep -------------------------------------------------------------
  LOG("Entering deep sleep\n");
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  esp_deep_sleep_start();

  // DEAD CODE AFTER THIS
  // DEAD CODE AFTER THIS
  // DEAD CODE AFTER THIS
}

My biggest mistake, not handling error

Yes, errors happen! In my first tests, I did not performed any error handling. So when the Wifi was unable to connect, the loop got infinite and drained the whole battery. After correctly handling connection errors, I managed to reach 33 days for now.

Statistics

To give you some statistics, after 33 days, I sent over 4755 messages over MQTT and got 13 errors. I am pretty confident that some errors are due to a power failure at my home. However, some other errors happened just because they needed to happen.

The test is still running. I hope it will last as long as possible.

6 Replies to “Keeping Lolin32 lite up for more than a month”

  1. Hard to help you with so few information. Check that you replaced the wifi ssid and password + your mqtt server.

  2. I did, I am using using node red with mqtt broker and esp 32
    I have been facing this issue for weeks now. the pubsubclient works finally when am not using sleep mode but the moment i introduce deep sleep to the code the esp stops receiving messages from node-red

  3. This is normal. When the cpu enters deep sleep, it shutdowns everything (except for an ultra low power co-processor). So there is no way for him to receive data.

  4. The Lolin32 lite last 30 days on battery (1200 mAh). However with the new Lolin D32, the same code and the same battery, it runs for 6 months. They changed the battery charging chip.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.