Arduino restarts on ESP8266 connection loss

Hi all.

I have a strange problem. I have an IOT project where I use Atmega328p with ESP01S module for communication with an MQTT broker.

All communication with the MQTT broker is implemented in the ESP8266 module, where I use the WiFi.h along with pubSubClient.h library. I use SoftwareSerial library for communication between Atmega328p and ESP8266.

For testing purposes, the only thing Atmega does is receiving a 'ping' message from ESP every 10 seconds and printing it to the serial console.

Now, my problem is the following: Whenever there is a connection loss between ESP8266 and the broker, Atmega328p restarts after some seconds!!!

I know it's strange. It drives me nuts. I have made many experiments to see what's going on. The problem only happens when connection to MQTT broker is lost. E.g. if I make a test and try jut to connect to WiFi and then shut down the connection, the Atmega328p does not restart. If I connect to the MQTT broker after connecting to WiFi and then shut down connection, then Atmega restarts.

I use 5V / 2.5W power supply to power Atmega. ESP is powered from the same 5V supply as well through a 3.3V voltage regulator. Serial communication is implemented on 3.3V using voltage divider with two resistors.

Your help is my last hope before going to a psychiatrist.