ESP Embedded Systems

Publish Date

My Previous Experience

Before I started working with the ESP32 & ESP8266, my experience with embedded systems was scarce. My only usage of C, C++ and Assembly was for competitive programming and operating systems. My codes were all run on powerful machines and I never had to worry about memory management, power consumption, or real-time constraints. This is why I was flabbergasted when I first started working with the ESP8266.

ESP32 (Smooth Sailing)

The ESP32 was a breeze to work with. It has a dual-core processor, 520KB of RAM, and 4MB of flash memory. My project ESP Rock was a success and I was able to implement server hosting, AHRS analysis and a useful web interface. The ESP32 was powerful enough to handle all of this and more. It didn't differ much from my previous experience with C++ and I was able to use the STL and other libraries without any issues.

ESP8266 (Rough Waters)

The ESP8266 was a different story. It has a single-core processor, 80KB of RAM, and 4MB of flash memory. My project IOT Connect faced a lot of issues, particularly the OOM (Out of Memory) error, which is notoriously difficult to debug, providing no useful information. Turns out, the async web server I was using had memory leaks and I had to switch to the good ol' synchronous web server <ESP8266WebServer.h> to fix the issue. For some reason, the ESP8266 also had issues with non-delaying analog reads, which I had to fix by using a non-blocking delay.

// Non-blocking delay example
void func() {
  static unsigned long lastMillis = 0;
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    // Do something
  }
}

New Learnings

First off, PROG_MEM is a thing. It's a special memory space that is used to store constant data in the flash, which is useful for storing web pages, images, and other static data. Another thing I learned is that the ESP8266 hates dynamic memory allocation, so I had to rewrite all my programs into stack-based memory allocation. Hopefully, I was already using c style arrays and structs, so it wasn't too much of a hassle, but still, without all the debugging tools I'm used to, it was a pain to find the root of the problem.

Debugging Cheatsheet


//...
#include <stack>
#include <LittleFS.h>

std::stack<std::function<void()>> garbage;

void setupLittleFS() {
  LittleFS.begin();
  garbage.push([]() {
    LittleFS.end();
  });
}

void cleanup()
{
  while (!garbage.empty()) {
    garbage.top()();
    garbage.pop();
  }
}

After Thoughts

While working with ESP8266 was a pain, it also gave me a sense of accomplishment. I was able to make a tiny little chip do so much with so little resources (squeeze hard!). It was a humbling experience and I learned a lot from it. As somebody once said, "Never judge a book by its cover," I would say, "Never judge a chip by its specs, and never think your code would work the same on it!"

Referenced Repositories