Buffer overflow when starting my app

Hmmm, you’ll see that I tend to describe these as the “worst of both worlds”, and for good reason.
The Mega itself is a poor starting point for an IOT solution. Coupled with an ESP in the way that these guys do it makes it even worse.
My guess is that its your firmware, but it really isn’t worth the effort of playing around with them, they will always be a poor compromise in my opinion.

If you really do need to run 32 relays from a single MCU then I’d daisy-chain 4 MCP23017 boards together and control them with a NodeMCU or Wemos D1 Mini, like this…

However, I seriously doubt that having a topography where all 32 relays are situated in one location is a practical option for most IoT projects. I prefer to have one or two relays connected to a single MCU and located next to the equipment that is being controlled, and linked together via WiFi, but it does depend on your application.

Pete.