Hi everyone
I had this exasperating problem as well and was able to resolve it:
1) As has been previously mentioned - on all devices, make sure your cell # (the one SMS messages are going to and from) is active.
2) On your devices other than your iPhone - where it says "start new conversations from" make sure the option that is checked is your email address used to log into your Apple ID / iMessage / etc account.
So for example, under the list "I can be reached at", I personally have 3 email addresses plus the phone number of my iPhone.
Then further down, it will give you an option to "start conversations from" one of the addresses / phone numbers on this list. Make sure the one you pick is the one you use for your Apple ID, etc login.
As soon as I did that the code popped up on both my Mac and my iPad and everything works perfectly.