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.

Leave a Reply

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