I confirm that the problem comes from dust. Some people has said that it can be solved using a can of compressed air in the earphone in order to blow dust away.
I don't know any app to do that but I think you can achieve this by using an app like Tasker to do that. Tasker is an app which allows you to perform something in a specifical context.
Configure Tasker in order to open the app which simulate a...