You might want to have a look at Peter's mqtt_as libraray which keeps the esp connected to wifi and mqtt and will automatically reconnect. You have to use uasyncio to use the library: https://github.com/peterhinch/micropython-mqtt
@pythoncoder: You are right about asyncio. As I'm trying to connect to WIFI before I even start the asyncio loop, I use it that way. If the wifi connection breaks down later on, then your mqtt_as library takes care of that asynchronously.