It is a little more complicated than just using a microphone and producing 'anti-noise' to cancel ambient noise. Your main problem as already mentioned is the frequency and phase response of a small microphone has to match that of the earphone. If you get significant phase difference it can go into oscillation, that would definitely wake you up!
The added complication is that to get as similar sound pickup as your ears, the microphone has to be placed very close to them and have a similar polar response. That would be very difficult to achieve with your head on a pillow, especially if you lie on your side.
An ESP32 might be capable of doing the job but check it's DAC capability carefully, I think it uses PWM so getting a suitable frequency response might be tricky. You can certainly use it to create an adjustable delay by using a long circular table of ADC measurements and using different write and read points.
There is a cheap but reliable solution - earplugs!
Brian.