My wemos disconnects from wifi and does not reconnect by itself

You seem fixated on restarting your device via ESP.restart

If you want to take that approach then I’m not going to assist.

I said before that…

Maybe you should start here…

Pete.