If you have two-factor authentication enabled (and you should) then don’t use your main facebook password that you log into facebook.com with, and don’t bother with the SMS codes. Instead, you need to generate a custom password just for your chat client (Pidgin, or whatever you have).
Open up the security page on facebook and hit “generate app passwords” in the App Passwords section:
Give it a name you can recognize (the name of your chat client, phone device, etc)
Use the newly generated password in your favorite chat app:
Now you have a secure and reliable way to connect to Facebook from Pidgin (or whatever you use) :)