This is no different than have 2 computers talk to each other over the Internet. It's absolutely possible, but requires some steps.
First and foremost, do both house's internet connections have static external-facing IP addresses? Probably not. So you'll have to deal with that. You see, each home will likely have a dynamic IP address that changes periodically. This could be one a day, several times a day, once a week, whatever. In order for internet device A to initiate communication to internet device B, it must know the externally facing IP address of device B. But if the IP address changes, how do you deal with that?
The answer is to use a dynamic DNS service. Some are free, some cost money. I'm currently paying for one that costs me like $15 a year, and I use to access my home automation in my house from anywhere in the world without having to know my current external IP address for my home network. You get to choose a partial domain on one of their hosts, like x.domain.com, where "x" is the part you choose. Then you run a client on one of the computers on your home network (some routers even can do it for you without the need to run a client) that automatically updates domain.com with your externally facing IP address whenever your IP address changes. When a computer attempts to connect to "x.domain.com", the first thing it does is attempt resolve the domain name to an actual IP address. When you type in "www.google.com" in your web browser, your web browser actually using some services on your computer and something called a domain name service to get an IP address. When your browser actually connects to the web server to download the page, it does this over the IP address, not the domain name. But that's all magic under the covers that you don't realize is happening. If you only need to initiate the communication from one house to the other, and not the other way around, then you only need to setup one of these houses with one of these services. Once device A establishes communication to device B, both can talk to each other as long as the socket is open. If you need device B to open a socket back to device A, then you will need dynamic DNS services for both houses.
So anyway, let's say you setup a dynamic domain name service for house B so that you can just connect to "houseb.somedomain.com". You will resolve the domain name to the currently correct IP externally facing IP address for home b's network.
The next step is you need to setup port forwarding on home B's router. houseb.somedomain.com resolves to the IP address of essentially your router, but not your Arduino. How do the packets know they need to go to the Arduino? You setup port forwarding. Let's say your Arduino has a local (LAN) network address of 192.168.0.17. In order for packets to be sent to that IP address, you have to setup port forwarding to make the packets go there. Typically you would choose a particular port so that only packets sent to that port will get redirected to your device. Let's just for the sake of argument use port 80 (which is what HTTP normally is under).
Let's walk through how communication is done:
House A:
Arduino A has local IP of 192.168.0.300 (just making this up).
House A has external IP of 200.203.156.177 (just making this up).
House B:
Arduino B has local IP of 192.168.1.17 (again, just making this up)
House B has external IP of 197.231.56.18
House B has dynamic DNS setup on houseb.dnsalias.org
Now the sequence of events:
- Arduino A wants to open a TCP socket to houseb.dnsalias.org on port 80.
- You need to do a DNS lookup on houseb.dnsalias.org to get the current IP address on Arduino A. Not sure if there is already a library to do this for you, but this must be accomplished. Result should be 197.231.56.18
- Arduino A attempts to open a TCP socket to 197.231.56.18:80 (port 80)
- Router in house B sees the socket request from Arduino A. It has a port forwarding rule that tells it forward packets on port 80 to internal IP 192.168.1.17
- Router forwards packets to 192.168.1.17
- Arduino B accepts the socket and begins processing the packets. It knows the external IP address of House A and the socket number and can respond to Arduino A. Two way communication is established and both devices are able to send and receive to each other as long as the socket stays open.
This would work even if house A and house B were in opposite sides of the planet. Assuming Internet traffic can get between those 2 homes.