OTA to ESP to Atmega

I've seen a good number of guides now on OTA updates, but none that quite fits what I want to do.

I have an Arduino Mega as the main MCU in a project, and it has both SPI and UART connections to an ESP12 module. Currently the Arduino does "some stuff", periodically sends a JSON over to the ESP, which sends it out using MQTT over WiFi. The ESP handles all network related stuff.

What I'd like to do is send an Arduino hex to the ESP, store it and verify it on the ESP, then flash the Arduino with it. The ESP currently has custom software running on it, so I was hoping this functionality can be added to it somehow, but I'm not really sure what first steps to take.

I guess I want to reset and trigger the bootloader on the Arduino and then shove the hex over serial or something, but I have no idea the format that needs to take. Any tips?

The protocol used to upload hex over serial with the arduino bootloader is STK500v2. You can find the specs online if you dig.

I'll bet someone has written code that will upload a .hex file over serial - though I'd wager you'll have a harder time finding one that will do it for the mega vs smaller AVRs that use STK500 (not v2). Some more googling is in order - I'm almost certain I've seen a reference to this.

To kick off the bootloader, just pulse RESET low, and once the reset pulse ends, the bootloader will be active for a second or so (exact time depends on the specific board - I think the mega gives you a couple of seconds).

the ESP8266AVRISP library is a OTA version of "Arduino as ISP".

Juraj:
the ESP8266AVRISP library is a OTA version of "Arduino as ISP".

Which is useful to the OP how?

He wishes to upload via serial using the bootloader, not via ISP using the spi pins.

DrAzzy:
Which is useful to the OP how?

He wishes to upload via serial using the bootloader, not via ISP using the spi pins.

"Arduino Mega as the main MCU in a project, and it has both SPI and UART connections to an ESP12 module"

Thanks for the pointers. Some initial googling on both those solutions looks like just what I was after. I will report back once I get something running in case anyone else out there is looking to do something similar.

for the AVRISP it is important that it can't stk500v2 configured for Mega avrdude network upload in platform.txt. You need the settings from programmers.txt section "Arduino as ISP"